pythondeps 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #!/usr/bin/env python3
  2. #
  3. # Determine dependencies of python scripts or available python modules in a search path.
  4. #
  5. # Given the -d argument and a filename/filenames, returns the modules imported by those files.
  6. # Given the -d argument and a directory/directories, recurses to find all
  7. # python packages and modules, returns the modules imported by these.
  8. # Given the -p argument and a path or paths, scans that path for available python modules/packages.
  9. import argparse
  10. import ast
  11. import importlib
  12. from importlib import machinery
  13. import logging
  14. import os.path
  15. import sys
  16. logger = logging.getLogger('pythondeps')
  17. suffixes = importlib.machinery.all_suffixes()
  18. class PythonDepError(Exception):
  19. pass
  20. class DependError(PythonDepError):
  21. def __init__(self, path, error):
  22. self.path = path
  23. self.error = error
  24. PythonDepError.__init__(self, error)
  25. def __str__(self):
  26. return "Failure determining dependencies of {}: {}".format(self.path, self.error)
  27. class ImportVisitor(ast.NodeVisitor):
  28. def __init__(self):
  29. self.imports = set()
  30. self.importsfrom = []
  31. def visit_Import(self, node):
  32. for alias in node.names:
  33. self.imports.add(alias.name)
  34. def visit_ImportFrom(self, node):
  35. self.importsfrom.append((node.module, [a.name for a in node.names], node.level))
  36. def walk_up(path):
  37. while path:
  38. yield path
  39. path, _, _ = path.rpartition(os.sep)
  40. def get_provides(path):
  41. path = os.path.realpath(path)
  42. def get_fn_name(fn):
  43. for suffix in suffixes:
  44. if fn.endswith(suffix):
  45. return fn[:-len(suffix)]
  46. isdir = os.path.isdir(path)
  47. if isdir:
  48. pkg_path = path
  49. walk_path = path
  50. else:
  51. pkg_path = get_fn_name(path)
  52. if pkg_path is None:
  53. return
  54. walk_path = os.path.dirname(path)
  55. for curpath in walk_up(walk_path):
  56. if not os.path.exists(os.path.join(curpath, '__init__.py')):
  57. libdir = curpath
  58. break
  59. else:
  60. libdir = ''
  61. package_relpath = pkg_path[len(libdir)+1:]
  62. package = '.'.join(package_relpath.split(os.sep))
  63. if not isdir:
  64. yield package, path
  65. else:
  66. if os.path.exists(os.path.join(path, '__init__.py')):
  67. yield package, path
  68. for dirpath, dirnames, filenames in os.walk(path):
  69. relpath = dirpath[len(path)+1:]
  70. if relpath:
  71. if '__init__.py' not in filenames:
  72. dirnames[:] = []
  73. continue
  74. else:
  75. context = '.'.join(relpath.split(os.sep))
  76. if package:
  77. context = package + '.' + context
  78. yield context, dirpath
  79. else:
  80. context = package
  81. for fn in filenames:
  82. adjusted_fn = get_fn_name(fn)
  83. if not adjusted_fn or adjusted_fn == '__init__':
  84. continue
  85. fullfn = os.path.join(dirpath, fn)
  86. if context:
  87. yield context + '.' + adjusted_fn, fullfn
  88. else:
  89. yield adjusted_fn, fullfn
  90. def get_code_depends(code_string, path=None, provide=None, ispkg=False):
  91. try:
  92. code = ast.parse(code_string, path)
  93. except TypeError as exc:
  94. raise DependError(path, exc)
  95. except SyntaxError as exc:
  96. raise DependError(path, exc)
  97. visitor = ImportVisitor()
  98. visitor.visit(code)
  99. for builtin_module in sys.builtin_module_names:
  100. if builtin_module in visitor.imports:
  101. visitor.imports.remove(builtin_module)
  102. if provide:
  103. provide_elements = provide.split('.')
  104. if ispkg:
  105. provide_elements.append("__self__")
  106. context = '.'.join(provide_elements[:-1])
  107. package_path = os.path.dirname(path)
  108. else:
  109. context = None
  110. package_path = None
  111. levelzero_importsfrom = (module for module, names, level in visitor.importsfrom
  112. if level == 0)
  113. for module in visitor.imports | set(levelzero_importsfrom):
  114. if context and path:
  115. module_basepath = os.path.join(package_path, module.replace('.', '/'))
  116. if os.path.exists(module_basepath):
  117. # Implicit relative import
  118. yield context + '.' + module, path
  119. continue
  120. for suffix in suffixes:
  121. if os.path.exists(module_basepath + suffix):
  122. # Implicit relative import
  123. yield context + '.' + module, path
  124. break
  125. else:
  126. yield module, path
  127. else:
  128. yield module, path
  129. for module, names, level in visitor.importsfrom:
  130. if level == 0:
  131. continue
  132. elif not provide:
  133. raise DependError("Error: ImportFrom non-zero level outside of a package: {0}".format((module, names, level)), path)
  134. elif level > len(provide_elements):
  135. raise DependError("Error: ImportFrom level exceeds package depth: {0}".format((module, names, level)), path)
  136. else:
  137. context = '.'.join(provide_elements[:-level])
  138. if module:
  139. if context:
  140. yield context + '.' + module, path
  141. else:
  142. yield module, path
  143. def get_file_depends(path):
  144. try:
  145. code_string = open(path, 'r').read()
  146. except (OSError, IOError) as exc:
  147. raise DependError(path, exc)
  148. return get_code_depends(code_string, path)
  149. def get_depends_recursive(directory):
  150. directory = os.path.realpath(directory)
  151. provides = dict((v, k) for k, v in get_provides(directory))
  152. for filename, provide in provides.items():
  153. if os.path.isdir(filename):
  154. filename = os.path.join(filename, '__init__.py')
  155. ispkg = True
  156. elif not filename.endswith('.py'):
  157. continue
  158. else:
  159. ispkg = False
  160. with open(filename, 'r') as f:
  161. source = f.read()
  162. depends = get_code_depends(source, filename, provide, ispkg)
  163. for depend, by in depends:
  164. yield depend, by
  165. def get_depends(path):
  166. if os.path.isdir(path):
  167. return get_depends_recursive(path)
  168. else:
  169. return get_file_depends(path)
  170. def main():
  171. logging.basicConfig()
  172. parser = argparse.ArgumentParser(description='Determine dependencies and provided packages for python scripts/modules')
  173. parser.add_argument('path', nargs='+', help='full path to content to be processed')
  174. group = parser.add_mutually_exclusive_group()
  175. group.add_argument('-p', '--provides', action='store_true',
  176. help='given a path, display the provided python modules')
  177. group.add_argument('-d', '--depends', action='store_true',
  178. help='given a filename, display the imported python modules')
  179. args = parser.parse_args()
  180. if args.provides:
  181. modules = set()
  182. for path in args.path:
  183. for provide, fn in get_provides(path):
  184. modules.add(provide)
  185. for module in sorted(modules):
  186. print(module)
  187. elif args.depends:
  188. for path in args.path:
  189. try:
  190. modules = get_depends(path)
  191. except PythonDepError as exc:
  192. logger.error(str(exc))
  193. sys.exit(1)
  194. for module, imp_by in modules:
  195. print("{}\t{}".format(module, imp_by))
  196. else:
  197. parser.print_help()
  198. sys.exit(2)
  199. if __name__ == '__main__':
  200. main()