[csw-devel] SF.net SVN: gar:[20981] csw/mgar/gar/v2/lib/python

chninkel at users.sourceforge.net chninkel at users.sourceforge.net
Sat May 4 11:56:31 CEST 2013


Revision: 20981
          http://gar.svn.sourceforge.net/gar/?rev=20981&view=rev
Author:   chninkel
Date:     2013-05-04 09:56:31 +0000 (Sat, 04 May 2013)
Log Message:
-----------
merge the two ShellCommand functions

Modified Paths:
--------------
    csw/mgar/gar/v2/lib/python/inspective_package.py
    csw/mgar/gar/v2/lib/python/inspective_package_test.py
    csw/mgar/gar/v2/lib/python/package.py
    csw/mgar/gar/v2/lib/python/shell.py
    csw/mgar/gar/v2/lib/python/system_pkgmap.py

Modified: csw/mgar/gar/v2/lib/python/inspective_package.py
===================================================================
--- csw/mgar/gar/v2/lib/python/inspective_package.py	2013-05-04 09:56:05 UTC (rev 20980)
+++ csw/mgar/gar/v2/lib/python/inspective_package.py	2013-05-04 09:56:31 UTC (rev 20981)
@@ -192,7 +192,7 @@
       binary_abspath = os.path.join(self.directory, self.GetFilesDir(), binary)
       # Get parsable, ld.so.1 relevant SHT_DYNSYM symbol information
       args = ["/usr/ccs/bin/nm", "-p", "-D", binary_abspath]
-      retcode, stdout, stderr = shell.ShellCommand(args)
+      retcode, stdout, stderr = shell.ShellCommand(args, allow_error=True)
       if retcode:
         logging.error("%s returned an error: %s", args, stderr)
         # Should it just skip over an error?
@@ -234,7 +234,7 @@
         binary = os.path.join(base_dir, binary)
       # elfdump is the only tool that give us all informations
       args = [common_constants.ELFDUMP_BIN, "-svy", binary_abspath]
-      retcode, stdout, stderr = shell.ShellCommand(args)
+      retcode, stdout, stderr = shell.ShellCommand(args, allow_error=True)
       if retcode or stderr:
         # we ignore for now these elfdump errors which can be catched
         # later by check functions,
@@ -350,7 +350,7 @@
       args = ["ldd", "-Ur", binary_abspath]
       # ldd can be stuck while ran on a some binaries, so we define
       # a timeout (problem encountered with uconv)
-      retcode, stdout, stderr = shell.ShellCommand(args, timeout=10)
+      retcode, stdout, stderr = shell.ShellCommand(args, timeout=10, allow_error=True)
       if retcode:
         # There three cases where we will ignore an ldd error
         #  - if we are trying to analyze a 64 bits binary on a Solaris 9 x86

Modified: csw/mgar/gar/v2/lib/python/inspective_package_test.py
===================================================================
--- csw/mgar/gar/v2/lib/python/inspective_package_test.py	2013-05-04 09:56:05 UTC (rev 20980)
+++ csw/mgar/gar/v2/lib/python/inspective_package_test.py	2013-05-04 09:56:31 UTC (rev 20981)
@@ -235,7 +235,7 @@
     args = [common_constants.ELFDUMP_BIN,
             '-svy',
             os.path.join(fake_package_path, "root", fake_binary)]
-    shell.ShellCommand(args).AndReturn((0, ELFDUMP_OUTPUT, ""))
+    shell.ShellCommand(args, allow_error=True).AndReturn((0, ELFDUMP_OUTPUT, ""))
     self.mox.ReplayAll()
 
     self.assertEqual(BINARY_ELFINFO, ip.GetBinaryElfInfo())
@@ -256,7 +256,7 @@
     args = [common_constants.ELFDUMP_BIN,
             '-svy',
             os.path.join(fake_package_path, "reloc", fake_binary)]
-    shell.ShellCommand(args).AndReturn((0, ELFDUMP_OUTPUT, ""))
+    shell.ShellCommand(args, allow_error=True).AndReturn((0, ELFDUMP_OUTPUT, ""))
     self.mox.ReplayAll()
 
     self.assertEqual(BINARY_ELFINFO, ip.GetBinaryElfInfo())
@@ -308,7 +308,7 @@
     args = [common_constants.ELFDUMP_BIN,
             '-svy',
             os.path.join(fake_package_path, "root", fake_binary)]
