5 def bzr_version_summary(path):
7 from bzrlib import branch, osutils, workingtree
9 return ("BZR-UNKNOWN", {})
11 from bzrlib.plugin import load_plugins
14 b = branch.Branch.open(path)
15 (revno, revid) = b.last_revision_info()
16 rev = b.repository.get_revision(revid)
19 "BZR_REVISION_ID": revid,
21 "COMMIT_DATE": osutils.format_date_with_offset_in_original_timezone(rev.timestamp,
23 "COMMIT_TIME": int(rev.timestamp),
24 "BZR_BRANCH": rev.properties.get("branch-nick", ""),
27 # If possible, retrieve the git sha
29 from bzrlib.plugins.git.object_store import get_object_store
32 ret = "BZR-%d" % revno
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
44 fields["COMMIT_IS_CLEAN"] = 1
48 def git_version_summary(path, env=None):
49 # Get version from GIT
51 return ("GIT-UNKNOWN", {})
53 environ = dict(os.environ)
54 environ["GIT_DIR"] = '%s/.git' % path
55 environ["GIT_WORK_TREE"] = path
56 git = Utils.cmd_output(env.GIT + ' show --pretty=format:"%h%n%ct%n%H%n%cd" --stat HEAD', silent=True, env=environ)
58 lines = git.splitlines()
59 if not lines or len(lines) < 4:
60 return ("GIT-UNKNOWN", {})
63 "GIT_COMMIT_ABBREV": lines[0],
64 "GIT_COMMIT_FULLREV": lines[2],
65 "COMMIT_TIME": int(lines[1]),
66 "COMMIT_DATE": lines[3],
69 ret = "GIT-" + fields["GIT_COMMIT_ABBREV"]
71 if env.GIT_LOCAL_CHANGES:
72 clean = Utils.cmd_output('git diff HEAD | wc -l', silent=True).strip()
74 fields["COMMIT_IS_CLEAN"] = 1
76 fields["COMMIT_IS_CLEAN"] = 0
82 class SambaVersion(object):
84 def __init__(self, version_dict, path, env=None):
85 '''Determine the version number of samba
87 See VERSION for the format. Entries on that file are
88 also accepted as dictionary entries here
96 self.ALPHA_RELEASE=None
100 self.RELEASE_NICKNAME=None
101 self.VENDOR_SUFFIX=None
102 self.VENDOR_PATCH=None
104 for a, b in version_dict.iteritems():
105 if a.startswith("SAMBA_VERSION_"):
106 setattr(self, a[14:], b)
110 if self.IS_GIT_SNAPSHOT == "yes":
111 self.IS_SNAPSHOT=True
112 elif self.IS_GIT_SNAPSHOT == "no":
113 self.IS_SNAPSHOT=False
115 raise Exception("Unknown value for IS_GIT_SNAPSHOT: %s" % self.IS_GIT_SNAPSHOT)
118 ## start with "3.0.22"
120 self.MAJOR=int(self.MAJOR)
121 self.MINOR=int(self.MINOR)
122 self.RELEASE=int(self.RELEASE)
124 SAMBA_VERSION_STRING = ("%u.%u.%u" % (self.MAJOR, self.MINOR, self.RELEASE))
127 ## maybe add "3.0.22a" or "4.0.0tp11" or "4.0.0alpha1" or "3.0.22pre1" or "3.0.22rc1"
128 ## We do not do pre or rc version on patch/letter releases
130 if self.REVISION is not None:
131 SAMBA_VERSION_STRING += self.REVISION
132 if self.TP_RELEASE is not None:
133 self.TP_RELEASE = int(self.TP_RELEASE)
134 SAMBA_VERSION_STRING += "tp%u" % self.TP_RELEASE
135 if self.ALPHA_RELEASE is not None:
136 self.ALPHA_RELEASE = int(self.ALPHA_RELEASE)
137 SAMBA_VERSION_STRING += ("alpha%u" % self.ALPHA_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)
146 if os.path.exists(os.path.join(path, ".git")):
147 suffix, self.vcs_fields = git_version_summary(path, env=env)
148 elif os.path.exists(os.path.join(path, ".bzr")):
149 suffix, self.vcs_fields = bzr_version_summary(path)
153 SAMBA_VERSION_STRING += "-" + suffix
157 self.OFFICIAL_STRING = SAMBA_VERSION_STRING
159 if self.VENDOR_SUFFIX is not None:
160 SAMBA_VERSION_STRING += ("-" + self.VENDOR_SUFFIX)
161 self.VENDOR_SUFFIX = self.VENDOR_SUFFIX
163 if self.VENDOR_PATCH is not None:
164 SAMBA_VERSION_STRING += ("-" + self.VENDOR_PATCH)
165 self.VENDOR_PATCH = self.VENDOR_PATCH
167 self.STRING = SAMBA_VERSION_STRING
169 if self.RELEASE_NICKNAME is not None:
170 self.STRING_WITH_NICKNAME = "%s (%s)" % (self.STRING, self.RELEASE_NICKNAME)
172 self.STRING_WITH_NICKNAME = self.STRING
175 string="/* Autogenerated by waf */\n"
176 string+="#define SAMBA_VERSION_MAJOR %u\n" % self.MAJOR
177 string+="#define SAMBA_VERSION_MINOR %u\n" % self.MINOR
178 string+="#define SAMBA_VERSION_RELEASE %u\n" % self.RELEASE
179 if self.REVISION is not None:
180 string+="#define SAMBA_VERSION_REVISION %u\n" % self.REVISION
182 if self.TP_RELEASE is not None:
183 string+="#define SAMBA_VERSION_TP_RELEASE %u\n" % self.TP_RELEASE
185 if self.ALPHA_RELEASE is not None:
186 string+="#define SAMBA_VERSION_ALPHA_RELEASE %u\n" % self.ALPHA_RELEASE
188 if self.PRE_RELEASE is not None:
189 string+="#define SAMBA_VERSION_PRE_RELEASE %u\n" % self.PRE_RELEASE
191 if self.RC_RELEASE is not None:
192 string+="#define SAMBA_VERSION_RC_RELEASE %u\n" % self.RC_RELEASE
194 for name in sorted(self.vcs_fields.keys()):
195 string+="#define SAMBA_VERSION_%s " % name
196 value = self.vcs_fields[name]
197 if isinstance(value, basestring):
198 string += "\"%s\"" % value
199 elif type(value) is int:
200 string += "%d" % value
202 raise Exception("Unknown type for %s: %r" % (name, value))
205 string+="#define SAMBA_VERSION_OFFICIAL_STRING \"" + self.OFFICIAL_STRING + "\"\n"
207 if self.VENDOR_SUFFIX is not None:
208 string+="#define SAMBA_VERSION_VENDOR_SUFFIX " + self.VENDOR_SUFFIX + "\n"
209 if self.VENDOR_PATCH is not None:
210 string+="#define SAMBA_VERSION_VENDOR_PATCH " + self.VENDOR_PATCH + "\n"
212 if self.RELEASE_NICKNAME is not None:
213 string+="#define SAMBA_VERSION_RELEASE_NICKNAME " + self.RELEASE_NICKNAME + "\n"
215 # We need to put this #ifdef in to the headers so that vendors can override the version with a function
217 #ifdef SAMBA_VERSION_VENDOR_FUNCTION
218 # define SAMBA_VERSION_STRING SAMBA_VERSION_VENDOR_FUNCTION
219 #else /* SAMBA_VERSION_VENDOR_FUNCTION */
220 # define SAMBA_VERSION_STRING "''' + self.STRING_WITH_NICKNAME + '''"
223 string+="/* Version for mkrelease.sh: \nSAMBA_VERSION_STRING=" + self.STRING_WITH_NICKNAME + "\n */\n"
228 def samba_version_file(version_file, path, env=None):
229 '''Parse the version information from a VERSION file'''
231 f = open(version_file, 'r')
237 if line.startswith("#"):
240 split_line = line.split("=")
241 if split_line[1] != "":
242 value = split_line[1].strip('"')
243 version_dict[split_line[0]] = value
245 print("Failed to parse line %s from %s" % (line, version_file))
248 return SambaVersion(version_dict, path, env=env)
252 def load_version(env=None):
253 '''load samba versions either from ./VERSION or git
254 return a version object for detailed breakdown'''
256 env = samba_utils.LOAD_ENVIRONMENT()
258 version = samba_version_file("./VERSION", "..", env)
259 Utils.g_module.VERSION = version.STRING