show simple progress message for long time operations
authorJF Ding <jian-feng.ding@intel.com>
Mon, 13 Feb 2012 11:46:03 +0000 (19:46 +0800)
committerJF Ding <jian-feng.ding@intel.com>
Mon, 13 Feb 2012 11:56:14 +0000 (19:56 +0800)
new deccorator in msger.py, and wrapping the potential long-time
functions

gitbuildsys/cmd_build.py
gitbuildsys/msger.py
gitbuildsys/obspkg.py

index babbd2f..3b0bf7c 100644 (file)
@@ -113,7 +113,7 @@ def do(opts, args):
     localpkg.remove_all()
 
     tarball = '%s/%s-%s-tizen.tar.bz2' % (workdir, name, version)
-    msger.info('archive git tree to tar ball: %s' % tarball)
+    msger.info('archive git tree to tarball: %s' % os.path.basename(tarball))
     mygit.archive("%s-%s/" % (name, version), tarball)
 
     for f in glob.glob('packaging/*'):
index 4d6f771..1f7d28d 100644 (file)
@@ -34,6 +34,7 @@ __ALL__ = ['set_mode',
            'error',
            'ask',
            'pause',
+           'waiting',
            'PrintBuf',
            'PrintBufWrapper',
           ]
@@ -295,6 +296,56 @@ def error(msg):
     _color_perror(head, ERR_COLOR, msg)
     sys.exit(1)
 
+def waiting(f):
+    """Function decorator to show simple waiting message for
+    long time operations.
+    """
+
+    import functools
+
+    @functools.wraps(f)
+    def _wait_with_print(*args, **kwargs):
+        import threading
+
+        class _WaitingTimer(threading.Thread):
+            def __init__(self):
+                threading.Thread.__init__(self)
+                self.event = threading.Event()
+                self.waited = False
+
+            def run(self):
+                while not self.event.is_set():
+                    # put the waiting above the actual
+                    # printing to avoid unnecessary msg
+                    self.event.wait(1)
+                    if self.event.is_set():
+                        break
+
+                    self.waited = True
+                    STDERR.write('.')
+                    STDERR.flush()
+
+            def stop(self):
+                self.event.set()
+
+                if self.waited:
+                    STDERR.write('\n')
+                    STDERR.flush()
+
+        timer = _WaitingTimer()
+        timer.start()
+
+        try:
+            out = f(*args, **kwargs)
+        except:
+            raise
+        finally:
+            timer.stop()
+
+        return out
+
+    return _wait_with_print
+
 def ask(msg, default=True):
     _general_print('\rQ', ASK_COLOR, '')
     try:
index b00bbb8..2a3491a 100644 (file)
@@ -74,6 +74,7 @@ class ObsPackage(object):
         with Workdir(self._bdir):
             self._bs.mkPac(self._prj, self._pkg)
 
+    @msger.waiting
     def _checkout_latest(self):
         """ checkout the 'latest' revision of package with link expanded
         """
@@ -124,6 +125,7 @@ class ObsPackage(object):
             else:
                 msger.warning('Invalid pac working dir, skip')
 
+    @msger.waiting
     def commit(self, msg):
         with Workdir(self._pkgpath):
             self._bs.submit(msg)
@@ -150,6 +152,7 @@ class ObsProject(object):
         self._apiurl = self._bs.apiurl
         self._prj = prj
 
+    @msger.waiting
     def is_new(self):
         return self._bs.isNewProject(self._prj)