Commits

Alain Leufroy  committed f8f345b

new config/mapping: config object now manage loading config

It will be different in the new format.

I've moved ``Gestrepo`` type definition to prevent import cycle.

  • Participants
  • Parent commits ef6a23c
  • Branches aleufroy/configfile

Comments (0)

Files changed (2)

File guestrepo/grconfig.py

 #
 # Author: alain.leufroy@logilab.fr
 """This module contains configuration metadata and data"""
+import os
+from mercurial import hg, util, scmutil, config, bookmarks
 
 def get_conf(ui, repo):
     """Return the right configuration handler"""
     return Configuration(ui, repo)
 
+class Guestrepo(object):
+    '''An aggregate representing a guest repository'''
+    def __init__(self, name, configpath, canonpath, uri, csid, root):
+        self.name = name
+        self.configpath = configpath
+        self.canonpath = canonpath
+        self.uri = uri
+        self.csid = csid
+        self.root = root
+
+    def isrepo(self):
+        return os.path.exists(os.path.join(self.root, '.hg'))
+
+    def get_repo(self, ui):
+        return hg.repository(ui, self.root, create=False)
+
+    def get_cset(self, ui, rev=None):
+        return self.get_repo(ui)[rev]
+
+    def to_dict(self, ui):
+        '''
+        Output all of the metadata associated with a Guestrepo as a dict
+        '''
+        guestrepo = self.get_repo(ui)
+        guestctx = guestrepo[None]
+
+        # Set up GR attributes in JSON
+        json_dict = {'path' : self.canonpath}
+        json_dict['branch'] = guestctx.branch()
+        json_dict['remote_name'] = self.name
+
+        parents = guestctx.parents()
+        json_dict['id'] = '+'.join(cset.hex() for cset in parents)[:12]
+
+        if guestctx.tags():
+            json_dict['tags'] = '/'.join(guestctx.tags())
+
+        if guestctx.bookmarks():
+            json_dict['bookmarks'] = '/'.join(guestctx.bookmarks())
+
+        current_bookmark = bookmarks.readcurrent(guestrepo)
+
+        if current_bookmark:
+            json_dict['current_bookmark'] = current_bookmark
+
+        if any(guestrepo.status()[:4]):
+            json_dict['changed'] = True
+
+        return json_dict
+
+    def to_mapping_entry(self, ui):
+        return "%s = %s" % self.configpath.ljust(10), self.name
+
+
 class Configuration(object):
     """Class that handles configuration meatadata and data"""
     configpath = '.hgguestrepo'
                                  guest.name.ljust(10),
                                  changeset_id)
 
