##// END OF EJS Templates
import-checker: allow symbol imports from typing module...
Gregory Szorc -
r49816:8dec9abf default
parent child Browse files
Show More
@@ -1,769 +1,770 b''
1 #!/usr/bin/env python3
1 #!/usr/bin/env python3
2
2
3
3
4 import ast
4 import ast
5 import collections
5 import collections
6 import io
6 import io
7 import os
7 import os
8 import sys
8 import sys
9
9
10 # Import a minimal set of stdlib modules needed for list_stdlib_modules()
10 # Import a minimal set of stdlib modules needed for list_stdlib_modules()
11 # to work when run from a virtualenv. The modules were chosen empirically
11 # to work when run from a virtualenv. The modules were chosen empirically
12 # so that the return value matches the return value without virtualenv.
12 # so that the return value matches the return value without virtualenv.
13 if True: # disable lexical sorting checks
13 if True: # disable lexical sorting checks
14 try:
14 try:
15 import BaseHTTPServer as basehttpserver
15 import BaseHTTPServer as basehttpserver
16 except ImportError:
16 except ImportError:
17 basehttpserver = None
17 basehttpserver = None
18 import zlib
18 import zlib
19
19
20 import testparseutil
20 import testparseutil
21
21
22 # Whitelist of modules that symbols can be directly imported from.
22 # Allow list of modules that symbols can be directly imported from.
23 allowsymbolimports = (
23 allowsymbolimports = (
24 '__future__',
24 '__future__',
25 'breezy',
25 'breezy',
26 'concurrent',
26 'concurrent',
27 'hgclient',
27 'hgclient',
28 'mercurial',
28 'mercurial',
29 'mercurial.hgweb.common',
29 'mercurial.hgweb.common',
30 'mercurial.hgweb.request',
30 'mercurial.hgweb.request',
31 'mercurial.i18n',
31 'mercurial.i18n',
32 'mercurial.interfaces',
32 'mercurial.interfaces',
33 'mercurial.node',
33 'mercurial.node',
34 'mercurial.pycompat',
34 'mercurial.pycompat',
35 # for revlog to re-export constant to extensions
35 # for revlog to re-export constant to extensions
36 'mercurial.revlogutils.constants',
36 'mercurial.revlogutils.constants',
37 'mercurial.revlogutils.flagutil',
37 'mercurial.revlogutils.flagutil',
38 # for cffi modules to re-export pure functions
38 # for cffi modules to re-export pure functions
39 'mercurial.pure.base85',
39 'mercurial.pure.base85',
40 'mercurial.pure.bdiff',
40 'mercurial.pure.bdiff',
41 'mercurial.pure.mpatch',
41 'mercurial.pure.mpatch',
42 'mercurial.pure.osutil',
42 'mercurial.pure.osutil',
43 'mercurial.pure.parsers',
43 'mercurial.pure.parsers',
44 # third-party imports should be directly imported
44 # third-party imports should be directly imported
45 'mercurial.thirdparty',
45 'mercurial.thirdparty',
46 'mercurial.thirdparty.attr',
46 'mercurial.thirdparty.attr',
47 'mercurial.thirdparty.zope',
47 'mercurial.thirdparty.zope',
48 'mercurial.thirdparty.zope.interface',
48 'mercurial.thirdparty.zope.interface',
49 'typing',
49 )
50 )
50
51
51 # Whitelist of symbols that can be directly imported.
52 # Allow list of symbols that can be directly imported.
52 directsymbols = ('demandimport',)
53 directsymbols = ('demandimport',)
53
54
54 # Modules that must be aliased because they are commonly confused with
55 # Modules that must be aliased because they are commonly confused with
55 # common variables and can create aliasing and readability issues.
56 # common variables and can create aliasing and readability issues.
56 requirealias = {
57 requirealias = {
57 'ui': 'uimod',
58 'ui': 'uimod',
58 }
59 }
59
60
60
61
61 def walklocal(root):
62 def walklocal(root):
62 """Recursively yield all descendant nodes but not in a different scope"""
63 """Recursively yield all descendant nodes but not in a different scope"""
63 todo = collections.deque(ast.iter_child_nodes(root))
64 todo = collections.deque(ast.iter_child_nodes(root))
64 yield root, False
65 yield root, False
65 while todo:
66 while todo:
66 node = todo.popleft()
67 node = todo.popleft()
67 newscope = isinstance(node, ast.FunctionDef)
68 newscope = isinstance(node, ast.FunctionDef)
68 if not newscope:
69 if not newscope:
69 todo.extend(ast.iter_child_nodes(node))
70 todo.extend(ast.iter_child_nodes(node))
70 yield node, newscope
71 yield node, newscope
71
72
72
73
73 def dotted_name_of_path(path):
74 def dotted_name_of_path(path):
74 """Given a relative path to a source file, return its dotted module name.
75 """Given a relative path to a source file, return its dotted module name.
75
76
76 >>> dotted_name_of_path('mercurial/error.py')
77 >>> dotted_name_of_path('mercurial/error.py')
77 'mercurial.error'
78 'mercurial.error'
78 >>> dotted_name_of_path('zlibmodule.so')
79 >>> dotted_name_of_path('zlibmodule.so')
79 'zlib'
80 'zlib'
80 """
81 """
81 parts = path.replace(os.sep, '/').split('/')
82 parts = path.replace(os.sep, '/').split('/')
82 parts[-1] = parts[-1].split('.', 1)[0] # remove .py and .so and .ARCH.so
83 parts[-1] = parts[-1].split('.', 1)[0] # remove .py and .so and .ARCH.so
83 if parts[-1].endswith('module'):
84 if parts[-1].endswith('module'):
84 parts[-1] = parts[-1][:-6]
85 parts[-1] = parts[-1][:-6]
85 return '.'.join(parts)
86 return '.'.join(parts)
86
87
87
88
88 def fromlocalfunc(modulename, localmods):
89 def fromlocalfunc(modulename, localmods):
89 """Get a function to examine which locally defined module the
90 """Get a function to examine which locally defined module the
90 target source imports via a specified name.
91 target source imports via a specified name.
91
92
92 `modulename` is an `dotted_name_of_path()`-ed source file path,
93 `modulename` is an `dotted_name_of_path()`-ed source file path,
93 which may have `.__init__` at the end of it, of the target source.
94 which may have `.__init__` at the end of it, of the target source.
94
95
95 `localmods` is a set of absolute `dotted_name_of_path()`-ed source file
96 `localmods` is a set of absolute `dotted_name_of_path()`-ed source file
96 paths of locally defined (= Mercurial specific) modules.
97 paths of locally defined (= Mercurial specific) modules.
97
98
98 This function assumes that module names not existing in
99 This function assumes that module names not existing in
99 `localmods` are from the Python standard library.
100 `localmods` are from the Python standard library.
100
101
101 This function returns the function, which takes `name` argument,
102 This function returns the function, which takes `name` argument,
102 and returns `(absname, dottedpath, hassubmod)` tuple if `name`
103 and returns `(absname, dottedpath, hassubmod)` tuple if `name`
103 matches against locally defined module. Otherwise, it returns
104 matches against locally defined module. Otherwise, it returns
104 False.
105 False.
105
106
106 It is assumed that `name` doesn't have `.__init__`.
107 It is assumed that `name` doesn't have `.__init__`.
107
108
108 `absname` is an absolute module name of specified `name`
109 `absname` is an absolute module name of specified `name`
109 (e.g. "hgext.convert"). This can be used to compose prefix for sub
110 (e.g. "hgext.convert"). This can be used to compose prefix for sub
110 modules or so.
111 modules or so.
111
112
112 `dottedpath` is a `dotted_name_of_path()`-ed source file path
113 `dottedpath` is a `dotted_name_of_path()`-ed source file path
113 (e.g. "hgext.convert.__init__") of `name`. This is used to look
114 (e.g. "hgext.convert.__init__") of `name`. This is used to look
114 module up in `localmods` again.
115 module up in `localmods` again.
115
116
116 `hassubmod` is whether it may have sub modules under it (for
117 `hassubmod` is whether it may have sub modules under it (for
117 convenient, even though this is also equivalent to "absname !=
118 convenient, even though this is also equivalent to "absname !=
118 dottednpath")
119 dottednpath")
119
120
120 >>> localmods = {'foo.__init__', 'foo.foo1',
121 >>> localmods = {'foo.__init__', 'foo.foo1',
121 ... 'foo.bar.__init__', 'foo.bar.bar1',
122 ... 'foo.bar.__init__', 'foo.bar.bar1',
122 ... 'baz.__init__', 'baz.baz1'}
123 ... 'baz.__init__', 'baz.baz1'}
123 >>> fromlocal = fromlocalfunc('foo.xxx', localmods)
124 >>> fromlocal = fromlocalfunc('foo.xxx', localmods)
124 >>> # relative
125 >>> # relative
125 >>> fromlocal('foo1')
126 >>> fromlocal('foo1')
126 ('foo.foo1', 'foo.foo1', False)
127 ('foo.foo1', 'foo.foo1', False)
127 >>> fromlocal('bar')
128 >>> fromlocal('bar')
128 ('foo.bar', 'foo.bar.__init__', True)
129 ('foo.bar', 'foo.bar.__init__', True)
129 >>> fromlocal('bar.bar1')
130 >>> fromlocal('bar.bar1')
130 ('foo.bar.bar1', 'foo.bar.bar1', False)
131 ('foo.bar.bar1', 'foo.bar.bar1', False)
131 >>> # absolute
132 >>> # absolute
132 >>> fromlocal('baz')
133 >>> fromlocal('baz')
133 ('baz', 'baz.__init__', True)
134 ('baz', 'baz.__init__', True)
134 >>> fromlocal('baz.baz1')
135 >>> fromlocal('baz.baz1')
135 ('baz.baz1', 'baz.baz1', False)
136 ('baz.baz1', 'baz.baz1', False)
136 >>> # unknown = maybe standard library
137 >>> # unknown = maybe standard library
137 >>> fromlocal('os')
138 >>> fromlocal('os')
138 False
139 False
139 >>> fromlocal(None, 1)
140 >>> fromlocal(None, 1)
140 ('foo', 'foo.__init__', True)
141 ('foo', 'foo.__init__', True)
141 >>> fromlocal('foo1', 1)
142 >>> fromlocal('foo1', 1)
142 ('foo.foo1', 'foo.foo1', False)
143 ('foo.foo1', 'foo.foo1', False)
143 >>> fromlocal2 = fromlocalfunc('foo.xxx.yyy', localmods)
144 >>> fromlocal2 = fromlocalfunc('foo.xxx.yyy', localmods)
144 >>> fromlocal2(None, 2)
145 >>> fromlocal2(None, 2)
145 ('foo', 'foo.__init__', True)
146 ('foo', 'foo.__init__', True)
146 >>> fromlocal2('bar2', 1)
147 >>> fromlocal2('bar2', 1)
147 False
148 False
148 >>> fromlocal2('bar', 2)
149 >>> fromlocal2('bar', 2)
149 ('foo.bar', 'foo.bar.__init__', True)
150 ('foo.bar', 'foo.bar.__init__', True)
150 """
151 """
151 if not isinstance(modulename, str):
152 if not isinstance(modulename, str):
152 modulename = modulename.decode('ascii')
153 modulename = modulename.decode('ascii')
153 prefix = '.'.join(modulename.split('.')[:-1])
154 prefix = '.'.join(modulename.split('.')[:-1])
154 if prefix:
155 if prefix:
155 prefix += '.'
156 prefix += '.'
156
157
157 def fromlocal(name, level=0):
158 def fromlocal(name, level=0):
158 # name is false value when relative imports are used.
159 # name is false value when relative imports are used.
159 if not name:
160 if not name:
160 # If relative imports are used, level must not be absolute.
161 # If relative imports are used, level must not be absolute.
161 assert level > 0
162 assert level > 0
162 candidates = ['.'.join(modulename.split('.')[:-level])]
163 candidates = ['.'.join(modulename.split('.')[:-level])]
163 else:
164 else:
164 if not level:
165 if not level:
165 # Check relative name first.
166 # Check relative name first.
166 candidates = [prefix + name, name]
167 candidates = [prefix + name, name]
167 else:
168 else:
168 candidates = [
169 candidates = [
169 '.'.join(modulename.split('.')[:-level]) + '.' + name
170 '.'.join(modulename.split('.')[:-level]) + '.' + name
170 ]
171 ]
171
172
172 for n in candidates:
173 for n in candidates:
173 if n in localmods:
174 if n in localmods:
174 return (n, n, False)
175 return (n, n, False)
175 dottedpath = n + '.__init__'
176 dottedpath = n + '.__init__'
176 if dottedpath in localmods:
177 if dottedpath in localmods:
177 return (n, dottedpath, True)
178 return (n, dottedpath, True)
178 return False
179 return False
179
180
180 return fromlocal
181 return fromlocal
181
182
182
183
183 def populateextmods(localmods):
184 def populateextmods(localmods):
184 """Populate C extension modules based on pure modules"""
185 """Populate C extension modules based on pure modules"""
185 newlocalmods = set(localmods)
186 newlocalmods = set(localmods)
186 for n in localmods:
187 for n in localmods:
187 if n.startswith('mercurial.pure.'):
188 if n.startswith('mercurial.pure.'):
188 m = n[len('mercurial.pure.') :]
189 m = n[len('mercurial.pure.') :]
189 newlocalmods.add('mercurial.cext.' + m)
190 newlocalmods.add('mercurial.cext.' + m)
190 newlocalmods.add('mercurial.cffi._' + m)
191 newlocalmods.add('mercurial.cffi._' + m)
191 return newlocalmods
192 return newlocalmods
192
193
193
194
194 def list_stdlib_modules():
195 def list_stdlib_modules():
195 """List the modules present in the stdlib.
196 """List the modules present in the stdlib.
196
197
197 >>> py3 = sys.version_info[0] >= 3
198 >>> py3 = sys.version_info[0] >= 3
198 >>> mods = set(list_stdlib_modules())
199 >>> mods = set(list_stdlib_modules())
199 >>> 'BaseHTTPServer' in mods or py3
200 >>> 'BaseHTTPServer' in mods or py3
200 True
201 True
201
202
202 os.path isn't really a module, so it's missing:
203 os.path isn't really a module, so it's missing:
203
204
204 >>> 'os.path' in mods
205 >>> 'os.path' in mods
205 False
206 False
206
207
207 sys requires special treatment, because it's baked into the
208 sys requires special treatment, because it's baked into the
208 interpreter, but it should still appear:
209 interpreter, but it should still appear:
209
210
210 >>> 'sys' in mods
211 >>> 'sys' in mods
211 True
212 True
212
213
213 >>> 'collections' in mods
214 >>> 'collections' in mods
214 True
215 True
215
216
216 >>> 'cStringIO' in mods or py3
217 >>> 'cStringIO' in mods or py3
217 True
218 True
218
219
219 >>> 'cffi' in mods
220 >>> 'cffi' in mods
220 True
221 True
221 """
222 """
222 for m in sys.builtin_module_names:
223 for m in sys.builtin_module_names:
223 yield m
224 yield m
224 # These modules only exist on windows, but we should always
225 # These modules only exist on windows, but we should always
225 # consider them stdlib.
226 # consider them stdlib.
226 for m in ['msvcrt', '_winreg']:
227 for m in ['msvcrt', '_winreg']:
227 yield m
228 yield m
228 yield '__builtin__'
229 yield '__builtin__'
229 yield 'builtins' # python3 only
230 yield 'builtins' # python3 only
230 yield 'importlib.abc' # python3 only
231 yield 'importlib.abc' # python3 only
231 yield 'importlib.machinery' # python3 only
232 yield 'importlib.machinery' # python3 only
232 yield 'importlib.util' # python3 only
233 yield 'importlib.util' # python3 only
233 for m in 'fcntl', 'grp', 'pwd', 'termios': # Unix only
234 for m in 'fcntl', 'grp', 'pwd', 'termios': # Unix only
234 yield m
235 yield m
235 for m in 'cPickle', 'datetime': # in Python (not C) on PyPy
236 for m in 'cPickle', 'datetime': # in Python (not C) on PyPy
236 yield m
237 yield m
237 for m in ['cffi']:
238 for m in ['cffi']:
238 yield m
239 yield m
239 stdlib_prefixes = {sys.prefix, sys.exec_prefix}
240 stdlib_prefixes = {sys.prefix, sys.exec_prefix}
240 # We need to supplement the list of prefixes for the search to work
241 # We need to supplement the list of prefixes for the search to work
241 # when run from within a virtualenv.
242 # when run from within a virtualenv.
242 for mod in (basehttpserver, zlib):
243 for mod in (basehttpserver, zlib):
243 if mod is None:
244 if mod is None:
244 continue
245 continue
245 try:
246 try:
246 # Not all module objects have a __file__ attribute.
247 # Not all module objects have a __file__ attribute.
247 filename = mod.__file__
248 filename = mod.__file__
248 except AttributeError:
249 except AttributeError:
249 continue
250 continue
250 dirname = os.path.dirname(filename)
251 dirname = os.path.dirname(filename)
251 for prefix in stdlib_prefixes:
252 for prefix in stdlib_prefixes:
252 if dirname.startswith(prefix):
253 if dirname.startswith(prefix):
253 # Then this directory is redundant.
254 # Then this directory is redundant.
254 break
255 break
255 else:
256 else:
256 stdlib_prefixes.add(dirname)
257 stdlib_prefixes.add(dirname)
257 sourceroot = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
258 sourceroot = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
258 for libpath in sys.path:
259 for libpath in sys.path:
259 # We want to walk everything in sys.path that starts with something in
260 # We want to walk everything in sys.path that starts with something in
260 # stdlib_prefixes, but not directories from the hg sources.
261 # stdlib_prefixes, but not directories from the hg sources.
261 if os.path.abspath(libpath).startswith(sourceroot) or not any(
262 if os.path.abspath(libpath).startswith(sourceroot) or not any(
262 libpath.startswith(p) for p in stdlib_prefixes
263 libpath.startswith(p) for p in stdlib_prefixes
263 ):
264 ):
264 continue
265 continue
265 for top, dirs, files in os.walk(libpath):
266 for top, dirs, files in os.walk(libpath):
266 if 'dist-packages' in top.split(os.path.sep):
267 if 'dist-packages' in top.split(os.path.sep):
267 continue
268 continue
268 for i, d in reversed(list(enumerate(dirs))):
269 for i, d in reversed(list(enumerate(dirs))):
269 if (
270 if (
270 not os.path.exists(os.path.join(top, d, '__init__.py'))
271 not os.path.exists(os.path.join(top, d, '__init__.py'))
271 or top == libpath
272 or top == libpath
272 and d in ('hgdemandimport', 'hgext', 'mercurial')
273 and d in ('hgdemandimport', 'hgext', 'mercurial')
273 ):
274 ):
274 del dirs[i]
275 del dirs[i]
275 for name in files:
276 for name in files:
276 if not name.endswith(('.py', '.so', '.pyc', '.pyo', '.pyd')):
277 if not name.endswith(('.py', '.so', '.pyc', '.pyo', '.pyd')):
277 continue
278 continue
278 if name.startswith('__init__.py'):
279 if name.startswith('__init__.py'):
279 full_path = top
280 full_path = top
280 else:
281 else:
281 full_path = os.path.join(top, name)
282 full_path = os.path.join(top, name)
282 rel_path = full_path[len(libpath) + 1 :]
283 rel_path = full_path[len(libpath) + 1 :]
283 mod = dotted_name_of_path(rel_path)
284 mod = dotted_name_of_path(rel_path)
284 yield mod
285 yield mod
285
286
286
287
287 stdlib_modules = set(list_stdlib_modules())
288 stdlib_modules = set(list_stdlib_modules())
288
289
289
290
290 def imported_modules(source, modulename, f, localmods, ignore_nested=False):
291 def imported_modules(source, modulename, f, localmods, ignore_nested=False):
291 """Given the source of a file as a string, yield the names
292 """Given the source of a file as a string, yield the names
292 imported by that file.
293 imported by that file.
293
294
294 Args:
295 Args:
295 source: The python source to examine as a string.
296 source: The python source to examine as a string.
296 modulename: of specified python source (may have `__init__`)
297 modulename: of specified python source (may have `__init__`)
297 localmods: set of locally defined module names (may have `__init__`)
298 localmods: set of locally defined module names (may have `__init__`)
298 ignore_nested: If true, import statements that do not start in
299 ignore_nested: If true, import statements that do not start in
299 column zero will be ignored.
300 column zero will be ignored.
300
301
301 Returns:
302 Returns:
302 A list of absolute module names imported by the given source.
303 A list of absolute module names imported by the given source.
303
304
304 >>> f = 'foo/xxx.py'
305 >>> f = 'foo/xxx.py'
305 >>> modulename = 'foo.xxx'
306 >>> modulename = 'foo.xxx'
306 >>> localmods = {'foo.__init__': True,
307 >>> localmods = {'foo.__init__': True,
307 ... 'foo.foo1': True, 'foo.foo2': True,
308 ... 'foo.foo1': True, 'foo.foo2': True,
308 ... 'foo.bar.__init__': True, 'foo.bar.bar1': True,
309 ... 'foo.bar.__init__': True, 'foo.bar.bar1': True,
309 ... 'baz.__init__': True, 'baz.baz1': True }
310 ... 'baz.__init__': True, 'baz.baz1': True }
310 >>> # standard library (= not locally defined ones)
311 >>> # standard library (= not locally defined ones)
311 >>> sorted(imported_modules(
312 >>> sorted(imported_modules(
312 ... 'from stdlib1 import foo, bar; import stdlib2',
313 ... 'from stdlib1 import foo, bar; import stdlib2',
313 ... modulename, f, localmods))
314 ... modulename, f, localmods))
314 []
315 []
315 >>> # relative importing
316 >>> # relative importing
316 >>> sorted(imported_modules(
317 >>> sorted(imported_modules(
317 ... 'import foo1; from bar import bar1',
318 ... 'import foo1; from bar import bar1',
318 ... modulename, f, localmods))
319 ... modulename, f, localmods))
319 ['foo.bar.bar1', 'foo.foo1']
320 ['foo.bar.bar1', 'foo.foo1']
320 >>> sorted(imported_modules(
321 >>> sorted(imported_modules(
321 ... 'from bar.bar1 import name1, name2, name3',
322 ... 'from bar.bar1 import name1, name2, name3',
322 ... modulename, f, localmods))
323 ... modulename, f, localmods))
323 ['foo.bar.bar1']
324 ['foo.bar.bar1']
324 >>> # absolute importing
325 >>> # absolute importing
325 >>> sorted(imported_modules(
326 >>> sorted(imported_modules(
326 ... 'from baz import baz1, name1',
327 ... 'from baz import baz1, name1',
327 ... modulename, f, localmods))
328 ... modulename, f, localmods))
328 ['baz.__init__', 'baz.baz1']
329 ['baz.__init__', 'baz.baz1']
329 >>> # mixed importing, even though it shouldn't be recommended
330 >>> # mixed importing, even though it shouldn't be recommended
330 >>> sorted(imported_modules(
331 >>> sorted(imported_modules(
331 ... 'import stdlib, foo1, baz',
332 ... 'import stdlib, foo1, baz',
332 ... modulename, f, localmods))
333 ... modulename, f, localmods))
333 ['baz.__init__', 'foo.foo1']
334 ['baz.__init__', 'foo.foo1']
334 >>> # ignore_nested
335 >>> # ignore_nested
335 >>> sorted(imported_modules(
336 >>> sorted(imported_modules(
336 ... '''import foo
337 ... '''import foo
337 ... def wat():
338 ... def wat():
338 ... import bar
339 ... import bar
339 ... ''', modulename, f, localmods))
340 ... ''', modulename, f, localmods))
340 ['foo.__init__', 'foo.bar.__init__']
341 ['foo.__init__', 'foo.bar.__init__']
341 >>> sorted(imported_modules(
342 >>> sorted(imported_modules(
342 ... '''import foo
343 ... '''import foo
343 ... def wat():
344 ... def wat():
344 ... import bar
345 ... import bar
345 ... ''', modulename, f, localmods, ignore_nested=True))
346 ... ''', modulename, f, localmods, ignore_nested=True))
346 ['foo.__init__']
347 ['foo.__init__']
347 """
348 """
348 fromlocal = fromlocalfunc(modulename, localmods)
349 fromlocal = fromlocalfunc(modulename, localmods)
349 for node in ast.walk(ast.parse(source, f)):
350 for node in ast.walk(ast.parse(source, f)):
350 if ignore_nested and getattr(node, 'col_offset', 0) > 0:
351 if ignore_nested and getattr(node, 'col_offset', 0) > 0:
351 continue
352 continue
352 if isinstance(node, ast.Import):
353 if isinstance(node, ast.Import):
353 for n in node.names:
354 for n in node.names:
354 found = fromlocal(n.name)
355 found = fromlocal(n.name)
355 if not found:
356 if not found:
356 # this should import standard library
357 # this should import standard library
357 continue
358 continue
358 yield found[1]
359 yield found[1]
359 elif isinstance(node, ast.ImportFrom):
360 elif isinstance(node, ast.ImportFrom):
360 found = fromlocal(node.module, node.level)
361 found = fromlocal(node.module, node.level)
361 if not found:
362 if not found:
362 # this should import standard library
363 # this should import standard library
363 continue
364 continue
364
365
365 absname, dottedpath, hassubmod = found
366 absname, dottedpath, hassubmod = found
366 if not hassubmod:
367 if not hassubmod:
367 # "dottedpath" is not a package; must be imported
368 # "dottedpath" is not a package; must be imported
368 yield dottedpath
369 yield dottedpath
369 # examination of "node.names" should be redundant
370 # examination of "node.names" should be redundant
370 # e.g.: from mercurial.node import nullid, nullrev
371 # e.g.: from mercurial.node import nullid, nullrev
371 continue
372 continue
372
373
373 modnotfound = False
374 modnotfound = False
374 prefix = absname + '.'
375 prefix = absname + '.'
375 for n in node.names:
376 for n in node.names:
376 found = fromlocal(prefix + n.name)
377 found = fromlocal(prefix + n.name)
377 if not found:
378 if not found:
378 # this should be a function or a property of "node.module"
379 # this should be a function or a property of "node.module"
379 modnotfound = True
380 modnotfound = True
380 continue
381 continue
381 yield found[1]
382 yield found[1]
382 if modnotfound and dottedpath != modulename:
383 if modnotfound and dottedpath != modulename:
383 # "dottedpath" is a package, but imported because of non-module
384 # "dottedpath" is a package, but imported because of non-module
384 # lookup
385 # lookup
385 # specifically allow "from . import foo" from __init__.py
386 # specifically allow "from . import foo" from __init__.py
386 yield dottedpath
387 yield dottedpath
387
388
388
389
389 def verify_import_convention(module, source, localmods):
390 def verify_import_convention(module, source, localmods):
390 """Verify imports match our established coding convention."""
391 """Verify imports match our established coding convention."""
391 root = ast.parse(source)
392 root = ast.parse(source)
392
393
393 return verify_modern_convention(module, root, localmods)
394 return verify_modern_convention(module, root, localmods)
394
395
395
396
396 def verify_modern_convention(module, root, localmods, root_col_offset=0):
397 def verify_modern_convention(module, root, localmods, root_col_offset=0):
397 """Verify a file conforms to the modern import convention rules.
398 """Verify a file conforms to the modern import convention rules.
398
399
399 The rules of the modern convention are:
400 The rules of the modern convention are:
400
401
401 * Ordering is stdlib followed by local imports. Each group is lexically
402 * Ordering is stdlib followed by local imports. Each group is lexically
402 sorted.
403 sorted.
403 * Importing multiple modules via "import X, Y" is not allowed: use
404 * Importing multiple modules via "import X, Y" is not allowed: use
404 separate import statements.
405 separate import statements.
405 * Importing multiple modules via "from X import ..." is allowed if using
406 * Importing multiple modules via "from X import ..." is allowed if using
406 parenthesis and one entry per line.
407 parenthesis and one entry per line.
407 * Only 1 relative import statement per import level ("from .", "from ..")
408 * Only 1 relative import statement per import level ("from .", "from ..")
408 is allowed.
409 is allowed.
409 * Relative imports from higher levels must occur before lower levels. e.g.
410 * Relative imports from higher levels must occur before lower levels. e.g.
410 "from .." must be before "from .".
411 "from .." must be before "from .".
411 * Imports from peer packages should use relative import (e.g. do not
412 * Imports from peer packages should use relative import (e.g. do not
412 "import mercurial.foo" from a "mercurial.*" module).
413 "import mercurial.foo" from a "mercurial.*" module).
413 * Symbols can only be imported from specific modules (see
414 * Symbols can only be imported from specific modules (see
414 `allowsymbolimports`). For other modules, first import the module then
415 `allowsymbolimports`). For other modules, first import the module then
415 assign the symbol to a module-level variable. In addition, these imports
416 assign the symbol to a module-level variable. In addition, these imports
416 must be performed before other local imports. This rule only
417 must be performed before other local imports. This rule only
417 applies to import statements outside of any blocks.
418 applies to import statements outside of any blocks.
418 * Relative imports from the standard library are not allowed, unless that
419 * Relative imports from the standard library are not allowed, unless that
419 library is also a local module.
420 library is also a local module.
420 * Certain modules must be aliased to alternate names to avoid aliasing
421 * Certain modules must be aliased to alternate names to avoid aliasing
421 and readability problems. See `requirealias`.
422 and readability problems. See `requirealias`.
422 """
423 """
423 if not isinstance(module, str):
424 if not isinstance(module, str):
424 module = module.decode('ascii')
425 module = module.decode('ascii')
425 topmodule = module.split('.')[0]
426 topmodule = module.split('.')[0]
426 fromlocal = fromlocalfunc(module, localmods)
427 fromlocal = fromlocalfunc(module, localmods)
427
428
428 # Whether a local/non-stdlib import has been performed.
429 # Whether a local/non-stdlib import has been performed.
429 seenlocal = None
430 seenlocal = None
430 # Whether a local/non-stdlib, non-symbol import has been seen.
431 # Whether a local/non-stdlib, non-symbol import has been seen.
431 seennonsymbollocal = False
432 seennonsymbollocal = False
432 # The last name to be imported (for sorting).
433 # The last name to be imported (for sorting).
433 lastname = None
434 lastname = None
434 laststdlib = None
435 laststdlib = None
435 # Relative import levels encountered so far.
436 # Relative import levels encountered so far.
436 seenlevels = set()
437 seenlevels = set()
437
438
438 for node, newscope in walklocal(root):
439 for node, newscope in walklocal(root):
439
440
440 def msg(fmt, *args):
441 def msg(fmt, *args):
441 return (fmt % args, node.lineno)
442 return (fmt % args, node.lineno)
442
443
443 if newscope:
444 if newscope:
444 # Check for local imports in function
445 # Check for local imports in function
445 for r in verify_modern_convention(
446 for r in verify_modern_convention(
446 module, node, localmods, node.col_offset + 4
447 module, node, localmods, node.col_offset + 4
447 ):
448 ):
448 yield r
449 yield r
449 elif isinstance(node, ast.Import):
450 elif isinstance(node, ast.Import):
450 # Disallow "import foo, bar" and require separate imports
451 # Disallow "import foo, bar" and require separate imports
451 # for each module.
452 # for each module.
452 if len(node.names) > 1:
453 if len(node.names) > 1:
453 yield msg(
454 yield msg(
454 'multiple imported names: %s',
455 'multiple imported names: %s',
455 ', '.join(n.name for n in node.names),
456 ', '.join(n.name for n in node.names),
456 )
457 )
457
458
458 name = node.names[0].name
459 name = node.names[0].name
459 asname = node.names[0].asname
460 asname = node.names[0].asname
460
461
461 stdlib = name in stdlib_modules
462 stdlib = name in stdlib_modules
462
463
463 # Ignore sorting rules on imports inside blocks.
464 # Ignore sorting rules on imports inside blocks.
464 if node.col_offset == root_col_offset:
465 if node.col_offset == root_col_offset:
465 if lastname and name < lastname and laststdlib == stdlib:
466 if lastname and name < lastname and laststdlib == stdlib:
466 yield msg(
467 yield msg(
467 'imports not lexically sorted: %s < %s', name, lastname
468 'imports not lexically sorted: %s < %s', name, lastname
468 )
469 )
469
470
470 lastname = name
471 lastname = name
471 laststdlib = stdlib
472 laststdlib = stdlib
472
473
473 # stdlib imports should be before local imports.
474 # stdlib imports should be before local imports.
474 if stdlib and seenlocal and node.col_offset == root_col_offset:
475 if stdlib and seenlocal and node.col_offset == root_col_offset:
475 yield msg(
476 yield msg(
476 'stdlib import "%s" follows local import: %s',
477 'stdlib import "%s" follows local import: %s',
477 name,
478 name,
478 seenlocal,
479 seenlocal,
479 )
480 )
480
481
481 if not stdlib:
482 if not stdlib:
482 seenlocal = name
483 seenlocal = name
483
484
484 # Import of sibling modules should use relative imports.
485 # Import of sibling modules should use relative imports.
485 topname = name.split('.')[0]
486 topname = name.split('.')[0]
486 if topname == topmodule:
487 if topname == topmodule:
487 yield msg('import should be relative: %s', name)
488 yield msg('import should be relative: %s', name)
488
489
489 if name in requirealias and asname != requirealias[name]:
490 if name in requirealias and asname != requirealias[name]:
490 yield msg(
491 yield msg(
491 '%s module must be "as" aliased to %s',
492 '%s module must be "as" aliased to %s',
492 name,
493 name,
493 requirealias[name],
494 requirealias[name],
494 )
495 )
495
496
496 elif isinstance(node, ast.ImportFrom):
497 elif isinstance(node, ast.ImportFrom):
497 # Resolve the full imported module name.
498 # Resolve the full imported module name.
498 if node.level > 0:
499 if node.level > 0:
499 fullname = '.'.join(module.split('.')[: -node.level])
500 fullname = '.'.join(module.split('.')[: -node.level])
500 if node.module:
501 if node.module:
501 fullname += '.%s' % node.module
502 fullname += '.%s' % node.module
502 else:
503 else:
503 assert node.module
504 assert node.module
504 fullname = node.module
505 fullname = node.module
505
506
506 topname = fullname.split('.')[0]
507 topname = fullname.split('.')[0]
507 if topname == topmodule:
508 if topname == topmodule:
508 yield msg('import should be relative: %s', fullname)
509 yield msg('import should be relative: %s', fullname)
509
510
510 # __future__ is special since it needs to come first and use
511 # __future__ is special since it needs to come first and use
511 # symbol import.
512 # symbol import.
512 if fullname != '__future__':
513 if fullname != '__future__':
513 if not fullname or (
514 if not fullname or (
514 fullname in stdlib_modules
515 fullname in stdlib_modules
515 # allow standard 'from typing import ...' style
516 # allow standard 'from typing import ...' style
516 and fullname.startswith('.')
517 and fullname.startswith('.')
517 and fullname not in localmods
518 and fullname not in localmods
518 and fullname + '.__init__' not in localmods
519 and fullname + '.__init__' not in localmods
519 ):
520 ):
520 yield msg('relative import of stdlib module')
521 yield msg('relative import of stdlib module')
521 else:
522 else:
522 seenlocal = fullname
523 seenlocal = fullname
523
524
524 # Direct symbol import is only allowed from certain modules and
525 # Direct symbol import is only allowed from certain modules and
525 # must occur before non-symbol imports.
526 # must occur before non-symbol imports.
526 found = fromlocal(node.module, node.level)
527 found = fromlocal(node.module, node.level)
527 if found and found[2]: # node.module is a package
528 if found and found[2]: # node.module is a package
528 prefix = found[0] + '.'
529 prefix = found[0] + '.'
529 symbols = (
530 symbols = (
530 n.name for n in node.names if not fromlocal(prefix + n.name)
531 n.name for n in node.names if not fromlocal(prefix + n.name)
531 )
532 )
532 else:
533 else:
533 symbols = (n.name for n in node.names)
534 symbols = (n.name for n in node.names)
534 symbols = [sym for sym in symbols if sym not in directsymbols]
535 symbols = [sym for sym in symbols if sym not in directsymbols]
535 if node.module and node.col_offset == root_col_offset:
536 if node.module and node.col_offset == root_col_offset:
536 if symbols and fullname not in allowsymbolimports:
537 if symbols and fullname not in allowsymbolimports:
537 yield msg(
538 yield msg(
538 'direct symbol import %s from %s',
539 'direct symbol import %s from %s',
539 ', '.join(symbols),
540 ', '.join(symbols),
540 fullname,
541 fullname,
541 )
542 )
542
543
543 if symbols and seennonsymbollocal:
544 if symbols and seennonsymbollocal:
544 yield msg(
545 yield msg(
545 'symbol import follows non-symbol import: %s', fullname
546 'symbol import follows non-symbol import: %s', fullname
546 )
547 )
547 if not symbols and fullname not in stdlib_modules:
548 if not symbols and fullname not in stdlib_modules:
548 seennonsymbollocal = True
549 seennonsymbollocal = True
549
550
550 if not node.module:
551 if not node.module:
551 assert node.level
552 assert node.level
552
553
553 # Only allow 1 group per level.
554 # Only allow 1 group per level.
554 if (
555 if (
555 node.level in seenlevels
556 node.level in seenlevels
556 and node.col_offset == root_col_offset
557 and node.col_offset == root_col_offset
557 ):
558 ):
558 yield msg(
559 yield msg(
559 'multiple "from %s import" statements', '.' * node.level
560 'multiple "from %s import" statements', '.' * node.level
560 )
561 )
561
562
562 # Higher-level groups come before lower-level groups.
563 # Higher-level groups come before lower-level groups.
563 if any(node.level > l for l in seenlevels):
564 if any(node.level > l for l in seenlevels):
564 yield msg(
565 yield msg(
565 'higher-level import should come first: %s', fullname
566 'higher-level import should come first: %s', fullname
566 )
567 )
567
568
568 seenlevels.add(node.level)
569 seenlevels.add(node.level)
569
570
570 # Entries in "from .X import ( ... )" lists must be lexically
571 # Entries in "from .X import ( ... )" lists must be lexically
571 # sorted.
572 # sorted.
572 lastentryname = None
573 lastentryname = None
573
574
574 for n in node.names:
575 for n in node.names:
575 if lastentryname and n.name < lastentryname:
576 if lastentryname and n.name < lastentryname:
576 yield msg(
577 yield msg(
577 'imports from %s not lexically sorted: %s < %s',
578 'imports from %s not lexically sorted: %s < %s',
578 fullname,
579 fullname,
579 n.name,
580 n.name,
580 lastentryname,
581 lastentryname,
581 )
582 )
582
583
583 lastentryname = n.name
584 lastentryname = n.name
584
585
585 if n.name in requirealias and n.asname != requirealias[n.name]:
586 if n.name in requirealias and n.asname != requirealias[n.name]:
586 yield msg(
587 yield msg(
587 '%s from %s must be "as" aliased to %s',
588 '%s from %s must be "as" aliased to %s',
588 n.name,
589 n.name,
589 fullname,
590 fullname,
590 requirealias[n.name],
591 requirealias[n.name],
591 )
592 )
592
593
593
594
594 class CircularImport(Exception):
595 class CircularImport(Exception):
595 pass
596 pass
596
597
597
598
598 def checkmod(mod, imports):
599 def checkmod(mod, imports):
599 shortest = {}
600 shortest = {}
600 visit = [[mod]]
601 visit = [[mod]]
601 while visit:
602 while visit:
602 path = visit.pop(0)
603 path = visit.pop(0)
603 for i in sorted(imports.get(path[-1], [])):
604 for i in sorted(imports.get(path[-1], [])):
604 if len(path) < shortest.get(i, 1000):
605 if len(path) < shortest.get(i, 1000):
605 shortest[i] = len(path)
606 shortest[i] = len(path)
606 if i in path:
607 if i in path:
607 if i == path[0]:
608 if i == path[0]:
608 raise CircularImport(path)
609 raise CircularImport(path)
609 continue
610 continue
610 visit.append(path + [i])
611 visit.append(path + [i])
611
612
612
613
613 def rotatecycle(cycle):
614 def rotatecycle(cycle):
614 """arrange a cycle so that the lexicographically first module listed first
615 """arrange a cycle so that the lexicographically first module listed first
615
616
616 >>> rotatecycle(['foo', 'bar'])
617 >>> rotatecycle(['foo', 'bar'])
617 ['bar', 'foo', 'bar']
618 ['bar', 'foo', 'bar']
618 """
619 """
619 lowest = min(cycle)
620 lowest = min(cycle)
620 idx = cycle.index(lowest)
621 idx = cycle.index(lowest)
621 return cycle[idx:] + cycle[:idx] + [lowest]
622 return cycle[idx:] + cycle[:idx] + [lowest]
622
623
623
624
624 def find_cycles(imports):
625 def find_cycles(imports):
625 """Find cycles in an already-loaded import graph.
626 """Find cycles in an already-loaded import graph.
626
627
627 All module names recorded in `imports` should be absolute one.
628 All module names recorded in `imports` should be absolute one.
628
629
629 >>> imports = {'top.foo': ['top.bar', 'os.path', 'top.qux'],
630 >>> imports = {'top.foo': ['top.bar', 'os.path', 'top.qux'],
630 ... 'top.bar': ['top.baz', 'sys'],
631 ... 'top.bar': ['top.baz', 'sys'],
631 ... 'top.baz': ['top.foo'],
632 ... 'top.baz': ['top.foo'],
632 ... 'top.qux': ['top.foo']}
633 ... 'top.qux': ['top.foo']}
633 >>> print('\\n'.join(sorted(find_cycles(imports))))
634 >>> print('\\n'.join(sorted(find_cycles(imports))))
634 top.bar -> top.baz -> top.foo -> top.bar
635 top.bar -> top.baz -> top.foo -> top.bar
635 top.foo -> top.qux -> top.foo
636 top.foo -> top.qux -> top.foo
636 """
637 """
637 cycles = set()
638 cycles = set()
638 for mod in sorted(imports.keys()):
639 for mod in sorted(imports.keys()):
639 try:
640 try:
640 checkmod(mod, imports)
641 checkmod(mod, imports)
641 except CircularImport as e:
642 except CircularImport as e:
642 cycle = e.args[0]
643 cycle = e.args[0]
643 cycles.add(" -> ".join(rotatecycle(cycle)))
644 cycles.add(" -> ".join(rotatecycle(cycle)))
644 return cycles
645 return cycles
645
646
646
647
647 def _cycle_sortkey(c):
648 def _cycle_sortkey(c):
648 return len(c), c
649 return len(c), c
649
650
650
651
651 def embedded(f, modname, src):
652 def embedded(f, modname, src):
652 """Extract embedded python code
653 """Extract embedded python code
653
654
654 >>> def _forcestr(thing):
655 >>> def _forcestr(thing):
655 ... if not isinstance(thing, str):
656 ... if not isinstance(thing, str):
656 ... return thing.decode('ascii')
657 ... return thing.decode('ascii')
657 ... return thing
658 ... return thing
658 >>> def test(fn, lines):
659 >>> def test(fn, lines):
659 ... for s, m, f, l in embedded(fn, b"example", lines):
660 ... for s, m, f, l in embedded(fn, b"example", lines):
660 ... print("%s %s %d" % (_forcestr(m), _forcestr(f), l))
661 ... print("%s %s %d" % (_forcestr(m), _forcestr(f), l))
661 ... print(repr(_forcestr(s)))
662 ... print(repr(_forcestr(s)))
662 >>> lines = [
663 >>> lines = [
663 ... 'comment',
664 ... 'comment',
664 ... ' >>> from __future__ import print_function',
665 ... ' >>> from __future__ import print_function',
665 ... " >>> ' multiline",
666 ... " >>> ' multiline",
666 ... " ... string'",
667 ... " ... string'",
667 ... ' ',
668 ... ' ',
668 ... 'comment',
669 ... 'comment',
669 ... ' $ cat > foo.py <<EOF',
670 ... ' $ cat > foo.py <<EOF',
670 ... ' > from __future__ import print_function',
671 ... ' > from __future__ import print_function',
671 ... ' > EOF',
672 ... ' > EOF',
672 ... ]
673 ... ]
673 >>> test(b"example.t", lines)
674 >>> test(b"example.t", lines)
674 example[2] doctest.py 1
675 example[2] doctest.py 1
675 "from __future__ import print_function\\n' multiline\\nstring'\\n\\n"
676 "from __future__ import print_function\\n' multiline\\nstring'\\n\\n"
676 example[8] foo.py 7
677 example[8] foo.py 7
677 'from __future__ import print_function\\n'
678 'from __future__ import print_function\\n'
678 """
679 """
679 errors = []
680 errors = []
680 for name, starts, ends, code in testparseutil.pyembedded(f, src, errors):
681 for name, starts, ends, code in testparseutil.pyembedded(f, src, errors):
681 if not name:
682 if not name:
682 # use 'doctest.py', in order to make already existing
683 # use 'doctest.py', in order to make already existing
683 # doctest above pass instantly
684 # doctest above pass instantly
684 name = 'doctest.py'
685 name = 'doctest.py'
685 # "starts" is "line number" (1-origin), but embedded() is
686 # "starts" is "line number" (1-origin), but embedded() is
686 # expected to return "line offset" (0-origin). Therefore, this
687 # expected to return "line offset" (0-origin). Therefore, this
687 # yields "starts - 1".
688 # yields "starts - 1".
688 if not isinstance(modname, str):
689 if not isinstance(modname, str):
689 modname = modname.decode('utf8')
690 modname = modname.decode('utf8')
690 yield code, "%s[%d]" % (modname, starts), name, starts - 1
691 yield code, "%s[%d]" % (modname, starts), name, starts - 1
691
692
692
693
693 def sources(f, modname):
694 def sources(f, modname):
694 """Yields possibly multiple sources from a filepath
695 """Yields possibly multiple sources from a filepath
695
696
696 input: filepath, modulename
697 input: filepath, modulename
697 yields: script(string), modulename, filepath, linenumber
698 yields: script(string), modulename, filepath, linenumber
698
699
699 For embedded scripts, the modulename and filepath will be different
700 For embedded scripts, the modulename and filepath will be different
700 from the function arguments. linenumber is an offset relative to
701 from the function arguments. linenumber is an offset relative to
701 the input file.
702 the input file.
702 """
703 """
703 py = False
704 py = False
704 if not f.endswith('.t'):
705 if not f.endswith('.t'):
705 with open(f, 'rb') as src:
706 with open(f, 'rb') as src:
706 yield src.read(), modname, f, 0
707 yield src.read(), modname, f, 0
707 py = True
708 py = True
708 if py or f.endswith('.t'):
709 if py or f.endswith('.t'):
709 # Strictly speaking we should sniff for the magic header that denotes
710 # Strictly speaking we should sniff for the magic header that denotes
710 # Python source file encoding. But in reality we don't use anything
711 # Python source file encoding. But in reality we don't use anything
711 # other than ASCII (mainly) and UTF-8 (in a few exceptions), so
712 # other than ASCII (mainly) and UTF-8 (in a few exceptions), so
712 # simplicity is fine.
713 # simplicity is fine.
713 with io.open(f, 'r', encoding='utf-8') as src:
714 with io.open(f, 'r', encoding='utf-8') as src:
714 for script, modname, t, line in embedded(f, modname, src):
715 for script, modname, t, line in embedded(f, modname, src):
715 yield script, modname.encode('utf8'), t, line
716 yield script, modname.encode('utf8'), t, line
716
717
717
718
718 def main(argv):
719 def main(argv):
719 if len(argv) < 2 or (argv[1] == '-' and len(argv) > 2):
720 if len(argv) < 2 or (argv[1] == '-' and len(argv) > 2):
720 print('Usage: %s {-|file [file] [file] ...}')
721 print('Usage: %s {-|file [file] [file] ...}')
721 return 1
722 return 1
722 if argv[1] == '-':
723 if argv[1] == '-':
723 argv = argv[:1]
724 argv = argv[:1]
724 argv.extend(l.rstrip() for l in sys.stdin.readlines())
725 argv.extend(l.rstrip() for l in sys.stdin.readlines())
725 localmodpaths = {}
726 localmodpaths = {}
726 used_imports = {}
727 used_imports = {}
727 any_errors = False
728 any_errors = False
728 for source_path in argv[1:]:
729 for source_path in argv[1:]:
729 modname = dotted_name_of_path(source_path)
730 modname = dotted_name_of_path(source_path)
730 localmodpaths[modname] = source_path
731 localmodpaths[modname] = source_path
731 localmods = populateextmods(localmodpaths)
732 localmods = populateextmods(localmodpaths)
732 for localmodname, source_path in sorted(localmodpaths.items()):
733 for localmodname, source_path in sorted(localmodpaths.items()):
733 if not isinstance(localmodname, bytes):
734 if not isinstance(localmodname, bytes):
734 # This is only safe because all hg's files are ascii
735 # This is only safe because all hg's files are ascii
735 localmodname = localmodname.encode('ascii')
736 localmodname = localmodname.encode('ascii')
736 for src, modname, name, line in sources(source_path, localmodname):
737 for src, modname, name, line in sources(source_path, localmodname):
737 try:
738 try:
738 used_imports[modname] = sorted(
739 used_imports[modname] = sorted(
739 imported_modules(
740 imported_modules(
740 src, modname, name, localmods, ignore_nested=True
741 src, modname, name, localmods, ignore_nested=True
741 )
742 )
742 )
743 )
743 for error, lineno in verify_import_convention(
744 for error, lineno in verify_import_convention(
744 modname, src, localmods
745 modname, src, localmods
745 ):
746 ):
746 any_errors = True
747 any_errors = True
747 print('%s:%d: %s' % (source_path, lineno + line, error))
748 print('%s:%d: %s' % (source_path, lineno + line, error))
748 except SyntaxError as e:
749 except SyntaxError as e:
749 print(
750 print(
750 '%s:%d: SyntaxError: %s' % (source_path, e.lineno + line, e)
751 '%s:%d: SyntaxError: %s' % (source_path, e.lineno + line, e)
751 )
752 )
752 cycles = find_cycles(used_imports)
753 cycles = find_cycles(used_imports)
753 if cycles:
754 if cycles:
754 firstmods = set()
755 firstmods = set()
755 for c in sorted(cycles, key=_cycle_sortkey):
756 for c in sorted(cycles, key=_cycle_sortkey):
756 first = c.split()[0]
757 first = c.split()[0]
757 # As a rough cut, ignore any cycle that starts with the
758 # As a rough cut, ignore any cycle that starts with the
758 # same module as some other cycle. Otherwise we see lots
759 # same module as some other cycle. Otherwise we see lots
759 # of cycles that are effectively duplicates.
760 # of cycles that are effectively duplicates.
760 if first in firstmods:
761 if first in firstmods:
761 continue
762 continue
762 print('Import cycle:', c)
763 print('Import cycle:', c)
763 firstmods.add(first)
764 firstmods.add(first)
764 any_errors = True
765 any_errors = True
765 return any_errors != 0
766 return any_errors != 0
766
767
767
768
768 if __name__ == '__main__':
769 if __name__ == '__main__':
769 sys.exit(int(main(sys.argv)))
770 sys.exit(int(main(sys.argv)))
General Comments 0
You need to be logged in to leave comments. Login now