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