diff treepkg/subversion.py @ 224:6bac65dcf258

Handle the svn repositories and working copies in a more object oriented way
author Bernhard Herzog <bh@intevation.de>
date Thu, 08 Jan 2009 16:55:18 +0000
parents 1527c37bd7aa
children d2ddd037ddaf
line wrap: on
line diff
--- a/treepkg/subversion.py	Thu Jan 08 14:26:12 2009 +0000
+++ b/treepkg/subversion.py	Thu Jan 08 16:55:18 2009 +0000
@@ -1,11 +1,11 @@
-# Copyright (C) 2007, 2008 by Intevation GmbH
+# Copyright (C) 2007, 2008, 2009 by Intevation GmbH
 # Authors:
 # Bernhard Herzog <bh@intevation.de>
 #
 # This program is free software under the GPL (>=v2)
 # Read the file COPYING coming with the software for details.
 
-"""Collection of subversion utility functions"""
+"""Collection of subversion utility code"""
 
 import os
 
@@ -51,3 +51,78 @@
                                 env=env)
     return int(extract_value_for_key(output.splitlines(),
                                      "Last Changed Rev:"))
+
+
+class SvnRepository(object):
+
+    """Describes a subversion repository"""
+
+    def __init__(self, url, external_subdirs=()):
+        """Initialize the subversion repository description
+        Parameters:
+          url -- The url of the repository
+          external_subdirs -- A list of subdirectories which are managed
+                              by svn externals definitions
+        """
+        self.url = url
+        self.external_subdirs = external_subdirs
+
+    def checkout(self, localdir, revision=None):
+        """Checks out the repository into localdir.  The revision
+        parameter should be an and indicates the revision to check out.
+        """
+        checkout(self.url, localdir, revision=revision)
+
+    def export(self, localdir, destdir):
+        """Exports the working copy in localdir to destdir"""
+        export(localdir, destdir)
+        for subdir in self.external_subdirs:
+            absdir = os.path.join(destdir, subdir)
+            if not os.path.isdir(absdir):
+                subversion.export(os.path.join(localdir, subdir), absdir)
+
+    def last_changed_revision(self, localdir):
+        """Returns the last changed revision of the working copy in localdir"""
+        return max([last_changed_revision(os.path.join(localdir, d))
+                    for d in [localdir] + list(self.external_subdirs)])
+
+
+class SvnWorkingCopy(object):
+
+    """Represents a checkout of a subversion repository"""
+
+    def __init__(self, repository, localdir, logger=None):
+        """
+        Initialize the working copy.
+        Parameters:
+          repository -- The SvnRepository instance describing the
+                        repository
+          localdir -- The directory for the working copy
+          logger -- logging object to use for some info/debug messages
+        """
+        self.repository = repository
+        self.localdir = localdir
+        self.logger = logger
+
+    def log_info(self, *args):
+        if self.logger is not None:
+            self.logger.info(*args)
+
+    def update_or_checkout(self, revision=None):
+        """Updates the working copy or creates by checking out the repository"""
+        if os.path.exists(self.localdir):
+            self.log_info("Updating the working copy in %r", self.localdir)
+            update(self.localdir, revision=revision)
+        else:
+            self.log_info("The working copy in %r doesn't exist yet."
+                          "  Checking out from %r",
+                          self.localdir, self.repository.url)
+            self.repository.checkout(self.localdir, revision=revision)
+
+    def export(self, destdir):
+        """Exports the working copy to destdir"""
+        self.repository.export(self.localdir, destdir)
+
+    def last_changed_revision(self):
+        """Returns the last changed rev of the working copy"""
+        return self.repository.last_changed_revision(self.localdir)
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)