##// END OF EJS Templates
tests: perform an ast parse with Python 3...
Gregory Szorc -
r28583:260ce2ee default
parent child Browse files
Show More
@@ -1,43 +1,59 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 sys
13 import sys
14
14
15 def check_compat(f):
15 def check_compat_py2(f):
16 """Check Python 3 compatibility for a file."""
16 """Check Python 3 compatibility for a file with Python 2"""
17 with open(f, 'rb') as fh:
17 with open(f, 'rb') as fh:
18 content = fh.read()
18 content = fh.read()
19 root = ast.parse(content)
19 root = ast.parse(content)
20
20
21 # Ignore empty files.
21 # Ignore empty files.
22 if not root.body:
22 if not root.body:
23 return
23 return
24
24
25 futures = set()
25 futures = set()
26 haveprint = False
26 haveprint = False
27 for node in ast.walk(root):
27 for node in ast.walk(root):
28 if isinstance(node, ast.ImportFrom):
28 if isinstance(node, ast.ImportFrom):
29 if node.module == '__future__':
29 if node.module == '__future__':
30 futures |= set(n.name for n in node.names)
30 futures |= set(n.name for n in node.names)
31 elif isinstance(node, ast.Print):
31 elif isinstance(node, ast.Print):
32 haveprint = True
32 haveprint = True
33
33
34 if 'absolute_import' not in futures:
34 if 'absolute_import' not in futures:
35 print('%s not using absolute_import' % f)
35 print('%s not using absolute_import' % f)
36 if haveprint and 'print_function' not in futures:
36 if haveprint and 'print_function' not in futures:
37 print('%s requires print_function' % f)
37 print('%s requires print_function' % f)
38
38
39 def check_compat_py3(f):
40 """Check Python 3 compatibility of a file with Python 3."""
41 with open(f, 'rb') as fh:
42 content = fh.read()
43
44 try:
45 ast.parse(content)
46 except SyntaxError as e:
47 print('%s: invalid syntax: %s' % (f, e))
48 return
49
39 if __name__ == '__main__':
50 if __name__ == '__main__':
51 if sys.version_info[0] == 2:
52 fn = check_compat_py2
53 else:
54 fn = check_compat_py3
55
40 for f in sys.argv[1:]:
56 for f in sys.argv[1:]:
41 check_compat(f)
57 fn(f)
42
58
43 sys.exit(0)
59 sys.exit(0)
@@ -1,120 +1,163 b''
1 #require test-repo
1 #require test-repo
2
2
3 $ cd "$TESTDIR"/..
3 $ cd "$TESTDIR"/..
4
4
5 $ hg files 'set:(**.py)' | sed 's|\\|/|g' | xargs python contrib/check-py3-compat.py
5 $ hg files 'set:(**.py)' | sed 's|\\|/|g' | xargs python contrib/check-py3-compat.py
6 contrib/import-checker.py not using absolute_import
6 contrib/import-checker.py not using absolute_import
7 contrib/import-checker.py requires print_function
7 contrib/import-checker.py requires print_function
8 doc/check-seclevel.py not using absolute_import
8 doc/check-seclevel.py not using absolute_import
9 doc/gendoc.py not using absolute_import
9 doc/gendoc.py not using absolute_import
10 doc/hgmanpage.py not using absolute_import
10 doc/hgmanpage.py not using absolute_import
11 hgext/color.py not using absolute_import
11 hgext/color.py not using absolute_import
12 hgext/eol.py not using absolute_import
12 hgext/eol.py not using absolute_import
13 hgext/extdiff.py not using absolute_import
13 hgext/extdiff.py not using absolute_import
14 hgext/factotum.py not using absolute_import
14 hgext/factotum.py not using absolute_import
15 hgext/fetch.py not using absolute_import
15 hgext/fetch.py not using absolute_import
16 hgext/fsmonitor/pywatchman/__init__.py not using absolute_import
16 hgext/fsmonitor/pywatchman/__init__.py not using absolute_import
17 hgext/fsmonitor/pywatchman/__init__.py requires print_function
17 hgext/fsmonitor/pywatchman/__init__.py requires print_function
18 hgext/fsmonitor/pywatchman/capabilities.py not using absolute_import
18 hgext/fsmonitor/pywatchman/capabilities.py not using absolute_import
19 hgext/fsmonitor/pywatchman/pybser.py not using absolute_import
19 hgext/fsmonitor/pywatchman/pybser.py not using absolute_import
20 hgext/gpg.py not using absolute_import
20 hgext/gpg.py not using absolute_import
21 hgext/graphlog.py not using absolute_import
21 hgext/graphlog.py not using absolute_import
22 hgext/hgcia.py not using absolute_import
22 hgext/hgcia.py not using absolute_import
23 hgext/hgk.py not using absolute_import
23 hgext/hgk.py not using absolute_import
24 hgext/highlight/__init__.py not using absolute_import
24 hgext/highlight/__init__.py not using absolute_import
25 hgext/highlight/highlight.py not using absolute_import
25 hgext/highlight/highlight.py not using absolute_import
26 hgext/histedit.py not using absolute_import
26 hgext/histedit.py not using absolute_import
27 hgext/largefiles/__init__.py not using absolute_import
27 hgext/largefiles/__init__.py not using absolute_import
28 hgext/largefiles/basestore.py not using absolute_import
28 hgext/largefiles/basestore.py not using absolute_import
29 hgext/largefiles/lfcommands.py not using absolute_import
29 hgext/largefiles/lfcommands.py not using absolute_import
30 hgext/largefiles/lfutil.py not using absolute_import
30 hgext/largefiles/lfutil.py not using absolute_import
31 hgext/largefiles/localstore.py not using absolute_import
31 hgext/largefiles/localstore.py not using absolute_import
32 hgext/largefiles/overrides.py not using absolute_import
32 hgext/largefiles/overrides.py not using absolute_import
33 hgext/largefiles/proto.py not using absolute_import
33 hgext/largefiles/proto.py not using absolute_import
34 hgext/largefiles/remotestore.py not using absolute_import
34 hgext/largefiles/remotestore.py not using absolute_import
35 hgext/largefiles/reposetup.py not using absolute_import
35 hgext/largefiles/reposetup.py not using absolute_import
36 hgext/largefiles/uisetup.py not using absolute_import
36 hgext/largefiles/uisetup.py not using absolute_import
37 hgext/largefiles/wirestore.py not using absolute_import
37 hgext/largefiles/wirestore.py not using absolute_import
38 hgext/mq.py not using absolute_import
38 hgext/mq.py not using absolute_import
39 hgext/rebase.py not using absolute_import
39 hgext/rebase.py not using absolute_import
40 hgext/share.py not using absolute_import
40 hgext/share.py not using absolute_import
41 hgext/win32text.py not using absolute_import
41 hgext/win32text.py not using absolute_import
42 i18n/check-translation.py not using absolute_import
42 i18n/check-translation.py not using absolute_import
43 i18n/polib.py not using absolute_import
43 i18n/polib.py not using absolute_import
44 setup.py not using absolute_import
44 setup.py not using absolute_import
45 tests/filterpyflakes.py requires print_function
45 tests/filterpyflakes.py requires print_function
46 tests/generate-working-copy-states.py requires print_function
46 tests/generate-working-copy-states.py requires print_function
47 tests/get-with-headers.py requires print_function
47 tests/get-with-headers.py requires print_function
48 tests/heredoctest.py requires print_function
48 tests/heredoctest.py requires print_function
49 tests/hypothesishelpers.py not using absolute_import
49 tests/hypothesishelpers.py not using absolute_import
50 tests/hypothesishelpers.py requires print_function
50 tests/hypothesishelpers.py requires print_function
51 tests/killdaemons.py not using absolute_import
51 tests/killdaemons.py not using absolute_import
52 tests/md5sum.py not using absolute_import
52 tests/md5sum.py not using absolute_import
53 tests/mockblackbox.py not using absolute_import
53 tests/mockblackbox.py not using absolute_import
54 tests/printenv.py not using absolute_import
54 tests/printenv.py not using absolute_import
55 tests/readlink.py not using absolute_import
55 tests/readlink.py not using absolute_import
56 tests/readlink.py requires print_function
56 tests/readlink.py requires print_function
57 tests/revlog-formatv0.py not using absolute_import
57 tests/revlog-formatv0.py not using absolute_import
58 tests/run-tests.py not using absolute_import
58 tests/run-tests.py not using absolute_import
59 tests/seq.py not using absolute_import
59 tests/seq.py not using absolute_import
60 tests/seq.py requires print_function
60 tests/seq.py requires print_function
61 tests/silenttestrunner.py not using absolute_import
61 tests/silenttestrunner.py not using absolute_import
62 tests/silenttestrunner.py requires print_function
62 tests/silenttestrunner.py requires print_function
63 tests/sitecustomize.py not using absolute_import
63 tests/sitecustomize.py not using absolute_import
64 tests/svn-safe-append.py not using absolute_import
64 tests/svn-safe-append.py not using absolute_import
65 tests/svnxml.py not using absolute_import
65 tests/svnxml.py not using absolute_import
66 tests/test-ancestor.py requires print_function
66 tests/test-ancestor.py requires print_function
67 tests/test-atomictempfile.py not using absolute_import
67 tests/test-atomictempfile.py not using absolute_import
68 tests/test-batching.py not using absolute_import
68 tests/test-batching.py not using absolute_import
69 tests/test-batching.py requires print_function
69 tests/test-batching.py requires print_function
70 tests/test-bdiff.py not using absolute_import
70 tests/test-bdiff.py not using absolute_import
71 tests/test-bdiff.py requires print_function
71 tests/test-bdiff.py requires print_function
72 tests/test-context.py not using absolute_import
72 tests/test-context.py not using absolute_import
73 tests/test-context.py requires print_function
73 tests/test-context.py requires print_function
74 tests/test-demandimport.py not using absolute_import
74 tests/test-demandimport.py not using absolute_import
75 tests/test-demandimport.py requires print_function
75 tests/test-demandimport.py requires print_function
76 tests/test-doctest.py not using absolute_import
76 tests/test-doctest.py not using absolute_import
77 tests/test-duplicateoptions.py not using absolute_import
77 tests/test-duplicateoptions.py not using absolute_import
78 tests/test-duplicateoptions.py requires print_function
78 tests/test-duplicateoptions.py requires print_function
79 tests/test-filecache.py not using absolute_import
79 tests/test-filecache.py not using absolute_import
80 tests/test-filecache.py requires print_function
80 tests/test-filecache.py requires print_function
81 tests/test-filelog.py not using absolute_import
81 tests/test-filelog.py not using absolute_import
82 tests/test-filelog.py requires print_function
82 tests/test-filelog.py requires print_function
83 tests/test-hg-parseurl.py not using absolute_import
83 tests/test-hg-parseurl.py not using absolute_import
84 tests/test-hg-parseurl.py requires print_function
84 tests/test-hg-parseurl.py requires print_function
85 tests/test-hgweb-auth.py not using absolute_import
85 tests/test-hgweb-auth.py not using absolute_import
86 tests/test-hgweb-auth.py requires print_function
86 tests/test-hgweb-auth.py requires print_function
87 tests/test-hgwebdir-paths.py not using absolute_import
87 tests/test-hgwebdir-paths.py not using absolute_import
88 tests/test-hybridencode.py not using absolute_import
88 tests/test-hybridencode.py not using absolute_import
89 tests/test-hybridencode.py requires print_function
89 tests/test-hybridencode.py requires print_function
90 tests/test-lrucachedict.py not using absolute_import
90 tests/test-lrucachedict.py not using absolute_import
91 tests/test-lrucachedict.py requires print_function
91 tests/test-lrucachedict.py requires print_function
92 tests/test-manifest.py not using absolute_import
92 tests/test-manifest.py not using absolute_import
93 tests/test-minirst.py not using absolute_import
93 tests/test-minirst.py not using absolute_import
94 tests/test-minirst.py requires print_function
94 tests/test-minirst.py requires print_function
95 tests/test-parseindex2.py not using absolute_import
95 tests/test-parseindex2.py not using absolute_import
96 tests/test-parseindex2.py requires print_function
96 tests/test-parseindex2.py requires print_function
97 tests/test-pathencode.py not using absolute_import
97 tests/test-pathencode.py not using absolute_import
98 tests/test-pathencode.py requires print_function
98 tests/test-pathencode.py requires print_function
99 tests/test-propertycache.py not using absolute_import
99 tests/test-propertycache.py not using absolute_import
100 tests/test-propertycache.py requires print_function
100 tests/test-propertycache.py requires print_function
101 tests/test-revlog-ancestry.py not using absolute_import
101 tests/test-revlog-ancestry.py not using absolute_import
102 tests/test-revlog-ancestry.py requires print_function
102 tests/test-revlog-ancestry.py requires print_function
103 tests/test-run-tests.py not using absolute_import
103 tests/test-run-tests.py not using absolute_import
104 tests/test-simplemerge.py not using absolute_import
104 tests/test-simplemerge.py not using absolute_import
105 tests/test-status-inprocess.py not using absolute_import
105 tests/test-status-inprocess.py not using absolute_import
106 tests/test-status-inprocess.py requires print_function
106 tests/test-status-inprocess.py requires print_function
107 tests/test-symlink-os-yes-fs-no.py not using absolute_import
107 tests/test-symlink-os-yes-fs-no.py not using absolute_import
108 tests/test-trusted.py not using absolute_import
108 tests/test-trusted.py not using absolute_import
109 tests/test-trusted.py requires print_function
109 tests/test-trusted.py requires print_function
110 tests/test-ui-color.py not using absolute_import
110 tests/test-ui-color.py not using absolute_import
111 tests/test-ui-color.py requires print_function
111 tests/test-ui-color.py requires print_function
112 tests/test-ui-config.py not using absolute_import
112 tests/test-ui-config.py not using absolute_import
113 tests/test-ui-config.py requires print_function
113 tests/test-ui-config.py requires print_function
114 tests/test-ui-verbosity.py not using absolute_import
114 tests/test-ui-verbosity.py not using absolute_import
115 tests/test-ui-verbosity.py requires print_function
115 tests/test-ui-verbosity.py requires print_function
116 tests/test-url.py not using absolute_import
116 tests/test-url.py not using absolute_import
117 tests/test-url.py requires print_function
117 tests/test-url.py requires print_function
118 tests/test-walkrepo.py requires print_function
118 tests/test-walkrepo.py requires print_function
119 tests/test-wireproto.py requires print_function
119 tests/test-wireproto.py requires print_function
120 tests/tinyproxy.py requires print_function
120 tests/tinyproxy.py requires print_function
121
122 #if py3exe
123 $ hg files 'set:(**.py)' | sed 's|\\|/|g' | xargs $PYTHON3 contrib/check-py3-compat.py
124 contrib/check-code.py: invalid syntax: (unicode error) 'unicodeescape' codec can't decode bytes in position 18-19: malformed \N character escape (<unknown>, line 106)
125 contrib/import-checker.py: invalid syntax: Missing parentheses in call to 'print' (<unknown>, line 569)
126 contrib/revsetbenchmarks.py: invalid syntax: invalid syntax (<unknown>, line 186)
127 doc/hgmanpage.py: invalid syntax: invalid syntax (<unknown>, line 286)
128 hgext/color.py: invalid syntax: invalid syntax (<unknown>, line 551)
129 mercurial/archival.py: invalid syntax: invalid syntax (<unknown>, line 234)
130 mercurial/bundle2.py: invalid syntax: invalid syntax (<unknown>, line 977)
131 mercurial/commands.py: invalid syntax: invalid syntax (<unknown>, line 3324)
132 tests/filterpyflakes.py: invalid syntax: Missing parentheses in call to 'print' (<unknown>, line 61)
133 tests/generate-working-copy-states.py: invalid syntax: Missing parentheses in call to 'print' (<unknown>, line 69)
134 tests/get-with-headers.py: invalid syntax: Missing parentheses in call to 'print' (<unknown>, line 44)
135 tests/readlink.py: invalid syntax: invalid syntax (<unknown>, line 7)
136 tests/seq.py: invalid syntax: Missing parentheses in call to 'print' (<unknown>, line 23)
137 tests/silenttestrunner.py: invalid syntax: Missing parentheses in call to 'print' (<unknown>, line 11)
138 tests/test-ancestor.py: invalid syntax: Missing parentheses in call to 'print' (<unknown>, line 187)
139 tests/test-batching.py: invalid syntax: invalid syntax (<unknown>, line 34)
140 tests/test-bdiff.py: invalid syntax: invalid syntax (<unknown>, line 10)
141 tests/test-context.py: invalid syntax: invalid syntax (<unknown>, line 21)
142 tests/test-demandimport.py: invalid syntax: invalid syntax (<unknown>, line 26)
143 tests/test-duplicateoptions.py: invalid syntax: invalid syntax (<unknown>, line 34)
144 tests/test-filecache.py: invalid syntax: Missing parentheses in call to 'print' (<unknown>, line 23)
145 tests/test-filelog.py: invalid syntax: Missing parentheses in call to 'print' (<unknown>, line 33)
146 tests/test-hg-parseurl.py: invalid syntax: invalid syntax (<unknown>, line 4)
147 tests/test-hgweb-auth.py: invalid syntax: invalid syntax (<unknown>, line 24)
148 tests/test-hybridencode.py: invalid syntax: invalid syntax (<unknown>, line 5)
149 tests/test-lrucachedict.py: invalid syntax: invalid syntax (<unknown>, line 6)
150 tests/test-minirst.py: invalid syntax: Missing parentheses in call to 'print' (<unknown>, line 6)
151 tests/test-parseindex2.py: invalid syntax: Missing parentheses in call to 'print' (<unknown>, line 173)
152 tests/test-propertycache.py: invalid syntax: Missing parentheses in call to 'print' (<unknown>, line 50)
153 tests/test-revlog-ancestry.py: invalid syntax: Missing parentheses in call to 'print' (<unknown>, line 49)
154 tests/test-status-inprocess.py: invalid syntax: Missing parentheses in call to 'print' (<unknown>, line 8)
155 tests/test-trusted.py: invalid syntax: invalid syntax (<unknown>, line 60)
156 tests/test-ui-color.py: invalid syntax: invalid syntax (<unknown>, line 11)
157 tests/test-ui-config.py: invalid syntax: invalid syntax (<unknown>, line 32)
158 tests/test-ui-verbosity.py: invalid syntax: Missing parentheses in call to 'print' (<unknown>, line 9)
159 tests/test-walkrepo.py: invalid syntax: invalid syntax (<unknown>, line 37)
160 tests/test-wireproto.py: invalid syntax: invalid syntax (<unknown>, line 55)
161 tests/tinyproxy.py: invalid syntax: Missing parentheses in call to 'print' (<unknown>, line 53)
162
163 #endif
General Comments 0
You need to be logged in to leave comments. Login now