euscan: fixed progressbar

added -p option, now the progressbar is "global" and shows the total
progress

Signed-off-by: volpino <fox91@anche.no>
This commit is contained in:
volpino 2012-06-28 12:20:57 +02:00
parent b06965f386
commit 0d6376681f
4 changed files with 50 additions and 47 deletions

View File

@ -21,8 +21,8 @@ __description__ = "A tool to detect new upstream releases."
import sys import sys
import os import os
import getopt import getopt
import errno from errno import EINTR, EINVAL
import httplib from httplib import HTTPConnection
from portage.output import white, yellow, turquoise, green from portage.output import white, yellow, turquoise, green
from portage.exception import AmbiguousPackageName from portage.exception import AmbiguousPackageName
@ -37,6 +37,8 @@ from euscan.out import progress_bar
# Globals # Globals
isatty = os.environ.get('TERM') != 'dumb' and sys.stdout.isatty()
def exit_helper(status): def exit_helper(status):
if CONFIG["format"]: if CONFIG["format"]:
@ -52,7 +54,7 @@ def setup_signals():
signal.signal(signal.SIGINT, signal.SIG_IGN) signal.signal(signal.SIGINT, signal.SIG_IGN)
signal.signal(signal.SIGTERM, signal.SIG_IGN) signal.signal(signal.SIGTERM, signal.SIG_IGN)
print() print()
exit_helper(errno.EINTR) exit_helper(EINTR)
signal.signal(signal.SIGINT, exithandler) signal.signal(signal.SIGINT, exithandler)
signal.signal(signal.SIGTERM, exithandler) signal.signal(signal.SIGTERM, exithandler)
@ -121,6 +123,8 @@ def print_usage(_error=None, help=None):
print(yellow(" -f, --format=<format>") + print(yellow(" -f, --format=<format>") +
" - define the output " + yellow("<format>") + " - define the output " + yellow("<format>") +
" (available: json)", file=out) " (available: json)", file=out)
print(yellow(" -p, --progress") +
" - display a progress bar", file=out)
print(file=out) print(file=out)
if _error in ('packages',) or help: if _error in ('packages',) or help:
@ -172,6 +176,8 @@ def parse_args():
CONFIG['format'] = a CONFIG['format'] = a
CONFIG['nocolor'] = True CONFIG['nocolor'] = True
pp.output.nocolor() pp.output.nocolor()
elif o in ("-p", "--progress"):
CONFIG['progress'] = isatty
else: else:
return_code = False return_code = False
@ -179,7 +185,7 @@ def parse_args():
# here are the different allowed command line options (getopt args) # here are the different allowed command line options (getopt args)
getopt_options = {'short': {}, 'long': {}} getopt_options = {'short': {}, 'long': {}}
getopt_options['short']['global'] = "hVCqv1bf:" getopt_options['short']['global'] = "hVCqv1bf:p"
getopt_options['long']['global'] = [ getopt_options['long']['global'] = [
"help", "version", "nocolor", "quiet", "verbose", "oneshot", "help", "version", "nocolor", "quiet", "verbose", "oneshot",
"brute-force=", "format=" "brute-force=", "format="
@ -207,7 +213,7 @@ def parse_args():
def main(): def main():
"""Parse command line and execute all actions.""" """Parse command line and execute all actions."""
CONFIG['nocolor'] = ( 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']: if CONFIG['nocolor']:
pp.output.nocolor() pp.output.nocolor()
@ -230,19 +236,22 @@ def main():
else: else:
print_usage(e.value) print_usage(e.value)
exit_helper(errno.EINVAL) exit_helper(EINVAL)
if CONFIG['verbose'] > 2: 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
for query in queries:
on_progress = None on_progress = None
if (CONFIG['format'] or CONFIG['quiet']) and isatty: if CONFIG['progress']:
print("%s:" % query, file=sys.stderr)
on_progress_gen = progress_bar() on_progress_gen = progress_bar()
on_progress = on_progress_gen.next() on_progress = on_progress_gen.next()
on_progress(maxval=len(queries) * 100, increment=0)
for query in queries:
on_progress(increment=10, label=query)
ret = [] ret = []
@ -271,16 +280,16 @@ def main():
output.eerror('%s: %s' % (query, str(err))) output.eerror('%s: %s' % (query, str(err)))
exit_helper(1) 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']: if not ret and not CONFIG['quiet']:
output.ewarn( output.ewarn(
"Didn't find any new version, check package's homepage " + "Didn't find any new version, check package's homepage " +
"for more informations" "for more informations"
) )
if CONFIG['progress']:
on_progress_gen.next()
print("\n", file=sys.stderr)
output.set_query(None) output.set_query(None)

View File

@ -19,7 +19,8 @@ CONFIG = {
'skip-robots-txt': False, 'skip-robots-txt': False,
'cache': False, 'cache': False,
'format': None, 'format': None,
'indent': 2 'indent': 2,
'progress': False
} }
BLACKLIST_VERSIONS = [ BLACKLIST_VERSIONS = [

View File

@ -11,15 +11,20 @@ from portage.output import EOutput, TermProgressBar
class ProgressHandler(object): class ProgressHandler(object):
def __init__(self): def __init__(self, progress_bar):
self.curval = 0 self.curval = 0
self.maxval = 0 self.maxval = 0
self.last_update = 0 self.last_update = 0
self.min_display_latency = 0.2 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() cur_time = time.time()
if cur_time - self.last_update >= self.min_display_latency: if cur_time - self.last_update >= self.min_display_latency:
self.last_update = cur_time self.last_update = cur_time
@ -33,7 +38,7 @@ def progress_bar():
on_progress = None on_progress = None
progress_bar = TermProgressBar() progress_bar = TermProgressBar()
progress_handler = ProgressHandler() progress_handler = ProgressHandler(progress_bar)
on_progress = progress_handler.on_progress on_progress = progress_handler.on_progress
def display(): def display():

View File

@ -45,15 +45,17 @@ def filter_versions(cp, versions):
def scan_upstream_urls(cpv, urls, on_progress): def scan_upstream_urls(cpv, urls, on_progress):
versions = [] versions = []
maxval = len(urls) + 5 progress_available = 70
curval = 1 num_urls = sum([len(urls[fn]) for fn in urls])
progress_increment = progress_available / num_urls
for filename in urls: for filename in urls:
curval += 1
if on_progress:
on_progress(maxval, curval)
for url in urls[filename]: 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']: if not CONFIG['quiet'] and not CONFIG['format']:
pp.uprint() pp.uprint()
output.einfo("SRC_URI is '%s'" % url) 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) cp, ver, rev = portage.pkgsplit(cpv)
curval += 1
if on_progress:
on_progress(maxval, curval)
result = filter_versions(cp, versions) result = filter_versions(cp, versions)
curval += 1 if on_progress and progress_available > 0:
if on_progress: on_progress(increment=progress_available)
on_progress(maxval, curval)
return result return result
@ -118,9 +115,6 @@ def scan_upstream(query, on_progress=None):
Scans the upstream searching new versions for the given query Scans the upstream searching new versions for the given query
""" """
maxval = 3
curval = 0
matches = [] matches = []
if query.endswith(".ebuild"): if query.endswith(".ebuild"):
@ -159,9 +153,8 @@ def scan_upstream(query, on_progress=None):
output.metadata("cp", pkg.cp, show=False) output.metadata("cp", pkg.cp, show=False)
output.metadata("cpv", pkg.cpv, show=False) output.metadata("cpv", pkg.cpv, show=False)
curval += 1
if on_progress: if on_progress:
on_progress(maxval, curval) on_progress(increment=10)
if pkg.cp in BLACKLIST_PACKAGES: if pkg.cp in BLACKLIST_PACKAGES:
output.ewarn( output.ewarn(
@ -213,15 +206,10 @@ def scan_upstream(query, on_progress=None):
scan_time = (datetime.now() - start_time).total_seconds() scan_time = (datetime.now() - start_time).total_seconds()
output.metadata("scan_time", scan_time, show=False) 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) result = scan_upstream_urls(pkg.cpv, urls, on_progress)
curval += 1
if on_progress: if on_progress:
on_progress(maxval, curval) on_progress(increment=10)
if len(result) > 0: if len(result) > 0:
if not (CONFIG['format'] or CONFIG['quiet']): if not (CONFIG['format'] or CONFIG['quiet']):