comparison contrib/check-py3-compat.py @ 43076:2372284d9457

formatting: blacken the codebase This is using my patch to black (https://github.com/psf/black/pull/826) so we don't un-wrap collection literals. Done with: hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**"' | xargs black -S # skip-blame mass-reformatting only # no-check-commit reformats foo_bar functions Differential Revision: https://phab.mercurial-scm.org/D6971
author Augie Fackler <augie@google.com>
date Sun, 06 Oct 2019 09:45:02 -0400
parents ba7eaff26474
children 9d2b2df2c2ba
comparison
equal deleted inserted replaced
43075:57875cf423c9 43076:2372284d9457
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 import warnings
18
18 19
19 def check_compat_py2(f): 20 def check_compat_py2(f):
20 """Check Python 3 compatibility for a file with Python 2""" 21 """Check Python 3 compatibility for a file with Python 2"""
21 with open(f, 'rb') as fh: 22 with open(f, 'rb') as fh:
22 content = fh.read() 23 content = fh.read()
38 if 'absolute_import' not in futures: 39 if 'absolute_import' not in futures:
39 print('%s not using absolute_import' % f) 40 print('%s not using absolute_import' % f)
40 if haveprint and 'print_function' not in futures: 41 if haveprint and 'print_function' not in futures:
41 print('%s requires print_function' % f) 42 print('%s requires print_function' % f)
42 43
44
43 def check_compat_py3(f): 45 def check_compat_py3(f):
44 """Check Python 3 compatibility of a file with Python 3.""" 46 """Check Python 3 compatibility of a file with Python 3."""
45 with open(f, 'rb') as fh: 47 with open(f, 'rb') as fh:
46 content = fh.read() 48 content = fh.read()
47 49
52 return 54 return
53 55
54 # Try to import the module. 56 # Try to import the module.
55 # For now we only support modules in packages because figuring out module 57 # For now we only support modules in packages because figuring out module
56 # paths for things not in a package can be confusing. 58 # paths for things not in a package can be confusing.
57 if (f.startswith(('hgdemandimport/', 'hgext/', 'mercurial/')) 59 if f.startswith(
58 and not f.endswith('__init__.py')): 60 ('hgdemandimport/', 'hgext/', 'mercurial/')
61 ) and not f.endswith('__init__.py'):
59 assert f.endswith('.py') 62 assert f.endswith('.py')
60 name = f.replace('/', '.')[:-3] 63 name = f.replace('/', '.')[:-3]
61 try: 64 try:
62 importlib.import_module(name) 65 importlib.import_module(name)
63 except Exception as e: 66 except Exception as e:
77 continue 80 continue
78 break 81 break
79 82
80 if frame.filename: 83 if frame.filename:
81 filename = os.path.basename(frame.filename) 84 filename = os.path.basename(frame.filename)
82 print('%s: error importing: <%s> %s (error at %s:%d)' % ( 85 print(
83 f, type(e).__name__, e, filename, frame.lineno)) 86 '%s: error importing: <%s> %s (error at %s:%d)'
87 % (f, type(e).__name__, e, filename, frame.lineno)
88 )
84 else: 89 else:
85 print('%s: error importing module: <%s> %s (line %d)' % ( 90 print(
86 f, type(e).__name__, e, frame.lineno)) 91 '%s: error importing module: <%s> %s (line %d)'
92 % (f, type(e).__name__, e, frame.lineno)
93 )
94
87 95
88 if __name__ == '__main__': 96 if __name__ == '__main__':
89 if sys.version_info[0] == 2: 97 if sys.version_info[0] == 2:
90 fn = check_compat_py2 98 fn = check_compat_py2
91 else: 99 else:
94 for f in sys.argv[1:]: 102 for f in sys.argv[1:]:
95 with warnings.catch_warnings(record=True) as warns: 103 with warnings.catch_warnings(record=True) as warns:
96 fn(f) 104 fn(f)
97 105
98 for w in warns: 106 for w in warns:
99 print(warnings.formatwarning(w.message, w.category, 107 print(
100 w.filename, w.lineno).rstrip()) 108 warnings.formatwarning(
109 w.message, w.category, w.filename, w.lineno
110 ).rstrip()
111 )
101 112
102 sys.exit(0) 113 sys.exit(0)