From 0d6376681f72b78fc43e7861586f76039a9c722a Mon Sep 17 00:00:00 2001 From: volpino Date: Thu, 28 Jun 2012 12:20:57 +0200 Subject: [PATCH] euscan: fixed progressbar added -p option, now the progressbar is "global" and shows the total progress Signed-off-by: volpino --- bin/euscan | 43 +++++++++++++++++++++++++----------------- pym/euscan/__init__.py | 3 ++- pym/euscan/out.py | 15 ++++++++++----- pym/euscan/scan.py | 36 ++++++++++++----------------------- 4 files changed, 50 insertions(+), 47 deletions(-) diff --git a/bin/euscan b/bin/euscan index 7bea5bd..89f61bf 100755 --- a/bin/euscan +++ b/bin/euscan @@ -21,8 +21,8 @@ __description__ = "A tool to detect new upstream releases." import sys import os import getopt -import errno -import httplib +from errno import EINTR, EINVAL +from httplib import HTTPConnection from portage.output import white, yellow, turquoise, green from portage.exception import AmbiguousPackageName @@ -37,6 +37,8 @@ from euscan.out import progress_bar # Globals +isatty = os.environ.get('TERM') != 'dumb' and sys.stdout.isatty() + def exit_helper(status): if CONFIG["format"]: @@ -52,7 +54,7 @@ def setup_signals(): signal.signal(signal.SIGINT, signal.SIG_IGN) signal.signal(signal.SIGTERM, signal.SIG_IGN) print() - exit_helper(errno.EINTR) + exit_helper(EINTR) signal.signal(signal.SIGINT, exithandler) signal.signal(signal.SIGTERM, exithandler) @@ -121,6 +123,8 @@ def print_usage(_error=None, help=None): print(yellow(" -f, --format=") + " - define the output " + yellow("") + " (available: json)", file=out) + print(yellow(" -p, --progress") + + " - display a progress bar", file=out) print(file=out) if _error in ('packages',) or help: @@ -172,6 +176,8 @@ def parse_args(): CONFIG['format'] = a CONFIG['nocolor'] = True pp.output.nocolor() + elif o in ("-p", "--progress"): + CONFIG['progress'] = isatty else: return_code = False @@ -179,7 +185,7 @@ def parse_args(): # here are the different allowed command line options (getopt args) getopt_options = {'short': {}, 'long': {}} - getopt_options['short']['global'] = "hVCqv1bf:" + getopt_options['short']['global'] = "hVCqv1bf:p" getopt_options['long']['global'] = [ "help", "version", "nocolor", "quiet", "verbose", "oneshot", "brute-force=", "format=" @@ -207,7 +213,7 @@ def parse_args(): def main(): """Parse command line and execute all actions.""" CONFIG['nocolor'] = ( - port_settings["NOCOLOR"] in ('yes', 'true') or not sys.stdout.isatty() + port_settings["NOCOLOR"] in ('yes', 'true') or not isatty ) if CONFIG['nocolor']: pp.output.nocolor() @@ -230,19 +236,22 @@ def main(): else: print_usage(e.value) - exit_helper(errno.EINVAL) + exit_helper(EINVAL) if CONFIG['verbose'] > 2: - httplib.HTTPConnection.debuglevel = 1 + HTTPConnection.debuglevel = 1 - isatty = os.environ.get('TERM') != 'dumb' and sys.stdout.isatty() + if not CONFIG["format"]: + CONFIG["progress"] = False + + on_progress = None + if CONFIG['progress']: + on_progress_gen = progress_bar() + on_progress = on_progress_gen.next() + on_progress(maxval=len(queries) * 100, increment=0) for query in queries: - on_progress = None - if (CONFIG['format'] or CONFIG['quiet']) and isatty: - print("%s:" % query, file=sys.stderr) - on_progress_gen = progress_bar() - on_progress = on_progress_gen.next() + on_progress(increment=10, label=query) ret = [] @@ -271,16 +280,16 @@ def main(): output.eerror('%s: %s' % (query, str(err))) exit_helper(1) - if (CONFIG['format'] or CONFIG['quiet']) and isatty: - on_progress_gen.next() - print("\n", file=sys.stderr) - if not ret and not CONFIG['quiet']: output.ewarn( "Didn't find any new version, check package's homepage " + "for more informations" ) + if CONFIG['progress']: + on_progress_gen.next() + print("\n", file=sys.stderr) + output.set_query(None) diff --git a/pym/euscan/__init__.py b/pym/euscan/__init__.py index 84754ee..9c125be 100644 --- a/pym/euscan/__init__.py +++ b/pym/euscan/__init__.py @@ -19,7 +19,8 @@ CONFIG = { 'skip-robots-txt': False, 'cache': False, 'format': None, - 'indent': 2 + 'indent': 2, + 'progress': False } BLACKLIST_VERSIONS = [ diff --git a/pym/euscan/out.py b/pym/euscan/out.py index 26f5417..1928980 100644 --- a/pym/euscan/out.py +++ b/pym/euscan/out.py @@ -11,15 +11,20 @@ from portage.output import EOutput, TermProgressBar class ProgressHandler(object): - def __init__(self): + def __init__(self, progress_bar): self.curval = 0 self.maxval = 0 self.last_update = 0 self.min_display_latency = 0.2 + self.progress_bar = progress_bar + + def on_progress(self, maxval=None, increment=1, label=None): + self.maxval = maxval or self.maxval + self.curval += increment + + if label: + self.progress_bar.label(label) - def on_progress(self, maxval, curval): - self.maxval = maxval - self.curval = curval cur_time = time.time() if cur_time - self.last_update >= self.min_display_latency: self.last_update = cur_time @@ -33,7 +38,7 @@ def progress_bar(): on_progress = None progress_bar = TermProgressBar() - progress_handler = ProgressHandler() + progress_handler = ProgressHandler(progress_bar) on_progress = progress_handler.on_progress def display(): diff --git a/pym/euscan/scan.py b/pym/euscan/scan.py index 2ac828f..be49b96 100644 --- a/pym/euscan/scan.py +++ b/pym/euscan/scan.py @@ -45,15 +45,17 @@ def filter_versions(cp, versions): def scan_upstream_urls(cpv, urls, on_progress): versions = [] - maxval = len(urls) + 5 - curval = 1 + progress_available = 70 + num_urls = sum([len(urls[fn]) for fn in urls]) + progress_increment = progress_available / num_urls for filename in urls: - curval += 1 - if on_progress: - on_progress(maxval, curval) - for url in urls[filename]: + + if on_progress and progress_available > 0: + on_progress(increment=progress_increment) + progress_available -= progress_increment + if not CONFIG['quiet'] and not CONFIG['format']: pp.uprint() output.einfo("SRC_URI is '%s'" % url) @@ -82,15 +84,10 @@ def scan_upstream_urls(cpv, urls, on_progress): cp, ver, rev = portage.pkgsplit(cpv) - curval += 1 - if on_progress: - on_progress(maxval, curval) - result = filter_versions(cp, versions) - curval += 1 - if on_progress: - on_progress(maxval, curval) + if on_progress and progress_available > 0: + on_progress(increment=progress_available) return result @@ -118,9 +115,6 @@ def scan_upstream(query, on_progress=None): Scans the upstream searching new versions for the given query """ - maxval = 3 - curval = 0 - matches = [] if query.endswith(".ebuild"): @@ -159,9 +153,8 @@ def scan_upstream(query, on_progress=None): output.metadata("cp", pkg.cp, show=False) output.metadata("cpv", pkg.cpv, show=False) - curval += 1 if on_progress: - on_progress(maxval, curval) + on_progress(increment=10) if pkg.cp in BLACKLIST_PACKAGES: output.ewarn( @@ -213,15 +206,10 @@ def scan_upstream(query, on_progress=None): scan_time = (datetime.now() - start_time).total_seconds() output.metadata("scan_time", scan_time, show=False) - curval += 1 - if on_progress: - on_progress(maxval, curval) - result = scan_upstream_urls(pkg.cpv, urls, on_progress) - curval += 1 if on_progress: - on_progress(maxval, curval) + on_progress(increment=10) if len(result) > 0: if not (CONFIG['format'] or CONFIG['quiet']):