[Python-modules-commits] r9691 - in tools (find_python_dependencies.py)
dktrkranz at users.alioth.debian.org
dktrkranz at users.alioth.debian.org
Mon Sep 7 14:21:04 UTC 2009
Date: Monday, September 7, 2009 @ 14:21:03
Author: dktrkranz
Revision: 9691
Search for Python modules recursively
Modified:
tools/find_python_dependencies.py
Modified: tools/find_python_dependencies.py
===================================================================
--- tools/find_python_dependencies.py 2009-09-07 13:32:00 UTC (rev 9690)
+++ tools/find_python_dependencies.py 2009-09-07 14:21:03 UTC (rev 9691)
@@ -1,6 +1,7 @@
#!/usr/bin/python
#
# Copyright (c) 2008 Sandro Tosi <morph at debian.org>
+# Copyright (c) 2009 Luca Falavigna <dktrkranz at debian.org>
# License: Public Domain
#
#
@@ -109,24 +110,24 @@
for subfile in find_py_files_in_dir_recursive(subpath):
yield subfile
-def parse_file_import(file):
- """Parses file's syntax tree to extract import statements"""
- try:
- # creates the syntax tree
- mod = compiler.parseFile(file)
+def parse_file_import(data):
+ try:
+ # scan nodes...
+ for child in data.getChildren():
+ # ... until you reach an Import object...
+ if isinstance(child, Import):
+ # ... then add it to import dict
+ for name, alias in child.names:
+ add_value_to_dict(import_dict, name, 1)
+ # the same for From objects
+ elif isinstance(child, From):
+ add_value_to_dict(import_dict, child.modname, 1)
+ else:
+ # if object is not From or Import, check his childred
+ parse_file_import(child)
+ except:
+ pass
- nodes = mod.node.nodes
-
- for node in nodes:
- if isinstance(node, Import):
- for name, alias in node.names:
- yield name
- if isinstance(node, From):
- yield node.modname
- except Exception, e:
- print "Error parsing " + file + "; exception: " + str(e)
-
-
def add_value_to_dict(dict, key, value):
"""Adds value to dict[key], or add the item if missing"""
if key in dict:
@@ -143,8 +144,11 @@
# main file parse loop
for file in find_py_files_in_dir_recursive(sys.argv[1]):
- for imp in parse_file_import(file):
- add_value_to_dict(import_dict, imp, 1)
+ try:
+ # parses the syntax tree
+ parse_file_import(compiler.parseFile(file))
+ except Exception, e:
+ print "Error parsing " + file + "; exception: " + str(e)
# loop to identify the deb pkg containg each module, or add to discards list
for module, count in import_dict.iteritems():
More information about the Python-modules-commits
mailing list