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

wahwah at users.sourceforge.net wahwah at users.sourceforge.net
Sun Jan 13 18:38:22 CET 2013


Revision: 20111
          http://gar.svn.sourceforge.net/gar/?rev=20111&view=rev
Author:   wahwah
Date:     2013-01-13 17:38:22 +0000 (Sun, 13 Jan 2013)
Log Message:
-----------
checkpkg: Route shellouts through ShellCommand

Many commands were calling Popen in random places. They were all rerouted to
a common place.

Use ShellCommand

Modified Paths:
--------------
    csw/mgar/gar/v2/lib/python/inspective_package.py
    csw/mgar/gar/v2/lib/python/package.py
    csw/mgar/gar/v2/lib/python/pkgdb.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-01-13 17:37:18 UTC (rev 20110)
+++ csw/mgar/gar/v2/lib/python/inspective_package.py	2013-01-13 17:38:22 UTC (rev 20111)
@@ -8,11 +8,10 @@
 import magic
 import copy
 import common_constants
-import subprocess
 import ldd_emul
 import configuration as c
 import time
-import signal
+import shell
 
 """This file isolates code dependent on hachoir parser.
 
@@ -83,37 +82,6 @@
             "Error in hachoir_parser processing %s: %r", file_path, e)
   return file_info
 
-class TimeoutExpired(Exception):
-    pass
-
-def TimeoutHandler(signum, frame):
-  raise TimeoutExpired
-
-def ShellCommand(args, env=None, timeout=None):
-  logging.debug("Running: %s", args)
-  proc = subprocess.Popen(args,
-                          stdout=subprocess.PIPE,
-                          stderr=subprocess.PIPE,
-                          env=env,
-                          preexec_fn=os.setsid)
-  # Python 3.3 have the timeout option
-  # we have to roughly emulate it with python 2.x
-  if timeout:
-    signal.signal(signal.SIGALRM, TimeoutHandler)
-    signal.alarm(timeout)
-
-  try:
-    stdout, stderr = proc.communicate()
-    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
-
-
 class InspectivePackage(package.DirectoryFormatPackage):
   """Extends DirectoryFormatPackage to allow package inspection."""
 
@@ -221,7 +189,7 @@
       binary_abs_path = os.path.join(self.directory, self.GetFilesDir(), binary_in_tmp_dir)
       binary_base_name = os.path.basename(binary_in_tmp_dir)
       args = [common_constants.DUMP_BIN, "-Lv", binary_abs_path]
-      retcode, stdout, stderr = ShellCommand(args, env)
+      retcode, stdout, stderr = shell.ShellCommand(args, env)
       binary_data = ldd_emul.ParseDumpOutput(stdout)
       binary_data["path"] = binary
       if basedir:
@@ -248,14 +216,10 @@
       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]
-      nm_proc = subprocess.Popen(
-          args,
-          stdout=subprocess.PIPE,
-          stderr=subprocess.PIPE)
-      stdout, stderr = nm_proc.communicate()
-      retcode = nm_proc.wait()
+      retcode, stdout, stderr = shell.ShellCommand(args)
       if retcode:
         logging.error("%s returned an error: %s", args, stderr)
+      	# Should it just skip over an error?
         continue
       nm_out = stdout.splitlines()
 
@@ -291,7 +255,7 @@
       binary_abspath = os.path.join(self.directory, self.GetFilesDir(), binary)
       # elfdump is the only tool that give us all informations
       args = [common_constants.ELFDUMP_BIN, "-svy", binary_abspath]
-      retcode, stdout, stderr = ShellCommand(args)
+      retcode, stdout, stderr = shell.ShellCommand(args)
       if retcode or stderr:
         # we ignore for now these elfdump errors which can be catched
         # later by check functions,
@@ -394,7 +358,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 = ShellCommand(args, timeout=10)
+      retcode, stdout, stderr = shell.ShellCommand(args, timeout=10)
       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/package.py
===================================================================
--- csw/mgar/gar/v2/lib/python/package.py	2013-01-13 17:37:18 UTC (rev 20110)
+++ csw/mgar/gar/v2/lib/python/package.py	2013-01-13 17:38:22 UTC (rev 20111)
@@ -113,11 +113,7 @@
             src_file,
             destdir,
             pkgname ]
-    pkgtrans_proc = subprocess.Popen(args,
-                                     stdout=subprocess.PIPE,
-                                     stderr=subprocess.PIPE)
-    stdout, stderr = pkgtrans_proc.communicate()
-    ret = pkgtrans_proc.wait()
+    ret, stdout, stderr = shell.ShellCommand(args)
     if ret:
       logging.error(stdout)
       logging.error(stderr)
@@ -130,9 +126,7 @@
     if not self.pkgname:
       gunzipped_path = self.GetGunzippedPath()
       args = ["nawk", "NR == 2 {print $1; exit;}", gunzipped_path]
-      nawk_proc = subprocess.Popen(args, stdout=subprocess.PIPE)
-      stdout, stderr = nawk_proc.communicate()
-      ret_code = nawk_proc.wait()
+      ret_code, stdout, stderr = shell.ShellCommand(args)
       self.pkgname = stdout.strip()
       logging.debug("GetPkgname(): %s", repr(self.pkgname))
     return self.pkgname
@@ -204,11 +198,7 @@
   def GetPkgchkOutput(self):
     """Returns: (exit code, stdout, stderr)."""
     args = ["/usr/sbin/pkgchk", "-d", self.GetGunzippedPath(), "all"]
-    pkgchk_proc = subprocess.Popen(
-        args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
-    stdout, stderr = pkgchk_proc.communicate()
-    ret = pkgchk_proc.wait()
-    return ret, stdout, stderr
+    return shell.ShellCommand(args)
 
   def __del__(self):
     if self.workdir:
@@ -308,13 +298,10 @@
         # 4: sum
         pkginfo_path = os.path.join(self.directory, "pkginfo")
         args = ["cksum", pkginfo_path]
-        cksum_process = subprocess.Popen(args, stdout=subprocess.PIPE)
-        stdout, stderr = cksum_process.communicate()
-        cksum_process.wait()
+        _, stdout, stderr = shell.ShellCommand(args)
         size = ws_re.split(stdout)[1]
         args = ["sum", pkginfo_path]
-        sum_process = subprocess.Popen(args, stdout=subprocess.PIPE)
-        stdout, stderr = sum_process.communicate()
+        _, stdout, stderr = shell.ShellCommand(args)
         sum_process.wait()
         sum_value = ws_re.split(stdout)[0]
         fields[3] = size

Modified: csw/mgar/gar/v2/lib/python/pkgdb.py
===================================================================
--- csw/mgar/gar/v2/lib/python/pkgdb.py	2013-01-13 17:37:18 UTC (rev 20110)
+++ csw/mgar/gar/v2/lib/python/pkgdb.py	2013-01-13 17:38:22 UTC (rev 20111)
@@ -20,6 +20,7 @@
 import package_checks
 import package_stats
 import re
+import shell
 import socket
 import sqlobject
 import struct_util
@@ -326,6 +327,8 @@
           "The catalog release %s is not one of the default releases.",
           repr(catrel))
     sqo_catrel = m.CatalogRelease.selectBy(name=catrel).getOne()
+    _, uname_stdout, _ = shell.ShellCommand(["uname", "-p"])
+    current_host_arch = uname_stdout.strip()
     for osrel in common_constants.OS_RELS:
       logging.info("  OS release: %s", repr(osrel))
       sqo_osrel = m.OsRelease.selectBy(short_name=osrel).getOne()

Modified: csw/mgar/gar/v2/lib/python/shell.py
===================================================================
--- csw/mgar/gar/v2/lib/python/shell.py	2013-01-13 17:37:18 UTC (rev 20110)
+++ csw/mgar/gar/v2/lib/python/shell.py	2013-01-13 17:38:22 UTC (rev 20111)
@@ -1,12 +1,20 @@
 import logging
+import os
+import signal
 import subprocess
 
 class Error(Exception):
-  "Generic error"
+  """Generic error"""
 
 class ShellError(Error):
-  "Problem running a shell command."
+  """Problem running a shell command."""
 
+class TimeoutExpired(Error):
+  pass
+
+def TimeoutHandler(signum, frame):
+  raise TimeoutExpired
+
 class ShellMixin(object):
 
   def ShellCommand(self, args, quiet=False):
@@ -25,3 +33,30 @@
       logging.critical(stderr)
       raise Error("Running %s has failed." % repr(args))
     return retcode
+
+def ShellCommand(args, env=None, timeout=None):
+  logging.debug("Running: %s", args)
+  proc = subprocess.Popen(args,
+                          stdout=subprocess.PIPE,
+                          stderr=subprocess.PIPE,
+                          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:
+    signal.signal(signal.SIGALRM, TimeoutHandler)
+    signal.alarm(timeout)
+
+  try:
+    stdout, stderr = proc.communicate()
+    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
+
+

Modified: csw/mgar/gar/v2/lib/python/system_pkgmap.py
===================================================================
--- csw/mgar/gar/v2/lib/python/system_pkgmap.py	2013-01-13 17:37:18 UTC (rev 20110)
+++ csw/mgar/gar/v2/lib/python/system_pkgmap.py	2013-01-13 17:38:22 UTC (rev 20111)
@@ -3,7 +3,6 @@
 
 import re
 import configuration as c
-import subprocess
 import logging
 import common_constants
 import marshal
@@ -285,10 +284,7 @@
     if uname_option:
       args.append(uname_option)
     # TODO: Don't fork during unit tests
-    uname_proc = subprocess.Popen(args,
-                                  stdout=subprocess.PIPE)
-    stdout, stderr = uname_proc.communicate()
-    ret = uname_proc.wait()
+    ret, stdout, unused_stderr = shell.ShellCommand(args)
     if ret:
       raise SubprocessError("Running uname has failed.")
     return stdout.strip()
@@ -352,9 +348,7 @@
     args = ["pkg", "contents", "-H", "-o",
             "path,action.name,pkg.name,target,mode,owner,group",
             "-t", "dir,file,hardlink,link"]
-    pkg_proc = subprocess.Popen(args, stdout=subprocess.PIPE)
-    stdout, stderr = pkg_proc.communicate()
-    ret = pkg_proc.wait()
+    ret, stdout, unused_stderr = shell.ShellCommand(args)
     return stdout.splitlines()
 
   def _GetSrv4PkginfosStream(self):
@@ -363,18 +357,14 @@
       pkginfo_stream = open(self.infile_pkginfo, "r")
     else:
       args = ["pkginfo"]
-      pkginfo_proc = subprocess.Popen(args, stdout=subprocess.PIPE)
-      stdout, stderr = pkginfo_proc.communicate()
-      ret = pkginfo_proc.wait()
+      ret, stdout, stderr = shell.ShellCommand(args)
       pkginfo_stream = stdout.splitlines()
 
     return pkginfo_stream
 
   def _GetIpsPkginfosStream(self):
     args = ["pkg", "list", "-H", "-s"]
-    pkg_proc = subprocess.Popen(args, stdout=subprocess.PIPE)
-    stdout, stderr = pkg_proc.communicate()
-    ret = pkg_proc.wait()
+    ret, stdout, stderr = shell.ShellCommand(args)
     pkglist_stream = stdout.splitlines()
     return pkglist_stream
 

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