blob: 995c686a650a2edb14f54790aab6c78fe28906f0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
#!/usr/bin/env python
# Copyright 2007 Nanorex, Inc. See LICENSE file for details.
"""
FindExternalImports.py
Takes a list of python files as arguments, writes a list of
packages that any of them import outside of the input list.
Suitable for feeding to SymbolsInPackage.py to create an expanded
global symbol list for use by ResolveGlobals.py.
"""
import sys
import re
fromImportLineRegex = re.compile(r'^\s*from\s+(\S+)\s+import\s')
importLineRegex = re.compile(r'^\s*import\s+([^#]+)')
asRegex = re.compile(r'^(\S+)\s+as\s+')
allProcessedModules = set([])
importedModules = set([])
def fileNameToModuleName(fileName):
if (fileName.startswith("./")):
fileName = fileName[2:]
if (fileName.startswith("src/")):
fileName = fileName[4:]
if (fileName.endswith(".py")):
fileName = fileName[:-3]
fileName = fileName.replace("/", ".")
return fileName
def collectImportedModules(fileName):
importSet = set([])
fromModuleName = fileNameToModuleName(fileName)
allProcessedModules.add(fromModuleName)
f = open(fileName)
for line in f:
m = fromImportLineRegex.match(line)
if (m):
toModuleName = m.group(1)
importSet.add(toModuleName)
continue
m = importLineRegex.match(line)
if (m):
toModuleList = m.group(1).strip().split(',')
for toModuleName in toModuleList:
toModuleName = toModuleName.strip()
m = asRegex.match(toModuleName)
if (m):
toModuleName = m.group(1)
importSet.add(toModuleName)
f.close()
importedModules.update(importSet)
if (__name__ == '__main__'):
for sourceFile in sys.argv[1:]:
collectImportedModules(sourceFile)
externalImports = importedModules.difference(allProcessedModules)
importList = list(externalImports)
importList.sort()
for moduleName in importList:
print moduleName
|