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