Mercurial > hg
annotate contrib/check-config.py @ 27142:060f83d219b9
extensions: refuse to load extensions if minimum hg version not met
As the author of several 3rd party extensions, I frequently see bug
reports from users attempting to run my extension with an old version
of Mercurial that I no longer support in my extension. Oftentimes, the
extension will import just fine. But as soon as we run extsetup(),
reposetup(), or get into the guts of a wrapped function, we encounter
an exception and abort. Today, Mercurial will print a message about
extensions that don't have a "testedwith" declaring explicit
compatibility with the current version.
The existing mechanism is a good start. But it isn't as robust as I
would like. Specifically, Mercurial assumes compatibility by default.
This means extension authors must perform compatibility checking in
their extsetup() or we wait and see if we encounter an abort at
runtime. And, compatibility checking can involve a lot of code and
lots of error checking. It's a lot of effort for extension authors.
Oftentimes, extension authors know which versions of Mercurial there
extension works on and more importantly where it is broken.
This patch introduces a magic "minimumhgversion" attribute in
extensions. When found, the extension loading mechanism will compare
the declared version against the current Mercurial version. If the
extension explicitly states we require a newer Mercurial version, a
warning is printed and the extension isn't loaded beyond importing
the Python module. This causes a graceful failure while alerting
the user of the compatibility issue.
I would be receptive to the idea of making the failure more fatal.
However, care would need to be taken to not criple every hg command.
e.g. the user may use `hg config` to fix the hgrc and if we aborted
trying to run that, the user would effectively be locked out of `hg`!
A potential future improvement to this functionality would be to catch
ImportError for the extension/module and parse the source code for
"minimumhgversion = 'XXX'" and do similar checking. This way we could
give more information about why the extension failed to load.
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Tue, 24 Nov 2015 15:16:25 -0800 |
parents | d1cb185b9ee2 |
children | 9c98fe1416c2 |
rev | line source |
---|---|
25790
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
2 # |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
3 # check-config - a config flag documentation checker for Mercurial |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
4 # |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
5 # Copyright 2015 Matt Mackall <mpm@selenic.com> |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
6 # |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
7 # This software may be used and distributed according to the terms of the |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
8 # GNU General Public License version 2 or any later version. |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
9 |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
10 import re |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
11 import sys |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
12 |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
13 foundopts = {} |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
14 documented = {} |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
15 |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
16 configre = (r"""ui\.config(|int|bool|list)\(['"](\S+)['"], ?""" |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
17 r"""['"](\S+)['"](,\s(?:default=)?(\S+?))?\)""") |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
18 |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
19 def main(args): |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
20 for f in args: |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
21 sect = '' |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
22 prevname = '' |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
23 confsect = '' |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
24 for l in open(f): |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
25 |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
26 # check topic-like bits |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
27 m = re.match('\s*``(\S+)``', l) |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
28 if m: |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
29 prevname = m.group(1) |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
30 if re.match('^\s*-+$', l): |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
31 sect = prevname |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
32 prevname = '' |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
33 |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
34 if sect and prevname: |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
35 name = sect + '.' + prevname |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
36 documented[name] = 1 |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
37 |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
38 # check docstring bits |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
39 m = re.match(r'^\s+\[(\S+)\]', l) |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
40 if m: |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
41 confsect = m.group(1) |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
42 continue |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
43 m = re.match(r'^\s+(?:#\s*)?([a-z._]+) = ', l) |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
44 if m: |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
45 name = confsect + '.' + m.group(1) |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
46 documented[name] = 1 |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
47 |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
48 # like the bugzilla extension |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
49 m = re.match(r'^\s*([a-z]+\.[a-z]+)$', l) |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
50 if m: |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
51 documented[m.group(1)] = 1 |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
52 |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
53 # quoted in help or docstrings |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
54 m = re.match(r'.*?``([-a-z_]+\.[-a-z_]+)``', l) |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
55 if m: |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
56 documented[m.group(1)] = 1 |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
57 |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
58 # look for ignore markers |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
59 m = re.search(r'# (?:internal|experimental|deprecated|developer)' |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
60 ' config: (\S+.\S+)$', l) |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
61 if m: |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
62 documented[m.group(1)] = 1 |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
63 |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
64 # look for code-like bits |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
65 m = re.search(configre, l) |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
66 if m: |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
67 ctype = m.group(1) |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
68 if not ctype: |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
69 ctype = 'str' |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
70 name = m.group(2) + "." + m.group(3) |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
71 default = m.group(5) |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
72 if default in (None, 'False', 'None', '0', '[]', '""', "''"): |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
73 default = '' |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
74 if re.match('[a-z.]+$', default): |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
75 default = '<variable>' |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
76 if name in foundopts and (ctype, default) != foundopts[name]: |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
77 print l |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
78 print "conflict on %s: %r != %r" % (name, (ctype, default), |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
79 foundopts[name]) |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
80 foundopts[name] = (ctype, default) |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
81 |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
82 for name in sorted(foundopts): |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
83 if name not in documented: |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
84 if not (name.startswith("devel.") or |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
85 name.startswith("experimental.") or |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
86 name.startswith("debug.")): |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
87 ctype, default = foundopts[name] |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
88 if default: |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
89 default = ' [%s]' % default |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
90 print "undocumented: %s (%s)%s" % (name, ctype, default) |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
91 |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
92 if __name__ == "__main__": |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
93 sys.exit(main(sys.argv[1:])) |