[PATCH] Factorize more VC constructor code



Now that the previous bits are in, here is a big factorization patch.

By creating a new CachedVc class, we can remove a lot of
duplicated code from all VC plugins that use caching of the
underlying VC files status.

$ svn diff|diffstat
 _vc.py      |   21 +++++++++++++++++++++
 bzr.py      |   22 +++-------------------
 darcs.py    |   21 +++------------------
 git.py      |   22 +++-------------------
 monotone.py |   19 +++----------------
 tla.py      |   21 +++------------------
 6 files changed, 36 insertions(+), 90 deletions(-)

There are slight behaviour changes:

bzr, monotone & git used to have a get_tree() which
always called lookup_tree() if it was not already cached,
but without caching at that time either. I think this was
wrong and only cut'n'pasted between plugins. But
would like to hear from the creators of those plugins.

The remaining of the patch is mostly renaming and
passing an additional "directory" argument to
_lookup_tree_cache() as was required for bzr, but
which remains unused for other plugins.

Please review.

-- 
Vincent Legoll
Index: vc/darcs.py
===================================================================
--- vc/darcs.py	(revision 1344)
+++ vc/darcs.py	(working copy)
@@ -33,7 +33,7 @@
     "R": _vc.STATE_REMOVED
 }
 
-class Vc(_vc.Vc):
+class Vc(_vc.CachedVc):
 
     CMD = "darcs"
     NAME = "Darcs"
@@ -41,10 +41,6 @@
     PATCH_STRIP_NUM = 1
     PATCH_INDEX_RE = "--- old.+?/(.+?)\\t+.*[0-9]{4}$"
 
