comparison mercurial_all_paths.py @ 73:d262139732f7

Fixed to work against mercurial >= 4.1 (which requires using @command decorator for commands). (internally using mercurial_extension_utils to define command portably)
author Marcin Kasperski <Marcin.Kasperski@mekk.waw.pl>
date Thu, 18 May 2017 17:36:19 +0200
parents 95b5ba2cd363
children 03cc0603800e
comparison
equal deleted inserted replaced
72:29fa6cfad89b 73:d262139732f7
9 9
10 import mercurial.util 10 import mercurial.util
11 import mercurial.commands 11 import mercurial.commands
12 import mercurial.cmdutil 12 import mercurial.cmdutil
13 from mercurial.i18n import _ 13 from mercurial.i18n import _
14
15
16 def import_meu():
17 """Importing mercurial_extension_utils so it can be found also outside
18 Python PATH (support for TortoiseHG/Win and similar setups)"""
19 try:
20 import mercurial_extension_utils
21 except ImportError:
22 my_dir = os.path.dirname(__file__)
23 sys.path.extend([
24 # In the same dir (manual or site-packages after pip)
25 my_dir,
26 # Developer clone
27 os.path.join(os.path.dirname(my_dir), "extension_utils"),
28 # Side clone
29 os.path.join(os.path.dirname(my_dir), "mercurial-extension_utils"),
30 ])
31 try:
32 import mercurial_extension_utils
33 except ImportError:
34 raise util.Abort(_("""Can not import mercurial_extension_utils.
35 Please install this module in Python path.
36 See Installation chapter in https://bitbucket.org/Mekk/mercurial-dynamic_username/ for details
37 (and for info about TortoiseHG on Windows, or other bundled Python)."""))
38 return mercurial_extension_utils
39
40 meu = import_meu()
41
14 42
15 # pylint:disable=invalid-name,broad-except,line-too-long 43 # pylint:disable=invalid-name,broad-except,line-too-long
16 44
17 def _find_all_paths(ui, skip_ignored=False, sort_by_priority=False): 45 def _find_all_paths(ui, skip_ignored=False, sort_by_priority=False):
18 """ 46 """
36 prior_val = {} 64 prior_val = {}
37 for idx, item in enumerate(prior): 65 for idx, item in enumerate(prior):
38 prior_val[item] = idx 66 prior_val[item] = idx
39 higher = len(prior) 67 higher = len(prior)
40 paths.sort(key = lambda it: prior_val.get(it[0], higher)) 68 paths.sort(key = lambda it: prior_val.get(it[0], higher))
41 69
42 return paths 70 return paths
43 71
44 def _find_paths(ui, group=None): 72 def _find_paths(ui, group=None):
45 """ 73 """
46 Finds and returns all paths defined in given group, or all paths 74 Finds and returns all paths defined in given group, or all paths
61 for item in grp_def: 89 for item in grp_def:
62 if item in all_paths: 90 if item in all_paths:
63 paths.append((item, all_paths[item])) 91 paths.append((item, all_paths[item]))
64 if not paths: 92 if not paths:
65 raise mercurial.util.Abort(_('None of the paths from group %s is defined in this repository') % group) 93 raise mercurial.util.Abort(_('None of the paths from group %s is defined in this repository') % group)
66 94
67 return paths 95 return paths
68 96
69 # „Legacy” syntax, used also for all paths 97 # „Legacy” syntax, used also for all paths
70 paths = ui.configitems(group) 98 paths = ui.configitems(group)
71 if not paths: 99 if not paths:
72 raise mercurial.util.Abort(_('No paths defined in section %s') % group) 100 raise mercurial.util.Abort(_('No paths defined in section %s') % group)
73 return paths 101 return paths
74 102
75 103
76 def _iter_over_paths(command, ui, repo, add_sep, **opts): 104 def _iter_over_paths(command, ui, repo, add_sep, **opts):
77 """execute given command on multiple paths""" 105 """execute given command on multiple paths"""
78 # Extract our options and filter them out 106 # Extract our options and filter them out
79 group = opts.pop('group', None) 107 group = opts.pop('group', None)
102 if not ignore_errors: 130 if not ignore_errors:
103 raise 131 raise
104 ui.warn(_('error handling %s: %s\n') % (alias, e)) 132 ui.warn(_('error handling %s: %s\n') % (alias, e))
105 sep = '\n' 133 sep = '\n'
106 134
107 def pushall(ui, repo, **opts):
108 """execute push on multiple paths"""
109 _iter_over_paths(mercurial.commands.push, ui, repo, True, **opts)
110 135
111 136 EXT_OPTS = [
112 def pullall(ui, repo, **opts): 137 ('g', 'group', '', _('use a named group instead of all paths')),
113 """execute pull on multiple paths""" 138 ('', 'ignore-errors', None, _('continue execution despite errors')),
114 _iter_over_paths(mercurial.commands.pull, ui, repo, True, **opts) 139 ]
115
116
117 def incomingall(ui, repo, **opts):
118 """execute incoming on multiple paths"""
119 _iter_over_paths(mercurial.commands.incoming, ui, repo, False, **opts)
120
121
122 def outgoingall(ui, repo, **opts):
123 """execute outgoing on multiple paths"""
124 _iter_over_paths(mercurial.commands.outgoing, ui, repo, False, **opts)
125 140
126 141
127 def _original_options(cmdname): 142 def _original_options(cmdname):
128 """Gets list of given command options as specified in Mercurial core""" 143 """Gets list of given command options as specified in Mercurial core"""
129 _, spec = mercurial.cmdutil.findcmd(cmdname, mercurial.commands.table) 144 _, spec = mercurial.cmdutil.findcmd(cmdname, mercurial.commands.table)
130 return spec[1] 145 return spec[1]
131 146
132 147
133 EXT_OPTS = [ 148 cmdtable = {}
134 ('g', 'group', '', _('use a named group instead of all paths')), 149 command = meu.command(cmdtable)
135 ('', 'ignore-errors', None, _('continue execution despite errors')),
136 ]
137 150
138 cmdtable = {
139 "pushall": (
140 pushall,
141 EXT_OPTS + _original_options('push'),
142 _('[-g GROUP] [--ignore-errors] <push options>')),
143 "pullall": (
144 pullall,
145 EXT_OPTS + _original_options('pull'),
146 _('[-g GROUP] [--ignore-errors] <pull options>')),
147 # For incoming and outgoing -g is taken (--git diff format)
148 "incomingall": (
149 incomingall,
150 EXT_OPTS + _original_options('incoming'),
151 _('[--group GROUP] [--ignore-errors] <incoming options>')),
152 "outgoingall": (
153 outgoingall,
154 EXT_OPTS + _original_options('outgoing'),
155 _('[--group GROUP] [--ignore-errors] <outgoing options>')),
156 }
157 151
158 testedwith = '2.7 2.9 3.0 3.3 3.6 3.7 3.8 4.0' 152 @command("pushall",
153 EXT_OPTS + _original_options('push'),
154 _('[-g GROUP] [--ignore-errors] <push options>'))
155 def pushall(ui, repo, **opts):
156 """execute push on multiple paths"""
157 _iter_over_paths(mercurial.commands.push, ui, repo, True, **opts)
158
159
160 @command("pullall",
161 EXT_OPTS + _original_options('pull'),
162 _('[-g GROUP] [--ignore-errors] <pull options>'))
163 def pullall(ui, repo, **opts):
164 """execute pull on multiple paths"""
165 _iter_over_paths(mercurial.commands.pull, ui, repo, True, **opts)
166
167
168 @command("incomingall",
169 EXT_OPTS + _original_options('incoming'),
170 _('[--group GROUP] [--ignore-errors] <incoming options>'))
171 def incomingall(ui, repo, **opts):
172 """execute incoming on multiple paths"""
173 _iter_over_paths(mercurial.commands.incoming, ui, repo, False, **opts)
174
175
176 @command("outgoingall",
177 EXT_OPTS + _original_options('outgoing'),
178 _('[--group GROUP] [--ignore-errors] <outgoing options>'))
179 def outgoingall(ui, repo, **opts):
180 """execute outgoing on multiple paths"""
181 _iter_over_paths(mercurial.commands.outgoing, ui, repo, False, **opts)
182
183
184 testedwith = '2.7 2.9 3.0 3.3 3.6 3.7 3.8 4.0 4.1 4.2'
159 buglink = 'https://bitbucket.org/Mekk/mercurial-all_paths/issues' 185 buglink = 'https://bitbucket.org/Mekk/mercurial-all_paths/issues'