Put git tree finding into a separate function.
[abartlet/samba.git/.git] / buildtools / wafsamba / samba_version.py
index 9832c79f280ce553afbca509c8bb48fdffa7f843..bddf917ba27fde6695c4889a807a688334f03ff8 100644 (file)
@@ -1,8 +1,34 @@
-import os;
-import subprocess;
+import Utils
+
+def git_version_summary(have_git):
+    # Get version from GIT
+    if not have_git:
+        return ("GIT-UNKNOWN", {})
+
+    git = Utils.cmd_output('git show --pretty=format:"%h%n%ct%n%H%n%cd" --stat HEAD')
+
+    lines = git.splitlines()
+    fields = {
+            "GIT_COMMIT_ABBREV": lines[0],
+            "GIT_COMMIT_TIME": lines[1],
+            "GIT_COMMIT_FULLREV": lines[2],
+            "GIT_COMMIT_DATE": lines[3],
+            }
+
+    ret = "GIT-" + fields["GIT_COMMIT_ABBREV"]
+
+    clean = Utils.cmd_output('git diff HEAD | wc -l', silent=True)
+    if clean == "0\n":
+        fields["GIT_COMMIT_IS_CLEAN"] = "1"
+    else:
+        fields["GIT_COMMIT_IS_CLEAN"] = "0"
+        ret += "+"
+    return (ret, fields)
+
 
 class samba_version(object):
-    def __init__(self, version_dict):
+
+    def __init__(self, version_dict, have_git=False):
         '''Determine the version number of samba
 
 See VERSION for the format.  Entries on that file are 
@@ -17,7 +43,7 @@ also accepted as dictionary entries here
         self.ALPHA_RELEASE=None
         self.PRE_RELEASE=None
         self.RC_RELEASE=None
-        self.IS_GIT_SNAPSHOT=True
+        self.IS_SNAPSHOT=True
         self.RELEASE_NICKNAME=None
         self.VENDOR_SUFFIX=None
         self.VENDOR_PATCH=None
@@ -28,11 +54,13 @@ also accepted as dictionary entries here
             else:
                 setattr(self, a, b)
 
-        if self.IS_GIT_SNAPSHOT is "yes":
-            self.IS_GIT_SNAPSHOT=True
-        elif self.IS_GIT_SNAPSHOT is "no":
-            self.IS_GIT_SNAPSHOT=False
-                
+        if self.IS_GIT_SNAPSHOT == "yes":
+            self.IS_SNAPSHOT=True
+        elif self.IS_GIT_SNAPSHOT == "no":
+            self.IS_SNAPSHOT=False
+        else:
+            raise Exception("Unknown value for IS_GIT_SNAPSHOT: %s" % self.IS_GIT_SNAPSHOT)
+
  ##
  ## start with "3.0.22"
  ##
@@ -50,7 +78,7 @@ also accepted as dictionary entries here
             SAMBA_VERSION_STRING += self.REVISION
         if self.TP_RELEASE is not None:
             self.TP_RELEASE = int(self.TP_RELEASE)
-            SAMBA_VERSION_STRING += ("tp%u" % self.TP_RELEASE)
+            SAMBA_VERSION_STRING += "tp%u" % self.TP_RELEASE
         if self.ALPHA_RELEASE is not None:
             self.ALPHA_RELEASE = int(self.ALPHA_RELEASE)
             SAMBA_VERSION_STRING += ("alpha%u" % self.ALPHA_RELEASE)
@@ -61,23 +89,13 @@ also accepted as dictionary entries here
             self.RC_RELEASE = int(self.RC_RELEASE)
             SAMBA_VERSION_STRING += ("rc%u" % self.RC_RELEASE)
 
-        if self.IS_GIT_SNAPSHOT:
-            #Get version from GIT
-            try:
-                git = subprocess.Popen('git show --pretty=format:"%h%n%ct%n%H%n%cd" --stat HEAD', stdout=subprocess.PIPE, close_fds=True, shell=True)
-                (output, errors) = git.communicate()
-                lines = output.splitlines();
-                self.GIT_COMMIT_ABBREV = lines[0]
-                self.GIT_COMMIT_TIME = lines[1]
-                self.GIT_COMMIT_FULLREV = lines[2]
-                self.GIT_COMMIT_DATE = lines[3]
-
-                SAMBA_VERSION_STRING += ("-GIT-" + self.GIT_COMMIT_ABBREV)
-            except IndexError:
-                SAMBA_VERSION_STRING += "-GIT-UNKNOWN"
-                pass
+        if self.IS_SNAPSHOT:
+            suffix, self.vcs_fields = git_version_summary(have_git)
+            SAMBA_VERSION_STRING += "-" + suffix
+        else:
+            self.vcs_fields = {}
 
-        self.OFFICIAL_STRING=SAMBA_VERSION_STRING
+        self.OFFICIAL_STRING = SAMBA_VERSION_STRING
 
         if self.VENDOR_SUFFIX is not None:
             SAMBA_VERSION_STRING += ("-" + self.VENDOR_SUFFIX)
@@ -94,7 +112,7 @@ also accepted as dictionary entries here
             self.RELEASE_NICKNAME = self.RELEASE_NICKNAME
         else:
             self.STRING_WITH_NICKNAME = self.STRING
-    
+
     def __str__(self):
         string="/* Autogenerated by waf */\n"
         string+="#define SAMBA_VERSION_MAJOR %u\n" % self.MAJOR
@@ -115,13 +133,8 @@ also accepted as dictionary entries here
         if self.RC_RELEASE is not None:
             string+="#define SAMBA_VERSION_RC_RELEASE %u\n" % self.RC_RELEASE
 
-        try:
-            string+="#define SAMBA_VERSION_GIT_COMMIT_ABBREV " + self.GIT_COMMIT_ABBREV + "\n"
-            string+="#define SAMBA_VERSION_GIT_COMMIT_TIME " + self.GIT_COMMIT_TIME + "\n"
-            string+="#define SAMBA_VERSION_GIT_COMMIT_FULLREV " + self.GIT_COMMIT_TIME + "\n"
-            string+="#define SAMBA_VERSION_GIT_COMMIT_DATE " + self.GIT_COMMIT_DATA + "\n"
-        except AttributeError:
-            pass
+        for name, value in self.vcs_fields.iteritems():
+            string+="#define SAMBA_VERSION_%s \"%s\"\n" % (name, value)
 
         string+="#define SAMBA_VERSION_OFFICIAL_STRING \"" + self.OFFICIAL_STRING + "\"\n"
 
@@ -147,24 +160,24 @@ also accepted as dictionary entries here
 
 
 class samba_version_file(samba_version):
-    def __init__(self, version_file):
+
+    def __init__(self, version_file, have_git=False):
         '''Parse the version information from a VERSION file'''
         f = open(version_file, 'r')
         version_dict = {}
         for line in f:
+            line = line.strip()
+            if line == '':
+                continue
+            if line.startswith("#"):
+                continue
             try:
-                line = line.strip()
-                if line == '':
-                    continue
-                if line.startswith("#"):
-                    continue
-                split_line=line.split("=")
+                split_line = line.split("=")
                 if split_line[1] != "":
                     value = split_line[1].strip('"')
                     version_dict[split_line[0]] = value
             except:
-                print "Failed to parse line %s from %s" % (line, version_file)
+                print("Failed to parse line %s from %s" % (line, version_file))
                 raise
-            
-        super(samba_version_file, self).__init__(version_dict)
-        
+
+        super(samba_version_file, self).__init__(version_dict, have_git=have_git)