author | Gregory Szorc <gregory.szorc@gmail.com> |
Sun, 20 Feb 2022 14:52:40 -0700 | |
changeset 48872 | 968b29a5a7fc |
parent 45830 | c102b704edb5 |
child 48875 | 6000f5b25c9b |
permissions | -rwxr-xr-x |
45830
c102b704edb5
global: use python3 in shebangs
Gregory Szorc <gregory.szorc@gmail.com>
parents:
45217
diff
changeset
|
1 |
#!/usr/bin/env python3 |
27279
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
2 |
# |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
3 |
# check-py3-compat - check Python 3 compatibility of Mercurial files |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
4 |
# |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
5 |
# Copyright 2015 Gregory Szorc <gregory.szorc@gmail.com> |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
6 |
# |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
7 |
# This software may be used and distributed according to the terms of the |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
8 |
# GNU General Public License version 2 or any later version. |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
9 |
|
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
10 |
from __future__ import absolute_import, print_function |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
11 |
|
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
12 |
import ast |
32280
9b81fb217820
py3: remove delayed import of importlib
Gregory Szorc <gregory.szorc@gmail.com>
parents:
32212
diff
changeset
|
13 |
import importlib |
28584
d69172ddfdca
tests: try to import modules with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28583
diff
changeset
|
14 |
import os |
27279
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
15 |
import sys |
28584
d69172ddfdca
tests: try to import modules with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28583
diff
changeset
|
16 |
import traceback |
41555
ba7eaff26474
check-py3-compat: manually format and print warnings
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41554
diff
changeset
|
17 |
import warnings |
27279
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
18 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41555
diff
changeset
|
19 |
|
28583
260ce2eed951
tests: perform an ast parse with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28475
diff
changeset
|
20 |
def check_compat_py3(f): |
260ce2eed951
tests: perform an ast parse with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28475
diff
changeset
|
21 |
"""Check Python 3 compatibility of a file with Python 3.""" |
260ce2eed951
tests: perform an ast parse with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28475
diff
changeset
|
22 |
with open(f, 'rb') as fh: |
260ce2eed951
tests: perform an ast parse with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28475
diff
changeset
|
23 |
content = fh.read() |
260ce2eed951
tests: perform an ast parse with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28475
diff
changeset
|
24 |
|
260ce2eed951
tests: perform an ast parse with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28475
diff
changeset
|
25 |
try: |
41554
01417ca7f2e2
check-py3-compat: provide filename to ast.parse()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
32421
diff
changeset
|
26 |
ast.parse(content, filename=f) |
28583
260ce2eed951
tests: perform an ast parse with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28475
diff
changeset
|
27 |
except SyntaxError as e: |
260ce2eed951
tests: perform an ast parse with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28475
diff
changeset
|
28 |
print('%s: invalid syntax: %s' % (f, e)) |
260ce2eed951
tests: perform an ast parse with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28475
diff
changeset
|
29 |
return |
260ce2eed951
tests: perform an ast parse with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28475
diff
changeset
|
30 |
|
28584
d69172ddfdca
tests: try to import modules with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28583
diff
changeset
|
31 |
# Try to import the module. |
32421
778dc37ce683
check: check modules in hgdemandimport
Siddharth Agarwal <sid0@fb.com>
parents:
32374
diff
changeset
|
32 |
# For now we only support modules in packages because figuring out module |
778dc37ce683
check: check modules in hgdemandimport
Siddharth Agarwal <sid0@fb.com>
parents:
32374
diff
changeset
|
33 |
# paths for things not in a package can be confusing. |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41555
diff
changeset
|
34 |
if f.startswith( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41555
diff
changeset
|
35 |
('hgdemandimport/', 'hgext/', 'mercurial/') |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41555
diff
changeset
|
36 |
) and not f.endswith('__init__.py'): |
28584
d69172ddfdca
tests: try to import modules with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28583
diff
changeset
|
37 |
assert f.endswith('.py') |
32212
65cd7e705ff6
policy: eliminate ".pure." from module name only if marked as dual
Yuya Nishihara <yuya@tcha.org>
parents:
30117
diff
changeset
|
38 |
name = f.replace('/', '.')[:-3] |
30095
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
39 |
try: |
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
40 |
importlib.import_module(name) |
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
41 |
except Exception as e: |
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
42 |
exc_type, exc_value, tb = sys.exc_info() |
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
43 |
# We walk the stack and ignore frames from our custom importer, |
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
44 |
# import mechanisms, and stdlib modules. This kinda/sorta |
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
45 |
# emulates CPython behavior in import.c while also attempting |
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
46 |
# to pin blame on a Mercurial file. |
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
47 |
for frame in reversed(traceback.extract_tb(tb)): |
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
48 |
if frame.name == '_call_with_frames_removed': |
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
49 |
continue |
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
50 |
if 'importlib' in frame.filename: |
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
51 |
continue |
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
52 |
if 'mercurial/__init__.py' in frame.filename: |
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
53 |
continue |
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
54 |
if frame.filename.startswith(sys.prefix): |
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
55 |
continue |
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
56 |
break |
28584
d69172ddfdca
tests: try to import modules with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28583
diff
changeset
|
57 |
|
30095
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
58 |
if frame.filename: |
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
59 |
filename = os.path.basename(frame.filename) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41555
diff
changeset
|
60 |
print( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41555
diff
changeset
|
61 |
'%s: error importing: <%s> %s (error at %s:%d)' |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41555
diff
changeset
|
62 |
% (f, type(e).__name__, e, filename, frame.lineno) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41555
diff
changeset
|
63 |
) |
30095
e8aeeb28e35e
py3: remove superfluous indent from check-py3-compat.py
Yuya Nishihara <yuya@tcha.org>
parents:
30094
diff
changeset
|
64 |
else: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41555
diff
changeset
|
65 |
print( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41555
diff
changeset
|
66 |
'%s: error importing module: <%s> %s (line %d)' |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41555
diff
changeset
|
67 |
% (f, type(e).__name__, e, frame.lineno) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41555
diff
changeset
|
68 |
) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41555
diff
changeset
|
69 |
|
28584
d69172ddfdca
tests: try to import modules with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28583
diff
changeset
|
70 |
|
27279
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
71 |
if __name__ == '__main__': |
48872
968b29a5a7fc
check-py3-compat: drop support for Python 2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
45830
diff
changeset
|
72 |
# check_compat_py3 will import every filename we specify as long as it |
968b29a5a7fc
check-py3-compat: drop support for Python 2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
45830
diff
changeset
|
73 |
# starts with one of a few prefixes. It does this by converting |
968b29a5a7fc
check-py3-compat: drop support for Python 2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
45830
diff
changeset
|
74 |
# specified filenames like 'mercurial/foo.py' to 'mercurial.foo' and |
968b29a5a7fc
check-py3-compat: drop support for Python 2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
45830
diff
changeset
|
75 |
# importing that. When running standalone (not as part of a test), this |
968b29a5a7fc
check-py3-compat: drop support for Python 2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
45830
diff
changeset
|
76 |
# means we actually import the installed versions, not the files we just |
968b29a5a7fc
check-py3-compat: drop support for Python 2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
45830
diff
changeset
|
77 |
# specified. When running as test-check-py3-compat.t, we technically |
968b29a5a7fc
check-py3-compat: drop support for Python 2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
45830
diff
changeset
|
78 |
# would import the correct paths, but it's cleaner to have both cases |
968b29a5a7fc
check-py3-compat: drop support for Python 2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
45830
diff
changeset
|
79 |
# use the same import logic. |
968b29a5a7fc
check-py3-compat: drop support for Python 2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
45830
diff
changeset
|
80 |
sys.path.insert(0, '.') |
28583
260ce2eed951
tests: perform an ast parse with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28475
diff
changeset
|
81 |
|
27279
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
82 |
for f in sys.argv[1:]: |
41555
ba7eaff26474
check-py3-compat: manually format and print warnings
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41554
diff
changeset
|
83 |
with warnings.catch_warnings(record=True) as warns: |
48872
968b29a5a7fc
check-py3-compat: drop support for Python 2
Gregory Szorc <gregory.szorc@gmail.com>
parents:
45830
diff
changeset
|
84 |
check_compat_py3(f) |
41555
ba7eaff26474
check-py3-compat: manually format and print warnings
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41554
diff
changeset
|
85 |
|
ba7eaff26474
check-py3-compat: manually format and print warnings
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41554
diff
changeset
|
86 |
for w in warns: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41555
diff
changeset
|
87 |
print( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41555
diff
changeset
|
88 |
warnings.formatwarning( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41555
diff
changeset
|
89 |
w.message, w.category, w.filename, w.lineno |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41555
diff
changeset
|
90 |
).rstrip() |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41555
diff
changeset
|
91 |
) |
27279
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
92 |
|
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
93 |
sys.exit(0) |