diagnose_imports.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #!/usr/bin/env python
  2. """
  3. Import diagnostics. Run bin/diagnose_imports.py --help for details.
  4. """
  5. from __future__ import annotations
  6. if __name__ == "__main__":
  7. import sys
  8. import inspect
  9. import builtins
  10. import optparse
  11. from os.path import abspath, dirname, join, normpath
  12. this_file = abspath(__file__)
  13. sympy_dir = join(dirname(this_file), '..', '..', '..')
  14. sympy_dir = normpath(sympy_dir)
  15. sys.path.insert(0, sympy_dir)
  16. option_parser = optparse.OptionParser(
  17. usage=
  18. "Usage: %prog option [options]\n"
  19. "\n"
  20. "Import analysis for imports between SymPy modules.")
  21. option_group = optparse.OptionGroup(
  22. option_parser,
  23. 'Analysis options',
  24. 'Options that define what to do. Exactly one of these must be given.')
  25. option_group.add_option(
  26. '--problems',
  27. help=
  28. 'Print all import problems, that is: '
  29. 'If an import pulls in a package instead of a module '
  30. '(e.g. sympy.core instead of sympy.core.add); ' # see ##PACKAGE##
  31. 'if it imports a symbol that is already present; ' # see ##DUPLICATE##
  32. 'if it imports a symbol '
  33. 'from somewhere other than the defining module.', # see ##ORIGIN##
  34. action='count')
  35. option_group.add_option(
  36. '--origins',
  37. help=
  38. 'For each imported symbol in each module, '
  39. 'print the module that defined it. '
  40. '(This is useful for import refactoring.)',
  41. action='count')
  42. option_parser.add_option_group(option_group)
  43. option_group = optparse.OptionGroup(
  44. option_parser,
  45. 'Sort options',
  46. 'These options define the sort order for output lines. '
  47. 'At most one of these options is allowed. '
  48. 'Unsorted output will reflect the order in which imports happened.')
  49. option_group.add_option(
  50. '--by-importer',
  51. help='Sort output lines by name of importing module.',
  52. action='count')
  53. option_group.add_option(
  54. '--by-origin',
  55. help='Sort output lines by name of imported module.',
  56. action='count')
  57. option_parser.add_option_group(option_group)
  58. (options, args) = option_parser.parse_args()
  59. if args:
  60. option_parser.error(
  61. 'Unexpected arguments %s (try %s --help)' % (args, sys.argv[0]))
  62. if options.problems > 1:
  63. option_parser.error('--problems must not be given more than once.')
  64. if options.origins > 1:
  65. option_parser.error('--origins must not be given more than once.')
  66. if options.by_importer > 1:
  67. option_parser.error('--by-importer must not be given more than once.')
  68. if options.by_origin > 1:
  69. option_parser.error('--by-origin must not be given more than once.')
  70. options.problems = options.problems == 1
  71. options.origins = options.origins == 1
  72. options.by_importer = options.by_importer == 1
  73. options.by_origin = options.by_origin == 1
  74. if not options.problems and not options.origins:
  75. option_parser.error(
  76. 'At least one of --problems and --origins is required')
  77. if options.problems and options.origins:
  78. option_parser.error(
  79. 'At most one of --problems and --origins is allowed')
  80. if options.by_importer and options.by_origin:
  81. option_parser.error(
  82. 'At most one of --by-importer and --by-origin is allowed')
  83. options.by_process = not options.by_importer and not options.by_origin
  84. builtin_import = builtins.__import__
  85. class Definition:
  86. """Information about a symbol's definition."""
  87. def __init__(self, name, value, definer):
  88. self.name = name
  89. self.value = value
  90. self.definer = definer
  91. def __hash__(self):
  92. return hash(self.name)
  93. def __eq__(self, other):
  94. return self.name == other.name and self.value == other.value
  95. def __ne__(self, other):
  96. return not (self == other)
  97. def __repr__(self):
  98. return 'Definition(%s, ..., %s)' % (
  99. repr(self.name), repr(self.definer))
  100. # Maps each function/variable to name of module to define it
  101. symbol_definers: dict[Definition, str] = {}
  102. def in_module(a, b):
  103. """Is a the same module as or a submodule of b?"""
  104. return a == b or a != None and b != None and a.startswith(b + '.')
  105. def relevant(module):
  106. """Is module relevant for import checking?
  107. Only imports between relevant modules will be checked."""
  108. return in_module(module, 'sympy')
  109. sorted_messages = []
  110. def msg(msg, *args):
  111. global options, sorted_messages
  112. if options.by_process:
  113. print(msg % args)
  114. else:
  115. sorted_messages.append(msg % args)
  116. def tracking_import(module, globals=globals(), locals=[], fromlist=None, level=-1):
  117. """__import__ wrapper - does not change imports at all, but tracks them.
  118. Default order is implemented by doing output directly.
  119. All other orders are implemented by collecting output information into
  120. a sorted list that will be emitted after all imports are processed.
  121. Indirect imports can only occur after the requested symbol has been
  122. imported directly (because the indirect import would not have a module
  123. to pick the symbol up from).
  124. So this code detects indirect imports by checking whether the symbol in
  125. question was already imported.
  126. Keeps the semantics of __import__ unchanged."""
  127. global options, symbol_definers
  128. caller_frame = inspect.getframeinfo(sys._getframe(1))
  129. importer_filename = caller_frame.filename
  130. importer_module = globals['__name__']
  131. if importer_filename == caller_frame.filename:
  132. importer_reference = '%s line %s' % (
  133. importer_filename, str(caller_frame.lineno))
  134. else:
  135. importer_reference = importer_filename
  136. result = builtin_import(module, globals, locals, fromlist, level)
  137. importee_module = result.__name__
  138. # We're only interested if importer and importee are in SymPy
  139. if relevant(importer_module) and relevant(importee_module):
  140. for symbol in result.__dict__.iterkeys():
  141. definition = Definition(
  142. symbol, result.__dict__[symbol], importer_module)
  143. if definition not in symbol_definers:
  144. symbol_definers[definition] = importee_module
  145. if hasattr(result, '__path__'):
  146. ##PACKAGE##
  147. # The existence of __path__ is documented in the tutorial on modules.
  148. # Python 3.3 documents this in http://docs.python.org/3.3/reference/import.html
  149. if options.by_origin:
  150. msg('Error: %s (a package) is imported by %s',
  151. module, importer_reference)
  152. else:
  153. msg('Error: %s contains package import %s',
  154. importer_reference, module)
  155. if fromlist != None:
  156. symbol_list = fromlist
  157. if '*' in symbol_list:
  158. if (importer_filename.endswith('__init__.py')
  159. or importer_filename.endswith('__init__.pyc')
  160. or importer_filename.endswith('__init__.pyo')):
  161. # We do not check starred imports inside __init__
  162. # That's the normal "please copy over its imports to my namespace"
  163. symbol_list = []
  164. else:
  165. symbol_list = result.__dict__.iterkeys()
  166. for symbol in symbol_list:
  167. if symbol not in result.__dict__:
  168. if options.by_origin:
  169. msg('Error: %s.%s is not defined (yet), but %s tries to import it',
  170. importee_module, symbol, importer_reference)
  171. else:
  172. msg('Error: %s tries to import %s.%s, which did not define it (yet)',
  173. importer_reference, importee_module, symbol)
  174. else:
  175. definition = Definition(
  176. symbol, result.__dict__[symbol], importer_module)
  177. symbol_definer = symbol_definers[definition]
  178. if symbol_definer == importee_module:
  179. ##DUPLICATE##
  180. if options.by_origin:
  181. msg('Error: %s.%s is imported again into %s',
  182. importee_module, symbol, importer_reference)
  183. else:
  184. msg('Error: %s imports %s.%s again',
  185. importer_reference, importee_module, symbol)
  186. else:
  187. ##ORIGIN##
  188. if options.by_origin:
  189. msg('Error: %s.%s is imported by %s, which should import %s.%s instead',
  190. importee_module, symbol, importer_reference, symbol_definer, symbol)
  191. else:
  192. msg('Error: %s imports %s.%s but should import %s.%s instead',
  193. importer_reference, importee_module, symbol, symbol_definer, symbol)
  194. return result
  195. builtins.__import__ = tracking_import
  196. __import__('sympy')
  197. sorted_messages.sort()
  198. for message in sorted_messages:
  199. print(message)