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