Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/extensions.py @ 7916:f779e1996e23
ability to load hooks from arbitrary python module
author | Alexander Solovyov <piranha@piranha.org.ua> |
---|---|
date | Fri, 27 Mar 2009 01:28:09 +0200 |
parents | 53c72ba36c2b |
children | 5c794e7331e7 |
comparison
equal
deleted
inserted
replaced
7915:fba93bbbca02 | 7916:f779e1996e23 |
---|---|
26 for k, v in _extensions.iteritems(): | 26 for k, v in _extensions.iteritems(): |
27 if k.endswith('.' + name) or k.endswith('/' + name): | 27 if k.endswith('.' + name) or k.endswith('/' + name): |
28 return v | 28 return v |
29 raise KeyError(name) | 29 raise KeyError(name) |
30 | 30 |
31 def loadpath(path, module_name): | |
32 module_name = module_name.replace('.', '_') | |
33 path = os.path.expanduser(path) | |
34 if os.path.isdir(path): | |
35 # module/__init__.py style | |
36 d, f = os.path.split(path) | |
37 fd, fpath, desc = imp.find_module(f, [d]) | |
38 return imp.load_module(module_name, fd, fpath, desc) | |
39 else: | |
40 return imp.load_source(module_name, path) | |
41 | |
31 def load(ui, name, path): | 42 def load(ui, name, path): |
32 if name.startswith('hgext.') or name.startswith('hgext/'): | 43 if name.startswith('hgext.') or name.startswith('hgext/'): |
33 shortname = name[6:] | 44 shortname = name[6:] |
34 else: | 45 else: |
35 shortname = name | 46 shortname = name |
38 _extensions[shortname] = None | 49 _extensions[shortname] = None |
39 if path: | 50 if path: |
40 # the module will be loaded in sys.modules | 51 # the module will be loaded in sys.modules |
41 # choose an unique name so that it doesn't | 52 # choose an unique name so that it doesn't |
42 # conflicts with other modules | 53 # conflicts with other modules |
43 module_name = "hgext_%s" % name.replace('.', '_') | 54 mod = loadpath(path, 'hgext.%s' % name) |
44 if os.path.isdir(path): | |
45 # module/__init__.py style | |
46 d, f = os.path.split(path) | |
47 fd, fpath, desc = imp.find_module(f, [d]) | |
48 mod = imp.load_module(module_name, fd, fpath, desc) | |
49 else: | |
50 mod = imp.load_source(module_name, path) | |
51 else: | 55 else: |
52 def importh(name): | 56 def importh(name): |
53 mod = __import__(name) | 57 mod = __import__(name) |
54 components = name.split('.') | 58 components = name.split('.') |
55 for comp in components[1:]: | 59 for comp in components[1:]: |
70 result = ui.configitems("extensions") | 74 result = ui.configitems("extensions") |
71 for (name, path) in result: | 75 for (name, path) in result: |
72 if path: | 76 if path: |
73 if path[0] == '!': | 77 if path[0] == '!': |
74 continue | 78 continue |
75 path = os.path.expanduser(path) | |
76 try: | 79 try: |
77 load(ui, name, path) | 80 load(ui, name, path) |
78 except KeyboardInterrupt: | 81 except KeyboardInterrupt: |
79 raise | 82 raise |
80 except Exception, inst: | 83 except Exception, inst: |