-    shell.ShellCommand(args).AndReturn((0, fake_elfdump_output, fake_elfdump_errors))
+    shell.ShellCommand(args, allow_error=True).AndReturn((0, fake_elfdump_output, fake_elfdump_errors))
     self.mox.ReplayAll()
 
     self.assertEqual(fake_binary_elfinfo, ip.GetBinaryElfInfo())
@@ -334,7 +334,7 @@
     self.mox.StubOutWithMock(shell, 'ShellCommand')
     shell.ShellCommand(
         ['ldd', '-Ur', '/tmp/CSWfake/root/opt/csw/bin/foo'],
-        timeout=10).AndReturn((0, "", ""))
+        allow_error=True, timeout=10).AndReturn((0, "", ""))
     self.mox.StubOutWithMock(ip, '_ParseLddDashRline')
     self.mox.ReplayAll()
     self.assertEqual({'opt/csw/bin/foo': []}, ip.GetLddMinusRlines())
@@ -359,7 +359,7 @@
     self.mox.StubOutWithMock(shell, 'ShellCommand')
     shell.ShellCommand(
         ['ldd', '-Ur', '/tmp/CSWfake/reloc/bin/foo'],
-        timeout=10).AndReturn((0, "", ""))
+        allow_error=True, timeout=10).AndReturn((0, "", ""))
     self.mox.StubOutWithMock(ip, '_ParseLddDashRline')
     self.mox.ReplayAll()
     self.assertEqual({'opt/csw/bin/foo': []}, ip.GetLddMinusRlines())
@@ -385,7 +385,7 @@
     self.mox.StubOutWithMock(shell, 'ShellCommand')
     shell.ShellCommand(
         ['ldd', '-Ur', '/tmp/CSWfake/root/opt/csw/bin/foo'],
-        timeout=10).AndReturn((1, "", "boo"))
+        allow_error=True, timeout=10).AndReturn((1, "", "boo"))
     self.mox.StubOutWithMock(ip, '_ParseLddDashRline')
     self.mox.ReplayAll()
     self.assertRaises(package.SystemUtilityError,

Modified: csw/mgar/gar/v2/lib/python/package.py
===================================================================
--- csw/mgar/gar/v2/lib/python/package.py	2013-05-04 09:56:05 UTC (rev 20980)
+++ csw/mgar/gar/v2/lib/python/package.py	2013-05-04 09:56:31 UTC (rev 20981)
@@ -53,7 +53,7 @@
   """Problem with data returned by a process."""
 
 
-class CswSrv4File(shell.ShellMixin, object):
+class CswSrv4File(object):
   """Represents a package in the srv4 format (pkg)."""
 
   def __init__(self, pkg_path, debug=False):
@@ -96,7 +96,7 @@
         self.gunzipped_path = os.path.join(self.GetWorkDir(), base_name)
         with open(self.gunzipped_path, 'w') as gunzipped_file:
           args = ["gunzip", "-f", "-c", self.pkg_path]
-          unused_retcode = shell.ShellCommand(args, stdout=gunzipped_file)
+          shell.ShellCommand(args, stdout=gunzipped_file)
       elif self.pkg_path.endswith(pkg_suffix):
         self.gunzipped_path = self.pkg_path
       else:
@@ -116,11 +116,7 @@
             src_file,
             destdir,
             pkgname ]
-    ret, stdout, stderr = shell.ShellCommand(args)
-    if ret:
-      logging.error(stdout)
-      logging.error(stderr)
-      logging.error("% has failed" % args)
+    shell.ShellCommand(args)
 
   def GetPkgname(self):
     """It's necessary to figure out the pkgname from the .pkg file.
@@ -129,7 +125,7 @@
     if not self.pkgname:
       gunzipped_path = self.GetGunzippedPath()
       args = ["nawk", "NR == 2 {print $1; exit;}", gunzipped_path]
-      ret_code, stdout, stderr = shell.ShellCommand(args)
+      shell.ShellCommand(args)
       self.pkgname = stdout.strip()
       logging.debug("GetPkgname(): %s", repr(self.pkgname))
     return self.pkgname
@@ -167,7 +163,7 @@
                            "..", "..", "bin", "custom-pkgtrans"),
               gunzipped_path, self.GetWorkDir(), pkgname]
       logging.debug("transforming: %s", args)
-      unused_retcode = self.ShellCommand(args, quiet=(not self.debug))
+      shell.ShellCommand(args, quiet=(not self.debug))
       dirs = self.GetDirs()
       if len(dirs) != 1:
         raise Error("Need exactly one package in the package stream: "
@@ -217,7 +213,7 @@
     return DirectoryFormatPackage
 
 
-class DirectoryFormatPackage(shell.ShellMixin, object):
+class DirectoryFormatPackage(object):
   """Represents a package in the directory format.
 
   Allows some read-write operations.
