From a32fffd172a9f4346c2459cf4d114eeb350fb1e8 Mon Sep 17 00:00:00 2001 From: Markus Lehtonen Date: Wed, 28 Nov 2012 15:12:01 +0200 Subject: [PATCH] rewrite the waiting decorator and move it to a new module In preparation for removing the msger module introduce a new module 'gitbuildsys.log' to contain all logging/output related functionality. This change introduces and takes into use a rewritten 'waiting' decorator in the new 'log' module. Signed-off-by: Markus Lehtonen --- gitbuildsys/log.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ gitbuildsys/oscapi.py | 5 +++-- 2 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 gitbuildsys/log.py diff --git a/gitbuildsys/log.py b/gitbuildsys/log.py new file mode 100644 index 0000000..022a3fd --- /dev/null +++ b/gitbuildsys/log.py @@ -0,0 +1,54 @@ +# vim: ai ts=4 sts=4 et sw=4 +# +# Copyright (c) 2012 Intel, Inc. +# +# This program is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the Free +# Software Foundation; version 2 of the License +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +# for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., 59 +# Temple Place - Suite 330, Boston, MA 02111-1307, USA. +"""Module for logging/output functionality""" + +import functools +import sys +import threading + +def waiting(func): + """ + Function decorator to show simple waiting message for long time operations. + """ + + @functools.wraps(func) + def _wait_with_print(*args, **kwargs): + """Wrapper that prints dots until func finishes""" + def _print_loop(stop, printed): + """Main loop for dot printing thread""" + while not stop.is_set(): + # Wait before printing to avoid output on short wait + stop.wait(1) + if not stop.is_set(): + sys.stderr.write('.') + sys.stderr.flush() + printed.set() + + stop = threading.Event() + printed = threading.Event() + threading.Thread(target=_print_loop, + kwargs={'stop': stop, 'printed': printed}).start() + try: + return func(*args, **kwargs) + finally: + stop.set() + if printed.is_set(): + sys.stderr.write('\n') + + return _wait_with_print + + diff --git a/gitbuildsys/oscapi.py b/gitbuildsys/oscapi.py index dc145d7..787ec44 100644 --- a/gitbuildsys/oscapi.py +++ b/gitbuildsys/oscapi.py @@ -34,6 +34,7 @@ from urllib import quote_plus, pathname2url from gitbuildsys import msger from gitbuildsys.utils import hexdigest from gitbuildsys.errors import ObsError +from gitbuildsys.log import waiting from osc import conf, core @@ -234,7 +235,7 @@ class OSC(object): return rdict.keys(), not_changed, changed, new - @msger.waiting + @waiting def commit_files(self, prj, pkg, files, message): """Commits files to OBS.""" @@ -263,7 +264,7 @@ class OSC(object): except OSCError, err: raise ObsError("can't commit files to %s/%s: %s" % (prj, pkg, err)) - @msger.waiting + @waiting def remove_files(self, prj, pkg, fnames=None): """ Remove file[s] from the package. -- 2.7.4