Show More
@@ -1,95 +1,95 | |||||
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 |
|
|||
14 | import os |
|
13 | import os | |
15 | import sys |
|
14 | import sys | |
16 | import traceback |
|
15 | import traceback | |
17 |
|
16 | |||
18 | def check_compat_py2(f): |
|
17 | def check_compat_py2(f): | |
19 | """Check Python 3 compatibility for a file with Python 2""" |
|
18 | """Check Python 3 compatibility for a file with Python 2""" | |
20 | with open(f, 'rb') as fh: |
|
19 | with open(f, 'rb') as fh: | |
21 | content = fh.read() |
|
20 | content = fh.read() | |
22 | root = ast.parse(content) |
|
21 | root = ast.parse(content) | |
23 |
|
22 | |||
24 | # Ignore empty files. |
|
23 | # Ignore empty files. | |
25 | if not root.body: |
|
24 | if not root.body: | |
26 | return |
|
25 | return | |
27 |
|
26 | |||
28 | futures = set() |
|
27 | futures = set() | |
29 | haveprint = False |
|
28 | haveprint = False | |
30 | for node in ast.walk(root): |
|
29 | for node in ast.walk(root): | |
31 | if isinstance(node, ast.ImportFrom): |
|
30 | if isinstance(node, ast.ImportFrom): | |
32 | if node.module == '__future__': |
|
31 | if node.module == '__future__': | |
33 | futures |= set(n.name for n in node.names) |
|
32 | futures |= set(n.name for n in node.names) | |
34 | elif isinstance(node, ast.Print): |
|
33 | elif isinstance(node, ast.Print): | |
35 | haveprint = True |
|
34 | haveprint = True | |
36 |
|
35 | |||
37 | if 'absolute_import' not in futures: |
|
36 | if 'absolute_import' not in futures: | |
38 | print('%s not using absolute_import' % f) |
|
37 | print('%s not using absolute_import' % f) | |
39 | if haveprint and 'print_function' not in futures: |
|
38 | if haveprint and 'print_function' not in futures: | |
40 | print('%s requires print_function' % f) |
|
39 | print('%s requires print_function' % f) | |
41 |
|
40 | |||
42 | def check_compat_py3(f): |
|
41 | def check_compat_py3(f): | |
43 | """Check Python 3 compatibility of a file with Python 3.""" |
|
42 | """Check Python 3 compatibility of a file with Python 3.""" | |
|
43 | import importlib # not available on Python 2.6 | |||
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 | try: |
|
59 | try: | |
60 | importlib.import_module(name) |
|
60 | importlib.import_module(name) | |
61 | except Exception as e: |
|
61 | except Exception as e: | |
62 | exc_type, exc_value, tb = sys.exc_info() |
|
62 | exc_type, exc_value, tb = sys.exc_info() | |
63 | # We walk the stack and ignore frames from our custom importer, |
|
63 | # We walk the stack and ignore frames from our custom importer, | |
64 | # import mechanisms, and stdlib modules. This kinda/sorta |
|
64 | # import mechanisms, and stdlib modules. This kinda/sorta | |
65 | # emulates CPython behavior in import.c while also attempting |
|
65 | # emulates CPython behavior in import.c while also attempting | |
66 | # to pin blame on a Mercurial file. |
|
66 | # to pin blame on a Mercurial file. | |
67 | for frame in reversed(traceback.extract_tb(tb)): |
|
67 | for frame in reversed(traceback.extract_tb(tb)): | |
68 | if frame.name == '_call_with_frames_removed': |
|
68 | if frame.name == '_call_with_frames_removed': | |
69 | continue |
|
69 | continue | |
70 | if 'importlib' in frame.filename: |
|
70 | if 'importlib' in frame.filename: | |
71 | continue |
|
71 | continue | |
72 | if 'mercurial/__init__.py' in frame.filename: |
|
72 | if 'mercurial/__init__.py' in frame.filename: | |
73 | continue |
|
73 | continue | |
74 | if frame.filename.startswith(sys.prefix): |
|
74 | if frame.filename.startswith(sys.prefix): | |
75 | continue |
|
75 | continue | |
76 | break |
|
76 | break | |
77 |
|
77 | |||
78 | if frame.filename: |
|
78 | if frame.filename: | |
79 | filename = os.path.basename(frame.filename) |
|
79 | filename = os.path.basename(frame.filename) | |
80 | print('%s: error importing: <%s> %s (error at %s:%d)' % ( |
|
80 | print('%s: error importing: <%s> %s (error at %s:%d)' % ( | |
81 | f, type(e).__name__, e, filename, frame.lineno)) |
|
81 | f, type(e).__name__, e, filename, frame.lineno)) | |
82 | else: |
|
82 | else: | |
83 | print('%s: error importing module: <%s> %s (line %d)' % ( |
|
83 | print('%s: error importing module: <%s> %s (line %d)' % ( | |
84 | f, type(e).__name__, e, frame.lineno)) |
|
84 | f, type(e).__name__, e, frame.lineno)) | |
85 |
|
85 | |||
86 | if __name__ == '__main__': |
|
86 | if __name__ == '__main__': | |
87 | if sys.version_info[0] == 2: |
|
87 | if sys.version_info[0] == 2: | |
88 | fn = check_compat_py2 |
|
88 | fn = check_compat_py2 | |
89 | else: |
|
89 | else: | |
90 | fn = check_compat_py3 |
|
90 | fn = check_compat_py3 | |
91 |
|
91 | |||
92 | for f in sys.argv[1:]: |
|
92 | for f in sys.argv[1:]: | |
93 | fn(f) |
|
93 | fn(f) | |
94 |
|
94 | |||
95 | sys.exit(0) |
|
95 | sys.exit(0) |
General Comments 0
You need to be logged in to leave comments.
Login now