transparent progress info output
authorTimo Savola <tsavola@movial.fi>
Sun, 6 Apr 2008 14:42:54 +0000 (17:42 +0300)
committerTimo Savola <tsavola@movial.fi>
Sun, 6 Apr 2008 14:43:00 +0000 (17:43 +0300)
matrix/build.py

index f31c540..c8a68a1 100644 (file)
@@ -3,6 +3,7 @@
 
 import os
 import signal
+import sys
 from sets import Set as set
 
 import cache
@@ -99,15 +100,18 @@ class Builder(object):
                self.wait_install = []
                self.in_install = None
 
-               self.progress_now = 0
-               self.progress_total = count * 2
+               self.progress = Progress(count * 2)
 
                self.error = False
 
        def run(self):
                try:
-                       while self.run_iteration():
-                               pass
+                       sys.stdout = self.progress
+                       try:
+                               while self.run_iteration():
+                                       pass
+                       finally:
+                               sys.stdout = self.progress.file
                except:
                        for pid in self.jobs:
                                try:
@@ -142,20 +146,6 @@ class Builder(object):
                else:
                        return False
 
-       def progress(self):
-               self.progress_now += 1
-
-       def message(self, arg1, arg2=None):
-               if arg2 is None:
-                       message = arg1
-               else:
-                       message = '%-10s %s' % (arg1, arg2)
-
-               progress = '%3d%%' % \
-                          (self.progress_now * 100 / self.progress_total)
-
-               print progress, message
-
        def can_start_build(self):
                return self.wait_build and self.can_start_job()
 
@@ -170,11 +160,11 @@ class Builder(object):
                if not c.repo.exists():
                        c.repo.clone()
 
-               self.message('Building', c.repo.path)
+               print 'Building', c.repo.path
                self.start_job(c, 'build_matrix_component', 'in-build')
 
        def start_install(self, c):
-               self.message('Installing', c.repo.path)
+               print 'Installing', c.repo.path
                self.start_job(c, 'install_matrix_component', 'in-install')
 
        def start_job(self, c, make_target, state):
@@ -196,9 +186,9 @@ class Builder(object):
                                log.error('Failed to build %s' % c.name)
                                return
 
-                       self.progress()
+                       self.progress.increment()
                        if config.debug:
-                               self.message('Built', c.repo.path)
+                               print 'Built', c.repo.path
 
                        c.state = 'wait-install'
                        self.wait_install.append(c)
@@ -221,8 +211,8 @@ class Builder(object):
                                log.error('Failed to install %s' % c.name)
                                return
 
-                       self.progress()
-                       self.message('Finished', c.repo.path)
+                       self.progress.increment()
+                       print 'Finished', c.repo.path
 
                        c.state = 'done'
                        self.in_install = None
@@ -232,6 +222,37 @@ class Builder(object):
                elif c.state != 'killed':
                        raise InternalError('Unexpected state: %s' % c.state)
 
+class Progress(object):
+       def __init__(self, max):
+               self.now = 0
+               self.max = max
+               self.file = sys.stdout
+
+               self.newline = True
+
+       def increment(self):
+               self.now += 1
+
+       def write(self, str):
+               progress = '%3d%% ' % (self.now * 100 / self.max)
+
+               while str:
+                       if self.newline:
+                               self.file.write(progress)
+
+                       index = str.find('\n')
+                       if index < 0:
+                               if self.newline and str.find(' ') < 0:
+                                       str = '%-10s' % str
+
+                               self.file.write(str)
+                               self.newline = False
+                               return
+
+                       self.file.write(str[:index+1])
+                       str = str[index+1:]
+                       self.newline = True
+
 def spawn_make(c, jobs, target):
        board = config.boards[config.board]