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