@@ -266,9 +262,9 @@
     if not os.path.isdir(target_dir):
       os.makedirs(target_dir)
     args = ["pkgtrans", "-s", pkg_container_dir, target_path, pkg_dir]
-    self.ShellCommand(args, quiet=True)
+    shell.ShellCommand(args, quiet=True)
     args = ["gzip", "-f", target_path]
-    self.ShellCommand(args, quiet=True)
+    shell.ShellCommand(args, quiet=True)
     return target_path
 
   def GetBasedir(self):
@@ -333,7 +329,7 @@
     # Some packages extract read-only. To be sure, change them to be
     # user-writable.
     args = ["chmod", "-R", "u+w", self.directory]
-    self.ShellCommand(args)
+    shell.ShellCommand(args)
     pkginfo_filename = self.GetPkginfoFilename()
     os.chmod(pkginfo_filename, 0644)
     pkginfo_fd = open(pkginfo_filename, "w")

Modified: csw/mgar/gar/v2/lib/python/shell.py
===================================================================
--- csw/mgar/gar/v2/lib/python/shell.py	2013-05-04 09:56:05 UTC (rev 20980)
+++ csw/mgar/gar/v2/lib/python/shell.py	2013-05-04 09:56:31 UTC (rev 20981)
@@ -15,35 +15,11 @@
 def TimeoutHandler(signum, frame):
   raise TimeoutExpired
 
-class ShellMixin(object):
-
-  def ShellCommand(self, args, quiet=False):
-    logging.debug("Calling: %s", repr(args))
-    stdout, stderr = None, None
-    if quiet:
-      process = subprocess.Popen(args,
-                                 stdout=subprocess.PIPE,
-                                 stderr=subprocess.PIPE)
-      stdout, stderr = process.communicate()
-      retcode = process.wait()
-    else:
-      retcode = subprocess.call(args)
-    if retcode:
-      logging.critical(stdout)
-      logging.critical(stderr)
-      raise Error("Running %s has failed." % repr(args))
-    return retcode
-
 def ShellCommand(args, env=None, timeout=None,
+                 quiet=True, allow_error=False,
                  stdout=subprocess.PIPE,
                  stderr=subprocess.PIPE):
   logging.debug("Running: %s", args)
-  proc = subprocess.Popen(args,
-                          stdout=stdout,
-                          stderr=stderr,
-                          env=env,
-                          preexec_fn=os.setsid,
-                          close_fds=True)
   # Python 3.3 have the timeout option
   # we have to roughly emulate it with python 2.x
   if timeout:
@@ -51,14 +27,33 @@
     signal.alarm(timeout)
 
   try:
-    stdout, stderr = proc.communicate()
+    if not quiet:
+      retcode = subprocess.call(args)
+
+    else:
+      proc = subprocess.Popen(args,
+                              stdout=stdout,
+                              stderr=stderr,
+                              env=env,
+                              preexec_fn=os.setsid,
+                              close_fds=True)
+      stdout, stderr = proc.communicate()
+      retcode = proc.wait()
+
     signal.alarm(0)
+      
   except TimeoutExpired:
     os.kill(-proc.pid, signal.SIGKILL)
     msg = "Process %s killed after timeout expiration" % args
     raise TimeoutExpired(msg)
 
-  retcode = proc.wait()
-  return retcode, stdout, stderr
+  if retcode and not allow_error:
+    logging.critical(stdout)
+    logging.critical(stderr)
+    raise Error("Running %s has failed." % repr(args))
 
+  if quiet:
+    return retcode, stdout, stderr
+  else:
+    return retcode
 

Modified: csw/mgar/gar/v2/lib/python/system_pkgmap.py
===================================================================
--- csw/mgar/gar/v2/lib/python/system_pkgmap.py	2013-05-04 09:56:05 UTC (rev 20980)
+++ csw/mgar/gar/v2/lib/python/system_pkgmap.py	2013-05-04 09:56:31 UTC (rev 20981)
@@ -285,7 +285,7 @@
     if uname_option:
       args.append(uname_option)
     # TODO: Don't fork during unit tests
-    ret, stdout, unused_stderr = shell.ShellCommand(args)
+    ret, stdout, unused_stderr = shell.ShellCommand(args, allow_error=True)
     if ret:
       raise SubprocessError("Running uname has failed.")
     return stdout.strip()

This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.



More information about the devel mailing list