Don't get VCS version in VCS.__init__().
authorW. Trevor King <wking@drexel.edu>
Fri, 4 Dec 2009 02:37:29 +0000 (21:37 -0500)
committerW. Trevor King <wking@drexel.edu>
Fri, 4 Dec 2009 02:37:29 +0000 (21:37 -0500)
Often, this just causes a slow subprocess.Popen() call to
get information we woln't even look at.

Old benchmark:
  $ time be list > /dev/null

  real    0m2.369s
  user    0m1.980s
  sys     0m0.388s
New benchmark:
  $ time be list > /dev/null

  real    0m1.472s
  user    0m1.304s
  sys     0m0.164s

libbe/vcs.py

index 1298a2c48968a4be96f3f6425c1b360f5bf6532a..44643a4be286e1544d9af6c4bb8d324ded58d632 100644 (file)
@@ -127,7 +127,6 @@ class VCS(object):
         self._duplicateBasedir = None
         self._duplicateDirname = None
         self.encoding = encoding
-        self.version = self._get_version()
     def __str__(self):
         return "<%s %s>" % (self.__class__.__name__, id(self))
     def __repr__(self):
@@ -235,6 +234,11 @@ class VCS(object):
         specified revision does not exist.
         """
         return None
+    def version(self):
+        """Cache version string for efficiency."""
+        if not hasattr(self, '_version'):
+            self._version = self._get_version()
+        return self._version
     def _get_version(self):
         try:
             ret = self._vcs_version()
@@ -247,7 +251,7 @@ class VCS(object):
         except CommandError:
             return None
     def installed(self):
-        if self.version != None:
+        if self.version() != None:
             return True
         return False
     def detect(self, path="."):