comparison mercurial_all_paths.py @ 47:916b05f73b53

preliminary impl of groups
author Marcin Kasperski <Marcin.Kasperski@mekk.waw.pl>
date Sun, 25 Sep 2016 10:22:37 +0200
parents 237dd8c4fe78
children 00995da9c204
comparison
equal deleted inserted replaced
46:e695060c716e 47:916b05f73b53
1 # -*- coding: utf-8 -*-
2 #
1 # allpaths.py - execute commands on multiple paths 3 # allpaths.py - execute commands on multiple paths
2 # 4 #
3 # This software may be used and distributed according to the terms of 5 # This software may be used and distributed according to the terms of
4 # the GNU General Public License version 2 or any later version. 6 # the GNU General Public License version 2 or any later version.
5 7
10 import mercurial.cmdutil 12 import mercurial.cmdutil
11 from mercurial.i18n import _ 13 from mercurial.i18n import _
12 14
13 # pylint:disable=invalid-name,broad-except,line-too-long 15 # pylint:disable=invalid-name,broad-except,line-too-long
14 16
17 def _find_all_paths(ui, skip_ignored=False, sort_by_priority=False):
18 """
19 Finds all paths defined for repo
20 :return: list of pairs (alias, path)
21 """
22 paths = ui.configitems(group)
23 if not paths:
24 raise mercurial.util.Abort(_('No paths defined for repository'))
25
26 if skip_ignored:
27 ignored = ui.configlist("all_paths", "ignore")
28 paths = [(alias, path) for alias, path in paths if alias not in ignored]
29 if not paths:
30 raise mercurial.util.Abort(_('All paths defined for this repository are ignored'))
31
32 if sort_by_priority:
33 #priority = ui.configlist("all_paths", "priority")
34 #paths.sort(…)
35 pass # TODO
36
37 return paths
38
39 def _find_paths(ui, group=None):
40 """
41 Finds and returns all paths defined in given group, or all paths
42 (sans config) if group is not specified.
43
44 :param ui: repository ui
45 :param group: group name or None for all paths
46 :return: list of pairs (alias, path)
47 """
48 if not group:
49 return _find_all_paths(ui, skip_ignored=True, sort_by_priority=True)
50
51 # „Modern” syntax
52 grp_def = ui.configlist("all_paths", "group." + group)
53 if grp_def:
54 all_paths = dict(_find_all_paths(ui))
55 paths = []
56 for item in grp_def:
57 if item in all_paths:
58 paths.append((item, all_paths[item]))
59 if not paths:
60 raise mercurial.util.Abort(_('None of the paths from group %s is defined in this repository') % group)
61
62 return paths
63
64 # „Legacy” syntax, used also for all paths
65 paths = ui.configitems(group)
66 if not paths:
67 raise mercurial.util.Abort(_('No paths defined in section %s') % group)
68 return paths
69
15 70
16 def _iter_over_paths(command, ui, repo, add_sep, **opts): 71 def _iter_over_paths(command, ui, repo, add_sep, **opts):
17 """execute given command on multiple paths""" 72 """execute given command on multiple paths"""
18 # Extract our options and filter them out 73 # Extract our options and filter them out
19 group = opts.pop('group', None) or 'paths' 74 group = opts.pop('group', None)
20 ignore_errors = opts.pop('ignore_errors', None) 75 ignore_errors = opts.pop('ignore_errors', None)
21 76
22 # Get the paths to push to. 77 # Get the paths to push to.
23 paths = ui.configitems(group) 78 paths = _find_paths(ui, group)
24 if not paths:
25 raise mercurial.util.Abort(_('No paths defined in section %s') % group)
26 79
27 # Used to avoid handling duplicate paths twice 80 # Used to avoid handling duplicate paths twice
28 handled = {} 81 handled = {}
29 # Used to add extra newline between items 82 # Used to add extra newline between items
30 sep = '' 83 sep = ''