changeset 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 230582ed7329
children 93301afac8d4
files treepkg/packager.py treepkg/subversion.py
diffstat 2 files changed, 88 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/treepkg/packager.py	Thu Jan 08 14:26:12 2009 +0000
+++ b/treepkg/packager.py	Thu Jan 08 16:55:18 2009 +0000
@@ -16,7 +16,7 @@
 import new
 
 import util
-import subversion
+from subversion import SvnRepository, SvnWorkingCopy
 import run
 import status
 import debian
@@ -322,7 +322,6 @@
                  signing_key_id="", do_build=True):
         self.name = name
         self.base_dir = base_dir
-        self.svn_url = svn_url
         self.builder = PBuilder(pbuilderrc, root_cmd,
                                 release_signing_keyid=signing_key_id)
         self.deb_email = deb_email
@@ -335,6 +334,9 @@
         self.pkg_dir_template = "%(revision)d-%(increment)d"
         self.pkg_dir_regex \
                    = re.compile(r"(?P<revision>[0-9]+)-(?P<increment>[0-9]+)$")
+        repo = SvnRepository(svn_url, self.svn_external_subdirs)
+        self.working_copy = SvnWorkingCopy(repo, self.checkout_dir,
+                                           logger=logging)
 
     checkout_dir = util.filenameproperty("checkout")
     debian_dir = util.filenameproperty("debian")
@@ -386,11 +388,7 @@
                             self.pkg_dir_template % locals())
 
     def last_changed_revision(self):
-        revisions = []
-        for directory in [self.checkout_dir] + self.svn_external_subdirs:
-            directory = os.path.join(self.checkout_dir, directory)
-            revisions.append(subversion.last_changed_revision(directory))
-        return max(revisions)
+        return self.working_copy.last_changed_revision()
 
     def get_revision_numbers(self):
         """Returns a list of the numbers of the packaged revisions"""
@@ -404,33 +402,17 @@
         return revisions
 
     def update_checkout(self, revision=None):
-        """Updates the working copy of self.svn_url in self.checkout_dir.
+        """Updates the working copy.
 
-        If self.checkout_dir doesn't exist yet, self.svn_url is checked
-        out into that directory.  The value of the revision parameter is
-        passed through to subversion.update.
+        If the checkout_dir doesn't exist yet, a new checkout is made
+        into that directory.  The value of the revision parameter is
+        passed through to the update method.
         """
-        localdir = self.checkout_dir
-        if os.path.exists(localdir):
-            logging.info("Updating the working copy in %r", localdir)
-            subversion.update(localdir, revision=revision)
-        else:
-            logging.info("The working copy in %r doesn't exist yet."
-                         "  Checking out from %r", localdir,
-                         self.svn_url)
-            subversion.checkout(self.svn_url, localdir)
+        self.working_copy.update_or_checkout(revision=revision)
 
     def export_sources(self, to_dir):
         logging.info("Exporting sources for tarball to %r", to_dir)
-        subversion.export(self.checkout_dir, to_dir)
-        # some versions of svn (notably version 1.4.2 shipped with etch)
-        # do not export externals such as the admin subdirectory.  We
-        # may have to do that in an extra step.
-        for subdir in self.svn_external_subdirs:
-            absdir = os.path.join(to_dir, subdir)
-            if not os.path.isdir(absdir):
-                subversion.export(os.path.join(self.checkout_dir, subdir),
-                                  absdir)
+        self.working_copy.export(to_dir)
 
     def copy_debian_directory(self, to_dir):
         logging.info("Copying debian directory to %r", to_dir)
--- 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)