Mercurial > public > mercurial-scm > hg
annotate contrib/check-py3-compat.py @ 27279:40eb385f798f
tests: add test for Python 3 compatibility
Python 3 is inevitable. There have been incremental movements towards
converting the code base to be Python 3 compatible. Unfortunately, we
don't have any tests that look for Python 3 compatibility. This patch
changes that.
We introduce a check-py3-compat.py script whose role is to verify
Python 3 compatibility of the files passed in. We add a test that
calls this script with all .py files from the source checkout.
The script currently only verifies that absolute_import and
print_function are used. These are the low hanging fruits for Python
compatbility. Over time, we can include more checks, including
verifying we're able to load each Python file with Python 3. You
have to start somewhere.
Accepting this patch means that all new .py files must have
absolute_import and print_function (if "print" is used) to avoid
a new warning about Python 3 incompatibility. We've already
converted several files to use absolute_import and print_function
is in the same boat, so I don't think this is such a radical
proposition.
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Sun, 06 Dec 2015 22:39:12 -0800 |
parents | |
children | 35e69407b1ac |
rev | line source |
---|---|
27279
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
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 |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
13 import sys |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
14 |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
15 def check_compat(f): |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
16 """Check Python 3 compatibility for a file.""" |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
17 with open(f, 'rb') as fh: |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
18 content = fh.read() |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
19 |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
20 root = ast.parse(content) |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
21 futures = set() |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
22 haveprint = False |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
23 for node in ast.walk(root): |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
24 if isinstance(node, ast.ImportFrom): |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
25 if node.module == '__future__': |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
26 futures |= set(n.name for n in node.names) |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
27 elif isinstance(node, ast.Print): |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
28 haveprint = True |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
29 |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
30 if 'absolute_import' not in futures: |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
31 print('%s not using absolute_import' % f) |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
32 if haveprint and 'print_function' not in futures: |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
33 print('%s requires print_function' % f) |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
34 |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
35 if __name__ == '__main__': |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
36 for f in sys.argv[1:]: |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
37 check_compat(f) |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
38 |
40eb385f798f
tests: add test for Python 3 compatibility
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
39 sys.exit(0) |