-    def __init__(self, location):
-        self._cached_statuses = None
-        super(Vc, self).__init__(location)
-
     def commit_command(self, message):
         return [self.CMD, "record",
                 "--skip-long-comment",
@@ -73,25 +69,14 @@
 
     def get_working_directory(self, workdir):
         return self.root
- 
-    def cache_inventory(self, rootdir):
-        self._cached_statuses = self._calculate_statuses()
 
-    def uncache_inventory(self):
-        self._cached_statuses = None
-
     def _get_dirsandfiles(self, directory, dirs, files):
-        whatsnew = self._get_cached_statuses()
+        whatsnew = self._get_tree_cache(directory)
         retfiles, retdirs = (self._get_statuses(whatsnew, files, _vc.File),
                              self._get_statuses(whatsnew, dirs, _vc.Dir))
         return retfiles, retdirs
 
-    def _get_cached_statuses(self):
-        if self._cached_statuses is None:
-            self._cached_statuses = self._calculate_statuses()
-        return self._cached_statuses
-    
-    def _calculate_statuses(self):
+    def _lookup_tree_cache(self, rootdir):
         non_boring = self._get_whatsnew()
         boring = self._get_whatsnew(boring=True)
         for path in boring:
Index: vc/bzr.py
===================================================================
--- vc/bzr.py	(revision 1344)
+++ vc/bzr.py	(working copy)
@@ -26,7 +26,7 @@
 import errno
 import _vc
 
-class Vc(_vc.Vc):
+class Vc(_vc.CachedVc):
 
     CMD = "bzr"
     NAME = "Bazaar-NG"
@@ -34,10 +34,6 @@
     PATCH_STRIP_NUM = 0
     PATCH_INDEX_RE = "^=== modified file '(.*)'$"
 
-    def __init__(self, location):
-        self._tree_cache = None
-        super(Vc, self).__init__(location)
-
     def commit_command(self, message):
         return [self.CMD,"commit","-m",message]
     def diff_command(self):
@@ -53,13 +49,7 @@
     def get_working_directory(self, workdir):
         return self.root
 
-    def cache_inventory(self, rootdir):
-        self._tree_cache = self.lookup_tree(rootdir)
-
-    def uncache_inventory(self):
-        self._tree_cache = None
-
-    def lookup_tree(self, rootdir):
+    def _lookup_tree_cache(self, rootdir):
         branch_root = os.popen("%s root %s" % (self.CMD, rootdir)).read().rstrip('\n')
         while 1:
             try:
@@ -88,15 +78,9 @@
                     tree_state[os.path.join(rootdir, entry[2:])] = cur_state
         return tree_state
 
-    def get_tree(self, directory):
-        if self._tree_cache is None:
-            return self.lookup_tree(directory)
-        else:
-            return self._tree_cache
-        
     def _get_dirsandfiles(self, directory, dirs, files):
 
-        tree = self.get_tree(directory)
+        tree = self._get_tree_cache(directory)
 
         retfiles = []
         retdirs = []
Index: vc/monotone.py
===================================================================
--- vc/monotone.py	(revision 1343)
+++ vc/monotone.py	(working copy)
@@ -26,14 +26,13 @@
 import _vc
 import errno
 
-class Vc(_vc.Vc):
+class Vc(_vc.CachedVc):
     NAME = "Monotone"
     VC_METADATA = ['MT', '_MTN']
     PATCH_STRIP_NUM = 0
     PATCH_INDEX_RE = "^[+]{3,3} ([^  ]*)\t[0-9a-f]{40,40}$"
 
     def __init__(self, location):
-        self._tree_cache = None
         self.interface_version = 0.0
         self.choose_monotone_version()
         super(Vc, self).__init__(os.path.normpath(location))
@@ -68,13 +67,7 @@
     def get_working_directory(self, workdir):
         return self.root
 
-    def cache_inventory(self, rootdir):
-        self._tree_cache = self.lookup_tree()
-
-    def uncache_inventory(self):
-        self._tree_cache = None
-
-    def lookup_tree(self):
+    def _lookup_tree_cache(self, rootdir):
         while 1:
             try:
                 entries = os.popen("cd %s && %s automate inventory" %
@@ -227,15 +220,9 @@
 
         return tree_state
 
-    def get_tree(self):
-        if self._tree_cache is None:
-            return self.lookup_tree()
-        else:
-            return self._tree_cache
-
     def _get_dirsandfiles(self, directory, dirs, files):
 
-        tree = self.get_tree()
+        tree = self._get_tree_cache(directory)
 
         retfiles = []
         retdirs = []
Index: vc/_vc.py
===================================================================
--- vc/_vc.py	(revision 1343)
+++ vc/_vc.py	(working copy)
@@ -163,3 +163,24 @@
 
     def _get_dirsandfiles(self, directory, dirs, files):
         raise NotImplementedError()
+
+
+class CachedVc(Vc):
+
+    def __init__(self, location):
+        super(CachedVc, self).__init__(location)
+        self._tree_cache = None
+
+    def cache_inventory(self, directory):
+        self._tree_cache = self._lookup_tree_cache(directory)
+
+    def uncache_inventory(self):
+        self._tree_cache = None
+
+    def _lookup_tree_cache(self, directory):
+        raise NotImplementedError()
+
+    def _get_tree_cache(self, directory):
+        if self._tree_cache is None:
+            self.cache_inventory(directory)
+        return self._tree_cache
Index: vc/tla.py
===================================================================
--- vc/tla.py	(revision 1344)
+++ vc/tla.py	(working copy)
@@ -57,7 +57,7 @@
     "-/": _vc.STATE_MODIFIED,
 }
 
-class Vc(_vc.Vc):
+class Vc(_vc.CachedVc):
 
     CMD = "tla"
     NAME = "Arch"
@@ -66,10 +66,6 @@
     PATCH_STRIP_NUM = 1
     PATCH_INDEX_RE = "--- orig/(.*)"
 
-    def __init__(self, location):
-        self._cached_statuses = None
-        super(Vc, self).__init__(location)
-
     def commit_command(self, message):
         return [self.CMD, "commit",
                 "-s", message]
@@ -92,25 +88,14 @@
 
     def get_working_directory(self, workdir):
         return self.root
- 
-    def cache_inventory(self, rootdir):
-        self._cached_statuses = self._calculate_statuses()
 
-    def uncache_inventory(self):
-        self._cached_statuses = None
-
     def _get_dirsandfiles(self, directory, dirs, files):
-        whatsnew = self._get_cached_statuses()
+        whatsnew = self._get_tree_cache(directory)
         retfiles, retdirs = (self._get_statuses(whatsnew, files, _vc.File),
                              self._get_statuses(whatsnew, dirs, _vc.Dir))
         return retfiles, retdirs
 
-    def _get_cached_statuses(self):
-        if self._cached_statuses is None:
-            self._cached_statuses = self._calculate_statuses()
-        return self._cached_statuses
-    
-    def _calculate_statuses(self):
+    def _lookup_tree_cache(self, rootdir):
         whatsnew = {}
         commandline = ('%s changes -d %s' % (self.CMD, self.root))
         while 1:
Index: vc/git.py
===================================================================
--- vc/git.py	(revision 1344)
+++ vc/git.py	(working copy)
@@ -31,7 +31,7 @@
 import errno
 import _vc
 
-class Vc(_vc.Vc):
+class Vc(_vc.CachedVc):
 
     CMD = "git"
     NAME = "Git"
@@ -39,10 +39,6 @@
     PATCH_STRIP_NUM = 1
     PATCH_INDEX_RE = "^diff --git a/(.*) b/.*$"
 
-    def __init__(self, location):
-        self._tree_cache = None
-        super(Vc, self).__init__(location)
-
     def commit_command(self, message):
         return [self.CMD,"commit","-m",message]
     def diff_command(self):
@@ -61,13 +57,7 @@
         else:
             return ''
 
-    def cache_inventory(self, topdir):
-        self._tree_cache = self.lookup_tree()
-
-    def uncache_inventory(self):
-        self._tree_cache = None
-
-    def lookup_tree(self):
+    def _lookup_tree_cache(self, rootdir):
         while 1:
             try:
                 proc = os.popen("cd %s && %s status --untracked-files" % (self.root, self.CMD))
@@ -110,15 +100,9 @@
                     tree_state[os.path.join(self.root, dst)] = _vc.STATE_NEW
         return tree_state
 
-    def get_tree(self):
-        if self._tree_cache is None:
-            return self.lookup_tree()
-        else:
-            return self._tree_cache
-
     def _get_dirsandfiles(self, directory, dirs, files):
 
-        tree = self.get_tree()
+        tree = self._get_tree_cache(directory)
 
         retfiles = []
         retdirs = []


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]