s3: Fix Coverity ID 2287: Uninitialized read
[Samba.git] / buildtools / wafsamba / samba_version.py
blobc5f424d750ad5cd159fe1ab03afef751f2cc7b58
1 import os
2 import Utils
3 import samba_utils
5 def bzr_version_summary(path):
6 try:
7 from bzrlib import branch, osutils, workingtree
8 except ImportError:
9 return ("BZR-UNKNOWN", {})
11 from bzrlib.plugin import load_plugins
12 load_plugins()
14 b = branch.Branch.open(path)
15 (revno, revid) = b.last_revision_info()
16 rev = b.repository.get_revision(revid)
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", ""),
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"]
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)
48 def git_version_summary(path, env=None):
49 # Get version from GIT
50 if not 'GIT' in env:
51 return ("GIT-UNKNOWN", {})
53 os.putenv('GIT_DIR', '%s/.git' % path)
54 git = Utils.cmd_output(env.GIT + ' show --pretty=format:"%h%n%ct%n%H%n%cd" --stat HEAD', silent=True)
56 lines = git.splitlines()
57 if not lines or len(lines) < 4:
58 return ("GIT-UNKNOWN", {})
60 fields = {
61 "GIT_COMMIT_ABBREV": lines[0],
62 "GIT_COMMIT_FULLREV": lines[2],
63 "COMMIT_TIME": int(lines[1]),
64 "COMMIT_DATE": lines[3],
67 ret = "GIT-" + fields["GIT_COMMIT_ABBREV"]
69 if env.GIT_LOCAL_CHANGES:
70 clean = Utils.cmd_output('git diff HEAD | wc -l', silent=True).strip()
71 if clean == "0":
72 fields["COMMIT_IS_CLEAN"] = 1
73 else:
74 fields["COMMIT_IS_CLEAN"] = 0
75 ret += "+"
77 return (ret, fields)
80 class SambaVersion(object):
82 def __init__(self, version_dict, path, env=None):
83 '''Determine the version number of samba
85 See VERSION for the format. Entries on that file are
86 also accepted as dictionary entries here
87 '''
89 self.MAJOR=None
90 self.MINOR=None
91 self.RELEASE=None
92 self.REVISION=None
93 self.TP_RELEASE=None
94 self.ALPHA_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
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)
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)
116 ## start with "3.0.22"
118 self.MAJOR=int(self.MAJOR)
119 self.MINOR=int(self.MINOR)
120 self.RELEASE=int(self.RELEASE)
122 SAMBA_VERSION_STRING = ("%u.%u.%u" % (self.MAJOR, self.MINOR, self.RELEASE))
125 ## maybe add "3.0.22a" or "4.0.0tp11" or "4.0.0alpha1" or "3.0.22pre1" or "3.0.22rc1"
126 ## We do not do pre or rc version on patch/letter releases
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.PRE_RELEASE is not None:
137 self.PRE_RELEASE = int(self.PRE_RELEASE)
138 SAMBA_VERSION_STRING += ("pre%u" % self.PRE_RELEASE)
139 if self.RC_RELEASE is not None:
140 self.RC_RELEASE = int(self.RC_RELEASE)
141 SAMBA_VERSION_STRING += ("rc%u" % self.RC_RELEASE)
143 if self.IS_SNAPSHOT:
144 if os.path.exists(os.path.join(path, ".git")):
145 suffix, self.vcs_fields = git_version_summary(path, env=env)
146 elif os.path.exists(os.path.join(path, ".bzr")):
147 suffix, self.vcs_fields = bzr_version_summary(path)
148 else:
149 suffix = "UNKNOWN"
150 self.vcs_fields = {}
151 SAMBA_VERSION_STRING += "-" + suffix
152 else:
153 self.vcs_fields = {}
155 self.OFFICIAL_STRING = SAMBA_VERSION_STRING
157 if self.VENDOR_SUFFIX is not None:
158 SAMBA_VERSION_STRING += ("-" + self.VENDOR_SUFFIX)
159 self.VENDOR_SUFFIX = self.VENDOR_SUFFIX
161 if self.VENDOR_PATCH is not None:
162 SAMBA_VERSION_STRING += ("-" + self.VENDOR_PATCH)
163 self.VENDOR_PATCH = self.VENDOR_PATCH
165 self.STRING = SAMBA_VERSION_STRING
167 if self.RELEASE_NICKNAME is not None:
168 self.STRING_WITH_NICKNAME += (" (" + self.RELEASE_NICKNAME + ")")
169 self.RELEASE_NICKNAME = self.RELEASE_NICKNAME
170 else:
171 self.STRING_WITH_NICKNAME = self.STRING
173 def __str__(self):
174 string="/* Autogenerated by waf */\n"
175 string+="#define SAMBA_VERSION_MAJOR %u\n" % self.MAJOR
176 string+="#define SAMBA_VERSION_MINOR %u\n" % self.MINOR
177 string+="#define SAMBA_VERSION_RELEASE %u\n" % self.RELEASE
178 if self.REVISION is not None:
179 string+="#define SAMBA_VERSION_REVISION %u\n" % self.REVISION
181 if self.TP_RELEASE is not None:
182 string+="#define SAMBA_VERSION_TP_RELEASE %u\n" % self.TP_RELEASE
184 if self.ALPHA_RELEASE is not None:
185 string+="#define SAMBA_VERSION_ALPHA_RELEASE %u\n" % self.ALPHA_RELEASE
187 if self.PRE_RELEASE is not None:
188 string+="#define SAMBA_VERSION_PRE_RELEASE %u\n" % self.PRE_RELEASE
190 if self.RC_RELEASE is not None:
191 string+="#define SAMBA_VERSION_RC_RELEASE %u\n" % self.RC_RELEASE
193 for name in sorted(self.vcs_fields.keys()):
194 string+="#define SAMBA_VERSION_%s " % name
195 value = self.vcs_fields[name]
196 if isinstance(value, basestring):
197 string += "\"%s\"" % value
198 elif type(value) is int:
199 string += "%d" % value
200 else:
201 raise Exception("Unknown type for %s: %r" % (name, value))
202 string += "\n"
204 string+="#define SAMBA_VERSION_OFFICIAL_STRING \"" + self.OFFICIAL_STRING + "\"\n"
206 if self.VENDOR_SUFFIX is not None:
207 string+="#define SAMBA_VERSION_VENDOR_SUFFIX " + self.VENDOR_SUFFIX + "\n"
208 if self.VENDOR_PATCH is not None:
209 string+="#define SAMBA_VERSION_VENDOR_PATCH " + self.VENDOR_PATCH + "\n"
211 if self.RELEASE_NICKNAME is not None:
212 string+="#define SAMBA_VERSION_RELEASE_NICKNAME " + self.RELEASE_NICKNAME + "\n"
214 # We need to put this #ifdef in to the headers so that vendors can override the version with a function
215 string+='''
216 #ifdef SAMBA_VERSION_VENDOR_FUNCTION
217 # define SAMBA_VERSION_STRING SAMBA_VERSION_VENDOR_FUNCTION
218 #else /* SAMBA_VERSION_VENDOR_FUNCTION */
219 # define SAMBA_VERSION_STRING "''' + self.STRING_WITH_NICKNAME + '''"
220 #endif
222 string+="/* Version for mkrelease.sh: \nSAMBA_VERSION_STRING=" + self.STRING_WITH_NICKNAME + "\n */\n"
224 return string
227 def samba_version_file(version_file, path, env=None):
228 '''Parse the version information from a VERSION file'''
230 f = open(version_file, 'r')
231 version_dict = {}
232 for line in f:
233 line = line.strip()
234 if line == '':
235 continue
236 if line.startswith("#"):
237 continue
238 try:
239 split_line = line.split("=")
240 if split_line[1] != "":
241 value = split_line[1].strip('"')
242 version_dict[split_line[0]] = value
243 except:
244 print("Failed to parse line %s from %s" % (line, version_file))
245 raise
247 return SambaVersion(version_dict, path, env=env)
251 def load_version(env=None):
252 '''load samba versions either from ./VERSION or git
253 return a version object for detailed breakdown'''
254 if not env:
255 env = samba_utils.LOAD_ENVIRONMENT()
257 version = samba_version_file("./VERSION", "..", env)
258 Utils.g_module.VERSION = version.STRING
259 return version