waf: print the library name in which we search for a function
[samba.git] / buildtools / wafsamba / samba_version.py
index e72e4ae6f08f0a2a30cefe6d579aab8fa7fecd2e..f0e7b4d0caf0e2ae8ad01c7e5d00e18ff14bcc52 100644 (file)
@@ -1,59 +1,83 @@
-import os
-import Utils
+import os, sys
+from waflib import Utils, Context
+import samba_utils
+from samba_git import find_git
 
-def bzr_version_summary():
-    vi = dict([l.split(":", 1) for l in Utils.cmd_output("bzr version-info").splitlines()])
+def git_version_summary(path, env=None):
+    git = find_git(env)
 
-    ret = "BZR-%d" % (vi["revno"])
+    if git is None:
+        return ("GIT-UNKNOWN", {})
 
-    fields = {
-        "BZR_REVISION_ID": vi["revision-id"],
-        "BZR_REVNO": vi["revno"],
-        "BZR_DATE": vi["date"],
-        "BZR_BRANCH": vi["branch-nick"],
-        }
-
-    clean = Utils.cmd_output('bzr diff | wc -l', silent=True)
-    if clean == "0\n":
-        fields["BZR_COMMIT_IS_CLEAN"] = "1"
-    else:
-        fields["BZR_COMMIT_IS_CLEAN"] = "0"
-        ret += "+"
-    return (ret, fields)
+    env.GIT = git
 
+    environ = dict(os.environ)
+    environ["GIT_DIR"] = '%s/.git' % path
+    environ["GIT_WORK_TREE"] = path
+    git = samba_utils.get_string(Utils.cmd_output(env.GIT + ' show --pretty=format:"%h%n%ct%n%H%n%cd" --stat HEAD', silent=True, env=environ))
 
-def git_version_summary(have_git):
-    # Get version from GIT
-    if not have_git:
+    lines = git.splitlines()
+    if not lines or len(lines) < 4:
         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],
+            "COMMIT_TIME": int(lines[1]),
+            "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 += "+"
+    if env.GIT_LOCAL_CHANGES:
+        clean = Utils.cmd_output('%s diff HEAD | wc -l' % env.GIT, silent=True).strip()
+        if clean == "0":
+            fields["COMMIT_IS_CLEAN"] = 1
+        else:
+            fields["COMMIT_IS_CLEAN"] = 0
+            ret += "+"
+
     return (ret, fields)
 
 
-class samba_version(object):
+def distversion_version_summary(path):
+    #get version from .distversion file
+    suffix = None
+    fields = {}
+
+    for line in Utils.readf(path + '/.distversion').splitlines():
+        if line == '':
+            continue
+        if line.startswith("#"):
+            continue
+        try:
+            split_line = line.split("=")
+            if split_line[1] != "":
+                key = split_line[0]
+                value = split_line[1]
+                if key == "SUFFIX":
+                    suffix = value
+                    continue
+                fields[key] = value
+        except:
+            print("Failed to parse line %s from .distversion file." % (line))
+            raise
+
+    if "COMMIT_TIME" in fields:
+        fields["COMMIT_TIME"] = int(fields["COMMIT_TIME"])
+
+    if suffix is None:
+        return ("UNKNOWN", fields)
+
+    return (suffix, fields)
 
-    def __init__(self, version_dict, have_git=False):
+
+class SambaVersion(object):
+
+    def __init__(self, version_dict, path, env=None, is_install=True):
         '''Determine the version number of samba
 
-See VERSION for the format.  Entries on that file are 
+See VERSION for the format.  Entries on that file are
 also accepted as dictionary entries here
         '''
 
@@ -63,6 +87,7 @@ also accepted as dictionary entries here
         self.REVISION=None
         self.TP_RELEASE=None
         self.ALPHA_RELEASE=None
+        self.BETA_RELEASE=None
         self.PRE_RELEASE=None
         self.RC_RELEASE=None
         self.IS_SNAPSHOT=True
@@ -70,7 +95,7 @@ also accepted as dictionary entries here
         self.VENDOR_SUFFIX=None
         self.VENDOR_PATCH=None
 
-        for a, b in version_dict.iteritems():
+        for a, b in version_dict.items():
             if a.startswith("SAMBA_VERSION_"):
                 setattr(self, a[14:], b)
             else:
@@ -93,7 +118,7 @@ also accepted as dictionary entries here
         SAMBA_VERSION_STRING = ("%u.%u.%u" % (self.MAJOR, self.MINOR, self.RELEASE))
 
 ##
-## maybe add "3.0.22a" or "4.0.0tp11" or "4.0.0alpha1" or "3.0.22pre1" or "3.0.22rc1"
+## maybe add "3.0.22a" or "4.0.0tp11" or "4.0.0alpha1" or "4.0.0beta1" or "3.0.22pre1" or "3.0.22rc1"
 ## We do not do pre or rc version on patch/letter releases
 ##
         if self.REVISION is not None:
