rewrite the waiting decorator and move it to a new module
authorMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Wed, 28 Nov 2012 13:12:01 +0000 (15:12 +0200)
committerMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Fri, 30 Nov 2012 07:12:56 +0000 (09:12 +0200)
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 <markus.lehtonen@linux.intel.com>
gitbuildsys/log.py [new file with mode: 0644]
gitbuildsys/oscapi.py

diff --git a/gitbuildsys/log.py b/gitbuildsys/log.py
new file mode 100644 (file)
index 0000000..022a3fd
--- /dev/null
@@ -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
+
+
index dc145d7..787ec44 100644 (file)
@@ -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.