[csw-devel] SF.net SVN: gar:[20010] csw/mgar/gar/v2-yann/lib/python/inspective_package. py

chninkel at users.sourceforge.net chninkel at users.sourceforge.net
Wed Jan 2 23:38:44 CET 2013


Revision: 20010
          http://gar.svn.sourceforge.net/gar/?rev=20010&view=rev
Author:   chninkel
Date:     2013-01-02 22:38:43 +0000 (Wed, 02 Jan 2013)
Log Message:
-----------
gar/v2-yann: added a timeout option to shell command execution

Modified Paths:
--------------
    csw/mgar/gar/v2-yann/lib/python/inspective_package.py

Modified: csw/mgar/gar/v2-yann/lib/python/inspective_package.py
===================================================================
--- csw/mgar/gar/v2-yann/lib/python/inspective_package.py	2013-01-02 14:56:18 UTC (rev 20009)
+++ csw/mgar/gar/v2-yann/lib/python/inspective_package.py	2013-01-02 22:38:43 UTC (rev 20010)
@@ -10,6 +10,7 @@
 import subprocess
 import ldd_emul
 import configuration as c
+import time
 
 """This file isolates code dependent on hachoir parser.
 
@@ -76,13 +77,30 @@
             "Error in hachoir_parser processing %s: %r", file_path, e)
   return file_info
 
+class TimeoutExpired(Exception):
+    pass
 
-def ShellCommand(args, env=None):
+def ShellCommand(args, env=None, timeout=None):
   logging.debug("Running: %s", args)
   proc = subprocess.Popen(args,
                           stdout=subprocess.PIPE,
                           stderr=subprocess.PIPE,
                           env=env)
+  # Python 3.3 have the timeout option
+  # we have to roughly emulate it with python 2.x
+  if timeout:
+    max_time = time.time() + timeout
+    while True:
+      proc.poll()
+      if proc.returncode is None:
+        time.sleep(0.1)
+        if time.time() >= max_time:
+          proc.kill()
+          msg = "Process %s killed after timeout expiration" % args
+          raise TimeoutExpired(msg)
+      else:
+        break
+
   stdout, stderr = proc.communicate()
   retcode = proc.wait()
 
@@ -360,7 +378,9 @@
       # ldd needs the binary to be executable
       os.chmod(binary_abspath, 0755)
       args = ["ldd", "-Ur", binary_abspath]
-      retcode, stdout, stderr = ShellCommand(args)
+      # 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)
       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

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