mock mock.py,1.29,1.30

Jeffrey Robert Pitman (symbiont) fedora-extras-commits at redhat.com
Sat Nov 12 18:56:19 UTC 2005


Author: symbiont

Update of /cvs/fedora/mock
In directory cvs-int.fedora.redhat.com:/tmp/cvs-serv7870

Modified Files:
	mock.py 
Log Message:
realtime logging allowing for `tail -f' of the logs




Index: mock.py
===================================================================
RCS file: /cvs/fedora/mock/mock.py,v
retrieving revision 1.29
retrieving revision 1.30
diff -u -r1.29 -r1.30
--- mock.py	12 Nov 2005 14:32:22 -0000	1.29
+++ mock.py	12 Nov 2005 18:56:17 -0000	1.30
@@ -18,7 +18,6 @@
 import os
 import os.path
 import sys
-import commands
 import rpmUtils
 import rpm
 import glob
@@ -77,12 +76,33 @@
         self.resultcode = 20
 
 
+class LogBuffer:
+
+    lines = []
+
+    def clear(self):
+        self.lines = []
+
+    def write(self, line):
+        if line[-1] != '\n':
+            line += '\n'
+        self.lines.append(line)
+
+    def writelines(self, lines):
+        for l in lines:
+            self.write(l)
+
+    def readlines(self):
+        return self.lines
+
+    def flush(self):
+        pass
 
 class Root:
     """base root object"""
     def __init__(self, config):
         self._state = 'unstarted'
-        self.tmplog = []
+        self.tmplog = LogBuffer()
         self.config = config
         root = config['root']
         if config.has_key('unique-ext'):
@@ -131,38 +151,18 @@
         if self.config['quiet']: return
         print msg
 
-        
-    def build_log(self, content):
-        if type(content) is types.ListType:
-            for line in content:
-                self._build_log.write('%s\n' % line)
-        elif type(content) is types.StringType:
-            self._build_log.write('%s\n' % content)
-        else:
-            # wtf?
-            pass
-        self._build_log.flush()
-
     def root_log(self, content):
-            
-        # do this so if the log dir isn't ready yet we can still get those logs
-        self.tmplog.append(content)
-        
-        if not hasattr(self, '_root_log'):
-            return
+
+        if type(content) is list:
+            self.tmplog.writelines(content)
+        else:
+            self.tmplog.write(content)
         
-        for content in self.tmplog:
-            if type(content) is types.ListType:
-                for line in content:
-                    self._root_log.write('%s\n' % line)
-            elif type(content) is types.StringType:
-                self._root_log.write('%s\n' % content)
-            else:
-                # wtf?
-                pass
-            
+        # do this so if the log dir isn't ready yet we can still get those logs
+        if hasattr(self, '_root_log'):
+            self._root_log.writelines(self.tmplog.readlines())
             self._root_log.flush()
-        self.tmplog = [] # zero out the logs
+            self.tmplog.clear()
 
     def debug(self, msg):
         if self.config['debug']:
@@ -226,9 +226,7 @@
         command = '%s %s' % (basecmd, cmd)
         self.debug("yum: command %s" % command)
 
-        self.root_log(command)
         (retval, output) = self.do(command)
-        self.root_log(output)
 
         if retval != 0:
             raise YumError, "Error peforming yum command: %s" % command
@@ -252,11 +250,8 @@
 
         cmd = "%s -c 'rpm -Uvh --nodeps %s' %s" % (self.config['runuser'], 
                           rootdest, self.config['chrootuser'])
-        self.root_log(cmd)
         (retval, output) = self.do_chroot(cmd)
         
-        self.root_log(output)
-        
         if retval != 0:
             msg = "Error installing srpm: %s" % srpmfn
             self.root_log(msg)
@@ -278,7 +273,6 @@
                     self.target_arch, chrootspec, self.config['chrootuser'])
         
         (retval, output) = self.do_chroot(cmd)
-        self.root_log(output)
         if retval != 0:
             raise PkgError, "Error building srpm from installed spec. See Root log."
             
@@ -305,7 +299,6 @@
         # pass build reqs (as strings) to installer
         if arg_string != "":
             (retval, output) = self.yum('resolvedep %s' % arg_string)
-            self.root_log(output)
             for line in output.split('\n'):
                 if line.find('No Package Found for') != -1:
                     errorpkg = line.replace('No Package Found for', '')
@@ -334,11 +327,8 @@
         
         self.state("build")
 
-        self.root_log(cmd)
         (retval, output) = self.do_chroot(cmd)
         
-        self.build_log(output)
-        
         if retval != 0:
             raise BuildError, "Error building package from %s, See build log" % srpmfn
         
@@ -396,7 +386,6 @@
         
         if retval != 0:
             if output.find('already mounted') == -1: # probably won't work in other LOCALES
-                self.root_log(output)
                 error("could not mount proc error was: %s" % output)
         
         # devpts
@@ -412,7 +401,6 @@
 
         if retval != 0:
             if output.find('already mounted') == -1: # probably won't work in other LOCALES
-                self.root_log(output)
                 raise RootError, "could not mount /dev/pts error was: %s" % output
         
 
@@ -424,7 +412,6 @@
     
         if retval != 0:
             if output.find('not mounted') == -1: # this probably won't work in other LOCALES
-                self.root_log(output)
                 raise RootError, "could not umount %s error was: %s" % (path, output)
 
     
@@ -454,8 +441,29 @@
         """execute given command outside of chroot"""
         
         retval = 0
-        self.debug("Executing %s" % command)
-        (status, output) = commands.getstatusoutput(command)
+        msg = "Executing %s" % command
+        self.debug(msg)
+        self.root_log(msg)
+
+        if hasattr(self, '_root_log'):
+            logfile = self._root_log
+        else:
+            logfile = self.tmplog
+        if self.state() == "build":
+            logfile = self._build_log
+
+        pipe = os.popen('{ ' + command + '; } 2>&1', 'r')
+        output = ""
+        for line in pipe:
+            logfile.write(line)
+            if self.config['debug']:
+                print line[:-1]
+                sys.stdout.flush()
+            logfile.flush()
+            output += line
+        status = pipe.close()
+        if status is None:
+            status = 0
         
         if os.WIFEXITED(status):
             retval = os.WEXITSTATUS(status)
@@ -540,7 +548,6 @@
             if not os.path.exists(devpath):
                 (retval, output) = self.do(cmd)
                 if retval != 0:
-                    self.root_log(output)
                     raise RootError, "could not mknod error was: %s" % output
 
         # link fd to ../proc/self/fd
@@ -810,4 +817,3 @@
     main()
 
 
-




More information about the fedora-extras-commits mailing list