Show More
@@ -1,96 +1,95 b'' | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 | # |
|
2 | # | |
3 | # check-py3-compat - check Python 3 compatibility of Mercurial files |
|
3 | # check-py3-compat - check Python 3 compatibility of Mercurial files | |
4 | # |
|
4 | # | |
5 | # Copyright 2015 Gregory Szorc <gregory.szorc@gmail.com> |
|
5 | # Copyright 2015 Gregory Szorc <gregory.szorc@gmail.com> | |
6 | # |
|
6 | # | |
7 | # This software may be used and distributed according to the terms of the |
|
7 | # This software may be used and distributed according to the terms of the | |
8 | # GNU General Public License version 2 or any later version. |
|
8 | # GNU General Public License version 2 or any later version. | |
9 |
|
9 | |||
10 | from __future__ import absolute_import, print_function |
|
10 | from __future__ import absolute_import, print_function | |
11 |
|
11 | |||
12 | import ast |
|
12 | import ast | |
13 | import importlib |
|
13 | import importlib | |
14 | import os |
|
14 | import os | |
15 | import sys |
|
15 | import sys | |
16 | import traceback |
|
16 | import traceback | |
17 |
|
17 | |||
18 | def check_compat_py2(f): |
|
18 | def check_compat_py2(f): | |
19 | """Check Python 3 compatibility for a file with Python 2""" |
|
19 | """Check Python 3 compatibility for a file with Python 2""" | |
20 | with open(f, 'rb') as fh: |
|
20 | with open(f, 'rb') as fh: | |
21 | content = fh.read() |
|
21 | content = fh.read() | |
22 | root = ast.parse(content) |
|
22 | root = ast.parse(content) | |
23 |
|
23 | |||
24 | # Ignore empty files. |
|
24 | # Ignore empty files. | |
25 | if not root.body: |
|
25 | if not root.body: | |
26 | return |
|
26 | return | |
27 |
|
27 | |||
28 | futures = set() |
|
28 | futures = set() | |
29 | haveprint = False |
|
29 | haveprint = False | |
30 | for node in ast.walk(root): |
|
30 | for node in ast.walk(root): | |
31 | if isinstance(node, ast.ImportFrom): |
|
31 | if isinstance(node, ast.ImportFrom): | |
32 | if node.module == '__future__': |
|
32 | if node.module == '__future__': | |
33 | futures |= set(n.name for n in node.names) |
|
33 | futures |= set(n.name for n in node.names) | |
34 | elif isinstance(node, ast.Print): |
|
34 | elif isinstance(node, ast.Print): | |
35 | haveprint = True |
|
35 | haveprint = True | |
36 |
|
36 | |||
37 | if 'absolute_import' not in futures: |
|
37 | if 'absolute_import' not in futures: | |
38 | print('%s not using absolute_import' % f) |
|
38 | print('%s not using absolute_import' % f) | |
39 | if haveprint and 'print_function' not in futures: |
|
39 | if haveprint and 'print_function' not in futures: | |
40 | print('%s requires print_function' % f) |
|
40 | print('%s requires print_function' % f) | |
41 |
|
41 | |||
42 | def check_compat_py3(f): |
|
42 | def check_compat_py3(f): | |
43 | """Check Python 3 compatibility of a file with Python 3.""" |
|
43 | """Check Python 3 compatibility of a file with Python 3.""" | |
44 | with open(f, 'rb') as fh: |
|
44 | with open(f, 'rb') as fh: | |
45 | content = fh.read() |
|
45 | content = fh.read() | |
46 |
|
46 | |||
47 | try: |
|
47 | try: | |
48 | ast.parse(content) |
|
48 | ast.parse(content) | |
49 | except SyntaxError as e: |
|
49 | except SyntaxError as e: | |
50 | print('%s: invalid syntax: %s' % (f, e)) |
|
50 | print('%s: invalid syntax: %s' % (f, e)) | |
51 | return |
|
51 | return | |
52 |
|
52 | |||
53 | # Try to import the module. |
|
53 | # Try to import the module. | |
54 | # For now we only support mercurial.* and hgext.* modules because figuring |
|
54 | # For now we only support mercurial.* and hgext.* modules because figuring | |
55 | # out module paths for things not in a package can be confusing. |
|
55 | # out module paths for things not in a package can be confusing. | |
56 | if f.startswith(('hgext/', 'mercurial/')) and not f.endswith('__init__.py'): |
|
56 | if f.startswith(('hgext/', 'mercurial/')) and not f.endswith('__init__.py'): | |
57 | assert f.endswith('.py') |
|
57 | assert f.endswith('.py') | |
58 | name = f.replace('/', '.')[:-3].replace('.pure.', '.') |
|
58 | name = f.replace('/', '.')[:-3].replace('.pure.', '.') | |
59 | if True: |
|
|||
60 |
|
|
59 | try: | |
61 |
|
|
60 | importlib.import_module(name) | |
62 |
|
|
61 | except Exception as e: | |
63 |
|
|
62 | exc_type, exc_value, tb = sys.exc_info() | |
64 |
|
|
63 | # We walk the stack and ignore frames from our custom importer, | |
65 |
|
|
64 | # import mechanisms, and stdlib modules. This kinda/sorta | |
66 |
|
|
65 | # emulates CPython behavior in import.c while also attempting | |
67 |
|
|
66 | # to pin blame on a Mercurial file. | |
68 |
|
|
67 | for frame in reversed(traceback.extract_tb(tb)): | |
69 |
|
|
68 | if frame.name == '_call_with_frames_removed': | |
70 |
|
|
69 | continue | |
71 |
|
|
70 | if 'importlib' in frame.filename: | |
72 |
|
|
71 | continue | |
73 |
|
|
72 | if 'mercurial/__init__.py' in frame.filename: | |
74 |
|
|
73 | continue | |
75 |
|
|
74 | if frame.filename.startswith(sys.prefix): | |
76 |
|
|
75 | continue | |
77 |
|
|
76 | break | |
78 |
|
77 | |||
79 |
|
|
78 | if frame.filename: | |
80 |
|
|
79 | filename = os.path.basename(frame.filename) | |
81 |
|
|
80 | print('%s: error importing: <%s> %s (error at %s:%d)' % ( | |
82 |
|
|
81 | f, type(e).__name__, e, filename, frame.lineno)) | |
83 |
|
|
82 | else: | |
84 |
|
|
83 | print('%s: error importing module: <%s> %s (line %d)' % ( | |
85 |
|
|
84 | f, type(e).__name__, e, frame.lineno)) | |
86 |
|
85 | |||
87 | if __name__ == '__main__': |
|
86 | if __name__ == '__main__': | |
88 | if sys.version_info[0] == 2: |
|
87 | if sys.version_info[0] == 2: | |
89 | fn = check_compat_py2 |
|
88 | fn = check_compat_py2 | |
90 | else: |
|
89 | else: | |
91 | fn = check_compat_py3 |
|
90 | fn = check_compat_py3 | |
92 |
|
91 | |||
93 | for f in sys.argv[1:]: |
|
92 | for f in sys.argv[1:]: | |
94 | fn(f) |
|
93 | fn(f) | |
95 |
|
94 | |||
96 | sys.exit(0) |
|
95 | sys.exit(0) |
General Comments 0
You need to be logged in to leave comments.
Login now