comparison tests/testlib/check-compat-strings.py @ 5350:73cd416a1653

tests: replace check-compat-strings.sh with a Python script Previously it was a bash script (using BASH_REMATCH, for example) that was used in tests, and we want our tests as portable as possible, so mostly that is the reason for this rewrite. It's also easier to add a cache to the Python version, which reduces the run time: from ~10s to ~5.5s. Another change worth mentioning is filtering the list of files to be only .py files. We don't currently have compat comments in any other files.
author Anton Shestakov <av6@dwimlabs.net>
date Fri, 29 May 2020 13:23:45 +0800
parents
children
comparison
equal deleted inserted replaced
5349:c3d87d106fbe 5350:73cd416a1653
1 #!/usr/bin/env python
2 """
3 This script finds compatibility-related comments with a node hash specified
4 in all files in a given directory (. by default) and looks up the hash in a
5 repo (~/hg by default) to determine if each of the comments is correct and,
6 if not, it suggests the correct release. This can prevent accidentally
7 removing a piece of code that was misattributed to a different (earlier)
8 release of core hg.
9
10 Usage: $0 WDIR HGREPO where WDIR is usually evolve/hgext3rd/ and HGREPO is
11 the place with core Mercurial repo (not just checkout). Said repo has to be
12 sufficiently up-to-date, otherwise this script may not work correctly.
13 """
14
15 from __future__ import print_function
16
17 import argparse
18 import os
19 import re
20 from subprocess import check_output
21
22 def grepall(workdir, linere):
23 for root, dirs, files in os.walk(workdir):
24 for fname in files:
25 if not fname.endswith('.py'):
26 continue
27 path = os.path.join(root, fname)
28 with open(path, 'r') as src:
29 for lineno, line in enumerate(src, 1):
30 for groups in linere.findall(line):
31 yield path, lineno, line, groups
32
33 def main():
34 ap = argparse.ArgumentParser()
35 ap.add_argument('workdir', nargs='?', default='.')
36 ap.add_argument('hgdir', nargs='?', default=os.path.expanduser('~/hg'))
37
38 opts = ap.parse_args()
39
40 linere = re.compile(r'hg <= ([0-9.]+) \(([0-9a-f+]+)\)')
41 basecmd = ['hg', '--cwd', opts.hgdir, 'log', '-T', '{tags}']
42 hgenv = {'HGPLAIN': '1', 'HGENCODING': 'UTF-8'}
43 relcache = {}
44
45 for path, lineno, line, match in grepall(opts.workdir, linere):
46 expected, revset = match
47
48 if revset not in relcache:
49 tagrevset = 'max(tag("re:^[0-9]\\.[0-9]$") - (%s)::)' % revset
50 cmd = basecmd + ['-r', tagrevset]
51 relcache[revset] = check_output(cmd, env=hgenv).decode('UTF-8')
52
53 lastrel = relcache[revset]
54
55 if lastrel != expected:
56 print('%s:%d:%s' % (path, lineno, line.rstrip('\r\n')))
57 print('\\ actual last major release without %s is %s'
58 % (revset, lastrel))
59 print()
60
61 if __name__ == '__main__':
62 main()