samba_version: When working from git checkout, display git revision SHA1 rather
[kai/samba-autobuild/.git] / buildtools / wafsamba / samba_version.py
1 import os
2 import Utils
3
4 def bzr_version_summary(path):
5     try:
6         from bzrlib import branch, osutils
7     except ImportError:
8         return ("BZR-UNKNOWN", {})
9
10     from bzrlib.plugin import load_plugins
11     load_plugins()
12
13     b = branch.Branch.open(path)
14     (revno, revid) = b.last_revision_info()
15     rev = b.repository.get_revision(revid)
16
17     fields = {
18         "BZR_REVISION_ID": revid,
19         "BZR_REVNO": str(revno),
20         "COMMIT_DATE": osutils.format_date_with_offset_in_original_timezone(rev.timestamp,
21             rev.timezone or 0),
22         "COMMIT_TIME": int(rev.timestamp),
23         "BZR_BRANCH": rev.properties.get("branch-nick", ""),
24         }
25
26     # If possible, retrieve the git sha
27     try:
28         from bzrlib.plugins.git.object_store import get_object_store
29     except ImportError:
30         # No git plugin
31         ret = "BZR-%d" % revno
32     else:
33         store = get_object_store(b.repository)
34         full_rev = store._lookup_revision_sha1(revid)
35         fields["GIT_COMMIT_ABBREV"] = full_rev[:7]
36         fields["GIT_COMMIT_FULLREV"] = full_rev
37         ret = "GIT-" + fields["GIT_COMMIT_ABBREV"]
38
39     clean = Utils.cmd_output('bzr diff | wc -l', silent=True)
40     if clean == "0\n":
41         fields["COMMIT_IS_CLEAN"] = "1"
42     else:
43         fields["COMMIT_IS_CLEAN"] = "0"
44         ret += "+"
45     return (ret, fields)
46
47
48 def git_version_summary(path, have_git):
49     # Get version from GIT
50     if not have_git:
51         return ("GIT-UNKNOWN", {})
52
53     git = Utils.cmd_output('GIT_DIR=%s/.git git show --pretty=format:"%h%n%ct%n%H%n%cd" --stat HEAD' % path)
54
55     lines = git.splitlines()
56     fields = {
57             "GIT_COMMIT_ABBREV": lines[0],
58             "GIT_COMMIT_FULLREV": lines[2],
59             "COMMIT_TIME": lines[1],
60             "COMMIT_DATE": lines[3],
61             }
62
63     ret = "GIT-" + fields["GIT_COMMIT_ABBREV"]
64
65     clean = Utils.cmd_output('git diff HEAD | wc -l', silent=True)
66     if clean == "0\n":
67         fields["COMMIT_IS_CLEAN"] = "1"
68     else:
69         fields["COMMIT_IS_CLEAN"] = "0"
70         ret += "+"
71     return (ret, fields)
72
73
74 class SambaVersion(object):
75
76     def __init__(self, version_dict, path, have_git=False):
77         '''Determine the version number of samba
78
79 See VERSION for the format.  Entries on that file are 
80 also accepted as dictionary entries here
81         '''
82
83         self.MAJOR=None
84         self.MINOR=None
85         self.RELEASE=None
86         self.REVISION=None
87         self.TP_RELEASE=None
88         self.ALPHA_RELEASE=None
89         self.PRE_RELEASE=None
90         self.RC_RELEASE=None
91         self.IS_SNAPSHOT=True
92         self.RELEASE_NICKNAME=None
93         self.VENDOR_SUFFIX=None
94         self.VENDOR_PATCH=None
95
96         for a, b in version_dict.iteritems():
97             if a.startswith("SAMBA_VERSION_"):
98                 setattr(self, a[14:], b)
99             else:
100                 setattr(self, a, b)
101
102         if self.IS_GIT_SNAPSHOT == "yes":
103             self.IS_SNAPSHOT=True
104         elif self.IS_GIT_SNAPSHOT == "no":
105             self.IS_SNAPSHOT=False
106         else:
107             raise Exception("Unknown value for IS_GIT_SNAPSHOT: %s" % self.IS_GIT_SNAPSHOT)
108
109  ##
110  ## start with "3.0.22"
111  ##
112         self.MAJOR=int(self.MAJOR)
113         self.MINOR=int(self.MINOR)
114         self.RELEASE=int(self.RELEASE)
115
116         SAMBA_VERSION_STRING = ("%u.%u.%u" % (self.MAJOR, self.MINOR, self.RELEASE))
117
118 ##
119 ## maybe add "3.0.22a" or "4.0.0tp11" or "4.0.0alpha1" or "3.0.22pre1" or "3.0.22rc1"
120 ## We do not do pre or rc version on patch/letter releases
121 ##
122         if self.REVISION is not None:
123             SAMBA_VERSION_STRING += self.REVISION
124         if self.TP_RELEASE is not None:
125             self.TP_RELEASE = int(self.TP_RELEASE)
126             SAMBA_VERSION_STRING += "tp%u" % self.TP_RELEASE
127         if self.ALPHA_RELEASE is not None:
128             self.ALPHA_RELEASE = int(self.ALPHA_RELEASE)
129             SAMBA_VERSION_STRING += ("alpha%u" % self.ALPHA_RELEASE)
130         if self.PRE_RELEASE is not None:
131             self.PRE_RELEASE = int(self.PRE_RELEASE)
132             SAMBA_VERSION_STRING += ("pre%u" % self.PRE_RELEASE)
133         if self.RC_RELEASE is not None:
134             self.RC_RELEASE = int(self.RC_RELEASE)
135             SAMBA_VERSION_STRING += ("rc%u" % self.RC_RELEASE)
136
137         if self.IS_SNAPSHOT:
138             if os.path.exists(os.path.join(path, ".git")):
139                 suffix, self.vcs_fields = git_version_summary(path, have_git)
140             elif os.path.exists(os.path.join(path, ".bzr")):
141                 suffix, self.vcs_fields = bzr_version_summary(path)
142             else:
143                 suffix = "UNKNOWN"
144                 self.vcs_fields = {}
145             SAMBA_VERSION_STRING += "-" + suffix
146         else:
147             self.vcs_fields = {}
148
149         self.OFFICIAL_STRING = SAMBA_VERSION_STRING
150
151         if self.VENDOR_SUFFIX is not None:
152             SAMBA_VERSION_STRING += ("-" + self.VENDOR_SUFFIX)
153             self.VENDOR_SUFFIX = self.VENDOR_SUFFIX
154
155             if self.VENDOR_PATCH is not None:
156                 SAMBA_VERSION_STRING += ("-" + self.VENDOR_PATCH)
157                 self.VENDOR_PATCH = self.VENDOR_PATCH
158
159         self.STRING = SAMBA_VERSION_STRING
160
161         if self.RELEASE_NICKNAME is not None:
162             self.STRING_WITH_NICKNAME += (" (" + self.RELEASE_NICKNAME + ")")
163             self.RELEASE_NICKNAME = self.RELEASE_NICKNAME
164         else:
165             self.STRING_WITH_NICKNAME = self.STRING
166
167     def __str__(self):
168         string="/* Autogenerated by waf */\n"
169         string+="#define SAMBA_VERSION_MAJOR %u\n" % self.MAJOR
170         string+="#define SAMBA_VERSION_MINOR %u\n" % self.MINOR
171         string+="#define SAMBA_VERSION_RELEASE %u\n" % self.RELEASE
172         if self.REVISION is not None:
173             string+="#define SAMBA_VERSION_REVISION %u\n" % self.REVISION
174
175         if self.TP_RELEASE is not None:
176             string+="#define SAMBA_VERSION_TP_RELEASE %u\n" % self.TP_RELEASE
177
178         if self.ALPHA_RELEASE is not None:
179             string+="#define SAMBA_VERSION_ALPHA_RELEASE %u\n" % self.ALPHA_RELEASE
180
181         if self.PRE_RELEASE is not None:
182             string+="#define SAMBA_VERSION_PRE_RELEASE %u\n" % self.PRE_RELEASE
183
184         if self.RC_RELEASE is not None:
185             string+="#define SAMBA_VERSION_RC_RELEASE %u\n" % self.RC_RELEASE
186
187         for name, value in self.vcs_fields.iteritems():
188             string+="#define SAMBA_VERSION_%s \"%s\"\n" % (name, value)
189
190         string+="#define SAMBA_VERSION_OFFICIAL_STRING \"" + self.OFFICIAL_STRING + "\"\n"
191
192         if self.VENDOR_SUFFIX is not None:
193             string+="#define SAMBA_VERSION_VENDOR_SUFFIX " + self.VENDOR_SUFFIX + "\n"
194             if self.VENDOR_PATCH is not None:
195                 string+="#define SAMBA_VERSION_VENDOR_PATCH " + self.VENDOR_PATCH + "\n"
196
197         if self.RELEASE_NICKNAME is not None:
198             string+="#define SAMBA_VERSION_RELEASE_NICKNAME " + self.RELEASE_NICKNAME + "\n"
199
200         # We need to put this #ifdef in to the headers so that vendors can override the version with a function
201         string+='''
202 #ifdef SAMBA_VERSION_VENDOR_FUNCTION
203 #  define SAMBA_VERSION_STRING SAMBA_VERSION_VENDOR_FUNCTION
204 #else /* SAMBA_VERSION_VENDOR_FUNCTION */
205 #  define SAMBA_VERSION_STRING "''' + self.STRING_WITH_NICKNAME + '''"
206 #endif
207 '''
208         string+="/* Version for mkrelease.sh: \nSAMBA_VERSION_STRING=" + self.STRING_WITH_NICKNAME + "\n */\n"
209
210         return string
211
212
213 def samba_version_file(version_file, path, have_git=False):
214     '''Parse the version information from a VERSION file'''
215
216     f = open(version_file, 'r')
217     version_dict = {}
218     for line in f:
219         line = line.strip()
220         if line == '':
221             continue
222         if line.startswith("#"):
223             continue
224         try:
225             split_line = line.split("=")
226             if split_line[1] != "":
227                 value = split_line[1].strip('"')
228                 version_dict[split_line[0]] = value
229         except:
230             print("Failed to parse line %s from %s" % (line, version_file))
231             raise
232
233     return SambaVersion(version_dict, path, have_git=have_git)