@@ -104,6 +129,9 @@ also accepted as dictionary entries here
         if self.ALPHA_RELEASE is not None:
             self.ALPHA_RELEASE = int(self.ALPHA_RELEASE)
             SAMBA_VERSION_STRING += ("alpha%u" % self.ALPHA_RELEASE)
+        if self.BETA_RELEASE is not None:
+            self.BETA_RELEASE = int(self.BETA_RELEASE)
+            SAMBA_VERSION_STRING += ("beta%u" % self.BETA_RELEASE)
         if self.PRE_RELEASE is not None:
             self.PRE_RELEASE = int(self.PRE_RELEASE)
             SAMBA_VERSION_STRING += ("pre%u" % self.PRE_RELEASE)
@@ -112,13 +140,17 @@ also accepted as dictionary entries here
             SAMBA_VERSION_STRING += ("rc%u" % self.RC_RELEASE)
 
         if self.IS_SNAPSHOT:
-            if os.path.exists(".git"):
-                suffix, self.vcs_fields = git_version_summary(have_git)
-            elif os.path.exists(".bzr"):
-                suffix, self.vcs_fields = bzr_version_summary()
+            if not is_install:
+                suffix = "DEVELOPERBUILD"
+                self.vcs_fields = {}
+            elif os.path.exists(os.path.join(path, ".git")):
+                suffix, self.vcs_fields = git_version_summary(path, env=env)
+            elif os.path.exists(os.path.join(path, ".distversion")):
+                suffix, self.vcs_fields = distversion_version_summary(path)
             else:
                 suffix = "UNKNOWN"
                 self.vcs_fields = {}
+            self.vcs_fields["SUFFIX"] = suffix
             SAMBA_VERSION_STRING += "-" + suffix
         else:
             self.vcs_fields = {}
@@ -136,8 +168,7 @@ also accepted as dictionary entries here
         self.STRING = SAMBA_VERSION_STRING
 
         if self.RELEASE_NICKNAME is not None:
-            self.STRING_WITH_NICKNAME += (" (" + self.RELEASE_NICKNAME + ")")
-            self.RELEASE_NICKNAME = self.RELEASE_NICKNAME
+            self.STRING_WITH_NICKNAME = "%s (%s)" % (self.STRING, self.RELEASE_NICKNAME)
         else:
             self.STRING_WITH_NICKNAME = self.STRING
 
@@ -155,14 +186,28 @@ also accepted as dictionary entries here
         if self.ALPHA_RELEASE is not None:
             string+="#define SAMBA_VERSION_ALPHA_RELEASE %u\n" % self.ALPHA_RELEASE
 
+        if self.BETA_RELEASE is not None:
+            string+="#define SAMBA_VERSION_BETA_RELEASE %u\n" % self.BETA_RELEASE
+
         if self.PRE_RELEASE is not None:
             string+="#define SAMBA_VERSION_PRE_RELEASE %u\n" % self.PRE_RELEASE
 
         if self.RC_RELEASE is not None:
             string+="#define SAMBA_VERSION_RC_RELEASE %u\n" % self.RC_RELEASE
 
-        for name, value in self.vcs_fields.iteritems():
-            string+="#define SAMBA_VERSION_%s \"%s\"\n" % (name, value)
+        for name in sorted(self.vcs_fields.keys()):
+            string+="#define SAMBA_VERSION_%s " % name
+            value = self.vcs_fields[name]
+            string_types = str
+            if sys.version_info[0] < 3:
+                string_types = basestring
+            if isinstance(value, string_types):
+                string += "\"%s\"" % value
+            elif type(value) is int:
+                string += "%d" % value
+            else:
+                raise Exception("Unknown type for %s: %r" % (name, value))
+            string += "\n"
 
         string+="#define SAMBA_VERSION_OFFICIAL_STRING \"" + self.OFFICIAL_STRING + "\"\n"
 
@@ -187,25 +232,36 @@ also accepted as dictionary entries here
         return string
 
 
-class samba_version_file(samba_version):
-
-    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:
-                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))
-                raise
-
-        super(samba_version_file, self).__init__(version_dict, have_git=have_git)
+def samba_version_file(version_file, path, env=None, is_install=True):
+    '''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:
+            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))
+            raise
+
+    return SambaVersion(version_dict, path, env=env, is_install=is_install)
+
+
+
+def load_version(env=None, is_install=True):
+    '''load samba versions either from ./VERSION or git
+    return a version object for detailed breakdown'''
+    if not env:
+        env = samba_utils.LOAD_ENVIRONMENT()
+
+    version = samba_version_file("./VERSION", ".", env, is_install=is_install)
+    Context.g_module.VERSION = version.STRING
+    return version