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