Mercurial > hg
annotate mercurial/extensions.py @ 50922:0e6cea0c3113
extension: check the command attributes using `sysstr`
Since we are checking attributes, lets use the native representation instead of
bytes.
author | Pierre-Yves David <pierre-yves.david@octobus.net> |
---|---|
date | Thu, 31 Aug 2023 01:54:48 +0200 |
parents | 538c5a48e8f4 |
children | c642c03969ff |
rev | line source |
---|---|
4544
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1 # extensions.py - extension handling for mercurial |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
2 # |
46819
d4ba4d51f85f
contributor: change mentions of mpm to olivia
Raphaël Gomès <rgomes@octobus.net>
parents:
46690
diff
changeset
|
3 # Copyright 2005-2007 Olivia Mackall <olivia@selenic.com> |
4544
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
4 # |
8225
46293a0c7e9f
updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents:
8206
diff
changeset
|
5 # This software may be used and distributed according to the terms of the |
10263 | 6 # GNU General Public License version 2 or any later version. |
4544
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
7 |
25946
5e0d80195a0f
extensions: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
8 |
38162
bdf344aea0ee
extensions: peek command table of disabled extensions without importing
Yuya Nishihara <yuya@tcha.org>
parents:
38020
diff
changeset
|
9 import ast |
bdf344aea0ee
extensions: peek command table of disabled extensions without importing
Yuya Nishihara <yuya@tcha.org>
parents:
38020
diff
changeset
|
10 import collections |
34087
5361771f9714
wrapfunction: use functools.partial if possible
Jun Wu <quark@fb.com>
parents:
34048
diff
changeset
|
11 import functools |
50757
19108906abaf
extensions: imp module is removed in Python 3.12 - use importlib to load files
Mads Kiilerich <mads@kiilerich.com>
parents:
50755
diff
changeset
|
12 import importlib |
31263
d79761fe697f
extensions: use inspect module instead of func_code.co_argcount
Augie Fackler <raf@durin42.com>
parents:
31074
diff
changeset
|
13 import inspect |
25946
5e0d80195a0f
extensions: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
14 import os |
50757
19108906abaf
extensions: imp module is removed in Python 3.12 - use importlib to load files
Mads Kiilerich <mads@kiilerich.com>
parents:
50755
diff
changeset
|
15 import sys |
25946
5e0d80195a0f
extensions: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
16 |
5e0d80195a0f
extensions: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
17 from .i18n import ( |
5e0d80195a0f
extensions: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
18 _, |
5e0d80195a0f
extensions: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
19 gettext, |
5e0d80195a0f
extensions: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
20 ) |
43087
66f2cc210a29
py3: manually import pycompat.setattr where it is needed
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43085
diff
changeset
|
21 from .pycompat import ( |
43089
c59eb1560c44
py3: manually import getattr where it is needed
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43087
diff
changeset
|
22 getattr, |
43087
66f2cc210a29
py3: manually import pycompat.setattr where it is needed
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43085
diff
changeset
|
23 open, |
66f2cc210a29
py3: manually import pycompat.setattr where it is needed
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43085
diff
changeset
|
24 setattr, |
66f2cc210a29
py3: manually import pycompat.setattr where it is needed
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43085
diff
changeset
|
25 ) |
25946
5e0d80195a0f
extensions: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
26 |
5e0d80195a0f
extensions: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
27 from . import ( |
5e0d80195a0f
extensions: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
28 cmdutil, |
33132
c467d13334ee
configitems: add an official API for extensions to register config item
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33052
diff
changeset
|
29 configitems, |
25946
5e0d80195a0f
extensions: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
30 error, |
30570
c4c51fd0e11d
py3: use pycompat.sysstr() in __import__()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30306
diff
changeset
|
31 pycompat, |
25946
5e0d80195a0f
extensions: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
32 util, |
5e0d80195a0f
extensions: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
33 ) |
4544
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
34 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
35 from .utils import stringutil |
37084
f0b6fbea00cf
stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents:
36922
diff
changeset
|
36 |
4544
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
37 _extensions = {} |
29895
b1ebc767563d
help: show content for explicitly disabled extension (issue5228)
liscju <piotr.listkiewicz@gmail.com>
parents:
29841
diff
changeset
|
38 _disabledextensions = {} |
24065
d8837ad682dd
extensions: support callbacks after another extension loads
Gregory Szorc <gregory.szorc@gmail.com>
parents:
23953
diff
changeset
|
39 _aftercallbacks = {} |
5192
60acf1432ee0
Move cmdtable and reposetup handling out of extensions.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
5152
diff
changeset
|
40 _order = [] |
33526
792d121f22ba
extensions: expand the builtins extensions declaration
Boris Feld <boris.feld@octobus.net>
parents:
33327
diff
changeset
|
41 _builtin = { |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
42 b'hbisect', |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
43 b'bookmarks', |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
44 b'color', |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
45 b'parentrevspec', |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
46 b'progress', |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
47 b'interhg', |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
48 b'inotify', |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
49 b'hgcia', |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
50 b'shelve', |
33526
792d121f22ba
extensions: expand the builtins extensions declaration
Boris Feld <boris.feld@octobus.net>
parents:
33327
diff
changeset
|
51 } |
5192
60acf1432ee0
Move cmdtable and reposetup handling out of extensions.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
5152
diff
changeset
|
52 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
53 |
19777
6f72e7d28b35
extensions: list up only enabled extensions, if "ui" is specified
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
19769
diff
changeset
|
54 def extensions(ui=None): |
6f72e7d28b35
extensions: list up only enabled extensions, if "ui" is specified
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
19769
diff
changeset
|
55 if ui: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
56 |
19777
6f72e7d28b35
extensions: list up only enabled extensions, if "ui" is specified
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
19769
diff
changeset
|
57 def enabled(name): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
58 for format in [b'%s', b'hgext.%s']: |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
59 conf = ui.config(b'extensions', format % name) |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
60 if conf is not None and not conf.startswith(b'!'): |
19777
6f72e7d28b35
extensions: list up only enabled extensions, if "ui" is specified
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
19769
diff
changeset
|
61 return True |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
62 |
19777
6f72e7d28b35
extensions: list up only enabled extensions, if "ui" is specified
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
19769
diff
changeset
|
63 else: |
6f72e7d28b35
extensions: list up only enabled extensions, if "ui" is specified
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
19769
diff
changeset
|
64 enabled = lambda name: True |
5192
60acf1432ee0
Move cmdtable and reposetup handling out of extensions.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
5152
diff
changeset
|
65 for name in _order: |
60acf1432ee0
Move cmdtable and reposetup handling out of extensions.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
5152
diff
changeset
|
66 module = _extensions[name] |
19777
6f72e7d28b35
extensions: list up only enabled extensions, if "ui" is specified
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
19769
diff
changeset
|
67 if module and enabled(name): |
5192
60acf1432ee0
Move cmdtable and reposetup handling out of extensions.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
5152
diff
changeset
|
68 yield name, module |
4544
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
69 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
70 |
4544
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
71 def find(name): |
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
72 '''return module with given extension name''' |
14415
c238b12a1ed4
extensions: raise when trying to find an extension that failed to load
Idan Kamara <idankk86@gmail.com>
parents:
14318
diff
changeset
|
73 mod = None |
4544
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
74 try: |
27637
b502138f5faa
cleanup: remove superfluous space after space after equals (python)
timeless <timeless@mozdev.org>
parents:
27142
diff
changeset
|
75 mod = _extensions[name] |
4544
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
76 except KeyError: |
48913
f254fc73d956
global: bulk replace simple pycompat.iteritems(x) with x.items()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48894
diff
changeset
|
77 for k, v in _extensions.items(): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
78 if k.endswith(b'.' + name) or k.endswith(b'/' + name): |
14415
c238b12a1ed4
extensions: raise when trying to find an extension that failed to load
Idan Kamara <idankk86@gmail.com>
parents:
14318
diff
changeset
|
79 mod = v |
c238b12a1ed4
extensions: raise when trying to find an extension that failed to load
Idan Kamara <idankk86@gmail.com>
parents:
14318
diff
changeset
|
80 break |
c238b12a1ed4
extensions: raise when trying to find an extension that failed to load
Idan Kamara <idankk86@gmail.com>
parents:
14318
diff
changeset
|
81 if not mod: |
4544
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
82 raise KeyError(name) |
14415
c238b12a1ed4
extensions: raise when trying to find an extension that failed to load
Idan Kamara <idankk86@gmail.com>
parents:
14318
diff
changeset
|
83 return mod |
4544
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
84 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
85 |
7916
f779e1996e23
ability to load hooks from arbitrary python module
Alexander Solovyov <piranha@piranha.org.ua>
parents:
7876
diff
changeset
|
86 def loadpath(path, module_name): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
87 module_name = module_name.replace(b'.', b'_') |
20645
7d83c3b6e8d9
extensions: use normpath to allow trailing '\' on Windows (issue4187)
Ed Morley <emorley@mozilla.com>
parents:
20622
diff
changeset
|
88 path = util.normpath(util.expandpath(path)) |
30575
5ffbaba9acac
py3: use pycompat.fsdecode() to pass to imp.* functions
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30570
diff
changeset
|
89 module_name = pycompat.fsdecode(module_name) |
5ffbaba9acac
py3: use pycompat.fsdecode() to pass to imp.* functions
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30570
diff
changeset
|
90 path = pycompat.fsdecode(path) |
7916
f779e1996e23
ability to load hooks from arbitrary python module
Alexander Solovyov <piranha@piranha.org.ua>
parents:
7876
diff
changeset
|
91 if os.path.isdir(path): |
f779e1996e23
ability to load hooks from arbitrary python module
Alexander Solovyov <piranha@piranha.org.ua>
parents:
7876
diff
changeset
|
92 # module/__init__.py style |
50757
19108906abaf
extensions: imp module is removed in Python 3.12 - use importlib to load files
Mads Kiilerich <mads@kiilerich.com>
parents:
50755
diff
changeset
|
93 init_py_path = os.path.join(path, '__init__.py') |
19108906abaf
extensions: imp module is removed in Python 3.12 - use importlib to load files
Mads Kiilerich <mads@kiilerich.com>
parents:
50755
diff
changeset
|
94 if not os.path.exists(init_py_path): |
19108906abaf
extensions: imp module is removed in Python 3.12 - use importlib to load files
Mads Kiilerich <mads@kiilerich.com>
parents:
50755
diff
changeset
|
95 raise ImportError("No module named '%s'" % os.path.basename(path)) |
19108906abaf
extensions: imp module is removed in Python 3.12 - use importlib to load files
Mads Kiilerich <mads@kiilerich.com>
parents:
50755
diff
changeset
|
96 path = init_py_path |
19108906abaf
extensions: imp module is removed in Python 3.12 - use importlib to load files
Mads Kiilerich <mads@kiilerich.com>
parents:
50755
diff
changeset
|
97 |
19108906abaf
extensions: imp module is removed in Python 3.12 - use importlib to load files
Mads Kiilerich <mads@kiilerich.com>
parents:
50755
diff
changeset
|
98 loader = importlib.machinery.SourceFileLoader(module_name, path) |
19108906abaf
extensions: imp module is removed in Python 3.12 - use importlib to load files
Mads Kiilerich <mads@kiilerich.com>
parents:
50755
diff
changeset
|
99 spec = importlib.util.spec_from_file_location(module_name, loader=loader) |
19108906abaf
extensions: imp module is removed in Python 3.12 - use importlib to load files
Mads Kiilerich <mads@kiilerich.com>
parents:
50755
diff
changeset
|
100 assert spec is not None # help Pytype |
19108906abaf
extensions: imp module is removed in Python 3.12 - use importlib to load files
Mads Kiilerich <mads@kiilerich.com>
parents:
50755
diff
changeset
|
101 module = importlib.util.module_from_spec(spec) |
19108906abaf
extensions: imp module is removed in Python 3.12 - use importlib to load files
Mads Kiilerich <mads@kiilerich.com>
parents:
50755
diff
changeset
|
102 sys.modules[module_name] = module |
19108906abaf
extensions: imp module is removed in Python 3.12 - use importlib to load files
Mads Kiilerich <mads@kiilerich.com>
parents:
50755
diff
changeset
|
103 spec.loader.exec_module(module) |
19108906abaf
extensions: imp module is removed in Python 3.12 - use importlib to load files
Mads Kiilerich <mads@kiilerich.com>
parents:
50755
diff
changeset
|
104 return module |
7916
f779e1996e23
ability to load hooks from arbitrary python module
Alexander Solovyov <piranha@piranha.org.ua>
parents:
7876
diff
changeset
|
105 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
106 |
28505
d5512a0a8ad6
extensions: extract the 'importh' closure as normal function
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
28312
diff
changeset
|
107 def _importh(name): |
d5512a0a8ad6
extensions: extract the 'importh' closure as normal function
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
28312
diff
changeset
|
108 """import and return the <name> module""" |
30570
c4c51fd0e11d
py3: use pycompat.sysstr() in __import__()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30306
diff
changeset
|
109 mod = __import__(pycompat.sysstr(name)) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
110 components = name.split(b'.') |
28505
d5512a0a8ad6
extensions: extract the 'importh' closure as normal function
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
28312
diff
changeset
|
111 for comp in components[1:]: |
d5512a0a8ad6
extensions: extract the 'importh' closure as normal function
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
28312
diff
changeset
|
112 mod = getattr(mod, comp) |
d5512a0a8ad6
extensions: extract the 'importh' closure as normal function
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
28312
diff
changeset
|
113 return mod |
d5512a0a8ad6
extensions: extract the 'importh' closure as normal function
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
28312
diff
changeset
|
114 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
115 |
30058
8f54f9b8010d
extensions: move the "import" logic out from "load"
Jun Wu <quark@fb.com>
parents:
30028
diff
changeset
|
116 def _importext(name, path=None, reportfunc=None): |
8f54f9b8010d
extensions: move the "import" logic out from "load"
Jun Wu <quark@fb.com>
parents:
30028
diff
changeset
|
117 if path: |
8f54f9b8010d
extensions: move the "import" logic out from "load"
Jun Wu <quark@fb.com>
parents:
30028
diff
changeset
|
118 # the module will be loaded in sys.modules |
8f54f9b8010d
extensions: move the "import" logic out from "load"
Jun Wu <quark@fb.com>
parents:
30028
diff
changeset
|
119 # choose an unique name so that it doesn't |
8f54f9b8010d
extensions: move the "import" logic out from "load"
Jun Wu <quark@fb.com>
parents:
30028
diff
changeset
|
120 # conflicts with other modules |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
121 mod = loadpath(path, b'hgext.%s' % name) |
30058
8f54f9b8010d
extensions: move the "import" logic out from "load"
Jun Wu <quark@fb.com>
parents:
30028
diff
changeset
|
122 else: |
8f54f9b8010d
extensions: move the "import" logic out from "load"
Jun Wu <quark@fb.com>
parents:
30028
diff
changeset
|
123 try: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
124 mod = _importh(b"hgext.%s" % name) |
30058
8f54f9b8010d
extensions: move the "import" logic out from "load"
Jun Wu <quark@fb.com>
parents:
30028
diff
changeset
|
125 except ImportError as err: |
8f54f9b8010d
extensions: move the "import" logic out from "load"
Jun Wu <quark@fb.com>
parents:
30028
diff
changeset
|
126 if reportfunc: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
127 reportfunc(err, b"hgext.%s" % name, b"hgext3rd.%s" % name) |
30058
8f54f9b8010d
extensions: move the "import" logic out from "load"
Jun Wu <quark@fb.com>
parents:
30028
diff
changeset
|
128 try: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
129 mod = _importh(b"hgext3rd.%s" % name) |
30058
8f54f9b8010d
extensions: move the "import" logic out from "load"
Jun Wu <quark@fb.com>
parents:
30028
diff
changeset
|
130 except ImportError as err: |
8f54f9b8010d
extensions: move the "import" logic out from "load"
Jun Wu <quark@fb.com>
parents:
30028
diff
changeset
|
131 if reportfunc: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
132 reportfunc(err, b"hgext3rd.%s" % name, name) |
30058
8f54f9b8010d
extensions: move the "import" logic out from "load"
Jun Wu <quark@fb.com>
parents:
30028
diff
changeset
|
133 mod = _importh(name) |
8f54f9b8010d
extensions: move the "import" logic out from "load"
Jun Wu <quark@fb.com>
parents:
30028
diff
changeset
|
134 return mod |
8f54f9b8010d
extensions: move the "import" logic out from "load"
Jun Wu <quark@fb.com>
parents:
30028
diff
changeset
|
135 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
136 |
28506
10252652c6e4
extensions: factor import error reporting out
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
28505
diff
changeset
|
137 def _reportimporterror(ui, err, failed, next): |
40996
6f2510b581a0
extensions: use ui.log() interface to provide detailed loading information
Yuya Nishihara <yuya@tcha.org>
parents:
40729
diff
changeset
|
138 # note: this ui.log happens before --debug is processed, |
30028
3741a8f86e88
extensions: add a note about debug output during extensions search
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
30027
diff
changeset
|
139 # Use --config ui.debug=1 to see them. |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
140 ui.log( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
141 b'extension', |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
142 b' - could not import %s (%s): trying %s\n', |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
143 failed, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
144 stringutil.forcebytestr(err), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
145 next, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
146 ) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
147 if ui.debugflag and ui.configbool(b'devel', b'debug.extensions'): |
40996
6f2510b581a0
extensions: use ui.log() interface to provide detailed loading information
Yuya Nishihara <yuya@tcha.org>
parents:
40729
diff
changeset
|
148 ui.traceback() |
28506
10252652c6e4
extensions: factor import error reporting out
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
28505
diff
changeset
|
149 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
150 |
36269
4088e568a411
extensions: reject any unicode strings in tables before loading
Yuya Nishihara <yuya@tcha.org>
parents:
36178
diff
changeset
|
151 def _rejectunicode(name, xs): |
4088e568a411
extensions: reject any unicode strings in tables before loading
Yuya Nishihara <yuya@tcha.org>
parents:
36178
diff
changeset
|
152 if isinstance(xs, (list, set, tuple)): |
4088e568a411
extensions: reject any unicode strings in tables before loading
Yuya Nishihara <yuya@tcha.org>
parents:
36178
diff
changeset
|
153 for x in xs: |
4088e568a411
extensions: reject any unicode strings in tables before loading
Yuya Nishihara <yuya@tcha.org>
parents:
36178
diff
changeset
|
154 _rejectunicode(name, x) |
4088e568a411
extensions: reject any unicode strings in tables before loading
Yuya Nishihara <yuya@tcha.org>
parents:
36178
diff
changeset
|
155 elif isinstance(xs, dict): |
4088e568a411
extensions: reject any unicode strings in tables before loading
Yuya Nishihara <yuya@tcha.org>
parents:
36178
diff
changeset
|
156 for k, v in xs.items(): |
4088e568a411
extensions: reject any unicode strings in tables before loading
Yuya Nishihara <yuya@tcha.org>
parents:
36178
diff
changeset
|
157 _rejectunicode(name, k) |
50921
538c5a48e8f4
extension: access special module members using sysstr
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
50894
diff
changeset
|
158 k = pycompat.sysstr(k) |
538c5a48e8f4
extension: access special module members using sysstr
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
50894
diff
changeset
|
159 _rejectunicode('%s.%s' % (name, k), v) |
538c5a48e8f4
extension: access special module members using sysstr
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
50894
diff
changeset
|
160 elif isinstance(xs, str): |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
161 raise error.ProgrammingError( |
50921
538c5a48e8f4
extension: access special module members using sysstr
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
50894
diff
changeset
|
162 b"unicode %r found in %s" % (xs, stringutil.forcebytestr(name)), |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
163 hint=b"use b'' to make it byte string", |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
164 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
165 |
36269
4088e568a411
extensions: reject any unicode strings in tables before loading
Yuya Nishihara <yuya@tcha.org>
parents:
36178
diff
changeset
|
166 |
32342
e5fbf9687600
extensions: prohibit registration of command without using @command (API)
Yuya Nishihara <yuya@tcha.org>
parents:
32341
diff
changeset
|
167 # attributes set by registrar.command |
50922
0e6cea0c3113
extension: check the command attributes using `sysstr`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
50921
diff
changeset
|
168 _cmdfuncattrs = ('norepo', 'optionalrepo', 'inferrepo') |
32342
e5fbf9687600
extensions: prohibit registration of command without using @command (API)
Yuya Nishihara <yuya@tcha.org>
parents:
32341
diff
changeset
|
169 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
170 |
32343
d47d7d3bd07b
extensions: show deprecation warning for the use of cmdutil.command
Yuya Nishihara <yuya@tcha.org>
parents:
32342
diff
changeset
|
171 def _validatecmdtable(ui, cmdtable): |
32342
e5fbf9687600
extensions: prohibit registration of command without using @command (API)
Yuya Nishihara <yuya@tcha.org>
parents:
32341
diff
changeset
|
172 """Check if extension commands have required attributes""" |
48913
f254fc73d956
global: bulk replace simple pycompat.iteritems(x) with x.items()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48894
diff
changeset
|
173 for c, e in cmdtable.items(): |
32342
e5fbf9687600
extensions: prohibit registration of command without using @command (API)
Yuya Nishihara <yuya@tcha.org>
parents:
32341
diff
changeset
|
174 f = e[0] |
e5fbf9687600
extensions: prohibit registration of command without using @command (API)
Yuya Nishihara <yuya@tcha.org>
parents:
32341
diff
changeset
|
175 missing = [a for a in _cmdfuncattrs if not util.safehasattr(f, a)] |
e5fbf9687600
extensions: prohibit registration of command without using @command (API)
Yuya Nishihara <yuya@tcha.org>
parents:
32341
diff
changeset
|
176 if not missing: |
e5fbf9687600
extensions: prohibit registration of command without using @command (API)
Yuya Nishihara <yuya@tcha.org>
parents:
32341
diff
changeset
|
177 continue |
50922
0e6cea0c3113
extension: check the command attributes using `sysstr`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
50921
diff
changeset
|
178 msg = b'missing attributes: %s' |
0e6cea0c3113
extension: check the command attributes using `sysstr`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
50921
diff
changeset
|
179 msg %= b', '.join([stringutil.forcebytestr(m) for m in missing]) |
0e6cea0c3113
extension: check the command attributes using `sysstr`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
50921
diff
changeset
|
180 hint = b"use @command decorator to register '%s'" % c |
0e6cea0c3113
extension: check the command attributes using `sysstr`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
50921
diff
changeset
|
181 raise error.ProgrammingError(msg, hint=hint) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
182 |
32342
e5fbf9687600
extensions: prohibit registration of command without using @command (API)
Yuya Nishihara <yuya@tcha.org>
parents:
32341
diff
changeset
|
183 |
36269
4088e568a411
extensions: reject any unicode strings in tables before loading
Yuya Nishihara <yuya@tcha.org>
parents:
36178
diff
changeset
|
184 def _validatetables(ui, mod): |
4088e568a411
extensions: reject any unicode strings in tables before loading
Yuya Nishihara <yuya@tcha.org>
parents:
36178
diff
changeset
|
185 """Sanity check for loadable tables provided by extension module""" |
50921
538c5a48e8f4
extension: access special module members using sysstr
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
50894
diff
changeset
|
186 for t in ['cmdtable', 'colortable', 'configtable']: |
36269
4088e568a411
extensions: reject any unicode strings in tables before loading
Yuya Nishihara <yuya@tcha.org>
parents:
36178
diff
changeset
|
187 _rejectunicode(t, getattr(mod, t, {})) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
188 for t in [ |
50921
538c5a48e8f4
extension: access special module members using sysstr
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
50894
diff
changeset
|
189 'filesetpredicate', |
538c5a48e8f4
extension: access special module members using sysstr
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
50894
diff
changeset
|
190 'internalmerge', |
538c5a48e8f4
extension: access special module members using sysstr
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
50894
diff
changeset
|
191 'revsetpredicate', |
538c5a48e8f4
extension: access special module members using sysstr
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
50894
diff
changeset
|
192 'templatefilter', |
538c5a48e8f4
extension: access special module members using sysstr
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
50894
diff
changeset
|
193 'templatefunc', |
538c5a48e8f4
extension: access special module members using sysstr
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
50894
diff
changeset
|
194 'templatekeyword', |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
195 ]: |
36269
4088e568a411
extensions: reject any unicode strings in tables before loading
Yuya Nishihara <yuya@tcha.org>
parents:
36178
diff
changeset
|
196 o = getattr(mod, t, None) |
4088e568a411
extensions: reject any unicode strings in tables before loading
Yuya Nishihara <yuya@tcha.org>
parents:
36178
diff
changeset
|
197 if o: |
4088e568a411
extensions: reject any unicode strings in tables before loading
Yuya Nishihara <yuya@tcha.org>
parents:
36178
diff
changeset
|
198 _rejectunicode(t, o._table) |
4088e568a411
extensions: reject any unicode strings in tables before loading
Yuya Nishihara <yuya@tcha.org>
parents:
36178
diff
changeset
|
199 _validatecmdtable(ui, getattr(mod, 'cmdtable', {})) |
4088e568a411
extensions: reject any unicode strings in tables before loading
Yuya Nishihara <yuya@tcha.org>
parents:
36178
diff
changeset
|
200 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
201 |
40996
6f2510b581a0
extensions: use ui.log() interface to provide detailed loading information
Yuya Nishihara <yuya@tcha.org>
parents:
40729
diff
changeset
|
202 def load(ui, name, path, loadingtime=None): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
203 if name.startswith(b'hgext.') or name.startswith(b'hgext/'): |
5031
af0995261f02
extensions: don't get confused by aliasing between "foo" and "hgext.foo"
Bryan O'Sullivan <bos@serpentine.com>
parents:
4818
diff
changeset
|
204 shortname = name[6:] |
af0995261f02
extensions: don't get confused by aliasing between "foo" and "hgext.foo"
Bryan O'Sullivan <bos@serpentine.com>
parents:
4818
diff
changeset
|
205 else: |
af0995261f02
extensions: don't get confused by aliasing between "foo" and "hgext.foo"
Bryan O'Sullivan <bos@serpentine.com>
parents:
4818
diff
changeset
|
206 shortname = name |
27111
9de814b35808
extensions: rename _ignore to _builtin, add descriptive comment
Bryan O'Sullivan <bos@serpentine.com>
parents:
26781
diff
changeset
|
207 if shortname in _builtin: |
13349
0d3f35394af4
extensions: add an ignore list for old extensions
Matt Mackall <mpm@selenic.com>
parents:
13191
diff
changeset
|
208 return None |
5031
af0995261f02
extensions: don't get confused by aliasing between "foo" and "hgext.foo"
Bryan O'Sullivan <bos@serpentine.com>
parents:
4818
diff
changeset
|
209 if shortname in _extensions: |
12779
891ddf76b73e
extensions.load: return module
Erik Zielke <ez@aragost.com>
parents:
11521
diff
changeset
|
210 return _extensions[shortname] |
40996
6f2510b581a0
extensions: use ui.log() interface to provide detailed loading information
Yuya Nishihara <yuya@tcha.org>
parents:
40729
diff
changeset
|
211 ui.log(b'extension', b' - loading extension: %s\n', shortname) |
5087
b3cc62268a91
Cache extension load failures.
Brendan Cully <brendan@kublai.com>
parents:
4635
diff
changeset
|
212 _extensions[shortname] = None |
43238
101ae8bbfa02
cleanup: hgdemandimport.tracing accepts strings, not bytes
Augie Fackler <augie@google.com>
parents:
43106
diff
changeset
|
213 with util.timedcm('load extension %s', shortname) as stats: |
38798
d58958676b3c
extensions: add detailed loading information
Martijn Pieters <mj@zopatista.com>
parents:
38727
diff
changeset
|
214 mod = _importext(name, path, bind(_reportimporterror, ui)) |
40996
6f2510b581a0
extensions: use ui.log() interface to provide detailed loading information
Yuya Nishihara <yuya@tcha.org>
parents:
40729
diff
changeset
|
215 ui.log(b'extension', b' > %s extension loaded in %s\n', shortname, stats) |
39511
1ab185c78cc3
extension: add a summary of total loading time per extension
Boris Feld <boris.feld@octobus.net>
parents:
39509
diff
changeset
|
216 if loadingtime is not None: |
1ab185c78cc3
extension: add a summary of total loading time per extension
Boris Feld <boris.feld@octobus.net>
parents:
39509
diff
changeset
|
217 loadingtime[shortname] += stats.elapsed |
27142
060f83d219b9
extensions: refuse to load extensions if minimum hg version not met
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27116
diff
changeset
|
218 |
060f83d219b9
extensions: refuse to load extensions if minimum hg version not met
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27116
diff
changeset
|
219 # Before we do anything with the extension, check against minimum stated |
060f83d219b9
extensions: refuse to load extensions if minimum hg version not met
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27116
diff
changeset
|
220 # compatibility. This gives extension authors a mechanism to have their |
060f83d219b9
extensions: refuse to load extensions if minimum hg version not met
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27116
diff
changeset
|
221 # extensions short circuit when loaded with a known incompatible version |
060f83d219b9
extensions: refuse to load extensions if minimum hg version not met
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27116
diff
changeset
|
222 # of Mercurial. |
060f83d219b9
extensions: refuse to load extensions if minimum hg version not met
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27116
diff
changeset
|
223 minver = getattr(mod, 'minimumhgversion', None) |
45899
f96059fa519c
extensions: gracefully warn when doing min version check with no local version
Matt Harbison <matt_harbison@yahoo.com>
parents:
45105
diff
changeset
|
224 if minver: |
f96059fa519c
extensions: gracefully warn when doing min version check with no local version
Matt Harbison <matt_harbison@yahoo.com>
parents:
45105
diff
changeset
|
225 curver = util.versiontuple(n=2) |
48016
5caec48d9a01
extensions: prevent a crash on py3 with a `minimumhgversion` str value
Matt Harbison <matt_harbison@yahoo.com>
parents:
48015
diff
changeset
|
226 extmin = util.versiontuple(stringutil.forcebytestr(minver), 2) |
45899
f96059fa519c
extensions: gracefully warn when doing min version check with no local version
Matt Harbison <matt_harbison@yahoo.com>
parents:
45105
diff
changeset
|
227 |
48015
a9bedc56f025
extensions: prevent a crash on py3 when testing a bad extension minimum
Matt Harbison <matt_harbison@yahoo.com>
parents:
47625
diff
changeset
|
228 if None in extmin: |
a9bedc56f025
extensions: prevent a crash on py3 when testing a bad extension minimum
Matt Harbison <matt_harbison@yahoo.com>
parents:
47625
diff
changeset
|
229 extmin = (extmin[0] or 0, extmin[1] or 0) |
a9bedc56f025
extensions: prevent a crash on py3 when testing a bad extension minimum
Matt Harbison <matt_harbison@yahoo.com>
parents:
47625
diff
changeset
|
230 |
a9bedc56f025
extensions: prevent a crash on py3 when testing a bad extension minimum
Matt Harbison <matt_harbison@yahoo.com>
parents:
47625
diff
changeset
|
231 if None in curver or extmin > curver: |
45899
f96059fa519c
extensions: gracefully warn when doing min version check with no local version
Matt Harbison <matt_harbison@yahoo.com>
parents:
45105
diff
changeset
|
232 msg = _( |
f96059fa519c
extensions: gracefully warn when doing min version check with no local version
Matt Harbison <matt_harbison@yahoo.com>
parents:
45105
diff
changeset
|
233 b'(third party extension %s requires version %s or newer ' |
f96059fa519c
extensions: gracefully warn when doing min version check with no local version
Matt Harbison <matt_harbison@yahoo.com>
parents:
45105
diff
changeset
|
234 b'of Mercurial (current: %s); disabling)\n' |
f96059fa519c
extensions: gracefully warn when doing min version check with no local version
Matt Harbison <matt_harbison@yahoo.com>
parents:
45105
diff
changeset
|
235 ) |
f96059fa519c
extensions: gracefully warn when doing min version check with no local version
Matt Harbison <matt_harbison@yahoo.com>
parents:
45105
diff
changeset
|
236 ui.warn(msg % (shortname, minver, util.version())) |
f96059fa519c
extensions: gracefully warn when doing min version check with no local version
Matt Harbison <matt_harbison@yahoo.com>
parents:
45105
diff
changeset
|
237 return |
40996
6f2510b581a0
extensions: use ui.log() interface to provide detailed loading information
Yuya Nishihara <yuya@tcha.org>
parents:
40729
diff
changeset
|
238 ui.log(b'extension', b' - validating extension tables: %s\n', shortname) |
36269
4088e568a411
extensions: reject any unicode strings in tables before loading
Yuya Nishihara <yuya@tcha.org>
parents:
36178
diff
changeset
|
239 _validatetables(ui, mod) |
27142
060f83d219b9
extensions: refuse to load extensions if minimum hg version not met
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27116
diff
changeset
|
240 |
5031
af0995261f02
extensions: don't get confused by aliasing between "foo" and "hgext.foo"
Bryan O'Sullivan <bos@serpentine.com>
parents:
4818
diff
changeset
|
241 _extensions[shortname] = mod |
5192
60acf1432ee0
Move cmdtable and reposetup handling out of extensions.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
5152
diff
changeset
|
242 _order.append(shortname) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
243 ui.log( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
244 b'extension', b' - invoking registered callbacks: %s\n', shortname |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
245 ) |
43238
101ae8bbfa02
cleanup: hgdemandimport.tracing accepts strings, not bytes
Augie Fackler <augie@google.com>
parents:
43106
diff
changeset
|
246 with util.timedcm('callbacks extension %s', shortname) as stats: |
38798
d58958676b3c
extensions: add detailed loading information
Martijn Pieters <mj@zopatista.com>
parents:
38727
diff
changeset
|
247 for fn in _aftercallbacks.get(shortname, []): |
d58958676b3c
extensions: add detailed loading information
Martijn Pieters <mj@zopatista.com>
parents:
38727
diff
changeset
|
248 fn(loaded=True) |
40996
6f2510b581a0
extensions: use ui.log() interface to provide detailed loading information
Yuya Nishihara <yuya@tcha.org>
parents:
40729
diff
changeset
|
249 ui.log(b'extension', b' > callbacks completed in %s\n', stats) |
12779
891ddf76b73e
extensions.load: return module
Erik Zielke <ez@aragost.com>
parents:
11521
diff
changeset
|
250 return mod |
4544
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
251 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
252 |
29461
7d88fde2309f
extensions: move uisetup and extsetup to standalone functions
Jun Wu <quark@fb.com>
parents:
29162
diff
changeset
|
253 def _runuisetup(name, ui): |
7d88fde2309f
extensions: move uisetup and extsetup to standalone functions
Jun Wu <quark@fb.com>
parents:
29162
diff
changeset
|
254 uisetup = getattr(_extensions[name], 'uisetup', None) |
7d88fde2309f
extensions: move uisetup and extsetup to standalone functions
Jun Wu <quark@fb.com>
parents:
29162
diff
changeset
|
255 if uisetup: |
32724
ea1c2eb7abd3
extensions: catch uisetup and extsetup failures and don't let them break hg
Augie Fackler <augie@google.com>
parents:
32722
diff
changeset
|
256 try: |
ea1c2eb7abd3
extensions: catch uisetup and extsetup failures and don't let them break hg
Augie Fackler <augie@google.com>
parents:
32722
diff
changeset
|
257 uisetup(ui) |
ea1c2eb7abd3
extensions: catch uisetup and extsetup failures and don't let them break hg
Augie Fackler <augie@google.com>
parents:
32722
diff
changeset
|
258 except Exception as inst: |
34845
78d9a7b7cdb6
extensions: always include traceback when extension setup fails
Martin von Zweigbergk <martinvonz@google.com>
parents:
34187
diff
changeset
|
259 ui.traceback(force=True) |
37084
f0b6fbea00cf
stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents:
36922
diff
changeset
|
260 msg = stringutil.forcebytestr(inst) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
261 ui.warn(_(b"*** failed to set up extension %s: %s\n") % (name, msg)) |
32724
ea1c2eb7abd3
extensions: catch uisetup and extsetup failures and don't let them break hg
Augie Fackler <augie@google.com>
parents:
32722
diff
changeset
|
262 return False |
ea1c2eb7abd3
extensions: catch uisetup and extsetup failures and don't let them break hg
Augie Fackler <augie@google.com>
parents:
32722
diff
changeset
|
263 return True |
29461
7d88fde2309f
extensions: move uisetup and extsetup to standalone functions
Jun Wu <quark@fb.com>
parents:
29162
diff
changeset
|
264 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
265 |
29461
7d88fde2309f
extensions: move uisetup and extsetup to standalone functions
Jun Wu <quark@fb.com>
parents:
29162
diff
changeset
|
266 def _runextsetup(name, ui): |
7d88fde2309f
extensions: move uisetup and extsetup to standalone functions
Jun Wu <quark@fb.com>
parents:
29162
diff
changeset
|
267 extsetup = getattr(_extensions[name], 'extsetup', None) |
7d88fde2309f
extensions: move uisetup and extsetup to standalone functions
Jun Wu <quark@fb.com>
parents:
29162
diff
changeset
|
268 if extsetup: |
7d88fde2309f
extensions: move uisetup and extsetup to standalone functions
Jun Wu <quark@fb.com>
parents:
29162
diff
changeset
|
269 try: |
42335
38b7b45627a2
extensions: drop support for extsetup() without `ui` argument (API)
Matt Harbison <matt_harbison@yahoo.com>
parents:
41068
diff
changeset
|
270 extsetup(ui) |
32724
ea1c2eb7abd3
extensions: catch uisetup and extsetup failures and don't let them break hg
Augie Fackler <augie@google.com>
parents:
32722
diff
changeset
|
271 except Exception as inst: |
34845
78d9a7b7cdb6
extensions: always include traceback when extension setup fails
Martin von Zweigbergk <martinvonz@google.com>
parents:
34187
diff
changeset
|
272 ui.traceback(force=True) |
37084
f0b6fbea00cf
stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents:
36922
diff
changeset
|
273 msg = stringutil.forcebytestr(inst) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
274 ui.warn(_(b"*** failed to set up extension %s: %s\n") % (name, msg)) |
32724
ea1c2eb7abd3
extensions: catch uisetup and extsetup failures and don't let them break hg
Augie Fackler <augie@google.com>
parents:
32722
diff
changeset
|
275 return False |
ea1c2eb7abd3
extensions: catch uisetup and extsetup failures and don't let them break hg
Augie Fackler <augie@google.com>
parents:
32722
diff
changeset
|
276 return True |
29461
7d88fde2309f
extensions: move uisetup and extsetup to standalone functions
Jun Wu <quark@fb.com>
parents:
29162
diff
changeset
|
277 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
278 |
32416
9a3e88d4a030
extensions: allow loading a whitelisted subset of extensions
Jun Wu <quark@fb.com>
parents:
32343
diff
changeset
|
279 def loadall(ui, whitelist=None): |
39511
1ab185c78cc3
extension: add a summary of total loading time per extension
Boris Feld <boris.feld@octobus.net>
parents:
39509
diff
changeset
|
280 loadingtime = collections.defaultdict(int) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
281 result = ui.configitems(b"extensions") |
32417
f40dc6f7c12f
profiling: allow loading profiling extension before everything else
Jun Wu <quark@fb.com>
parents:
32416
diff
changeset
|
282 if whitelist is not None: |
32416
9a3e88d4a030
extensions: allow loading a whitelisted subset of extensions
Jun Wu <quark@fb.com>
parents:
32343
diff
changeset
|
283 result = [(k, v) for (k, v) in result if k in whitelist] |
48358
c6d44457f7e3
extensions: ignore "sub-options" when looking for extensions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48016
diff
changeset
|
284 result = [(k, v) for (k, v) in result if b':' not in k] |
9410
1c83938b6a8e
extensions: load and configure extensions in well-defined phases
Martin Geisler <mg@lazybytes.net>
parents:
9136
diff
changeset
|
285 newindex = len(_order) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
286 ui.log( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
287 b'extension', |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
288 b'loading %sextensions\n', |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
289 b'additional ' if newindex else b'', |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
290 ) |
40996
6f2510b581a0
extensions: use ui.log() interface to provide detailed loading information
Yuya Nishihara <yuya@tcha.org>
parents:
40729
diff
changeset
|
291 ui.log(b'extension', b'- processing %d entries\n', len(result)) |
43238
101ae8bbfa02
cleanup: hgdemandimport.tracing accepts strings, not bytes
Augie Fackler <augie@google.com>
parents:
43106
diff
changeset
|
292 with util.timedcm('load all extensions') as stats: |
48362
7e6488aa1261
extensions: add a default "*" suboptions prefix
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48361
diff
changeset
|
293 default_sub_options = ui.configsuboptions(b"extensions", b"*")[1] |
7e6488aa1261
extensions: add a default "*" suboptions prefix
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48361
diff
changeset
|
294 |
38798
d58958676b3c
extensions: add detailed loading information
Martijn Pieters <mj@zopatista.com>
parents:
38727
diff
changeset
|
295 for (name, path) in result: |
6204
f8a86ea7521b
When failing to load an extension, show where Hg tried to load it from.
Jesse Glick <jesse.glick@sun.com>
parents:
5469
diff
changeset
|
296 if path: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
297 if path[0:1] == b'!': |
38798
d58958676b3c
extensions: add detailed loading information
Martijn Pieters <mj@zopatista.com>
parents:
38727
diff
changeset
|
298 if name not in _disabledextensions: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
299 ui.log( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
300 b'extension', |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
301 b' - skipping disabled extension: %s\n', |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
302 name, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
303 ) |
38798
d58958676b3c
extensions: add detailed loading information
Martijn Pieters <mj@zopatista.com>
parents:
38727
diff
changeset
|
304 _disabledextensions[name] = path[1:] |
d58958676b3c
extensions: add detailed loading information
Martijn Pieters <mj@zopatista.com>
parents:
38727
diff
changeset
|
305 continue |
d58958676b3c
extensions: add detailed loading information
Martijn Pieters <mj@zopatista.com>
parents:
38727
diff
changeset
|
306 try: |
40996
6f2510b581a0
extensions: use ui.log() interface to provide detailed loading information
Yuya Nishihara <yuya@tcha.org>
parents:
40729
diff
changeset
|
307 load(ui, name, path, loadingtime) |
38798
d58958676b3c
extensions: add detailed loading information
Martijn Pieters <mj@zopatista.com>
parents:
38727
diff
changeset
|
308 except Exception as inst: |
d58958676b3c
extensions: add detailed loading information
Martijn Pieters <mj@zopatista.com>
parents:
38727
diff
changeset
|
309 msg = stringutil.forcebytestr(inst) |
d58958676b3c
extensions: add detailed loading information
Martijn Pieters <mj@zopatista.com>
parents:
38727
diff
changeset
|
310 if path: |
48360
e4acdf5d94a2
extensions: highlight the name of the faulty extensions in the error message
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48359
diff
changeset
|
311 error_msg = _( |
e4acdf5d94a2
extensions: highlight the name of the faulty extensions in the error message
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48359
diff
changeset
|
312 b'failed to import extension "%s" from %s: %s' |
e4acdf5d94a2
extensions: highlight the name of the faulty extensions in the error message
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48359
diff
changeset
|
313 ) |
48359
e4e2ce328599
extensions: refactor handling of loading error make it reusable
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48358
diff
changeset
|
314 error_msg %= (name, path, msg) |
38798
d58958676b3c
extensions: add detailed loading information
Martijn Pieters <mj@zopatista.com>
parents:
38727
diff
changeset
|
315 else: |
48360
e4acdf5d94a2
extensions: highlight the name of the faulty extensions in the error message
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48359
diff
changeset
|
316 error_msg = _(b'failed to import extension "%s": %s') |
48359
e4e2ce328599
extensions: refactor handling of loading error make it reusable
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48358
diff
changeset
|
317 error_msg %= (name, msg) |
48361
0d0ce2529540
extension: add a `required` suboption to enforce the use of an extensions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48360
diff
changeset
|
318 |
48362
7e6488aa1261
extensions: add a default "*" suboptions prefix
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48361
diff
changeset
|
319 options = default_sub_options.copy() |
48361
0d0ce2529540
extension: add a `required` suboption to enforce the use of an extensions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48360
diff
changeset
|
320 ext_options = ui.configsuboptions(b"extensions", name)[1] |
48362
7e6488aa1261
extensions: add a default "*" suboptions prefix
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48361
diff
changeset
|
321 options.update(ext_options) |
7e6488aa1261
extensions: add a default "*" suboptions prefix
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48361
diff
changeset
|
322 if stringutil.parsebool(options.get(b"required", b'no')): |
48361
0d0ce2529540
extension: add a `required` suboption to enforce the use of an extensions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48360
diff
changeset
|
323 hint = None |
0d0ce2529540
extension: add a `required` suboption to enforce the use of an extensions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48360
diff
changeset
|
324 if isinstance(inst, error.Hint) and inst.hint: |
0d0ce2529540
extension: add a `required` suboption to enforce the use of an extensions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48360
diff
changeset
|
325 hint = inst.hint |
0d0ce2529540
extension: add a `required` suboption to enforce the use of an extensions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48360
diff
changeset
|
326 if hint is None: |
0d0ce2529540
extension: add a `required` suboption to enforce the use of an extensions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48360
diff
changeset
|
327 hint = _( |
0d0ce2529540
extension: add a `required` suboption to enforce the use of an extensions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48360
diff
changeset
|
328 b"loading of this extension was required, " |
0d0ce2529540
extension: add a `required` suboption to enforce the use of an extensions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48360
diff
changeset
|
329 b"see `hg help config.extensions` for details" |
0d0ce2529540
extension: add a `required` suboption to enforce the use of an extensions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48360
diff
changeset
|
330 ) |
0d0ce2529540
extension: add a `required` suboption to enforce the use of an extensions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48360
diff
changeset
|
331 raise error.Abort(error_msg, hint=hint) |
0d0ce2529540
extension: add a `required` suboption to enforce the use of an extensions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48360
diff
changeset
|
332 else: |
0d0ce2529540
extension: add a `required` suboption to enforce the use of an extensions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48360
diff
changeset
|
333 ui.warn((b"*** %s\n") % error_msg) |
0d0ce2529540
extension: add a `required` suboption to enforce the use of an extensions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48360
diff
changeset
|
334 if isinstance(inst, error.Hint) and inst.hint: |
0d0ce2529540
extension: add a `required` suboption to enforce the use of an extensions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48360
diff
changeset
|
335 ui.warn(_(b"*** (%s)\n") % inst.hint) |
0d0ce2529540
extension: add a `required` suboption to enforce the use of an extensions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
48360
diff
changeset
|
336 ui.traceback() |
38798
d58958676b3c
extensions: add detailed loading information
Martijn Pieters <mj@zopatista.com>
parents:
38727
diff
changeset
|
337 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
338 ui.log( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
339 b'extension', |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
340 b'> loaded %d extensions, total time %s\n', |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
341 len(_order) - newindex, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
342 stats, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
343 ) |
34187
4c5730c21523
extensions: register config item early
Boris Feld <boris.feld@octobus.net>
parents:
34186
diff
changeset
|
344 # list of (objname, loadermod, loadername) tuple: |
4c5730c21523
extensions: register config item early
Boris Feld <boris.feld@octobus.net>
parents:
34186
diff
changeset
|
345 # - objname is the name of an object in extension module, |
4c5730c21523
extensions: register config item early
Boris Feld <boris.feld@octobus.net>
parents:
34186
diff
changeset
|
346 # from which extra information is loaded |
4c5730c21523
extensions: register config item early
Boris Feld <boris.feld@octobus.net>
parents:
34186
diff
changeset
|
347 # - loadermod is the module where loader is placed |
4c5730c21523
extensions: register config item early
Boris Feld <boris.feld@octobus.net>
parents:
34186
diff
changeset
|
348 # - loadername is the name of the function, |
4c5730c21523
extensions: register config item early
Boris Feld <boris.feld@octobus.net>
parents:
34186
diff
changeset
|
349 # which takes (ui, extensionname, extraobj) arguments |
4c5730c21523
extensions: register config item early
Boris Feld <boris.feld@octobus.net>
parents:
34186
diff
changeset
|
350 # |
4c5730c21523
extensions: register config item early
Boris Feld <boris.feld@octobus.net>
parents:
34186
diff
changeset
|
351 # This one is for the list of item that must be run before running any setup |
4c5730c21523
extensions: register config item early
Boris Feld <boris.feld@octobus.net>
parents:
34186
diff
changeset
|
352 earlyextraloaders = [ |
50921
538c5a48e8f4
extension: access special module members using sysstr
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
50894
diff
changeset
|
353 ('configtable', configitems, 'loadconfigtable'), |
34187
4c5730c21523
extensions: register config item early
Boris Feld <boris.feld@octobus.net>
parents:
34186
diff
changeset
|
354 ] |
38798
d58958676b3c
extensions: add detailed loading information
Martijn Pieters <mj@zopatista.com>
parents:
38727
diff
changeset
|
355 |
40996
6f2510b581a0
extensions: use ui.log() interface to provide detailed loading information
Yuya Nishihara <yuya@tcha.org>
parents:
40729
diff
changeset
|
356 ui.log(b'extension', b'- loading configtable attributes\n') |
34187
4c5730c21523
extensions: register config item early
Boris Feld <boris.feld@octobus.net>
parents:
34186
diff
changeset
|
357 _loadextra(ui, newindex, earlyextraloaders) |
4544
930ed513c864
Create a separate module for managing extensions
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
358 |
32724
ea1c2eb7abd3
extensions: catch uisetup and extsetup failures and don't let them break hg
Augie Fackler <augie@google.com>
parents:
32722
diff
changeset
|
359 broken = set() |
40996
6f2510b581a0
extensions: use ui.log() interface to provide detailed loading information
Yuya Nishihara <yuya@tcha.org>
parents:
40729
diff
changeset
|
360 ui.log(b'extension', b'- executing uisetup hooks\n') |
43238
101ae8bbfa02
cleanup: hgdemandimport.tracing accepts strings, not bytes
Augie Fackler <augie@google.com>
parents:
43106
diff
changeset
|
361 with util.timedcm('all uisetup') as alluisetupstats: |
39508
1a2bfc4d756a
extensions: trace the total time of running all uisetup callbacks
Boris Feld <boris.feld@octobus.net>
parents:
39258
diff
changeset
|
362 for name in _order[newindex:]: |
40996
6f2510b581a0
extensions: use ui.log() interface to provide detailed loading information
Yuya Nishihara <yuya@tcha.org>
parents:
40729
diff
changeset
|
363 ui.log(b'extension', b' - running uisetup for %s\n', name) |
43238
101ae8bbfa02
cleanup: hgdemandimport.tracing accepts strings, not bytes
Augie Fackler <augie@google.com>
parents:
43106
diff
changeset
|
364 with util.timedcm('uisetup %s', name) as stats: |
39508
1a2bfc4d756a
extensions: trace the total time of running all uisetup callbacks
Boris Feld <boris.feld@octobus.net>
parents:
39258
diff
changeset
|
365 if not _runuisetup(name, ui): |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
366 ui.log( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
367 b'extension', |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
368 b' - the %s extension uisetup failed\n', |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
369 name, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
370 ) |
39508
1a2bfc4d756a
extensions: trace the total time of running all uisetup callbacks
Boris Feld <boris.feld@octobus.net>
parents:
39258
diff
changeset
|
371 broken.add(name) |
40996
6f2510b581a0
extensions: use ui.log() interface to provide detailed loading information
Yuya Nishihara <yuya@tcha.org>
parents:
40729
diff
changeset
|
372 ui.log(b'extension', b' > uisetup for %s took %s\n', name, stats) |
39511
1ab185c78cc3
extension: add a summary of total loading time per extension
Boris Feld <boris.feld@octobus.net>
parents:
39509
diff
changeset
|
373 loadingtime[name] += stats.elapsed |
40996
6f2510b581a0
extensions: use ui.log() interface to provide detailed loading information
Yuya Nishihara <yuya@tcha.org>
parents:
40729
diff
changeset
|
374 ui.log(b'extension', b'> all uisetup took %s\n', alluisetupstats) |
9410
1c83938b6a8e
extensions: load and configure extensions in well-defined phases
Martin Geisler <mg@lazybytes.net>
parents:
9136
diff
changeset
|
375 |
40996
6f2510b581a0
extensions: use ui.log() interface to provide detailed loading information
Yuya Nishihara <yuya@tcha.org>
parents:
40729
diff
changeset
|
376 ui.log(b'extension', b'- executing extsetup hooks\n') |
43238
101ae8bbfa02
cleanup: hgdemandimport.tracing accepts strings, not bytes
Augie Fackler <augie@google.com>
parents:
43106
diff
changeset
|
377 with util.timedcm('all extsetup') as allextetupstats: |
39509
3a86f7eb8b78
extensions: trace the total time of running all extsetup callbacks
Boris Feld <boris.feld@octobus.net>
parents:
39508
diff
changeset
|
378 for name in _order[newindex:]: |
3a86f7eb8b78
extensions: trace the total time of running all extsetup callbacks
Boris Feld <boris.feld@octobus.net>
parents:
39508
diff
changeset
|
379 if name in broken: |
3a86f7eb8b78
extensions: trace the total time of running all extsetup callbacks
Boris Feld <boris.feld@octobus.net>
parents:
39508
diff
changeset
|
380 continue |
40996
6f2510b581a0
extensions: use ui.log() interface to provide detailed loading information
Yuya Nishihara <yuya@tcha.org>
parents:
40729
diff
changeset
|
381 ui.log(b'extension', b' - running extsetup for %s\n', name) |
43238
101ae8bbfa02
cleanup: hgdemandimport.tracing accepts strings, not bytes
Augie Fackler <augie@google.com>
parents:
43106
diff
changeset
|
382 with util.timedcm('extsetup %s', name) as stats: |
39509
3a86f7eb8b78
extensions: trace the total time of running all extsetup callbacks
Boris Feld <boris.feld@octobus.net>
parents:
39508
diff
changeset
|
383 if not _runextsetup(name, ui): |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
384 ui.log( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
385 b'extension', |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
386 b' - the %s extension extsetup failed\n', |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
387 name, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
388 ) |
39509
3a86f7eb8b78
extensions: trace the total time of running all extsetup callbacks
Boris Feld <boris.feld@octobus.net>
parents:
39508
diff
changeset
|
389 broken.add(name) |
40996
6f2510b581a0
extensions: use ui.log() interface to provide detailed loading information
Yuya Nishihara <yuya@tcha.org>
parents:
40729
diff
changeset
|
390 ui.log(b'extension', b' > extsetup for %s took %s\n', name, stats) |
39511
1ab185c78cc3
extension: add a summary of total loading time per extension
Boris Feld <boris.feld@octobus.net>
parents:
39509
diff
changeset
|
391 loadingtime[name] += stats.elapsed |
40996
6f2510b581a0
extensions: use ui.log() interface to provide detailed loading information
Yuya Nishihara <yuya@tcha.org>
parents:
40729
diff
changeset
|
392 ui.log(b'extension', b'> all extsetup took %s\n', allextetupstats) |
32724
ea1c2eb7abd3
extensions: catch uisetup and extsetup failures and don't let them break hg
Augie Fackler <augie@google.com>
parents:
32722
diff
changeset
|
393 |
ea1c2eb7abd3
extensions: catch uisetup and extsetup failures and don't let them break hg
Augie Fackler <augie@google.com>
parents:
32722
diff
changeset
|
394 for name in broken: |
40996
6f2510b581a0
extensions: use ui.log() interface to provide detailed loading information
Yuya Nishihara <yuya@tcha.org>
parents:
40729
diff
changeset
|
395 ui.log(b'extension', b' - disabling broken %s extension\n', name) |
32724
ea1c2eb7abd3
extensions: catch uisetup and extsetup failures and don't let them break hg
Augie Fackler <augie@google.com>
parents:
32722
diff
changeset
|
396 _extensions[name] = None |
9660
e0eae93e6c67
extensions: changed to call extsetup() from extensions.loadall()
Yuya Nishihara <yuya@tcha.org>
parents:
9610
diff
changeset
|
397 |
24065
d8837ad682dd
extensions: support callbacks after another extension loads
Gregory Szorc <gregory.szorc@gmail.com>
parents:
23953
diff
changeset
|
398 # Call aftercallbacks that were never met. |
40996
6f2510b581a0
extensions: use ui.log() interface to provide detailed loading information
Yuya Nishihara <yuya@tcha.org>
parents:
40729
diff
changeset
|
399 ui.log(b'extension', b'- executing remaining aftercallbacks\n') |
43238
101ae8bbfa02
cleanup: hgdemandimport.tracing accepts strings, not bytes
Augie Fackler <augie@google.com>
parents:
43106
diff
changeset
|
400 with util.timedcm('aftercallbacks') as stats: |
38798
d58958676b3c
extensions: add detailed loading information
Martijn Pieters <mj@zopatista.com>
parents:
38727
diff
changeset
|
401 for shortname in _aftercallbacks: |
d58958676b3c
extensions: add detailed loading information
Martijn Pieters <mj@zopatista.com>
parents:
38727
diff
changeset
|
402 if shortname in _extensions: |
d58958676b3c
extensions: add detailed loading information
Martijn Pieters <mj@zopatista.com>
parents:
38727
diff
changeset
|
403 continue |
24065
d8837ad682dd
extensions: support callbacks after another extension loads
Gregory Szorc <gregory.szorc@gmail.com>
parents:
23953
diff
changeset
|
404 |
38798
d58958676b3c
extensions: add detailed loading information
Martijn Pieters <mj@zopatista.com>
parents:
38727
diff
changeset
|
405 for fn in _aftercallbacks[shortname]: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
406 ui.log( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
407 b'extension', |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
408 b' - extension %s not loaded, notify callbacks\n', |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
409 shortname, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
410 ) |
38798
d58958676b3c
extensions: add detailed loading information
Martijn Pieters <mj@zopatista.com>
parents:
38727
diff
changeset
|
411 fn(loaded=False) |
40996
6f2510b581a0
extensions: use ui.log() interface to provide detailed loading information
Yuya Nishihara <yuya@tcha.org>
parents:
40729
diff
changeset
|
412 ui.log(b'extension', b'> remaining aftercallbacks completed in %s\n', stats) |
24065
d8837ad682dd
extensions: support callbacks after another extension loads
Gregory Szorc <gregory.szorc@gmail.com>
parents:
23953
diff
changeset
|
413 |
24950
e6e7d1cce04d
extensions: clear aftercallbacks after execution (issue4646)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
24734
diff
changeset
|
414 # loadall() is called multiple times and lingering _aftercallbacks |
e6e7d1cce04d
extensions: clear aftercallbacks after execution (issue4646)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
24734
diff
changeset
|
415 # entries could result in double execution. See issue4646. |
e6e7d1cce04d
extensions: clear aftercallbacks after execution (issue4646)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
24734
diff
changeset
|
416 _aftercallbacks.clear() |
e6e7d1cce04d
extensions: clear aftercallbacks after execution (issue4646)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
24734
diff
changeset
|
417 |
33052
45b0e9d05ee9
extensions: register functions always at loading extension (issue5601)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33014
diff
changeset
|
418 # delay importing avoids cyclic dependency (especially commands) |
45b0e9d05ee9
extensions: register functions always at loading extension (issue5601)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33014
diff
changeset
|
419 from . import ( |
45b0e9d05ee9
extensions: register functions always at loading extension (issue5601)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33014
diff
changeset
|
420 color, |
45b0e9d05ee9
extensions: register functions always at loading extension (issue5601)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33014
diff
changeset
|
421 commands, |
33699
50c44dee741a
filemerge: move decorator definition for internal merge tools to registrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33682
diff
changeset
|
422 filemerge, |
33052
45b0e9d05ee9
extensions: register functions always at loading extension (issue5601)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33014
diff
changeset
|
423 fileset, |
45b0e9d05ee9
extensions: register functions always at loading extension (issue5601)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33014
diff
changeset
|
424 revset, |
45b0e9d05ee9
extensions: register functions always at loading extension (issue5601)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33014
diff
changeset
|
425 templatefilters, |
36922
521f6c7e1756
templater: split template functions to new module
Yuya Nishihara <yuya@tcha.org>
parents:
36269
diff
changeset
|
426 templatefuncs, |
33052
45b0e9d05ee9
extensions: register functions always at loading extension (issue5601)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33014
diff
changeset
|
427 templatekw, |
45b0e9d05ee9
extensions: register functions always at loading extension (issue5601)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33014
diff
changeset
|
428 ) |
45b0e9d05ee9
extensions: register functions always at loading extension (issue5601)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33014
diff
changeset
|
429 |
45b0e9d05ee9
extensions: register functions always at loading extension (issue5601)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33014
diff
changeset
|
430 # list of (objname, loadermod, loadername) tuple: |
45b0e9d05ee9
extensions: register functions always at loading extension (issue5601)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33014
diff
changeset
|
431 # - objname is the name of an object in extension module, |
45b0e9d05ee9
extensions: register functions always at loading extension (issue5601)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33014
diff
changeset
|
432 # from which extra information is loaded |
45b0e9d05ee9
extensions: register functions always at loading extension (issue5601)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33014
diff
changeset
|
433 # - loadermod is the module where loader is placed |
45b0e9d05ee9
extensions: register functions always at loading extension (issue5601)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33014
diff
changeset
|
434 # - loadername is the name of the function, |
45b0e9d05ee9
extensions: register functions always at loading extension (issue5601)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33014
diff
changeset
|
435 # which takes (ui, extensionname, extraobj) arguments |
40996
6f2510b581a0
extensions: use ui.log() interface to provide detailed loading information
Yuya Nishihara <yuya@tcha.org>
parents:
40729
diff
changeset
|
436 ui.log(b'extension', b'- loading extension registration objects\n') |
33052
45b0e9d05ee9
extensions: register functions always at loading extension (issue5601)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33014
diff
changeset
|
437 extraloaders = [ |
50921
538c5a48e8f4
extension: access special module members using sysstr
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
50894
diff
changeset
|
438 ('cmdtable', commands, 'loadcmdtable'), |
538c5a48e8f4
extension: access special module members using sysstr
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
50894
diff
changeset
|
439 ('colortable', color, 'loadcolortable'), |
538c5a48e8f4
extension: access special module members using sysstr
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
50894
diff
changeset
|
440 ('filesetpredicate', fileset, 'loadpredicate'), |
538c5a48e8f4
extension: access special module members using sysstr
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
50894
diff
changeset
|
441 ('internalmerge', filemerge, 'loadinternalmerge'), |
538c5a48e8f4
extension: access special module members using sysstr
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
50894
diff
changeset
|
442 ('revsetpredicate', revset, 'loadpredicate'), |
538c5a48e8f4
extension: access special module members using sysstr
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
50894
diff
changeset
|
443 ('templatefilter', templatefilters, 'loadfilter'), |
538c5a48e8f4
extension: access special module members using sysstr
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
50894
diff
changeset
|
444 ('templatefunc', templatefuncs, 'loadfunction'), |
538c5a48e8f4
extension: access special module members using sysstr
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
50894
diff
changeset
|
445 ('templatekeyword', templatekw, 'loadkeyword'), |
33052
45b0e9d05ee9
extensions: register functions always at loading extension (issue5601)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33014
diff
changeset
|
446 ] |
43238
101ae8bbfa02
cleanup: hgdemandimport.tracing accepts strings, not bytes
Augie Fackler <augie@google.com>
parents:
43106
diff
changeset
|
447 with util.timedcm('load registration objects') as stats: |
38798
d58958676b3c
extensions: add detailed loading information
Martijn Pieters <mj@zopatista.com>
parents:
38727
diff
changeset
|
448 _loadextra(ui, newindex, extraloaders) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
449 ui.log( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
450 b'extension', |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
451 b'> extension registration object loading took %s\n', |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
452 stats, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
453 ) |
39511
1ab185c78cc3
extension: add a summary of total loading time per extension
Boris Feld <boris.feld@octobus.net>
parents:
39509
diff
changeset
|
454 |
1ab185c78cc3
extension: add a summary of total loading time per extension
Boris Feld <boris.feld@octobus.net>
parents:
39509
diff
changeset
|
455 # Report per extension loading time (except reposetup) |
1ab185c78cc3
extension: add a summary of total loading time per extension
Boris Feld <boris.feld@octobus.net>
parents:
39509
diff
changeset
|
456 for name in sorted(loadingtime): |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
457 ui.log( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
458 b'extension', |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
459 b'> extension %s take a total of %s to load\n', |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
460 name, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
461 util.timecount(loadingtime[name]), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
462 ) |
39511
1ab185c78cc3
extension: add a summary of total loading time per extension
Boris Feld <boris.feld@octobus.net>
parents:
39509
diff
changeset
|
463 |
40996
6f2510b581a0
extensions: use ui.log() interface to provide detailed loading information
Yuya Nishihara <yuya@tcha.org>
parents:
40729
diff
changeset
|
464 ui.log(b'extension', b'extension loading complete\n') |
33052
45b0e9d05ee9
extensions: register functions always at loading extension (issue5601)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33014
diff
changeset
|
465 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
466 |
34186
f7c9c5d8c7f4
extensions: factor extra data loading out
Boris Feld <boris.feld@octobus.net>
parents:
34128
diff
changeset
|
467 def _loadextra(ui, newindex, extraloaders): |
33052
45b0e9d05ee9
extensions: register functions always at loading extension (issue5601)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33014
diff
changeset
|
468 for name in _order[newindex:]: |
45b0e9d05ee9
extensions: register functions always at loading extension (issue5601)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33014
diff
changeset
|
469 module = _extensions[name] |
45b0e9d05ee9
extensions: register functions always at loading extension (issue5601)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33014
diff
changeset
|
470 if not module: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
471 continue # loading this module failed |
33052
45b0e9d05ee9
extensions: register functions always at loading extension (issue5601)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33014
diff
changeset
|
472 |
45b0e9d05ee9
extensions: register functions always at loading extension (issue5601)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33014
diff
changeset
|
473 for objname, loadermod, loadername in extraloaders: |
45b0e9d05ee9
extensions: register functions always at loading extension (issue5601)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33014
diff
changeset
|
474 extraobj = getattr(module, objname, None) |
45b0e9d05ee9
extensions: register functions always at loading extension (issue5601)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33014
diff
changeset
|
475 if extraobj is not None: |
45b0e9d05ee9
extensions: register functions always at loading extension (issue5601)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33014
diff
changeset
|
476 getattr(loadermod, loadername)(ui, name, extraobj) |
45b0e9d05ee9
extensions: register functions always at loading extension (issue5601)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33014
diff
changeset
|
477 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
478 |
24065
d8837ad682dd
extensions: support callbacks after another extension loads
Gregory Szorc <gregory.szorc@gmail.com>
parents:
23953
diff
changeset
|
479 def afterloaded(extension, callback): |
45942
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
45899
diff
changeset
|
480 """Run the specified function after a named extension is loaded. |
24065
d8837ad682dd
extensions: support callbacks after another extension loads
Gregory Szorc <gregory.szorc@gmail.com>
parents:
23953
diff
changeset
|
481 |
d8837ad682dd
extensions: support callbacks after another extension loads
Gregory Szorc <gregory.szorc@gmail.com>
parents:
23953
diff
changeset
|
482 If the named extension is already loaded, the callback will be called |
d8837ad682dd
extensions: support callbacks after another extension loads
Gregory Szorc <gregory.szorc@gmail.com>
parents:
23953
diff
changeset
|
483 immediately. |
d8837ad682dd
extensions: support callbacks after another extension loads
Gregory Szorc <gregory.szorc@gmail.com>
parents:
23953
diff
changeset
|
484 |
d8837ad682dd
extensions: support callbacks after another extension loads
Gregory Szorc <gregory.szorc@gmail.com>
parents:
23953
diff
changeset
|
485 If the named extension never loads, the callback will be called after |
d8837ad682dd
extensions: support callbacks after another extension loads
Gregory Szorc <gregory.szorc@gmail.com>
parents:
23953
diff
changeset
|
486 all extensions have been loaded. |
d8837ad682dd
extensions: support callbacks after another extension loads
Gregory Szorc <gregory.szorc@gmail.com>
parents:
23953
diff
changeset
|
487 |
d8837ad682dd
extensions: support callbacks after another extension loads
Gregory Szorc <gregory.szorc@gmail.com>
parents:
23953
diff
changeset
|
488 The callback receives the named argument ``loaded``, which is a boolean |
d8837ad682dd
extensions: support callbacks after another extension loads
Gregory Szorc <gregory.szorc@gmail.com>
parents:
23953
diff
changeset
|
489 indicating whether the dependent extension actually loaded. |
45942
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
45899
diff
changeset
|
490 """ |
24065
d8837ad682dd
extensions: support callbacks after another extension loads
Gregory Szorc <gregory.szorc@gmail.com>
parents:
23953
diff
changeset
|
491 |
d8837ad682dd
extensions: support callbacks after another extension loads
Gregory Szorc <gregory.szorc@gmail.com>
parents:
23953
diff
changeset
|
492 if extension in _extensions: |
33014
80a5d237a4ae
extensions: call afterloaded() with loaded=False for disabled extensions
Adam Simpkins <simpkins@fb.com>
parents:
32724
diff
changeset
|
493 # Report loaded as False if the extension is disabled |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
494 loaded = _extensions[extension] is not None |
33014
80a5d237a4ae
extensions: call afterloaded() with loaded=False for disabled extensions
Adam Simpkins <simpkins@fb.com>
parents:
32724
diff
changeset
|
495 callback(loaded=loaded) |
24065
d8837ad682dd
extensions: support callbacks after another extension loads
Gregory Szorc <gregory.szorc@gmail.com>
parents:
23953
diff
changeset
|
496 else: |
d8837ad682dd
extensions: support callbacks after another extension loads
Gregory Szorc <gregory.szorc@gmail.com>
parents:
23953
diff
changeset
|
497 _aftercallbacks.setdefault(extension, []).append(callback) |
d8837ad682dd
extensions: support callbacks after another extension loads
Gregory Szorc <gregory.szorc@gmail.com>
parents:
23953
diff
changeset
|
498 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
499 |
40729
c93d046d4300
extensions: add "uipopulate" hook, called per instance, not per process
Yuya Nishihara <yuya@tcha.org>
parents:
40463
diff
changeset
|
500 def populateui(ui): |
c93d046d4300
extensions: add "uipopulate" hook, called per instance, not per process
Yuya Nishihara <yuya@tcha.org>
parents:
40463
diff
changeset
|
501 """Run extension hooks on the given ui to populate additional members, |
c93d046d4300
extensions: add "uipopulate" hook, called per instance, not per process
Yuya Nishihara <yuya@tcha.org>
parents:
40463
diff
changeset
|
502 extend the class dynamically, etc. |
c93d046d4300
extensions: add "uipopulate" hook, called per instance, not per process
Yuya Nishihara <yuya@tcha.org>
parents:
40463
diff
changeset
|
503 |
c93d046d4300
extensions: add "uipopulate" hook, called per instance, not per process
Yuya Nishihara <yuya@tcha.org>
parents:
40463
diff
changeset
|
504 This will be called after the configuration is loaded, and/or extensions |
c93d046d4300
extensions: add "uipopulate" hook, called per instance, not per process
Yuya Nishihara <yuya@tcha.org>
parents:
40463
diff
changeset
|
505 are loaded. In general, it's once per ui instance, but in command-server |
c93d046d4300
extensions: add "uipopulate" hook, called per instance, not per process
Yuya Nishihara <yuya@tcha.org>
parents:
40463
diff
changeset
|
506 and hgweb, this may be called more than once with the same ui. |
c93d046d4300
extensions: add "uipopulate" hook, called per instance, not per process
Yuya Nishihara <yuya@tcha.org>
parents:
40463
diff
changeset
|
507 """ |
c93d046d4300
extensions: add "uipopulate" hook, called per instance, not per process
Yuya Nishihara <yuya@tcha.org>
parents:
40463
diff
changeset
|
508 for name, mod in extensions(ui): |
c93d046d4300
extensions: add "uipopulate" hook, called per instance, not per process
Yuya Nishihara <yuya@tcha.org>
parents:
40463
diff
changeset
|
509 hook = getattr(mod, 'uipopulate', None) |
c93d046d4300
extensions: add "uipopulate" hook, called per instance, not per process
Yuya Nishihara <yuya@tcha.org>
parents:
40463
diff
changeset
|
510 if not hook: |
c93d046d4300
extensions: add "uipopulate" hook, called per instance, not per process
Yuya Nishihara <yuya@tcha.org>
parents:
40463
diff
changeset
|
511 continue |
c93d046d4300
extensions: add "uipopulate" hook, called per instance, not per process
Yuya Nishihara <yuya@tcha.org>
parents:
40463
diff
changeset
|
512 try: |
c93d046d4300
extensions: add "uipopulate" hook, called per instance, not per process
Yuya Nishihara <yuya@tcha.org>
parents:
40463
diff
changeset
|
513 hook(ui) |
c93d046d4300
extensions: add "uipopulate" hook, called per instance, not per process
Yuya Nishihara <yuya@tcha.org>
parents:
40463
diff
changeset
|
514 except Exception as inst: |
c93d046d4300
extensions: add "uipopulate" hook, called per instance, not per process
Yuya Nishihara <yuya@tcha.org>
parents:
40463
diff
changeset
|
515 ui.traceback(force=True) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
516 ui.warn( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
517 _(b'*** failed to populate ui by extension %s: %s\n') |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
518 % (name, stringutil.forcebytestr(inst)) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
519 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
520 |
40729
c93d046d4300
extensions: add "uipopulate" hook, called per instance, not per process
Yuya Nishihara <yuya@tcha.org>
parents:
40463
diff
changeset
|
521 |
24734
fb6cb1b82f4f
extensions: extract partial application into a bind() function
Eric Sumner <ericsumner@fb.com>
parents:
24145
diff
changeset
|
522 def bind(func, *args): |
45942
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
45899
diff
changeset
|
523 """Partial function application |
24734
fb6cb1b82f4f
extensions: extract partial application into a bind() function
Eric Sumner <ericsumner@fb.com>
parents:
24145
diff
changeset
|
524 |
45942
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
45899
diff
changeset
|
525 Returns a new function that is the partial application of args and kwargs |
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
45899
diff
changeset
|
526 to func. For example, |
24734
fb6cb1b82f4f
extensions: extract partial application into a bind() function
Eric Sumner <ericsumner@fb.com>
parents:
24145
diff
changeset
|
527 |
45942
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
45899
diff
changeset
|
528 f(1, 2, bar=3) === bind(f, 1)(2, bar=3)""" |
24734
fb6cb1b82f4f
extensions: extract partial application into a bind() function
Eric Sumner <ericsumner@fb.com>
parents:
24145
diff
changeset
|
529 assert callable(func) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
530 |
24734
fb6cb1b82f4f
extensions: extract partial application into a bind() function
Eric Sumner <ericsumner@fb.com>
parents:
24145
diff
changeset
|
531 def closure(*a, **kw): |
fb6cb1b82f4f
extensions: extract partial application into a bind() function
Eric Sumner <ericsumner@fb.com>
parents:
24145
diff
changeset
|
532 return func(*(args + a), **kw) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
533 |
24734
fb6cb1b82f4f
extensions: extract partial application into a bind() function
Eric Sumner <ericsumner@fb.com>
parents:
24145
diff
changeset
|
534 return closure |
fb6cb1b82f4f
extensions: extract partial application into a bind() function
Eric Sumner <ericsumner@fb.com>
parents:
24145
diff
changeset
|
535 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
536 |
29763
ce6317dcb944
extensions: set attributes to wrappers so we can trace them back
Jun Wu <quark@fb.com>
parents:
29461
diff
changeset
|
537 def _updatewrapper(wrap, origfn, unboundwrapper): |
ce6317dcb944
extensions: set attributes to wrappers so we can trace them back
Jun Wu <quark@fb.com>
parents:
29461
diff
changeset
|
538 '''Copy and add some useful attributes to wrapper''' |
34128
82bd4c5a81e5
extensions: fix wrapcommand/function of class instance
Yuya Nishihara <yuya@tcha.org>
parents:
34088
diff
changeset
|
539 try: |
82bd4c5a81e5
extensions: fix wrapcommand/function of class instance
Yuya Nishihara <yuya@tcha.org>
parents:
34088
diff
changeset
|
540 wrap.__name__ = origfn.__name__ |
82bd4c5a81e5
extensions: fix wrapcommand/function of class instance
Yuya Nishihara <yuya@tcha.org>
parents:
34088
diff
changeset
|
541 except AttributeError: |
82bd4c5a81e5
extensions: fix wrapcommand/function of class instance
Yuya Nishihara <yuya@tcha.org>
parents:
34088
diff
changeset
|
542 pass |
28310
01dc11e7191f
extensions: extract function that copies function attributes to wrapper
Yuya Nishihara <yuya@tcha.org>
parents:
28155
diff
changeset
|
543 wrap.__module__ = getattr(origfn, '__module__') |
01dc11e7191f
extensions: extract function that copies function attributes to wrapper
Yuya Nishihara <yuya@tcha.org>
parents:
28155
diff
changeset
|
544 wrap.__doc__ = getattr(origfn, '__doc__') |
28312
24f1d3c70c41
extensions: copy extra __dict__ of original function
Yuya Nishihara <yuya@tcha.org>
parents:
28311
diff
changeset
|
545 wrap.__dict__.update(getattr(origfn, '__dict__', {})) |
29763
ce6317dcb944
extensions: set attributes to wrappers so we can trace them back
Jun Wu <quark@fb.com>
parents:
29461
diff
changeset
|
546 wrap._origfunc = origfn |
ce6317dcb944
extensions: set attributes to wrappers so we can trace them back
Jun Wu <quark@fb.com>
parents:
29461
diff
changeset
|
547 wrap._unboundwrapper = unboundwrapper |
28310
01dc11e7191f
extensions: extract function that copies function attributes to wrapper
Yuya Nishihara <yuya@tcha.org>
parents:
28155
diff
changeset
|
548 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
549 |
24124
042d95beeee8
extensions: allow extending command synopsis and docstring
Ryan McElroy <rm@fb.com>
parents:
24065
diff
changeset
|
550 def wrapcommand(table, command, wrapper, synopsis=None, docstring=None): |
11519
bbdf1fb1d3e3
extensions: add docstring for wrapcommand().
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
11402
diff
changeset
|
551 '''Wrap the command named `command' in table |
bbdf1fb1d3e3
extensions: add docstring for wrapcommand().
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
11402
diff
changeset
|
552 |
bbdf1fb1d3e3
extensions: add docstring for wrapcommand().
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
11402
diff
changeset
|
553 Replace command in the command table with wrapper. The wrapped command will |
bbdf1fb1d3e3
extensions: add docstring for wrapcommand().
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
11402
diff
changeset
|
554 be inserted into the command table specified by the table argument. |
bbdf1fb1d3e3
extensions: add docstring for wrapcommand().
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
11402
diff
changeset
|
555 |
bbdf1fb1d3e3
extensions: add docstring for wrapcommand().
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
11402
diff
changeset
|
556 The wrapper will be called like |
bbdf1fb1d3e3
extensions: add docstring for wrapcommand().
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
11402
diff
changeset
|
557 |
bbdf1fb1d3e3
extensions: add docstring for wrapcommand().
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
11402
diff
changeset
|
558 wrapper(orig, *args, **kwargs) |
bbdf1fb1d3e3
extensions: add docstring for wrapcommand().
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
11402
diff
changeset
|
559 |
bbdf1fb1d3e3
extensions: add docstring for wrapcommand().
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
11402
diff
changeset
|
560 where orig is the original (wrapped) function, and *args, **kwargs |
bbdf1fb1d3e3
extensions: add docstring for wrapcommand().
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
11402
diff
changeset
|
561 are the arguments passed to it. |
24124
042d95beeee8
extensions: allow extending command synopsis and docstring
Ryan McElroy <rm@fb.com>
parents:
24065
diff
changeset
|
562 |
042d95beeee8
extensions: allow extending command synopsis and docstring
Ryan McElroy <rm@fb.com>
parents:
24065
diff
changeset
|
563 Optionally append to the command synopsis and docstring, used for help. |
042d95beeee8
extensions: allow extending command synopsis and docstring
Ryan McElroy <rm@fb.com>
parents:
24065
diff
changeset
|
564 For example, if your extension wraps the ``bookmarks`` command to add the |
042d95beeee8
extensions: allow extending command synopsis and docstring
Ryan McElroy <rm@fb.com>
parents:
24065
diff
changeset
|
565 flags ``--remote`` and ``--all`` you might call this function like so: |
042d95beeee8
extensions: allow extending command synopsis and docstring
Ryan McElroy <rm@fb.com>
parents:
24065
diff
changeset
|
566 |
042d95beeee8
extensions: allow extending command synopsis and docstring
Ryan McElroy <rm@fb.com>
parents:
24065
diff
changeset
|
567 synopsis = ' [-a] [--remote]' |
042d95beeee8
extensions: allow extending command synopsis and docstring
Ryan McElroy <rm@fb.com>
parents:
24065
diff
changeset
|
568 docstring = """ |
042d95beeee8
extensions: allow extending command synopsis and docstring
Ryan McElroy <rm@fb.com>
parents:
24065
diff
changeset
|
569 |
042d95beeee8
extensions: allow extending command synopsis and docstring
Ryan McElroy <rm@fb.com>
parents:
24065
diff
changeset
|
570 The ``remotenames`` extension adds the ``--remote`` and ``--all`` (``-a``) |
042d95beeee8
extensions: allow extending command synopsis and docstring
Ryan McElroy <rm@fb.com>
parents:
24065
diff
changeset
|
571 flags to the bookmarks command. Either flag will show the remote bookmarks |
26781
1aee2ab0f902
spelling: trivial spell checking
Mads Kiilerich <madski@unity3d.com>
parents:
25946
diff
changeset
|
572 known to the repository; ``--remote`` will also suppress the output of the |
24124
042d95beeee8
extensions: allow extending command synopsis and docstring
Ryan McElroy <rm@fb.com>
parents:
24065
diff
changeset
|
573 local bookmarks. |
042d95beeee8
extensions: allow extending command synopsis and docstring
Ryan McElroy <rm@fb.com>
parents:
24065
diff
changeset
|
574 """ |
042d95beeee8
extensions: allow extending command synopsis and docstring
Ryan McElroy <rm@fb.com>
parents:
24065
diff
changeset
|
575 |
042d95beeee8
extensions: allow extending command synopsis and docstring
Ryan McElroy <rm@fb.com>
parents:
24065
diff
changeset
|
576 extensions.wrapcommand(commands.table, 'bookmarks', exbookmarks, |
042d95beeee8
extensions: allow extending command synopsis and docstring
Ryan McElroy <rm@fb.com>
parents:
24065
diff
changeset
|
577 synopsis, docstring) |
11519
bbdf1fb1d3e3
extensions: add docstring for wrapcommand().
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
11402
diff
changeset
|
578 ''' |
21795
711498bb4ff5
extensions: restore use of callable() since it was readded in Python 3.2
Augie Fackler <raf@durin42.com>
parents:
21229
diff
changeset
|
579 assert callable(wrapper) |
7215
0ab5f21c390b
extensions: add wrapping functions
Matt Mackall <mpm@selenic.com>
parents:
7011
diff
changeset
|
580 aliases, entry = cmdutil.findcmd(command, table) |
48913
f254fc73d956
global: bulk replace simple pycompat.iteritems(x) with x.items()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48894
diff
changeset
|
581 for alias, e in table.items(): |
7215
0ab5f21c390b
extensions: add wrapping functions
Matt Mackall <mpm@selenic.com>
parents:
7011
diff
changeset
|
582 if e is entry: |
0ab5f21c390b
extensions: add wrapping functions
Matt Mackall <mpm@selenic.com>
parents:
7011
diff
changeset
|
583 key = alias |
0ab5f21c390b
extensions: add wrapping functions
Matt Mackall <mpm@selenic.com>
parents:
7011
diff
changeset
|
584 break |
0ab5f21c390b
extensions: add wrapping functions
Matt Mackall <mpm@selenic.com>
parents:
7011
diff
changeset
|
585 |
0ab5f21c390b
extensions: add wrapping functions
Matt Mackall <mpm@selenic.com>
parents:
7011
diff
changeset
|
586 origfn = entry[0] |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
587 wrap = functools.partial( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
588 util.checksignature(wrapper), util.checksignature(origfn) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
589 ) |
29763
ce6317dcb944
extensions: set attributes to wrappers so we can trace them back
Jun Wu <quark@fb.com>
parents:
29461
diff
changeset
|
590 _updatewrapper(wrap, origfn, wrapper) |
24124
042d95beeee8
extensions: allow extending command synopsis and docstring
Ryan McElroy <rm@fb.com>
parents:
24065
diff
changeset
|
591 if docstring is not None: |
28310
01dc11e7191f
extensions: extract function that copies function attributes to wrapper
Yuya Nishihara <yuya@tcha.org>
parents:
28155
diff
changeset
|
592 wrap.__doc__ += docstring |
24124
042d95beeee8
extensions: allow extending command synopsis and docstring
Ryan McElroy <rm@fb.com>
parents:
24065
diff
changeset
|
593 |
7215
0ab5f21c390b
extensions: add wrapping functions
Matt Mackall <mpm@selenic.com>
parents:
7011
diff
changeset
|
594 newentry = list(entry) |
0ab5f21c390b
extensions: add wrapping functions
Matt Mackall <mpm@selenic.com>
parents:
7011
diff
changeset
|
595 newentry[0] = wrap |
24124
042d95beeee8
extensions: allow extending command synopsis and docstring
Ryan McElroy <rm@fb.com>
parents:
24065
diff
changeset
|
596 if synopsis is not None: |
042d95beeee8
extensions: allow extending command synopsis and docstring
Ryan McElroy <rm@fb.com>
parents:
24065
diff
changeset
|
597 newentry[2] += synopsis |
7215
0ab5f21c390b
extensions: add wrapping functions
Matt Mackall <mpm@selenic.com>
parents:
7011
diff
changeset
|
598 table[key] = tuple(newentry) |
0ab5f21c390b
extensions: add wrapping functions
Matt Mackall <mpm@selenic.com>
parents:
7011
diff
changeset
|
599 return entry |
0ab5f21c390b
extensions: add wrapping functions
Matt Mackall <mpm@selenic.com>
parents:
7011
diff
changeset
|
600 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
601 |
32722
de09138bf0f5
extensions: move wrapfilecache function from fsmonitor
Augie Fackler <augie@google.com>
parents:
32417
diff
changeset
|
602 def wrapfilecache(cls, propname, wrapper): |
de09138bf0f5
extensions: move wrapfilecache function from fsmonitor
Augie Fackler <augie@google.com>
parents:
32417
diff
changeset
|
603 """Wraps a filecache property. |
de09138bf0f5
extensions: move wrapfilecache function from fsmonitor
Augie Fackler <augie@google.com>
parents:
32417
diff
changeset
|
604 |
de09138bf0f5
extensions: move wrapfilecache function from fsmonitor
Augie Fackler <augie@google.com>
parents:
32417
diff
changeset
|
605 These can't be wrapped using the normal wrapfunction. |
de09138bf0f5
extensions: move wrapfilecache function from fsmonitor
Augie Fackler <augie@google.com>
parents:
32417
diff
changeset
|
606 """ |
33836
38a3767975a7
extensions: if on py3 and propname is a bytestr, convert to sysstr
Augie Fackler <augie@google.com>
parents:
33722
diff
changeset
|
607 propname = pycompat.sysstr(propname) |
32722
de09138bf0f5
extensions: move wrapfilecache function from fsmonitor
Augie Fackler <augie@google.com>
parents:
32417
diff
changeset
|
608 assert callable(wrapper) |
de09138bf0f5
extensions: move wrapfilecache function from fsmonitor
Augie Fackler <augie@google.com>
parents:
32417
diff
changeset
|
609 for currcls in cls.__mro__: |
de09138bf0f5
extensions: move wrapfilecache function from fsmonitor
Augie Fackler <augie@google.com>
parents:
32417
diff
changeset
|
610 if propname in currcls.__dict__: |
de09138bf0f5
extensions: move wrapfilecache function from fsmonitor
Augie Fackler <augie@google.com>
parents:
32417
diff
changeset
|
611 origfn = currcls.__dict__[propname].func |
de09138bf0f5
extensions: move wrapfilecache function from fsmonitor
Augie Fackler <augie@google.com>
parents:
32417
diff
changeset
|
612 assert callable(origfn) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
613 |
32722
de09138bf0f5
extensions: move wrapfilecache function from fsmonitor
Augie Fackler <augie@google.com>
parents:
32417
diff
changeset
|
614 def wrap(*args, **kwargs): |
de09138bf0f5
extensions: move wrapfilecache function from fsmonitor
Augie Fackler <augie@google.com>
parents:
32417
diff
changeset
|
615 return wrapper(origfn, *args, **kwargs) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
616 |
32722
de09138bf0f5
extensions: move wrapfilecache function from fsmonitor
Augie Fackler <augie@google.com>
parents:
32417
diff
changeset
|
617 currcls.__dict__[propname].func = wrap |
de09138bf0f5
extensions: move wrapfilecache function from fsmonitor
Augie Fackler <augie@google.com>
parents:
32417
diff
changeset
|
618 break |
de09138bf0f5
extensions: move wrapfilecache function from fsmonitor
Augie Fackler <augie@google.com>
parents:
32417
diff
changeset
|
619 |
de09138bf0f5
extensions: move wrapfilecache function from fsmonitor
Augie Fackler <augie@google.com>
parents:
32417
diff
changeset
|
620 if currcls is object: |
43503
313e3a279828
cleanup: remove pointless r-prefixes on double-quoted strings
Augie Fackler <augie@google.com>
parents:
43238
diff
changeset
|
621 raise AttributeError("type '%s' has no property '%s'" % (cls, propname)) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
622 |
32722
de09138bf0f5
extensions: move wrapfilecache function from fsmonitor
Augie Fackler <augie@google.com>
parents:
32417
diff
changeset
|
623 |
48946
642e31cb55f0
py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48913
diff
changeset
|
624 class wrappedfunction: |
34014
47e52f079a57
extensions: add wrappedfunction() context manager
Martin von Zweigbergk <martinvonz@google.com>
parents:
33837
diff
changeset
|
625 '''context manager for temporarily wrapping a function''' |
47e52f079a57
extensions: add wrappedfunction() context manager
Martin von Zweigbergk <martinvonz@google.com>
parents:
33837
diff
changeset
|
626 |
47e52f079a57
extensions: add wrappedfunction() context manager
Martin von Zweigbergk <martinvonz@google.com>
parents:
33837
diff
changeset
|
627 def __init__(self, container, funcname, wrapper): |
47e52f079a57
extensions: add wrappedfunction() context manager
Martin von Zweigbergk <martinvonz@google.com>
parents:
33837
diff
changeset
|
628 assert callable(wrapper) |
50800
94506fc107b7
wrapfunction: deprecates calling `wrappedfunction` with bytes
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
49781
diff
changeset
|
629 if not isinstance(funcname, str): |
94506fc107b7
wrapfunction: deprecates calling `wrappedfunction` with bytes
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
49781
diff
changeset
|
630 msg = b"pass wrappedfunction target name as `str`, not `bytes`" |
94506fc107b7
wrapfunction: deprecates calling `wrappedfunction` with bytes
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
49781
diff
changeset
|
631 util.nouideprecwarn(msg, b"6.6", stacklevel=2) |
94506fc107b7
wrapfunction: deprecates calling `wrappedfunction` with bytes
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
49781
diff
changeset
|
632 funcname = pycompat.sysstr(funcname) |
34014
47e52f079a57
extensions: add wrappedfunction() context manager
Martin von Zweigbergk <martinvonz@google.com>
parents:
33837
diff
changeset
|
633 self._container = container |
47e52f079a57
extensions: add wrappedfunction() context manager
Martin von Zweigbergk <martinvonz@google.com>
parents:
33837
diff
changeset
|
634 self._funcname = funcname |
47e52f079a57
extensions: add wrappedfunction() context manager
Martin von Zweigbergk <martinvonz@google.com>
parents:
33837
diff
changeset
|
635 self._wrapper = wrapper |
47e52f079a57
extensions: add wrappedfunction() context manager
Martin von Zweigbergk <martinvonz@google.com>
parents:
33837
diff
changeset
|
636 |
47e52f079a57
extensions: add wrappedfunction() context manager
Martin von Zweigbergk <martinvonz@google.com>
parents:
33837
diff
changeset
|
637 def __enter__(self): |
47e52f079a57
extensions: add wrappedfunction() context manager
Martin von Zweigbergk <martinvonz@google.com>
parents:
33837
diff
changeset
|
638 wrapfunction(self._container, self._funcname, self._wrapper) |
47e52f079a57
extensions: add wrappedfunction() context manager
Martin von Zweigbergk <martinvonz@google.com>
parents:
33837
diff
changeset
|
639 |
47e52f079a57
extensions: add wrappedfunction() context manager
Martin von Zweigbergk <martinvonz@google.com>
parents:
33837
diff
changeset
|
640 def __exit__(self, exctype, excvalue, traceback): |
47e52f079a57
extensions: add wrappedfunction() context manager
Martin von Zweigbergk <martinvonz@google.com>
parents:
33837
diff
changeset
|
641 unwrapfunction(self._container, self._funcname, self._wrapper) |
47e52f079a57
extensions: add wrappedfunction() context manager
Martin von Zweigbergk <martinvonz@google.com>
parents:
33837
diff
changeset
|
642 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
643 |
7215
0ab5f21c390b
extensions: add wrapping functions
Matt Mackall <mpm@selenic.com>
parents:
7011
diff
changeset
|
644 def wrapfunction(container, funcname, wrapper): |
45942
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
45899
diff
changeset
|
645 """Wrap the function named funcname in container |
11402
367ce8514da0
extensions: recommend against using wrapfunction for repo methods
Greg Ward <greg-hg@gerg.ca>
parents:
10364
diff
changeset
|
646 |
11520
94b3bbc886cf
extensions: improve language for wrapfunction() docstring.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
11519
diff
changeset
|
647 Replace the funcname member in the given container with the specified |
94b3bbc886cf
extensions: improve language for wrapfunction() docstring.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
11519
diff
changeset
|
648 wrapper. The container is typically a module, class, or instance. |
11402
367ce8514da0
extensions: recommend against using wrapfunction for repo methods
Greg Ward <greg-hg@gerg.ca>
parents:
10364
diff
changeset
|
649 |
367ce8514da0
extensions: recommend against using wrapfunction for repo methods
Greg Ward <greg-hg@gerg.ca>
parents:
10364
diff
changeset
|
650 The wrapper will be called like |
367ce8514da0
extensions: recommend against using wrapfunction for repo methods
Greg Ward <greg-hg@gerg.ca>
parents:
10364
diff
changeset
|
651 |
367ce8514da0
extensions: recommend against using wrapfunction for repo methods
Greg Ward <greg-hg@gerg.ca>
parents:
10364
diff
changeset
|
652 wrapper(orig, *args, **kwargs) |
367ce8514da0
extensions: recommend against using wrapfunction for repo methods
Greg Ward <greg-hg@gerg.ca>
parents:
10364
diff
changeset
|
653 |
367ce8514da0
extensions: recommend against using wrapfunction for repo methods
Greg Ward <greg-hg@gerg.ca>
parents:
10364
diff
changeset
|
654 where orig is the original (wrapped) function, and *args, **kwargs |
367ce8514da0
extensions: recommend against using wrapfunction for repo methods
Greg Ward <greg-hg@gerg.ca>
parents:
10364
diff
changeset
|
655 are the arguments passed to it. |
367ce8514da0
extensions: recommend against using wrapfunction for repo methods
Greg Ward <greg-hg@gerg.ca>
parents:
10364
diff
changeset
|
656 |
367ce8514da0
extensions: recommend against using wrapfunction for repo methods
Greg Ward <greg-hg@gerg.ca>
parents:
10364
diff
changeset
|
657 Wrapping methods of the repository object is not recommended since |
367ce8514da0
extensions: recommend against using wrapfunction for repo methods
Greg Ward <greg-hg@gerg.ca>
parents:
10364
diff
changeset
|
658 it conflicts with extensions that extend the repository by |
367ce8514da0
extensions: recommend against using wrapfunction for repo methods
Greg Ward <greg-hg@gerg.ca>
parents:
10364
diff
changeset
|
659 subclassing. All extensions that need to extend methods of |
367ce8514da0
extensions: recommend against using wrapfunction for repo methods
Greg Ward <greg-hg@gerg.ca>
parents:
10364
diff
changeset
|
660 localrepository should use this subclassing trick: namely, |
367ce8514da0
extensions: recommend against using wrapfunction for repo methods
Greg Ward <greg-hg@gerg.ca>
parents:
10364
diff
changeset
|
661 reposetup() should look like |
367ce8514da0
extensions: recommend against using wrapfunction for repo methods
Greg Ward <greg-hg@gerg.ca>
parents:
10364
diff
changeset
|
662 |
367ce8514da0
extensions: recommend against using wrapfunction for repo methods
Greg Ward <greg-hg@gerg.ca>
parents:
10364
diff
changeset
|
663 def reposetup(ui, repo): |
367ce8514da0
extensions: recommend against using wrapfunction for repo methods
Greg Ward <greg-hg@gerg.ca>
parents:
10364
diff
changeset
|
664 class myrepo(repo.__class__): |
367ce8514da0
extensions: recommend against using wrapfunction for repo methods
Greg Ward <greg-hg@gerg.ca>
parents:
10364
diff
changeset
|
665 def whatever(self, *args, **kwargs): |
367ce8514da0
extensions: recommend against using wrapfunction for repo methods
Greg Ward <greg-hg@gerg.ca>
parents:
10364
diff
changeset
|
666 [...extension stuff...] |
367ce8514da0
extensions: recommend against using wrapfunction for repo methods
Greg Ward <greg-hg@gerg.ca>
parents:
10364
diff
changeset
|
667 super(myrepo, self).whatever(*args, **kwargs) |
367ce8514da0
extensions: recommend against using wrapfunction for repo methods
Greg Ward <greg-hg@gerg.ca>
parents:
10364
diff
changeset
|
668 [...extension stuff...] |
367ce8514da0
extensions: recommend against using wrapfunction for repo methods
Greg Ward <greg-hg@gerg.ca>
parents:
10364
diff
changeset
|
669 |
367ce8514da0
extensions: recommend against using wrapfunction for repo methods
Greg Ward <greg-hg@gerg.ca>
parents:
10364
diff
changeset
|
670 repo.__class__ = myrepo |
367ce8514da0
extensions: recommend against using wrapfunction for repo methods
Greg Ward <greg-hg@gerg.ca>
parents:
10364
diff
changeset
|
671 |
367ce8514da0
extensions: recommend against using wrapfunction for repo methods
Greg Ward <greg-hg@gerg.ca>
parents:
10364
diff
changeset
|
672 In general, combining wrapfunction() with subclassing does not |
367ce8514da0
extensions: recommend against using wrapfunction for repo methods
Greg Ward <greg-hg@gerg.ca>
parents:
10364
diff
changeset
|
673 work. Since you cannot control what other extensions are loaded by |
367ce8514da0
extensions: recommend against using wrapfunction for repo methods
Greg Ward <greg-hg@gerg.ca>
parents:
10364
diff
changeset
|
674 your end users, you should play nicely with others by using the |
367ce8514da0
extensions: recommend against using wrapfunction for repo methods
Greg Ward <greg-hg@gerg.ca>
parents:
10364
diff
changeset
|
675 subclass trick. |
45942
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
45899
diff
changeset
|
676 """ |
21795
711498bb4ff5
extensions: restore use of callable() since it was readded in Python 3.2
Augie Fackler <raf@durin42.com>
parents:
21229
diff
changeset
|
677 assert callable(wrapper) |
7215
0ab5f21c390b
extensions: add wrapping functions
Matt Mackall <mpm@selenic.com>
parents:
7011
diff
changeset
|
678 |
50801
ee1617c04858
wrapfunction: deprecated calling "wrapfunction" with bytes
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
50800
diff
changeset
|
679 if not isinstance(funcname, str): |
ee1617c04858
wrapfunction: deprecated calling "wrapfunction" with bytes
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
50800
diff
changeset
|
680 msg = b"pass wrapfunction target name as `str`, not `bytes`" |
ee1617c04858
wrapfunction: deprecated calling "wrapfunction" with bytes
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
50800
diff
changeset
|
681 util.nouideprecwarn(msg, b"6.6", stacklevel=2) |
ee1617c04858
wrapfunction: deprecated calling "wrapfunction" with bytes
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
50800
diff
changeset
|
682 funcname = pycompat.sysstr(funcname) |
ee1617c04858
wrapfunction: deprecated calling "wrapfunction" with bytes
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
50800
diff
changeset
|
683 |
7215
0ab5f21c390b
extensions: add wrapping functions
Matt Mackall <mpm@selenic.com>
parents:
7011
diff
changeset
|
684 origfn = getattr(container, funcname) |
21795
711498bb4ff5
extensions: restore use of callable() since it was readded in Python 3.2
Augie Fackler <raf@durin42.com>
parents:
21229
diff
changeset
|
685 assert callable(origfn) |
34087
5361771f9714
wrapfunction: use functools.partial if possible
Jun Wu <quark@fb.com>
parents:
34048
diff
changeset
|
686 if inspect.ismodule(container): |
5361771f9714
wrapfunction: use functools.partial if possible
Jun Wu <quark@fb.com>
parents:
34048
diff
changeset
|
687 # origfn is not an instance or class method. "partial" can be used. |
5361771f9714
wrapfunction: use functools.partial if possible
Jun Wu <quark@fb.com>
parents:
34048
diff
changeset
|
688 # "partial" won't insert a frame in traceback. |
5361771f9714
wrapfunction: use functools.partial if possible
Jun Wu <quark@fb.com>
parents:
34048
diff
changeset
|
689 wrap = functools.partial(wrapper, origfn) |
5361771f9714
wrapfunction: use functools.partial if possible
Jun Wu <quark@fb.com>
parents:
34048
diff
changeset
|
690 else: |
5361771f9714
wrapfunction: use functools.partial if possible
Jun Wu <quark@fb.com>
parents:
34048
diff
changeset
|
691 # "partial" cannot be safely used. Emulate its effect by using "bind". |
5361771f9714
wrapfunction: use functools.partial if possible
Jun Wu <quark@fb.com>
parents:
34048
diff
changeset
|
692 # The downside is one more frame in traceback. |
5361771f9714
wrapfunction: use functools.partial if possible
Jun Wu <quark@fb.com>
parents:
34048
diff
changeset
|
693 wrap = bind(wrapper, origfn) |
29763
ce6317dcb944
extensions: set attributes to wrappers so we can trace them back
Jun Wu <quark@fb.com>
parents:
29461
diff
changeset
|
694 _updatewrapper(wrap, origfn, wrapper) |
28311
1b0ef07ba783
extensions: copy attributes to wrapper by wrapfunction()
Yuya Nishihara <yuya@tcha.org>
parents:
28310
diff
changeset
|
695 setattr(container, funcname, wrap) |
7215
0ab5f21c390b
extensions: add wrapping functions
Matt Mackall <mpm@selenic.com>
parents:
7011
diff
changeset
|
696 return origfn |
8871
20a25042fadc
extensions: move extensions listing functions from mercurial.help
Cédric Duval <cedricduval@free.fr>
parents:
8225
diff
changeset
|
697 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
698 |
29765
19578bb84731
extensions: add unwrapfunction to undo wrapfunction
Jun Wu <quark@fb.com>
parents:
29764
diff
changeset
|
699 def unwrapfunction(container, funcname, wrapper=None): |
45942
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
45899
diff
changeset
|
700 """undo wrapfunction |
29765
19578bb84731
extensions: add unwrapfunction to undo wrapfunction
Jun Wu <quark@fb.com>
parents:
29764
diff
changeset
|
701 |
19578bb84731
extensions: add unwrapfunction to undo wrapfunction
Jun Wu <quark@fb.com>
parents:
29764
diff
changeset
|
702 If wrappers is None, undo the last wrap. Otherwise removes the wrapper |
19578bb84731
extensions: add unwrapfunction to undo wrapfunction
Jun Wu <quark@fb.com>
parents:
29764
diff
changeset
|
703 from the chain of wrappers. |
19578bb84731
extensions: add unwrapfunction to undo wrapfunction
Jun Wu <quark@fb.com>
parents:
29764
diff
changeset
|
704 |
19578bb84731
extensions: add unwrapfunction to undo wrapfunction
Jun Wu <quark@fb.com>
parents:
29764
diff
changeset
|
705 Return the removed wrapper. |
19578bb84731
extensions: add unwrapfunction to undo wrapfunction
Jun Wu <quark@fb.com>
parents:
29764
diff
changeset
|
706 Raise IndexError if wrapper is None and nothing to unwrap; ValueError if |
19578bb84731
extensions: add unwrapfunction to undo wrapfunction
Jun Wu <quark@fb.com>
parents:
29764
diff
changeset
|
707 wrapper is not None but is not found in the wrapper chain. |
45942
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
45899
diff
changeset
|
708 """ |
29765
19578bb84731
extensions: add unwrapfunction to undo wrapfunction
Jun Wu <quark@fb.com>
parents:
29764
diff
changeset
|
709 chain = getwrapperchain(container, funcname) |
19578bb84731
extensions: add unwrapfunction to undo wrapfunction
Jun Wu <quark@fb.com>
parents:
29764
diff
changeset
|
710 origfn = chain.pop() |
19578bb84731
extensions: add unwrapfunction to undo wrapfunction
Jun Wu <quark@fb.com>
parents:
29764
diff
changeset
|
711 if wrapper is None: |
19578bb84731
extensions: add unwrapfunction to undo wrapfunction
Jun Wu <quark@fb.com>
parents:
29764
diff
changeset
|
712 wrapper = chain[0] |
19578bb84731
extensions: add unwrapfunction to undo wrapfunction
Jun Wu <quark@fb.com>
parents:
29764
diff
changeset
|
713 chain.remove(wrapper) |
19578bb84731
extensions: add unwrapfunction to undo wrapfunction
Jun Wu <quark@fb.com>
parents:
29764
diff
changeset
|
714 setattr(container, funcname, origfn) |
19578bb84731
extensions: add unwrapfunction to undo wrapfunction
Jun Wu <quark@fb.com>
parents:
29764
diff
changeset
|
715 for w in reversed(chain): |
19578bb84731
extensions: add unwrapfunction to undo wrapfunction
Jun Wu <quark@fb.com>
parents:
29764
diff
changeset
|
716 wrapfunction(container, funcname, w) |
19578bb84731
extensions: add unwrapfunction to undo wrapfunction
Jun Wu <quark@fb.com>
parents:
29764
diff
changeset
|
717 return wrapper |
19578bb84731
extensions: add unwrapfunction to undo wrapfunction
Jun Wu <quark@fb.com>
parents:
29764
diff
changeset
|
718 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
719 |
29764
8bf97c4c6c2a
extensions: add getwrapperchain to get a list of wrappers
Jun Wu <quark@fb.com>
parents:
29763
diff
changeset
|
720 def getwrapperchain(container, funcname): |
45942
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
45899
diff
changeset
|
721 """get a chain of wrappers of a function |
29764
8bf97c4c6c2a
extensions: add getwrapperchain to get a list of wrappers
Jun Wu <quark@fb.com>
parents:
29763
diff
changeset
|
722 |
8bf97c4c6c2a
extensions: add getwrapperchain to get a list of wrappers
Jun Wu <quark@fb.com>
parents:
29763
diff
changeset
|
723 Return a list of functions: [newest wrapper, ..., oldest wrapper, origfunc] |
8bf97c4c6c2a
extensions: add getwrapperchain to get a list of wrappers
Jun Wu <quark@fb.com>
parents:
29763
diff
changeset
|
724 |
8bf97c4c6c2a
extensions: add getwrapperchain to get a list of wrappers
Jun Wu <quark@fb.com>
parents:
29763
diff
changeset
|
725 The wrapper functions are the ones passed to wrapfunction, whose first |
8bf97c4c6c2a
extensions: add getwrapperchain to get a list of wrappers
Jun Wu <quark@fb.com>
parents:
29763
diff
changeset
|
726 argument is origfunc. |
45942
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
45899
diff
changeset
|
727 """ |
29764
8bf97c4c6c2a
extensions: add getwrapperchain to get a list of wrappers
Jun Wu <quark@fb.com>
parents:
29763
diff
changeset
|
728 result = [] |
8bf97c4c6c2a
extensions: add getwrapperchain to get a list of wrappers
Jun Wu <quark@fb.com>
parents:
29763
diff
changeset
|
729 fn = getattr(container, funcname) |
8bf97c4c6c2a
extensions: add getwrapperchain to get a list of wrappers
Jun Wu <quark@fb.com>
parents:
29763
diff
changeset
|
730 while fn: |
8bf97c4c6c2a
extensions: add getwrapperchain to get a list of wrappers
Jun Wu <quark@fb.com>
parents:
29763
diff
changeset
|
731 assert callable(fn) |
8bf97c4c6c2a
extensions: add getwrapperchain to get a list of wrappers
Jun Wu <quark@fb.com>
parents:
29763
diff
changeset
|
732 result.append(getattr(fn, '_unboundwrapper', fn)) |
8bf97c4c6c2a
extensions: add getwrapperchain to get a list of wrappers
Jun Wu <quark@fb.com>
parents:
29763
diff
changeset
|
733 fn = getattr(fn, '_origfunc', None) |
8bf97c4c6c2a
extensions: add getwrapperchain to get a list of wrappers
Jun Wu <quark@fb.com>
parents:
29763
diff
changeset
|
734 return result |
8bf97c4c6c2a
extensions: add getwrapperchain to get a list of wrappers
Jun Wu <quark@fb.com>
parents:
29763
diff
changeset
|
735 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
736 |
38163
b39958d6b81b
extensions: remove strip_init=True from _disabledpaths()
Yuya Nishihara <yuya@tcha.org>
parents:
38162
diff
changeset
|
737 def _disabledpaths(): |
b39958d6b81b
extensions: remove strip_init=True from _disabledpaths()
Yuya Nishihara <yuya@tcha.org>
parents:
38162
diff
changeset
|
738 '''find paths of disabled extensions. returns a dict of {name: path}''' |
8872
d0c0013f8713
extensions: simplify by selecting primary hgext
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
8871
diff
changeset
|
739 import hgext |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
740 |
49780
9d8757ddd0ab
extensions: process disabled external paths when `hgext` package is in-memory
Matt Harbison <matt_harbison@yahoo.com>
parents:
49026
diff
changeset
|
741 exts = {} |
9d8757ddd0ab
extensions: process disabled external paths when `hgext` package is in-memory
Matt Harbison <matt_harbison@yahoo.com>
parents:
49026
diff
changeset
|
742 |
45105
5d09a120b4be
extensions: make `hg nonexistent` not crash with PyOxidizer
Martin von Zweigbergk <martinvonz@google.com>
parents:
44657
diff
changeset
|
743 # The hgext might not have a __file__ attribute (e.g. in PyOxidizer) and |
5d09a120b4be
extensions: make `hg nonexistent` not crash with PyOxidizer
Martin von Zweigbergk <martinvonz@google.com>
parents:
44657
diff
changeset
|
744 # it might not be on a filesystem even if it does. |
5d09a120b4be
extensions: make `hg nonexistent` not crash with PyOxidizer
Martin von Zweigbergk <martinvonz@google.com>
parents:
44657
diff
changeset
|
745 if util.safehasattr(hgext, '__file__'): |
5d09a120b4be
extensions: make `hg nonexistent` not crash with PyOxidizer
Martin von Zweigbergk <martinvonz@google.com>
parents:
44657
diff
changeset
|
746 extpath = os.path.dirname( |
47625
7bafe40ab78a
windows: use abspath in extensions
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47055
diff
changeset
|
747 util.abspath(pycompat.fsencode(hgext.__file__)) |
45105
5d09a120b4be
extensions: make `hg nonexistent` not crash with PyOxidizer
Martin von Zweigbergk <martinvonz@google.com>
parents:
44657
diff
changeset
|
748 ) |
5d09a120b4be
extensions: make `hg nonexistent` not crash with PyOxidizer
Martin von Zweigbergk <martinvonz@google.com>
parents:
44657
diff
changeset
|
749 try: |
5d09a120b4be
extensions: make `hg nonexistent` not crash with PyOxidizer
Martin von Zweigbergk <martinvonz@google.com>
parents:
44657
diff
changeset
|
750 files = os.listdir(extpath) |
5d09a120b4be
extensions: make `hg nonexistent` not crash with PyOxidizer
Martin von Zweigbergk <martinvonz@google.com>
parents:
44657
diff
changeset
|
751 except OSError: |
49780
9d8757ddd0ab
extensions: process disabled external paths when `hgext` package is in-memory
Matt Harbison <matt_harbison@yahoo.com>
parents:
49026
diff
changeset
|
752 pass |
9d8757ddd0ab
extensions: process disabled external paths when `hgext` package is in-memory
Matt Harbison <matt_harbison@yahoo.com>
parents:
49026
diff
changeset
|
753 else: |
9d8757ddd0ab
extensions: process disabled external paths when `hgext` package is in-memory
Matt Harbison <matt_harbison@yahoo.com>
parents:
49026
diff
changeset
|
754 for e in files: |
9d8757ddd0ab
extensions: process disabled external paths when `hgext` package is in-memory
Matt Harbison <matt_harbison@yahoo.com>
parents:
49026
diff
changeset
|
755 if e.endswith(b'.py'): |
9d8757ddd0ab
extensions: process disabled external paths when `hgext` package is in-memory
Matt Harbison <matt_harbison@yahoo.com>
parents:
49026
diff
changeset
|
756 name = e.rsplit(b'.', 1)[0] |
9d8757ddd0ab
extensions: process disabled external paths when `hgext` package is in-memory
Matt Harbison <matt_harbison@yahoo.com>
parents:
49026
diff
changeset
|
757 path = os.path.join(extpath, e) |
9d8757ddd0ab
extensions: process disabled external paths when `hgext` package is in-memory
Matt Harbison <matt_harbison@yahoo.com>
parents:
49026
diff
changeset
|
758 else: |
9d8757ddd0ab
extensions: process disabled external paths when `hgext` package is in-memory
Matt Harbison <matt_harbison@yahoo.com>
parents:
49026
diff
changeset
|
759 name = e |
9d8757ddd0ab
extensions: process disabled external paths when `hgext` package is in-memory
Matt Harbison <matt_harbison@yahoo.com>
parents:
49026
diff
changeset
|
760 path = os.path.join(extpath, e, b'__init__.py') |
9d8757ddd0ab
extensions: process disabled external paths when `hgext` package is in-memory
Matt Harbison <matt_harbison@yahoo.com>
parents:
49026
diff
changeset
|
761 if not os.path.exists(path): |
9d8757ddd0ab
extensions: process disabled external paths when `hgext` package is in-memory
Matt Harbison <matt_harbison@yahoo.com>
parents:
49026
diff
changeset
|
762 continue |
9d8757ddd0ab
extensions: process disabled external paths when `hgext` package is in-memory
Matt Harbison <matt_harbison@yahoo.com>
parents:
49026
diff
changeset
|
763 if name in exts or name in _order or name == b'__init__': |
9d8757ddd0ab
extensions: process disabled external paths when `hgext` package is in-memory
Matt Harbison <matt_harbison@yahoo.com>
parents:
49026
diff
changeset
|
764 continue |
9d8757ddd0ab
extensions: process disabled external paths when `hgext` package is in-memory
Matt Harbison <matt_harbison@yahoo.com>
parents:
49026
diff
changeset
|
765 exts[name] = path |
8964
119d1f664eae
extensions: catch OSError when hgext is not accessible (issue1708)
Cédric Duval <cedricduval@free.fr>
parents:
8896
diff
changeset
|
766 |
48913
f254fc73d956
global: bulk replace simple pycompat.iteritems(x) with x.items()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48894
diff
changeset
|
767 for name, path in _disabledextensions.items(): |
33327
68b7ceda99d7
dispatch: fix typo suggestion for disabled extension
Martin von Zweigbergk <martinvonz@google.com>
parents:
33132
diff
changeset
|
768 # If no path was provided for a disabled extension (e.g. "color=!"), |
68b7ceda99d7
dispatch: fix typo suggestion for disabled extension
Martin von Zweigbergk <martinvonz@google.com>
parents:
33132
diff
changeset
|
769 # don't replace the path we already found by the scan above. |
68b7ceda99d7
dispatch: fix typo suggestion for disabled extension
Martin von Zweigbergk <martinvonz@google.com>
parents:
33132
diff
changeset
|
770 if path: |
68b7ceda99d7
dispatch: fix typo suggestion for disabled extension
Martin von Zweigbergk <martinvonz@google.com>
parents:
33132
diff
changeset
|
771 exts[name] = path |
10363
c07974215b3d
extensions: refactor disabled()
Brodie Rao <me+hg@dackz.net>
parents:
10263
diff
changeset
|
772 return exts |
8872
d0c0013f8713
extensions: simplify by selecting primary hgext
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
8871
diff
changeset
|
773 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
774 |
14317
660b0c1b6196
extensions: move moduledoc to break import loop with help
Matt Mackall <mpm@selenic.com>
parents:
14316
diff
changeset
|
775 def _moduledoc(file): |
45942
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
45899
diff
changeset
|
776 """return the top-level python documentation for the given file |
14317
660b0c1b6196
extensions: move moduledoc to break import loop with help
Matt Mackall <mpm@selenic.com>
parents:
14316
diff
changeset
|
777 |
660b0c1b6196
extensions: move moduledoc to break import loop with help
Matt Mackall <mpm@selenic.com>
parents:
14316
diff
changeset
|
778 Loosely inspired by pydoc.source_synopsis(), but rewritten to |
660b0c1b6196
extensions: move moduledoc to break import loop with help
Matt Mackall <mpm@selenic.com>
parents:
14316
diff
changeset
|
779 handle triple quotes and to return the whole text instead of just |
45942
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
45899
diff
changeset
|
780 the synopsis""" |
14317
660b0c1b6196
extensions: move moduledoc to break import loop with help
Matt Mackall <mpm@selenic.com>
parents:
14316
diff
changeset
|
781 result = [] |
660b0c1b6196
extensions: move moduledoc to break import loop with help
Matt Mackall <mpm@selenic.com>
parents:
14316
diff
changeset
|
782 |
660b0c1b6196
extensions: move moduledoc to break import loop with help
Matt Mackall <mpm@selenic.com>
parents:
14316
diff
changeset
|
783 line = file.readline() |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
784 while line[:1] == b'#' or not line.strip(): |
14317
660b0c1b6196
extensions: move moduledoc to break import loop with help
Matt Mackall <mpm@selenic.com>
parents:
14316
diff
changeset
|
785 line = file.readline() |
660b0c1b6196
extensions: move moduledoc to break import loop with help
Matt Mackall <mpm@selenic.com>
parents:
14316
diff
changeset
|
786 if not line: |
660b0c1b6196
extensions: move moduledoc to break import loop with help
Matt Mackall <mpm@selenic.com>
parents:
14316
diff
changeset
|
787 break |
660b0c1b6196
extensions: move moduledoc to break import loop with help
Matt Mackall <mpm@selenic.com>
parents:
14316
diff
changeset
|
788 |
660b0c1b6196
extensions: move moduledoc to break import loop with help
Matt Mackall <mpm@selenic.com>
parents:
14316
diff
changeset
|
789 start = line[:3] |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
790 if start == b'"""' or start == b"'''": |
14317
660b0c1b6196
extensions: move moduledoc to break import loop with help
Matt Mackall <mpm@selenic.com>
parents:
14316
diff
changeset
|
791 line = line[3:] |
660b0c1b6196
extensions: move moduledoc to break import loop with help
Matt Mackall <mpm@selenic.com>
parents:
14316
diff
changeset
|
792 while line: |
660b0c1b6196
extensions: move moduledoc to break import loop with help
Matt Mackall <mpm@selenic.com>
parents:
14316
diff
changeset
|
793 if line.rstrip().endswith(start): |
660b0c1b6196
extensions: move moduledoc to break import loop with help
Matt Mackall <mpm@selenic.com>
parents:
14316
diff
changeset
|
794 line = line.split(start)[0] |
660b0c1b6196
extensions: move moduledoc to break import loop with help
Matt Mackall <mpm@selenic.com>
parents:
14316
diff
changeset
|
795 if line: |
660b0c1b6196
extensions: move moduledoc to break import loop with help
Matt Mackall <mpm@selenic.com>
parents:
14316
diff
changeset
|
796 result.append(line) |
660b0c1b6196
extensions: move moduledoc to break import loop with help
Matt Mackall <mpm@selenic.com>
parents:
14316
diff
changeset
|
797 break |
660b0c1b6196
extensions: move moduledoc to break import loop with help
Matt Mackall <mpm@selenic.com>
parents:
14316
diff
changeset
|
798 elif not line: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
799 return None # unmatched delimiter |
14317
660b0c1b6196
extensions: move moduledoc to break import loop with help
Matt Mackall <mpm@selenic.com>
parents:
14316
diff
changeset
|
800 result.append(line) |
660b0c1b6196
extensions: move moduledoc to break import loop with help
Matt Mackall <mpm@selenic.com>
parents:
14316
diff
changeset
|
801 line = file.readline() |
660b0c1b6196
extensions: move moduledoc to break import loop with help
Matt Mackall <mpm@selenic.com>
parents:
14316
diff
changeset
|
802 else: |
660b0c1b6196
extensions: move moduledoc to break import loop with help
Matt Mackall <mpm@selenic.com>
parents:
14316
diff
changeset
|
803 return None |
660b0c1b6196
extensions: move moduledoc to break import loop with help
Matt Mackall <mpm@selenic.com>
parents:
14316
diff
changeset
|
804 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
805 return b''.join(result) |
14317
660b0c1b6196
extensions: move moduledoc to break import loop with help
Matt Mackall <mpm@selenic.com>
parents:
14316
diff
changeset
|
806 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
807 |
10363
c07974215b3d
extensions: refactor disabled()
Brodie Rao <me+hg@dackz.net>
parents:
10263
diff
changeset
|
808 def _disabledhelp(path): |
c07974215b3d
extensions: refactor disabled()
Brodie Rao <me+hg@dackz.net>
parents:
10263
diff
changeset
|
809 '''retrieve help synopsis of a disabled extension (without importing)''' |
c07974215b3d
extensions: refactor disabled()
Brodie Rao <me+hg@dackz.net>
parents:
10263
diff
changeset
|
810 try: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
811 with open(path, b'rb') as src: |
38344
c6f82a18a63d
extensions: use context manger for open()
Yuya Nishihara <yuya@tcha.org>
parents:
38343
diff
changeset
|
812 doc = _moduledoc(src) |
10363
c07974215b3d
extensions: refactor disabled()
Brodie Rao <me+hg@dackz.net>
parents:
10263
diff
changeset
|
813 except IOError: |
c07974215b3d
extensions: refactor disabled()
Brodie Rao <me+hg@dackz.net>
parents:
10263
diff
changeset
|
814 return |
c07974215b3d
extensions: refactor disabled()
Brodie Rao <me+hg@dackz.net>
parents:
10263
diff
changeset
|
815 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
816 if doc: # extracting localized synopsis |
30306
5581b294f3c6
help: show help for disabled extensions (issue5228)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30058
diff
changeset
|
817 return gettext(doc) |
10363
c07974215b3d
extensions: refactor disabled()
Brodie Rao <me+hg@dackz.net>
parents:
10263
diff
changeset
|
818 else: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
819 return _(b'(no help text available)') |
10363
c07974215b3d
extensions: refactor disabled()
Brodie Rao <me+hg@dackz.net>
parents:
10263
diff
changeset
|
820 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
821 |
10363
c07974215b3d
extensions: refactor disabled()
Brodie Rao <me+hg@dackz.net>
parents:
10263
diff
changeset
|
822 def disabled(): |
14530
cd31a1cc1521
extensions: update doc of enabled() and disabled() according to d5b525697ddb
Yuya Nishihara <yuya@tcha.org>
parents:
14415
diff
changeset
|
823 '''find disabled extensions from hgext. returns a dict of {name: desc}''' |
14539
558ec14ba6be
extensions: make disabled()/disabledext() load prebuilt index if available
Yuya Nishihara <yuya@tcha.org>
parents:
14530
diff
changeset
|
824 try: |
43705
1ea33dff7841
extensions: hide two confusing import statements from pytype
Augie Fackler <augie@google.com>
parents:
43506
diff
changeset
|
825 from hgext import __index__ # pytype: disable=import-error |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
826 |
44452
9d2b2df2c2ba
cleanup: run pyupgrade on our source tree to clean up varying things
Augie Fackler <augie@google.com>
parents:
43707
diff
changeset
|
827 return { |
9d2b2df2c2ba
cleanup: run pyupgrade on our source tree to clean up varying things
Augie Fackler <augie@google.com>
parents:
43707
diff
changeset
|
828 name: gettext(desc) |
48913
f254fc73d956
global: bulk replace simple pycompat.iteritems(x) with x.items()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48894
diff
changeset
|
829 for name, desc in __index__.docs.items() |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
830 if name not in _order |
44452
9d2b2df2c2ba
cleanup: run pyupgrade on our source tree to clean up varying things
Augie Fackler <augie@google.com>
parents:
43707
diff
changeset
|
831 } |
21229
54d7657d7d1e
setup.py, make: avoid problems with outdated, existing hgext/__index__.py*
Thomas Arendsen Hein <thomas@intevation.de>
parents:
20645
diff
changeset
|
832 except (ImportError, AttributeError): |
14539
558ec14ba6be
extensions: make disabled()/disabledext() load prebuilt index if available
Yuya Nishihara <yuya@tcha.org>
parents:
14530
diff
changeset
|
833 pass |
558ec14ba6be
extensions: make disabled()/disabledext() load prebuilt index if available
Yuya Nishihara <yuya@tcha.org>
parents:
14530
diff
changeset
|
834 |
10363
c07974215b3d
extensions: refactor disabled()
Brodie Rao <me+hg@dackz.net>
parents:
10263
diff
changeset
|
835 paths = _disabledpaths() |
c07974215b3d
extensions: refactor disabled()
Brodie Rao <me+hg@dackz.net>
parents:
10263
diff
changeset
|
836 if not paths: |
16709
9eca39a91964
extensions.disabled: return {} instead of None no extensions are disabled
Augie Fackler <raf@durin42.com>
parents:
16667
diff
changeset
|
837 return {} |
10363
c07974215b3d
extensions: refactor disabled()
Brodie Rao <me+hg@dackz.net>
parents:
10263
diff
changeset
|
838 |
c07974215b3d
extensions: refactor disabled()
Brodie Rao <me+hg@dackz.net>
parents:
10263
diff
changeset
|
839 exts = {} |
48913
f254fc73d956
global: bulk replace simple pycompat.iteritems(x) with x.items()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48894
diff
changeset
|
840 for name, path in paths.items(): |
10363
c07974215b3d
extensions: refactor disabled()
Brodie Rao <me+hg@dackz.net>
parents:
10263
diff
changeset
|
841 doc = _disabledhelp(path) |
46094
1ced08423d59
extensions: avoid including `__index__` in the disabled extension list
Matt Harbison <matt_harbison@yahoo.com>
parents:
46030
diff
changeset
|
842 if doc and name != b'__index__': |
49026
2d519511c5c3
extensions: use new function for getting first line of string
Martin von Zweigbergk <martinvonz@google.com>
parents:
48946
diff
changeset
|
843 exts[name] = stringutil.firstline(doc) |
8871
20a25042fadc
extensions: move extensions listing functions from mercurial.help
Cédric Duval <cedricduval@free.fr>
parents:
8225
diff
changeset
|
844 |
14316
d5b525697ddb
extensions: drop maxlength from enabled and disabled
Matt Mackall <mpm@selenic.com>
parents:
14079
diff
changeset
|
845 return exts |
8871
20a25042fadc
extensions: move extensions listing functions from mercurial.help
Cédric Duval <cedricduval@free.fr>
parents:
8225
diff
changeset
|
846 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
847 |
44657
843418dc0b1b
extensions: refactor function for obtaining disabled extension help
Gregory Szorc <gregory.szorc@gmail.com>
parents:
44452
diff
changeset
|
848 def disabled_help(name): |
843418dc0b1b
extensions: refactor function for obtaining disabled extension help
Gregory Szorc <gregory.szorc@gmail.com>
parents:
44452
diff
changeset
|
849 """Obtain the full help text for a disabled extension, or None.""" |
10364
de1e7099d100
dispatch: provide help for disabled extensions and commands
Brodie Rao <me+hg@dackz.net>
parents:
10363
diff
changeset
|
850 paths = _disabledpaths() |
de1e7099d100
dispatch: provide help for disabled extensions and commands
Brodie Rao <me+hg@dackz.net>
parents:
10363
diff
changeset
|
851 if name in paths: |
de1e7099d100
dispatch: provide help for disabled extensions and commands
Brodie Rao <me+hg@dackz.net>
parents:
10363
diff
changeset
|
852 return _disabledhelp(paths[name]) |
49781
f4a363b25859
extensions: load help from hgext.__index__ as a fallback this time
Matt Harbison <matt_harbison@yahoo.com>
parents:
49780
diff
changeset
|
853 else: |
f4a363b25859
extensions: load help from hgext.__index__ as a fallback this time
Matt Harbison <matt_harbison@yahoo.com>
parents:
49780
diff
changeset
|
854 try: |
f4a363b25859
extensions: load help from hgext.__index__ as a fallback this time
Matt Harbison <matt_harbison@yahoo.com>
parents:
49780
diff
changeset
|
855 import hgext |
f4a363b25859
extensions: load help from hgext.__index__ as a fallback this time
Matt Harbison <matt_harbison@yahoo.com>
parents:
49780
diff
changeset
|
856 from hgext import __index__ # pytype: disable=import-error |
f4a363b25859
extensions: load help from hgext.__index__ as a fallback this time
Matt Harbison <matt_harbison@yahoo.com>
parents:
49780
diff
changeset
|
857 |
f4a363b25859
extensions: load help from hgext.__index__ as a fallback this time
Matt Harbison <matt_harbison@yahoo.com>
parents:
49780
diff
changeset
|
858 # The extensions are filesystem based, so either an error occurred |
f4a363b25859
extensions: load help from hgext.__index__ as a fallback this time
Matt Harbison <matt_harbison@yahoo.com>
parents:
49780
diff
changeset
|
859 # or all are enabled. |
f4a363b25859
extensions: load help from hgext.__index__ as a fallback this time
Matt Harbison <matt_harbison@yahoo.com>
parents:
49780
diff
changeset
|
860 if util.safehasattr(hgext, '__file__'): |
f4a363b25859
extensions: load help from hgext.__index__ as a fallback this time
Matt Harbison <matt_harbison@yahoo.com>
parents:
49780
diff
changeset
|
861 return |
f4a363b25859
extensions: load help from hgext.__index__ as a fallback this time
Matt Harbison <matt_harbison@yahoo.com>
parents:
49780
diff
changeset
|
862 |
f4a363b25859
extensions: load help from hgext.__index__ as a fallback this time
Matt Harbison <matt_harbison@yahoo.com>
parents:
49780
diff
changeset
|
863 if name in _order: # enabled |
f4a363b25859
extensions: load help from hgext.__index__ as a fallback this time
Matt Harbison <matt_harbison@yahoo.com>
parents:
49780
diff
changeset
|
864 return |
f4a363b25859
extensions: load help from hgext.__index__ as a fallback this time
Matt Harbison <matt_harbison@yahoo.com>
parents:
49780
diff
changeset
|
865 else: |
f4a363b25859
extensions: load help from hgext.__index__ as a fallback this time
Matt Harbison <matt_harbison@yahoo.com>
parents:
49780
diff
changeset
|
866 return gettext(__index__.docs.get(name)) |
f4a363b25859
extensions: load help from hgext.__index__ as a fallback this time
Matt Harbison <matt_harbison@yahoo.com>
parents:
49780
diff
changeset
|
867 except (ImportError, AttributeError): |
f4a363b25859
extensions: load help from hgext.__index__ as a fallback this time
Matt Harbison <matt_harbison@yahoo.com>
parents:
49780
diff
changeset
|
868 pass |
10364
de1e7099d100
dispatch: provide help for disabled extensions and commands
Brodie Rao <me+hg@dackz.net>
parents:
10363
diff
changeset
|
869 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
870 |
38162
bdf344aea0ee
extensions: peek command table of disabled extensions without importing
Yuya Nishihara <yuya@tcha.org>
parents:
38020
diff
changeset
|
871 def _walkcommand(node): |
bdf344aea0ee
extensions: peek command table of disabled extensions without importing
Yuya Nishihara <yuya@tcha.org>
parents:
38020
diff
changeset
|
872 """Scan @command() decorators in the tree starting at node""" |
bdf344aea0ee
extensions: peek command table of disabled extensions without importing
Yuya Nishihara <yuya@tcha.org>
parents:
38020
diff
changeset
|
873 todo = collections.deque([node]) |
bdf344aea0ee
extensions: peek command table of disabled extensions without importing
Yuya Nishihara <yuya@tcha.org>
parents:
38020
diff
changeset
|
874 while todo: |
bdf344aea0ee
extensions: peek command table of disabled extensions without importing
Yuya Nishihara <yuya@tcha.org>
parents:
38020
diff
changeset
|
875 node = todo.popleft() |
bdf344aea0ee
extensions: peek command table of disabled extensions without importing
Yuya Nishihara <yuya@tcha.org>
parents:
38020
diff
changeset
|
876 if not isinstance(node, ast.FunctionDef): |
bdf344aea0ee
extensions: peek command table of disabled extensions without importing
Yuya Nishihara <yuya@tcha.org>
parents:
38020
diff
changeset
|
877 todo.extend(ast.iter_child_nodes(node)) |
bdf344aea0ee
extensions: peek command table of disabled extensions without importing
Yuya Nishihara <yuya@tcha.org>
parents:
38020
diff
changeset
|
878 continue |
bdf344aea0ee
extensions: peek command table of disabled extensions without importing
Yuya Nishihara <yuya@tcha.org>
parents:
38020
diff
changeset
|
879 for d in node.decorator_list: |
bdf344aea0ee
extensions: peek command table of disabled extensions without importing
Yuya Nishihara <yuya@tcha.org>
parents:
38020
diff
changeset
|
880 if not isinstance(d, ast.Call): |
bdf344aea0ee
extensions: peek command table of disabled extensions without importing
Yuya Nishihara <yuya@tcha.org>
parents:
38020
diff
changeset
|
881 continue |
bdf344aea0ee
extensions: peek command table of disabled extensions without importing
Yuya Nishihara <yuya@tcha.org>
parents:
38020
diff
changeset
|
882 if not isinstance(d.func, ast.Name): |
bdf344aea0ee
extensions: peek command table of disabled extensions without importing
Yuya Nishihara <yuya@tcha.org>
parents:
38020
diff
changeset
|
883 continue |
43506
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43503
diff
changeset
|
884 if d.func.id != 'command': |
38162
bdf344aea0ee
extensions: peek command table of disabled extensions without importing
Yuya Nishihara <yuya@tcha.org>
parents:
38020
diff
changeset
|
885 continue |
bdf344aea0ee
extensions: peek command table of disabled extensions without importing
Yuya Nishihara <yuya@tcha.org>
parents:
38020
diff
changeset
|
886 yield d |
bdf344aea0ee
extensions: peek command table of disabled extensions without importing
Yuya Nishihara <yuya@tcha.org>
parents:
38020
diff
changeset
|
887 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
888 |
38162
bdf344aea0ee
extensions: peek command table of disabled extensions without importing
Yuya Nishihara <yuya@tcha.org>
parents:
38020
diff
changeset
|
889 def _disabledcmdtable(path): |
bdf344aea0ee
extensions: peek command table of disabled extensions without importing
Yuya Nishihara <yuya@tcha.org>
parents:
38020
diff
changeset
|
890 """Construct a dummy command table without loading the extension module |
bdf344aea0ee
extensions: peek command table of disabled extensions without importing
Yuya Nishihara <yuya@tcha.org>
parents:
38020
diff
changeset
|
891 |
bdf344aea0ee
extensions: peek command table of disabled extensions without importing
Yuya Nishihara <yuya@tcha.org>
parents:
38020
diff
changeset
|
892 This may raise IOError or SyntaxError. |
bdf344aea0ee
extensions: peek command table of disabled extensions without importing
Yuya Nishihara <yuya@tcha.org>
parents:
38020
diff
changeset
|
893 """ |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
894 with open(path, b'rb') as src: |
38162
bdf344aea0ee
extensions: peek command table of disabled extensions without importing
Yuya Nishihara <yuya@tcha.org>
parents:
38020
diff
changeset
|
895 root = ast.parse(src.read(), path) |
bdf344aea0ee
extensions: peek command table of disabled extensions without importing
Yuya Nishihara <yuya@tcha.org>
parents:
38020
diff
changeset
|
896 cmdtable = {} |
50755
b9eb65a1ec14
extensions: address ast deprecations introduced in Python 3.12
Mads Kiilerich <mads@kiilerich.com>
parents:
49781
diff
changeset
|
897 |
b9eb65a1ec14
extensions: address ast deprecations introduced in Python 3.12
Mads Kiilerich <mads@kiilerich.com>
parents:
49781
diff
changeset
|
898 # Python 3.12 started removing Bytes and Str and deprecate harder |
b9eb65a1ec14
extensions: address ast deprecations introduced in Python 3.12
Mads Kiilerich <mads@kiilerich.com>
parents:
49781
diff
changeset
|
899 use_constant = 'Bytes' not in vars(ast) |
b9eb65a1ec14
extensions: address ast deprecations introduced in Python 3.12
Mads Kiilerich <mads@kiilerich.com>
parents:
49781
diff
changeset
|
900 |
38162
bdf344aea0ee
extensions: peek command table of disabled extensions without importing
Yuya Nishihara <yuya@tcha.org>
parents:
38020
diff
changeset
|
901 for node in _walkcommand(root): |
bdf344aea0ee
extensions: peek command table of disabled extensions without importing
Yuya Nishihara <yuya@tcha.org>
parents:
38020
diff
changeset
|
902 if not node.args: |
bdf344aea0ee
extensions: peek command table of disabled extensions without importing
Yuya Nishihara <yuya@tcha.org>
parents:
38020
diff
changeset
|
903 continue |
bdf344aea0ee
extensions: peek command table of disabled extensions without importing
Yuya Nishihara <yuya@tcha.org>
parents:
38020
diff
changeset
|
904 a = node.args[0] |
50755
b9eb65a1ec14
extensions: address ast deprecations introduced in Python 3.12
Mads Kiilerich <mads@kiilerich.com>
parents:
49781
diff
changeset
|
905 if use_constant: # Valid since Python 3.8 |
b9eb65a1ec14
extensions: address ast deprecations introduced in Python 3.12
Mads Kiilerich <mads@kiilerich.com>
parents:
49781
diff
changeset
|
906 if isinstance(a, ast.Constant): |
b9eb65a1ec14
extensions: address ast deprecations introduced in Python 3.12
Mads Kiilerich <mads@kiilerich.com>
parents:
49781
diff
changeset
|
907 if isinstance(a.value, str): |
b9eb65a1ec14
extensions: address ast deprecations introduced in Python 3.12
Mads Kiilerich <mads@kiilerich.com>
parents:
49781
diff
changeset
|
908 name = pycompat.sysbytes(a.value) |
b9eb65a1ec14
extensions: address ast deprecations introduced in Python 3.12
Mads Kiilerich <mads@kiilerich.com>
parents:
49781
diff
changeset
|
909 elif isinstance(a.value, bytes): |
b9eb65a1ec14
extensions: address ast deprecations introduced in Python 3.12
Mads Kiilerich <mads@kiilerich.com>
parents:
49781
diff
changeset
|
910 name = a.value |
b9eb65a1ec14
extensions: address ast deprecations introduced in Python 3.12
Mads Kiilerich <mads@kiilerich.com>
parents:
49781
diff
changeset
|
911 else: |
b9eb65a1ec14
extensions: address ast deprecations introduced in Python 3.12
Mads Kiilerich <mads@kiilerich.com>
parents:
49781
diff
changeset
|
912 continue |
b9eb65a1ec14
extensions: address ast deprecations introduced in Python 3.12
Mads Kiilerich <mads@kiilerich.com>
parents:
49781
diff
changeset
|
913 else: |
b9eb65a1ec14
extensions: address ast deprecations introduced in Python 3.12
Mads Kiilerich <mads@kiilerich.com>
parents:
49781
diff
changeset
|
914 continue |
b9eb65a1ec14
extensions: address ast deprecations introduced in Python 3.12
Mads Kiilerich <mads@kiilerich.com>
parents:
49781
diff
changeset
|
915 else: # Valid until 3.11 |
b9eb65a1ec14
extensions: address ast deprecations introduced in Python 3.12
Mads Kiilerich <mads@kiilerich.com>
parents:
49781
diff
changeset
|
916 if isinstance(a, ast.Str): |
b9eb65a1ec14
extensions: address ast deprecations introduced in Python 3.12
Mads Kiilerich <mads@kiilerich.com>
parents:
49781
diff
changeset
|
917 name = pycompat.sysbytes(a.s) |
b9eb65a1ec14
extensions: address ast deprecations introduced in Python 3.12
Mads Kiilerich <mads@kiilerich.com>
parents:
49781
diff
changeset
|
918 elif isinstance(a, ast.Bytes): |
b9eb65a1ec14
extensions: address ast deprecations introduced in Python 3.12
Mads Kiilerich <mads@kiilerich.com>
parents:
49781
diff
changeset
|
919 name = a.s |
b9eb65a1ec14
extensions: address ast deprecations introduced in Python 3.12
Mads Kiilerich <mads@kiilerich.com>
parents:
49781
diff
changeset
|
920 else: |
b9eb65a1ec14
extensions: address ast deprecations introduced in Python 3.12
Mads Kiilerich <mads@kiilerich.com>
parents:
49781
diff
changeset
|
921 continue |
38162
bdf344aea0ee
extensions: peek command table of disabled extensions without importing
Yuya Nishihara <yuya@tcha.org>
parents:
38020
diff
changeset
|
922 cmdtable[name] = (None, [], b'') |
bdf344aea0ee
extensions: peek command table of disabled extensions without importing
Yuya Nishihara <yuya@tcha.org>
parents:
38020
diff
changeset
|
923 return cmdtable |
bdf344aea0ee
extensions: peek command table of disabled extensions without importing
Yuya Nishihara <yuya@tcha.org>
parents:
38020
diff
changeset
|
924 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
925 |
37974
b45f4c1532c0
extensions: extract closure that looks for commands from disabled module
Yuya Nishihara <yuya@tcha.org>
parents:
37973
diff
changeset
|
926 def _finddisabledcmd(ui, cmd, name, path, strict): |
b45f4c1532c0
extensions: extract closure that looks for commands from disabled module
Yuya Nishihara <yuya@tcha.org>
parents:
37973
diff
changeset
|
927 try: |
38162
bdf344aea0ee
extensions: peek command table of disabled extensions without importing
Yuya Nishihara <yuya@tcha.org>
parents:
38020
diff
changeset
|
928 cmdtable = _disabledcmdtable(path) |
bdf344aea0ee
extensions: peek command table of disabled extensions without importing
Yuya Nishihara <yuya@tcha.org>
parents:
38020
diff
changeset
|
929 except (IOError, SyntaxError): |
37974
b45f4c1532c0
extensions: extract closure that looks for commands from disabled module
Yuya Nishihara <yuya@tcha.org>
parents:
37973
diff
changeset
|
930 return |
b45f4c1532c0
extensions: extract closure that looks for commands from disabled module
Yuya Nishihara <yuya@tcha.org>
parents:
37973
diff
changeset
|
931 try: |
38162
bdf344aea0ee
extensions: peek command table of disabled extensions without importing
Yuya Nishihara <yuya@tcha.org>
parents:
38020
diff
changeset
|
932 aliases, entry = cmdutil.findcmd(cmd, cmdtable, strict) |
37974
b45f4c1532c0
extensions: extract closure that looks for commands from disabled module
Yuya Nishihara <yuya@tcha.org>
parents:
37973
diff
changeset
|
933 except (error.AmbiguousCommand, error.UnknownCommand): |
b45f4c1532c0
extensions: extract closure that looks for commands from disabled module
Yuya Nishihara <yuya@tcha.org>
parents:
37973
diff
changeset
|
934 return |
b45f4c1532c0
extensions: extract closure that looks for commands from disabled module
Yuya Nishihara <yuya@tcha.org>
parents:
37973
diff
changeset
|
935 for c in aliases: |
b45f4c1532c0
extensions: extract closure that looks for commands from disabled module
Yuya Nishihara <yuya@tcha.org>
parents:
37973
diff
changeset
|
936 if c.startswith(cmd): |
b45f4c1532c0
extensions: extract closure that looks for commands from disabled module
Yuya Nishihara <yuya@tcha.org>
parents:
37973
diff
changeset
|
937 cmd = c |
b45f4c1532c0
extensions: extract closure that looks for commands from disabled module
Yuya Nishihara <yuya@tcha.org>
parents:
37973
diff
changeset
|
938 break |
b45f4c1532c0
extensions: extract closure that looks for commands from disabled module
Yuya Nishihara <yuya@tcha.org>
parents:
37973
diff
changeset
|
939 else: |
b45f4c1532c0
extensions: extract closure that looks for commands from disabled module
Yuya Nishihara <yuya@tcha.org>
parents:
37973
diff
changeset
|
940 cmd = aliases[0] |
38162
bdf344aea0ee
extensions: peek command table of disabled extensions without importing
Yuya Nishihara <yuya@tcha.org>
parents:
38020
diff
changeset
|
941 doc = _disabledhelp(path) |
37975
6e526b0961a8
help: load module doc of disabled extension in extensions.disabledcmd()
Yuya Nishihara <yuya@tcha.org>
parents:
37974
diff
changeset
|
942 return (cmd, name, doc) |
37974
b45f4c1532c0
extensions: extract closure that looks for commands from disabled module
Yuya Nishihara <yuya@tcha.org>
parents:
37973
diff
changeset
|
943 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
944 |
13191
1aea66b71f4f
extensions: warn about invalid extensions when listing disabled commands
Mads Kiilerich <mads@kiilerich.com>
parents:
12779
diff
changeset
|
945 def disabledcmd(ui, cmd, strict=False): |
45942
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
45899
diff
changeset
|
946 """find cmd from disabled extensions without importing. |
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
45899
diff
changeset
|
947 returns (cmdname, extname, doc)""" |
10364
de1e7099d100
dispatch: provide help for disabled extensions and commands
Brodie Rao <me+hg@dackz.net>
parents:
10363
diff
changeset
|
948 |
38162
bdf344aea0ee
extensions: peek command table of disabled extensions without importing
Yuya Nishihara <yuya@tcha.org>
parents:
38020
diff
changeset
|
949 paths = _disabledpaths() |
10364
de1e7099d100
dispatch: provide help for disabled extensions and commands
Brodie Rao <me+hg@dackz.net>
parents:
10363
diff
changeset
|
950 if not paths: |
de1e7099d100
dispatch: provide help for disabled extensions and commands
Brodie Rao <me+hg@dackz.net>
parents:
10363
diff
changeset
|
951 raise error.UnknownCommand(cmd) |
de1e7099d100
dispatch: provide help for disabled extensions and commands
Brodie Rao <me+hg@dackz.net>
parents:
10363
diff
changeset
|
952 |
16667
bdb7ae65c27c
extensions: don't suggest commands from deprecated extensions
Martin Geisler <mg@lazybytes.net>
parents:
16666
diff
changeset
|
953 ext = None |
10364
de1e7099d100
dispatch: provide help for disabled extensions and commands
Brodie Rao <me+hg@dackz.net>
parents:
10363
diff
changeset
|
954 # first, search for an extension with the same name as the command |
de1e7099d100
dispatch: provide help for disabled extensions and commands
Brodie Rao <me+hg@dackz.net>
parents:
10363
diff
changeset
|
955 path = paths.pop(cmd, None) |
de1e7099d100
dispatch: provide help for disabled extensions and commands
Brodie Rao <me+hg@dackz.net>
parents:
10363
diff
changeset
|
956 if path: |
37974
b45f4c1532c0
extensions: extract closure that looks for commands from disabled module
Yuya Nishihara <yuya@tcha.org>
parents:
37973
diff
changeset
|
957 ext = _finddisabledcmd(ui, cmd, cmd, path, strict=strict) |
16667
bdb7ae65c27c
extensions: don't suggest commands from deprecated extensions
Martin Geisler <mg@lazybytes.net>
parents:
16666
diff
changeset
|
958 if not ext: |
bdb7ae65c27c
extensions: don't suggest commands from deprecated extensions
Martin Geisler <mg@lazybytes.net>
parents:
16666
diff
changeset
|
959 # otherwise, interrogate each extension until there's a match |
48913
f254fc73d956
global: bulk replace simple pycompat.iteritems(x) with x.items()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48894
diff
changeset
|
960 for name, path in paths.items(): |
37974
b45f4c1532c0
extensions: extract closure that looks for commands from disabled module
Yuya Nishihara <yuya@tcha.org>
parents:
37973
diff
changeset
|
961 ext = _finddisabledcmd(ui, cmd, name, path, strict=strict) |
16667
bdb7ae65c27c
extensions: don't suggest commands from deprecated extensions
Martin Geisler <mg@lazybytes.net>
parents:
16666
diff
changeset
|
962 if ext: |
bdb7ae65c27c
extensions: don't suggest commands from deprecated extensions
Martin Geisler <mg@lazybytes.net>
parents:
16666
diff
changeset
|
963 break |
37973
5b60f7d652f2
extensions: drop dead code trying to exclude deprecated disabled commands
Yuya Nishihara <yuya@tcha.org>
parents:
37957
diff
changeset
|
964 if ext: |
16667
bdb7ae65c27c
extensions: don't suggest commands from deprecated extensions
Martin Geisler <mg@lazybytes.net>
parents:
16666
diff
changeset
|
965 return ext |
10364
de1e7099d100
dispatch: provide help for disabled extensions and commands
Brodie Rao <me+hg@dackz.net>
parents:
10363
diff
changeset
|
966 |
de1e7099d100
dispatch: provide help for disabled extensions and commands
Brodie Rao <me+hg@dackz.net>
parents:
10363
diff
changeset
|
967 raise error.UnknownCommand(cmd) |
de1e7099d100
dispatch: provide help for disabled extensions and commands
Brodie Rao <me+hg@dackz.net>
parents:
10363
diff
changeset
|
968 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
969 |
19769
83d79a00cc24
help: use full name of extensions to look up them for keyword search
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
18692
diff
changeset
|
970 def enabled(shortname=True): |
14530
cd31a1cc1521
extensions: update doc of enabled() and disabled() according to d5b525697ddb
Yuya Nishihara <yuya@tcha.org>
parents:
14415
diff
changeset
|
971 '''return a dict of {name: desc} of extensions''' |
8871
20a25042fadc
extensions: move extensions listing functions from mercurial.help
Cédric Duval <cedricduval@free.fr>
parents:
8225
diff
changeset
|
972 exts = {} |
20a25042fadc
extensions: move extensions listing functions from mercurial.help
Cédric Duval <cedricduval@free.fr>
parents:
8225
diff
changeset
|
973 for ename, ext in extensions(): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
974 doc = gettext(ext.__doc__) or _(b'(no help text available)') |
46690
90a92f041fc6
typing: add an assertion instead of blacklisting mercurial/extensions.py
Matt Harbison <matt_harbison@yahoo.com>
parents:
46094
diff
changeset
|
975 assert doc is not None # help pytype |
19769
83d79a00cc24
help: use full name of extensions to look up them for keyword search
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
18692
diff
changeset
|
976 if shortname: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
977 ename = ename.split(b'.')[-1] |
49026
2d519511c5c3
extensions: use new function for getting first line of string
Martin von Zweigbergk <martinvonz@google.com>
parents:
48946
diff
changeset
|
978 exts[ename] = stringutil.firstline(doc).strip() |
8871
20a25042fadc
extensions: move extensions listing functions from mercurial.help
Cédric Duval <cedricduval@free.fr>
parents:
8225
diff
changeset
|
979 |
14316
d5b525697ddb
extensions: drop maxlength from enabled and disabled
Matt Mackall <mpm@selenic.com>
parents:
14079
diff
changeset
|
980 return exts |
21848
ecdbbb6e5d06
version: show enabled extensions (issue4209)
anatoly techtonik <techtonik@gmail.com>
parents:
21795
diff
changeset
|
981 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
982 |
28155
7f430b2ac7fd
extensions: add notloaded method to return extensions failed to load
Jun Wu <quark@fb.com>
parents:
27990
diff
changeset
|
983 def notloaded(): |
7f430b2ac7fd
extensions: add notloaded method to return extensions failed to load
Jun Wu <quark@fb.com>
parents:
27990
diff
changeset
|
984 '''return short names of extensions that failed to load''' |
48913
f254fc73d956
global: bulk replace simple pycompat.iteritems(x) with x.items()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48894
diff
changeset
|
985 return [name for name, mod in _extensions.items() if mod is None] |
28155
7f430b2ac7fd
extensions: add notloaded method to return extensions failed to load
Jun Wu <quark@fb.com>
parents:
27990
diff
changeset
|
986 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
987 |
21848
ecdbbb6e5d06
version: show enabled extensions (issue4209)
anatoly techtonik <techtonik@gmail.com>
parents:
21795
diff
changeset
|
988 def moduleversion(module): |
ecdbbb6e5d06
version: show enabled extensions (issue4209)
anatoly techtonik <techtonik@gmail.com>
parents:
21795
diff
changeset
|
989 '''return version information from given module as a string''' |
50894
f75fd677cc05
extensions: use sysstr to check for attribute presence
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
50819
diff
changeset
|
990 if util.safehasattr(module, 'getversion') and callable(module.getversion): |
47055
553451522113
extensions: ignore exceptions from an extension's `getversion()` method
Matt Harbison <matt_harbison@yahoo.com>
parents:
46819
diff
changeset
|
991 try: |
553451522113
extensions: ignore exceptions from an extension's `getversion()` method
Matt Harbison <matt_harbison@yahoo.com>
parents:
46819
diff
changeset
|
992 version = module.getversion() |
553451522113
extensions: ignore exceptions from an extension's `getversion()` method
Matt Harbison <matt_harbison@yahoo.com>
parents:
46819
diff
changeset
|
993 except Exception: |
553451522113
extensions: ignore exceptions from an extension's `getversion()` method
Matt Harbison <matt_harbison@yahoo.com>
parents:
46819
diff
changeset
|
994 version = b'unknown' |
553451522113
extensions: ignore exceptions from an extension's `getversion()` method
Matt Harbison <matt_harbison@yahoo.com>
parents:
46819
diff
changeset
|
995 |
50894
f75fd677cc05
extensions: use sysstr to check for attribute presence
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
50819
diff
changeset
|
996 elif util.safehasattr(module, '__version__'): |
21848
ecdbbb6e5d06
version: show enabled extensions (issue4209)
anatoly techtonik <techtonik@gmail.com>
parents:
21795
diff
changeset
|
997 version = module.__version__ |
ecdbbb6e5d06
version: show enabled extensions (issue4209)
anatoly techtonik <techtonik@gmail.com>
parents:
21795
diff
changeset
|
998 else: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
999 version = b'' |
21848
ecdbbb6e5d06
version: show enabled extensions (issue4209)
anatoly techtonik <techtonik@gmail.com>
parents:
21795
diff
changeset
|
1000 if isinstance(version, (list, tuple)): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1001 version = b'.'.join(pycompat.bytestr(o) for o in version) |
46025
27c23c8f14da
extensions: avoid a crash when the version isn't properly byteified on py3
Matt Harbison <matt_harbison@yahoo.com>
parents:
45899
diff
changeset
|
1002 else: |
27c23c8f14da
extensions: avoid a crash when the version isn't properly byteified on py3
Matt Harbison <matt_harbison@yahoo.com>
parents:
45899
diff
changeset
|
1003 # version data should be bytes, but not all extensions are ported |
27c23c8f14da
extensions: avoid a crash when the version isn't properly byteified on py3
Matt Harbison <matt_harbison@yahoo.com>
parents:
45899
diff
changeset
|
1004 # to py3. |
27c23c8f14da
extensions: avoid a crash when the version isn't properly byteified on py3
Matt Harbison <matt_harbison@yahoo.com>
parents:
45899
diff
changeset
|
1005 version = stringutil.forcebytestr(version) |
21848
ecdbbb6e5d06
version: show enabled extensions (issue4209)
anatoly techtonik <techtonik@gmail.com>
parents:
21795
diff
changeset
|
1006 return version |
27990
96bfd2875213
version: verbose list internal and external extension source (issue4731)
liscju <piotr.listkiewicz@gmail.com>
parents:
27637
diff
changeset
|
1007 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42547
diff
changeset
|
1008 |
27990
96bfd2875213
version: verbose list internal and external extension source (issue4731)
liscju <piotr.listkiewicz@gmail.com>
parents:
27637
diff
changeset
|
1009 def ismoduleinternal(module): |
96bfd2875213
version: verbose list internal and external extension source (issue4731)
liscju <piotr.listkiewicz@gmail.com>
parents:
27637
diff
changeset
|
1010 exttestedwith = getattr(module, 'testedwith', None) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1011 return exttestedwith == b"ships-with-hg-core" |