annotate contrib/casesmash.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 9de689d20230
children 42a7301fb4d5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19322
ff1586a3adc5 cleanup: remove unused imports
Simon Heimberg <simohe@besonet.ch>
parents: 14730
diff changeset
1 import os, __builtin__
14730
f5765353d430 merge with stable
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
2 from mercurial import util
f5765353d430 merge with stable
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
3
f5765353d430 merge with stable
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
4 def lowerwrap(scope, funcname):
f5765353d430 merge with stable
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
5 f = getattr(scope, funcname)
f5765353d430 merge with stable
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
6 def wrap(fname, *args, **kwargs):
f5765353d430 merge with stable
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
7 d, base = os.path.split(fname)
f5765353d430 merge with stable
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
8 try:
f5765353d430 merge with stable
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
9 files = os.listdir(d or '.')
19378
9de689d20230 cleanup: drop unused variables and an unused import
Simon Heimberg <simohe@besonet.ch>
parents: 19322
diff changeset
10 except OSError:
14730
f5765353d430 merge with stable
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
11 files = []
f5765353d430 merge with stable
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
12 if base in files:
f5765353d430 merge with stable
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
13 return f(fname, *args, **kwargs)
f5765353d430 merge with stable
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
14 for fn in files:
f5765353d430 merge with stable
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
15 if fn.lower() == base.lower():
f5765353d430 merge with stable
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
16 return f(os.path.join(d, fn), *args, **kwargs)
f5765353d430 merge with stable
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
17 return f(fname, *args, **kwargs)
f5765353d430 merge with stable
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
18 scope.__dict__[funcname] = wrap
f5765353d430 merge with stable
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
19
f5765353d430 merge with stable
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
20 def normcase(path):
f5765353d430 merge with stable
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
21 return path.lower()
f5765353d430 merge with stable
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
22
f5765353d430 merge with stable
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
23 os.path.normcase = normcase
f5765353d430 merge with stable
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
24
f5765353d430 merge with stable
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
25 for f in 'file open'.split():
f5765353d430 merge with stable
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
26 lowerwrap(__builtin__, f)
f5765353d430 merge with stable
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
27
f5765353d430 merge with stable
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
28 for f in "chmod chown open lstat stat remove unlink".split():
f5765353d430 merge with stable
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
29 lowerwrap(os, f)
f5765353d430 merge with stable
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
30
f5765353d430 merge with stable
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
31 for f in "exists lexists".split():
f5765353d430 merge with stable
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
32 lowerwrap(os.path, f)
f5765353d430 merge with stable
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
33
f5765353d430 merge with stable
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
34 lowerwrap(util, 'posixfile')