Debian Codegraph @

tests/check-perf-code.py

raw
1 #!/usr/bin/env python3
2 #
3 # check-perf-code - (historical) portability checker for contrib/perf.py
4
5
6 import os
7 import sys
8
9 # write static check patterns here
10 perfpypats = [
11 [
12 (
13 r'(branchmap|repoview|repoviewutil)\.subsettable',
14 "use getbranchmapsubsettable() for early Mercurial",
15 ),
16 (
17 r'\.(vfs|svfs|opener|sopener)',
18 "use getvfs()/getsvfs() for early Mercurial",
19 ),
20 (
21 r'ui\.configint',
22 "use getint() instead of ui.configint() for early Mercurial",
23 ),
24 ],
25 # warnings
26 [],
27 ]
28
29
30 def modulewhitelist(names):
31 replacement = [
32 ('.py', ''),
33 ('.c', ''), # trim suffix
34 ('mercurial%s' % '/', ''), # trim "mercurial/" path
35 ]
36 ignored = {'__init__'}
37 modules = {}
38
39 # convert from file name to module name, and count # of appearances
40 for name in names:
41 name = name.strip()
42 for old, new in replacement:
43 name = name.replace(old, new)
44 if name not in ignored:
45 modules[name] = modules.get(name, 0) + 1
46
47 # list up module names, which appear multiple times
48 whitelist = []
49 for name, count in modules.items():
50 if count > 1:
51 whitelist.append(name)
52
53 return whitelist
54
55
56 if __name__ == "__main__":
57 # in this case, it is assumed that result of "hg files" at
58 # multiple revisions is given via stdin
59 whitelist = modulewhitelist(sys.stdin)
60 assert whitelist, "module whitelist is empty"
61
62 # build up module whitelist check from file names given at runtime
63 perfpypats[0].append(
64 # this matching pattern assumes importing modules from
65 # "mercurial" package in the current style below, for simplicity
66 #
67 # from mercurial import (
68 # foo,
69 # bar,
70 # baz
71 # )
72 (
73 (
74 r'from mercurial import [(][a-z0-9, \n#]*\n(?! *%s,|^[ #]*\n|[)])'
75 % ',| *'.join(whitelist)
76 ),
77 "import newer module separately in try clause for early Mercurial",
78 )
79 )
80
81 # import contrib/check-code.py as checkcode
82 assert 'RUNTESTDIR' in os.environ, "use check-perf-code.py in *.t script"
83 contribpath = os.path.join(os.environ['RUNTESTDIR'], '..', 'contrib')
84 sys.path.insert(0, contribpath)
85 checkcode = __import__('check-code')
86
87 # register perf.py specific entry with "checks" in check-code.py
88 checkcode.checks.append(
89 ('perf.py', r'contrib/perf.py$', '', checkcode.pyfilters, perfpypats)
90 )
91
92 sys.exit(checkcode.main())