+    def _config_entry_to_guest(self, configpath, value, pathauditor):
+        reporoot = self.repo.root
+
+        canonpath = scmutil.canonpath(reporoot,
+                                      reporoot,
+                                      configpath,
+                                      auditor=pathauditor)
+        if canonpath == '':
+            raise util.Abort("guest path '%s' refers to parent repository!" %
+                             configpath)
+        # parse the name, csid pair
+        split = value.rsplit(' ', 1)
+        name = split[0].rstrip(' ')
+        if len( split ) != 2:
+            raise util.Abort("guest at path '%s' has no changeset identifier" %
+                             configpath)
+        csid = split[1]
+        root = os.path.join(reporoot, canonpath)
+        return Guestrepo(name, configpath, canonpath, None, csid, root)
+
+
     def check_unmanaged(self):
         """Check if the configuration files are under version control.
         Simply print a warning if not.
         lines = ''.join(self._guest_to_config_line(guest, rev='.')
                         for guest in guests)
         self.repo.wwrite(filepath, lines, '')
+
+
+    def load_config(self, ctx, **opts):
+        """ Return a list of Guestrepo instance build from config file at
+        changetset context ctx.
+        """
+        hgconfig = config.config()
+        # Two choices: we can either mooch the local (uncommitted)
+        # changes (using --local), or we go for the local ctx
+        filepath = opts.get('frozen_file') or self.configpath
+        hgconfig.parse(filepath, ctx[filepath].data())
+        pathauditor = scmutil.pathauditor(self.repo.root)
+        guests = [self._config_entry_to_guest(configpath, value, pathauditor)
+                  for configpath, value in hgconfig[''].items()]
+        return guests

File guestrepo/guestrepo.py

 
 #####################
 
-class guestrepo(object):
-    '''An aggregate representing a guest repository'''
-    def __init__(self, name, configpath, canonpath, uri, csid, root):
-        self.name = name
-        self.configpath = configpath
-        self.canonpath = canonpath
-        self.uri = uri
-        self.csid = csid
-        self.root = root
-
-    def isrepo(self, warn=False):
-        return os.path.exists(os.path.join(self.root, '.hg'))
-
-    def to_dict(self, ui):
-        '''
-        Output all of the metadata associated with a Guestrepo as a dict
-        '''
-        guestrepo = hg.repository(ui, self.root, create=False)
-        guestctx = guestrepo[None]
-
-        # Set up GR attributes in JSON
-        json_dict = {'path' : self.canonpath}
-        json_dict['branch'] = guestctx.branch()
-        json_dict['remote_name'] = self.name
-
-        parents = guestctx.parents()
-        json_dict['id'] = (' '.join(['+'.join([node.hex(p.node()) for p in parents]), ]))[:12]
-
-        if guestctx.tags():
-            json_dict['tags'] = '/'.join(guestctx.tags())
-
-        if guestctx.bookmarks():
-            json_dict['bookmarks'] = '/'.join(guestctx.bookmarks())
-
-        current_bookmark = bookmarks.readcurrent(guestrepo)
-
-        if current_bookmark:
-            json_dict['current_bookmark'] = current_bookmark
-
-        if changed(guestrepo):
-            json_dict['changed'] = True
-
-        return json_dict
-
-    def get_cset(self, ui, rev=None):
-        guestrepo = hg.repository(ui, self.root, create=False)
-        return guestrepo[rev]
-
-    def to_mapping_entry(self, ui):
-        return "%s = %s" % self.configpath.ljust(10), self.name
-
-
 def showerrors(ui, workers):
     ''' Print errors that occurred during use of the worker pool'''
     if len(workers.errors) == 0:
        ctx = repo[None]
 
     conf = get_conf(ui, repo)
-    # parse .hgguestrepo file
-    if frozen_file:
-        guestconfig = readconfig(frozen_file, ctx)
-    else:
-       # Two choices: we can either mooch the local (uncommitted)
-       # changes (using --local), or we go for the local ctx
-
-       guestconfig = readconfig(conf.configpath, ctx)
-
-
-    guests = []
-    pathauditor = scmutil.pathauditor(repo.root)
-    for configpath, value in guestconfig[''].items():
-        canonpath = scmutil.canonpath(repo.root,
-                                      repo.root,
-                                      configpath,
-                                      auditor = pathauditor)
-        if canonpath == '':
-            raise util.Abort("guest path '%s' refers to parent repository!" %
-                                configpath)
-
-        # parse the name, csid pair
-        split = value.rsplit(' ', 1)
-        name = split[0].rstrip(' ')
-        if len( split ) != 2:
-            raise util.Abort("guest at path '%s' has no changeset identifier" %
-                                configpath)
-        csid = split[1]
-
-        root = os.path.join(repo.root, canonpath)
-
-        guests.append(guestrepo(name, configpath, canonpath, None, csid, root))
-
+    guests = conf.load_config(ctx)
     rejectnestedguests(guests)
 
     return guests
     stop = 4 # first entry to ignore
     if unknown:
         stop += 1
-    for filelist in repo.status(unknown=unknown)[:stop]:
-        if filelist:
-            return True
-    return False
+    return any(changes for changes in repo.status(unknown=unknown)[:stop])
 
 # This method creates and destroys lots of repos, better for memory, worse
 # for speed