Rename set_sd() to set_sd_blob() - this describes what it does. (cherry picked from...
[Samba.git] / buildtools / wafsamba / samba_version.py
blob0b0c159f55ff5ec8b361caf3c98df9ed6eb32b95
1 import os
2 import Utils
3 import samba_utils
4 import sys
6 def bzr_version_summary(path):
7 try:
8 import bzrlib
9 except ImportError:
10 return ("BZR-UNKNOWN", {})
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()
19 b = branch.Branch.open(path)
20 (revno, revid) = b.last_revision_info()
21 rev = b.repository.get_revision(revid)
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", ""),
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 full_rev = store._lookup_revision_sha1(revid)
41 fields["GIT_COMMIT_ABBREV"] = full_rev[:7]
42 fields["GIT_COMMIT_FULLREV"] = full_rev
43 ret = "GIT-" + fields["GIT_COMMIT_ABBREV"]
45 if workingtree.WorkingTree.open(path).has_changes():
46 fields["COMMIT_IS_CLEAN"] = 0
47 ret += "+"
48 else:
49 fields["COMMIT_IS_CLEAN"] = 1
50 return (ret, fields)
53 def git_version_summary(path, env=None):
54 # Get version from GIT
55 if not 'GIT' in env and os.path.exists("/usr/bin/git"):
56 # this is useful when doing make dist without configuring
57 env.GIT = "/usr/bin/git"
59 if not 'GIT' in env:
60 return ("GIT-UNKNOWN", {})
62 environ = dict(os.environ)
63 environ["GIT_DIR"] = '%s/.git' % path
64 environ["GIT_WORK_TREE"] = path
65 git = Utils.cmd_output(env.GIT + ' show --pretty=format:"%h%n%ct%n%H%n%cd" --stat HEAD', silent=True, env=environ)
67 lines = git.splitlines()
68 if not lines or len(lines) < 4:
69 return ("GIT-UNKNOWN", {})
71 fields = {
72 "GIT_COMMIT_ABBREV": lines[0],
73 "GIT_COMMIT_FULLREV": lines[2],
74 "COMMIT_TIME": int(lines[1]),
75 "COMMIT_DATE": lines[3],
78 ret = "GIT-" + fields["GIT_COMMIT_ABBREV"]
80 if env.GIT_LOCAL_CHANGES:
81 clean = Utils.cmd_output('%s diff HEAD | wc -l' % env.GIT, silent=True).strip()
82 if clean == "0":
83 fields["COMMIT_IS_CLEAN"] = 1
84 else:
85 fields["COMMIT_IS_CLEAN"] = 0
86 ret += "+"
88 return (ret, fields)
91 class SambaVersion(object):
93 def __init__(self, version_dict, path, env=None):
94 '''Determine the version number of samba
96 See VERSION for the format. Entries on that file are
97 also accepted as dictionary entries here
98 '''
100 self.MAJOR=None
101 self.MINOR=None
102 self.RELEASE=None
103 self.REVISION=None
104 self.TP_RELEASE=None
105 self.ALPHA_RELEASE=None
106 self.PRE_RELEASE=None
107 self.RC_RELEASE=None
108 self.IS_SNAPSHOT=True
109 self.RELEASE_NICKNAME=None
110 self.VENDOR_SUFFIX=None
111 self.VENDOR_PATCH=None
113 for a, b in version_dict.iteritems():
114 if a.startswith("SAMBA_VERSION_"):
115 setattr(self, a[14:], b)
116 else:
117 setattr(self, a, b)
119 if self.IS_GIT_SNAPSHOT == "yes":
120 self.IS_SNAPSHOT=True
121 elif self.IS_GIT_SNAPSHOT == "no":
122 self.IS_SNAPSHOT=False
123 else:
124 raise Exception("Unknown value for IS_GIT_SNAPSHOT: %s" % self.IS_GIT_SNAPSHOT)
127 ## start with "3.0.22"
129 self.MAJOR=int(self.MAJOR)
130 self.MINOR=int(self.MINOR)
131 self.RELEASE=int(self.RELEASE)
133 SAMBA_VERSION_STRING = ("%u.%u.%u" % (self.MAJOR, self.MINOR, self.RELEASE))
136 ## maybe add "3.0.22a" or "4.0.0tp11" or "4.0.0alpha1" or "3.0.22pre1" or "3.0.22rc1"
137 ## We do not do pre or rc version on patch/letter releases
139 if self.REVISION is not None:
140 SAMBA_VERSION_STRING += self.REVISION
141 if self.TP_RELEASE is not None:
142 self.TP_RELEASE = int(self.TP_RELEASE)
143 SAMBA_VERSION_STRING += "tp%u" % self.TP_RELEASE
144 if self.ALPHA_RELEASE is not None:
145 self.ALPHA_RELEASE = int(self.ALPHA_RELEASE)
146 SAMBA_VERSION_STRING += ("alpha%u" % self.ALPHA_RELEASE)
147 if self.PRE_RELEASE is not None:
148 self.PRE_RELEASE = int(self.PRE_RELEASE)
149 SAMBA_VERSION_STRING += ("pre%u" % self.PRE_RELEASE)
150 if self.RC_RELEASE is not None:
151 self.RC_RELEASE = int(self.RC_RELEASE)
152 SAMBA_VERSION_STRING += ("rc%u" % self.RC_RELEASE)
154 if self.IS_SNAPSHOT:
155 if os.path.exists(os.path.join(path, ".git")):
156 suffix, self.vcs_fields = git_version_summary(path, env=env)
157 elif os.path.exists(os.path.join(path, ".bzr")):
158 suffix, self.vcs_fields = bzr_version_summary(path)
159 else:
160 suffix = "UNKNOWN"
161 self.vcs_fields = {}
162 SAMBA_VERSION_STRING += "-" + suffix
163 else:
164 self.vcs_fields = {}
166 self.OFFICIAL_STRING = SAMBA_VERSION_STRING
168 if self.VENDOR_SUFFIX is not None:
169 SAMBA_VERSION_STRING += ("-" + self.VENDOR_SUFFIX)
170 self.VENDOR_SUFFIX = self.VENDOR_SUFFIX
172 if self.VENDOR_PATCH is not None:
173 SAMBA_VERSION_STRING += ("-" + self.VENDOR_PATCH)
174 self.VENDOR_PATCH = self.VENDOR_PATCH
176 self.STRING = SAMBA_VERSION_STRING
178 if self.RELEASE_NICKNAME is not None:
179 self.STRING_WITH_NICKNAME = "%s (%s)" % (self.STRING, self.RELEASE_NICKNAME)
180 else:
181 self.STRING_WITH_NICKNAME = self.STRING
183 def __str__(self):
184 string="/* Autogenerated by waf */\n"
185 string+="#define SAMBA_VERSION_MAJOR %u\n" % self.MAJOR
186 string+="#define SAMBA_VERSION_MINOR %u\n" % self.MINOR
187 string+="#define SAMBA_VERSION_RELEASE %u\n" % self.RELEASE
188 if self.REVISION is not None:
189 string+="#define SAMBA_VERSION_REVISION %u\n" % self.REVISION
191 if self.TP_RELEASE is not None:
192 string+="#define SAMBA_VERSION_TP_RELEASE %u\n" % self.TP_RELEASE
194 if self.ALPHA_RELEASE is not None:
195 string+="#define SAMBA_VERSION_ALPHA_RELEASE %u\n" % self.ALPHA_RELEASE
197 if self.PRE_RELEASE is not None:
198 string+="#define SAMBA_VERSION_PRE_RELEASE %u\n" % self.PRE_RELEASE
200 if self.RC_RELEASE is not None:
201 string+="#define SAMBA_VERSION_RC_RELEASE %u\n" % self.RC_RELEASE
203 for name in sorted(self.vcs_fields.keys()):
204 string+="#define SAMBA_VERSION_%s " % name
205 value = self.vcs_fields[name]
206 if isinstance(value, basestring):
207 string += "\"%s\"" % value
208 elif type(value) is int:
209 string += "%d" % value
210 else:
211 raise Exception("Unknown type for %s: %r" % (name, value))
212 string += "\n"
214 string+="#define SAMBA_VERSION_OFFICIAL_STRING \"" + self.OFFICIAL_STRING + "\"\n"
216 if self.VENDOR_SUFFIX is not None:
217 string+="#define SAMBA_VERSION_VENDOR_SUFFIX " + self.VENDOR_SUFFIX + "\n"
218 if self.VENDOR_PATCH is not None:
219 string+="#define SAMBA_VERSION_VENDOR_PATCH " + self.VENDOR_PATCH + "\n"
221 if self.RELEASE_NICKNAME is not None:
222 string+="#define SAMBA_VERSION_RELEASE_NICKNAME " + self.RELEASE_NICKNAME + "\n"
224 # We need to put this #ifdef in to the headers so that vendors can override the version with a function
225 string+='''
226 #ifdef SAMBA_VERSION_VENDOR_FUNCTION
227 # define SAMBA_VERSION_STRING SAMBA_VERSION_VENDOR_FUNCTION
228 #else /* SAMBA_VERSION_VENDOR_FUNCTION */
229 # define SAMBA_VERSION_STRING "''' + self.STRING_WITH_NICKNAME + '''"
230 #endif
232 string+="/* Version for mkrelease.sh: \nSAMBA_VERSION_STRING=" + self.STRING_WITH_NICKNAME + "\n */\n"
234 return string
237 def samba_version_file(version_file, path, env=None):
238 '''Parse the version information from a VERSION file'''
240 f = open(version_file, 'r')
241 version_dict = {}
242 for line in f:
243 line = line.strip()
244 if line == '':
245 continue
246 if line.startswith("#"):
247 continue
248 try:
249 split_line = line.split("=")
250 if split_line[1] != "":
251 value = split_line[1].strip('"')
252 version_dict[split_line[0]] = value
253 except:
254 print("Failed to parse line %s from %s" % (line, version_file))
255 raise
257 return SambaVersion(version_dict, path, env=env)
261 def load_version(env=None):
262 '''load samba versions either from ./VERSION or git
263 return a version object for detailed breakdown'''
264 if not env:
265 env = samba_utils.LOAD_ENVIRONMENT()
267 version = samba_version_file("./VERSION", ".", env)
268 Utils.g_module.VERSION = version.STRING
269 return version