euscan: naive progressbar implementation

Progressbar for formatted output

Signed-off-by: volpino <fox91@anche.no>
This commit is contained in:
volpino 2012-05-27 10:09:39 +02:00
parent 31bd4dc55e
commit a224cb27cb
3 changed files with 116 additions and 9 deletions

View File

@ -19,6 +19,7 @@ __description__ = "A tool to detect new upstream releases."
# Imports # Imports
import sys import sys
import os
import getopt import getopt
import errno import errno
import httplib import httplib
@ -32,6 +33,7 @@ from gentoolkit.errors import GentoolkitException
from euscan import CONFIG, output from euscan import CONFIG, output
from euscan.scan import scan_upstream from euscan.scan import scan_upstream
from euscan.out import progress_bar
# Globals # Globals
@ -233,13 +235,21 @@ def main():
if CONFIG['verbose'] > 2: if CONFIG['verbose'] > 2:
httplib.HTTPConnection.debuglevel = 1 httplib.HTTPConnection.debuglevel = 1
isatty = os.environ.get('TERM') != 'dumb' and sys.stdout.isatty()
for query in queries: 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()
ret = [] ret = []
output.set_query(query) output.set_query(query)
try: try:
ret = scan_upstream(query) ret = scan_upstream(query, on_progress)
except AmbiguousPackageName as e: except AmbiguousPackageName as e:
pkgs = e.args[0] pkgs = e.args[0]
output.eerror("\n".join(pkgs)) output.eerror("\n".join(pkgs))
@ -264,10 +274,18 @@ def main():
if not CONFIG['quiet'] and not CONFIG['format']: if not CONFIG['quiet'] and not CONFIG['format']:
print() print()
if (CONFIG['format'] or CONFIG['quiet']) and isatty:
on_progress_gen.next()
print("\n", file=sys.stderr)
if ret is not None: if ret is not None:
if len(ret) > 0: if len(ret) > 0:
for cp, url, version, handler, confidence in ret: for cp, url, version, handler, confidence in ret:
output.result(cp, version, url, handler, confidence) output.result(cp, version, url, handler, confidence)
if (CONFIG['format'] or CONFIG['quiet']) and isatty:
print("\n", file=sys.stderr)
elif not CONFIG['quiet']: elif 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 " +

View File

@ -1,9 +1,55 @@
from io import StringIO from io import StringIO
from collections import defaultdict from collections import defaultdict
import json import json
import signal
import time
from gentoolkit import pprinter as pp from gentoolkit import pprinter as pp
from portage.output import EOutput import portage
from portage.output import EOutput, TermProgressBar
class ProgressHandler(object):
def __init__(self):
self.curval = 0
self.maxval = 0
self.last_update = 0
self.min_display_latency = 0.2
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
self.display()
def display(self):
raise NotImplementedError(self)
def progress_bar():
on_progress = None
progress_bar = TermProgressBar()
progress_handler = ProgressHandler()
on_progress = progress_handler.on_progress
def display():
progress_bar.set(progress_handler.curval, progress_handler.maxval)
progress_handler.display = display
def sigwinch_handler(signum, frame):
lines, progress_bar.term_columns = portage.output.get_term_size()
signal.signal(signal.SIGWINCH, sigwinch_handler)
yield on_progress
# make sure the final progress is displayed
progress_handler.display()
signal.signal(signal.SIGWINCH, signal.SIG_DFL)
yield None
class EOutputMem(EOutput): class EOutputMem(EOutput):
@ -87,5 +133,8 @@ class EuscanOutput(object):
print "%s: %s" % (key.capitalize(), value) print "%s: %s" % (key.capitalize(), value)
def __getattr__(self, key): def __getattr__(self, key):
if not self.config["quiet"]:
output = self.queries[self.current_query]["output"] output = self.queries[self.current_query]["output"]
return getattr(output, key) return getattr(output, key)
else:
return lambda *x: None

View File

@ -13,7 +13,6 @@ from euscan import CONFIG, BLACKLIST_PACKAGES
from euscan import handlers, helpers, output from euscan import handlers, helpers, output
from euscan.ebuild import package_from_ebuild from euscan.ebuild import package_from_ebuild
import euscan
def filter_versions(cp, versions): def filter_versions(cp, versions):
filtered = {} filtered = {}
@ -41,10 +40,17 @@ def filter_versions(cp, versions):
] ]
def scan_upstream_urls(cpv, urls): def scan_upstream_urls(cpv, urls, on_progress):
versions = [] versions = []
maxval = len(urls) + 5
curval = 1
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 not CONFIG['quiet'] and not CONFIG['format']: if not CONFIG['quiet'] and not CONFIG['format']:
pp.uprint() pp.uprint()
@ -69,7 +75,19 @@ def scan_upstream_urls(cpv, urls):
break break
cp, ver, rev = portage.pkgsplit(cpv) cp, ver, rev = portage.pkgsplit(cpv)
return filter_versions(cp, versions)
curval += 1
if on_progress:
on_progress(maxval, curval)
result = filter_versions(cp, versions)
curval += 1
if on_progress:
on_progress(maxval, curval)
return result
# gentoolkit stores PORTDB, so even if we modify it to add an overlay # gentoolkit stores PORTDB, so even if we modify it to add an overlay
# it will still use the old dbapi # it will still use the old dbapi
@ -83,7 +101,15 @@ def reload_gentoolkit():
if hasattr(gentoolkit.query, 'PORTDB'): if hasattr(gentoolkit.query, 'PORTDB'):
gentoolkit.query.PORTDB = PORTDB gentoolkit.query.PORTDB = PORTDB
def scan_upstream(query):
def scan_upstream(query, on_progress=None):
"""
Scans the upstream searching new versions for the given query
"""
maxval = 3
curval = 0
matches = [] matches = []
if query.endswith(".ebuild"): if query.endswith(".ebuild"):
@ -122,6 +148,10 @@ def scan_upstream(query):
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:
on_progress(maxval, curval)
if pkg.cp in BLACKLIST_PACKAGES: if pkg.cp in BLACKLIST_PACKAGES:
output.ewarn( output.ewarn(
pp.warn("Package '%s' is blacklisted" % pp.pkgquery(pkg.cp)) pp.warn("Package '%s' is blacklisted" % pp.pkgquery(pkg.cp))
@ -172,4 +202,14 @@ def scan_upstream(query):
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)
return scan_upstream_urls(pkg.cpv, urls) 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)
return result