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