[Python-modules-commits] r11458 - in tools (find_python_dependencies.py)

dktrkranz at users.alioth.debian.org dktrkranz at users.alioth.debian.org
Sat Jan 30 15:34:36 UTC 2010


    Date: Saturday, January 30, 2010 @ 15:34:35
  Author: dktrkranz
Revision: 11458

Find all files with Python shebang in a given directory

Modified:
  tools/find_python_dependencies.py

Modified: tools/find_python_dependencies.py
===================================================================
--- tools/find_python_dependencies.py	2010-01-30 14:59:22 UTC (rev 11457)
+++ tools/find_python_dependencies.py	2010-01-30 15:34:35 UTC (rev 11458)
@@ -1,7 +1,7 @@
 #!/usr/bin/python
 #
 # Copyright (c) 2008 Sandro Tosi <morph at debian.org>
-# Copyright (c) 2009 Luca Falavigna <dktrkranz at debian.org>
+# Copyright (c) 2009-2010 Luca Falavigna <dktrkranz at debian.org>
 # License: Public Domain
 #
 #
@@ -41,7 +41,10 @@
 import dircache
 import stat
 import subprocess
+import re
 
+python_shebang = re.compile('#!/usr/bin/(env |)?python(\d\.\d|)?$')
+
 class ImportNotFound(Exception):
     """Errors in importing a module"""
     pass
@@ -95,11 +98,28 @@
     for match in glob.glob(os.path.join(path, "*.[Pp][Yy]")):
         yield match
 
+def find_py_shebang_in_dir(path):
+    """Find all files with Python shebang in a given directory"""
+    # pattern matching any case of "py" extension
+    for match in glob.glob(os.path.join(path, "*")):
+        if not os.path.isfile(match):
+            continue
+        try:
+            f = open(match)
+        except IOError:
+            continue
+        shebang = f.readline()
+        f.close()
+        if re.match(python_shebang, shebang):
+            yield match
+
 def find_py_files_in_dir_recursive(path):
     """Find all py files in a given directory, then go recursing subdirs"""
     # check first in the dir passed as parameter
     for match in find_py_files_in_dir(path):
         yield match
+    for match in find_py_shebang_in_dir(path):
+        yield match
     # dircache output is sorted and cached
     # let's join path and item, since files list
     # returned from listdir has path stripped off




More information about the Python-modules-commits mailing list