[Treepkg-commits] r247 - in trunk: . treepkg

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri Mar 12 10:13:34 CET 2010


Author: aheinecke
Date: 2010-03-12 10:13:33 +0100 (Fri, 12 Mar 2010)
New Revision: 247

Added:
   trunk/treepkg/git.py
Modified:
   trunk/README
   trunk/treepkg/packager.py
   trunk/treepkg/readconfig.py
Log:
Added basic Git support, configuration options:
git_url
git_branch



Modified: trunk/README
===================================================================
--- trunk/README	2009-12-21 13:21:40 UTC (rev 246)
+++ trunk/README	2010-03-12 09:13:33 UTC (rev 247)
@@ -2,7 +2,7 @@
 =======================
 
 
-TreePackager is a tool to automatically build debian packages from SVN.
+TreePackager is a tool to automatically build debian packages.
 
 
 Prerequisites
@@ -17,6 +17,7 @@
   Debian devscripts (devscripts)
   subversion
   pbuilder
+  git-core
   sudo
   bzip2
 

Added: trunk/treepkg/git.py
===================================================================
--- trunk/treepkg/git.py	2009-12-21 13:21:40 UTC (rev 246)
+++ trunk/treepkg/git.py	2010-03-12 09:13:33 UTC (rev 247)
@@ -0,0 +1,124 @@
+# Copyright (C) 2010 by Intevation GmbHi
+# Authors:
+# Andre Heinecke <aheinecke at intevation.de>
+#
+# This program is free software under the GPL (>=v2)
+# Read the file COPYING coming with the software for details.
+
+"""Collection of Git utility code"""
+
+import os
+import shutil
+import re
+import StringIO
+
+import run
+from cmdexpand import cmdexpand
+from util import extract_value_for_key
+
+
+class GitError(Exception):
+
+    """Base class for Git specific errors raised by TreePKG"""
+
+def checkout(url, localdir, branch=None):
+    """Clones the repository at url into the localdir"""
+    run.call(cmdexpand("git clone $url $localdir", **locals()))        
+    if branch:
+        run.call(cmdexpand("/bin/bash -c \"cd $localdir && git checkout --track -b local $branch\"",
+                            **locals()))
+    else:
+        run.call(cmdexpand("/bin/bash -c \"cd $localdir && git checkout --track -b local master\"",
+                            **locals()))
+
+def update(localdir, revision=None):
+    """Runs git pull on the localdir."""
+    run.call(cmdexpand("/bin/bash -c \"cd $localdir && git pull -q\"", **locals()))
+
+def export(src, dest):
+    """Exports the local branch from src to dest"""
+    run.call(cmdexpand("/bin/bash -c \"cd $src && git checkout-index -a -f --prefix=$dest\"", **locals()))
+
+def last_changed_revision(git_working_copy):
+    """return the last changed revision of a Git branch as a incrementing Number"""
+    output = run.capture_output(cmdexpand("/bin/bash -c \"cd $git_working_copy && git rev-list local | nl | tail -n 1 | awk \'{print $$1}\'\"", **locals()))
+    if output is None:
+        raise GitError("Cannot determine last changed revision for %r"
+                              % git_working_copy)
+    return int(output)
+
+class GitRepository(object):
+
+    """Describes a git repository"""
+
+    def __init__(self, url, branch=None):
+        """Initialize the git repository description
+        Parameters:
+          url -- The url of the repository
+
+          branch -- The name of the remote Branch to track
+                    defaults to master
+        """
+        self.url = url
+        self.branch = branch
+
+    def checkout(self, localdir):
+        """Checks out the repository into localdir."""
+        checkout(self.url , localdir, self.branch)
+        update(localdir)
+
+    def export(self, localdir, destdir):
+        """Exports the working copy in localdir to destdir"""
+        export(localdir, destdir)
+
+    def last_changed_revision(self, localdir):
+        """Returns the last changed revision of the working copy in localdir"""
+        return last_changed_revision(localdir)
+
+    def check_working_copy(self, localdir):
+        """
+	FIXME STUB: Not implemented for git
+        """
+        return None  
+
+class GitWorkingCopy(object):
+
+    """Represents a checkout of a git repository"""
+
+    def __init__(self, repository, localdir, logger=None):
+        """
+        Initialize the working copy.
+        Parameters:
+          repository -- The GitRepository 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=0):
+        """Updates the working copy or creates by checking out the repository.
+           Revision number included for compatibility
+        """
+        if os.path.exists(self.localdir):
+            self.log_info("Updating the working copy in %r", self.localdir)
+            update(self.localdir, self.repository.branch)
+        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)
+
+    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)

Modified: trunk/treepkg/packager.py
===================================================================
--- trunk/treepkg/packager.py	2009-12-21 13:21:40 UTC (rev 246)
+++ trunk/treepkg/packager.py	2010-03-12 09:13:33 UTC (rev 247)
@@ -17,6 +17,7 @@
 
 import util
 from subversion import SvnRepository, SvnWorkingCopy, ManualWorkingCopy
+from git import GitRepository, GitWorkingCopy
 import run
 import status
 import debian
@@ -340,14 +341,14 @@
 
     extra_config_desc = []
 
-    def __init__(self, name, base_dir, svn_url, root_cmd, pbuilderrc, deb_email,
-                 deb_fullname, packager_class="treepkg.packager",
+    def __init__(self, name, base_dir, root_cmd, pbuilderrc, deb_email,
+                 deb_fullname, svn_url="", packager_class="treepkg.packager",
                  version_template="%(revision)s",
                  pkg_revision_template="treepkg%(pkg_revision)d",
                  handle_dependencies=False, signing_key_id="", do_build=True,
                  rules_svn_url=None, deb_build_options="", pkg_basename="",
                  changelog_msg_template="Update to r%(revision)s",
-                 svn_subset=(), svn_externals=()):
+                 svn_subset=(), svn_externals=(), git_branch="", git_url=""):
         self.name = name
         if not pkg_basename:
             pkg_basename = name
@@ -371,9 +372,17 @@
         externals = svn_externals
         if not externals:
             externals = self.svn_external_subdirs
-        repo = SvnRepository(svn_url, externals, subset=svn_subset)
-        self.working_copy = SvnWorkingCopy(repo, self.checkout_dir,
-                                           logger=logging)
+        if svn_url:
+            repo = SvnRepository(svn_url, externals, subset=svn_subset)
+            self.working_copy = SvnWorkingCopy(repo, self.checkout_dir,
+                                              logger=logging)
+        elif git_url:
+            repo = GitRepository(git_url, branch=git_branch)
+            self.working_copy = GitWorkingCopy(repo, self.checkout_dir,
+                                              logger=logging) 
+        else:
+            print "No Repository URL for %s" % name
+            raise    
         if rules_svn_url:
             repo = SvnRepository(rules_svn_url)
             self.rules_working_copy = SvnWorkingCopy(repo, self.debian_dir,

Modified: trunk/treepkg/readconfig.py
===================================================================
--- trunk/treepkg/readconfig.py	2009-12-21 13:21:40 UTC (rev 246)
+++ trunk/treepkg/readconfig.py	2010-03-12 09:13:33 UTC (rev 247)
@@ -65,7 +65,8 @@
 
 packager_desc = [
     "name", "base_dir",
-    "svn_url", ("svn_subset", convert_subversion_subset, ""),
+    ("svn_url", str,""),
+    ("svn_subset", convert_subversion_subset, ""),
     ("svn_externals", shlex.split, ""),
     ("rules_svn_url", str, ""), "packager_class",
     ("root_cmd", shlex.split, "sudo"), "pbuilderrc",
@@ -75,6 +76,8 @@
     ("handle_dependencies", convert_bool),
     ("signing_key_id", str, ""),
     ("changelog_msg_template", str, "Update to revision %(revision)d"),
+    ("git_branch", str,""),
+    ("git_url", str,"")
     ]
 
 treepkg_desc = [



More information about the Treepkg-commits mailing list