changeset 47:916b05f73b53

preliminary impl of groups
author Marcin Kasperski <Marcin.Kasperski@mekk.waw.pl>
date Sun, 25 Sep 2016 10:22:37 +0200
parents e695060c716e
children 00995da9c204
files mercurial_all_paths.py
diffstat 1 files changed, 57 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial_all_paths.py	Sun Sep 25 09:44:23 2016 +0200
+++ b/mercurial_all_paths.py	Sun Sep 25 10:22:37 2016 +0200
@@ -1,3 +1,5 @@
+# -*- coding: utf-8 -*-
+#
 # allpaths.py - execute commands on multiple paths
 #
 # This software may be used and distributed according to the terms of
@@ -12,17 +14,68 @@
 
 # pylint:disable=invalid-name,broad-except,line-too-long
 
+def _find_all_paths(ui, skip_ignored=False, sort_by_priority=False):
+    """
+    Finds all paths defined for repo
+    :return: list of pairs (alias, path)
+    """
+    paths = ui.configitems(group)
+    if not paths:
+        raise mercurial.util.Abort(_('No paths defined for repository'))
+
+    if skip_ignored:
+        ignored = ui.configlist("all_paths", "ignore")
+        paths = [(alias, path) for alias, path in paths if alias not in ignored]
+        if not paths:
+            raise mercurial.util.Abort(_('All paths defined for this repository are ignored'))
+
+    if sort_by_priority:
+        #priority = ui.configlist("all_paths", "priority")
+        #paths.sort(…)
+        pass # TODO
+
+    return paths
+
+def _find_paths(ui, group=None):
+    """
+    Finds and returns all paths defined in given group, or all paths
+    (sans config) if group is not specified.
+
+    :param ui: repository ui
+    :param group: group name or None for all paths
+    :return: list of pairs (alias, path)
+    """
+    if not group:
+        return _find_all_paths(ui, skip_ignored=True, sort_by_priority=True)
+
+    # „Modern” syntax
+    grp_def = ui.configlist("all_paths", "group." + group)
+    if grp_def:
+        all_paths = dict(_find_all_paths(ui))
+        paths = []
+        for item in grp_def:
+            if item in all_paths:
+                paths.append((item, all_paths[item]))
+        if not paths:
+            raise mercurial.util.Abort(_('None of the paths from group %s is defined in this repository') % group)
+            
+        return paths
+
+    # „Legacy” syntax, used also for all paths
+    paths = ui.configitems(group)
+    if not paths:
+        raise mercurial.util.Abort(_('No paths defined in section %s') % group)
+    return paths
+       
 
 def _iter_over_paths(command, ui, repo, add_sep, **opts):
     """execute given command on multiple paths"""
     # Extract our options and filter them out
-    group = opts.pop('group', None) or 'paths'
+    group = opts.pop('group', None)
     ignore_errors = opts.pop('ignore_errors', None)
 
     # Get the paths to push to.
-    paths = ui.configitems(group)
-    if not paths:
-        raise mercurial.util.Abort(_('No paths defined in section %s') % group)
+    paths = _find_paths(ui, group)
 
     # Used to avoid handling duplicate paths twice
     handled = {}