6 def bzr_version_summary(path):
10 return ("BZR-UNKNOWN", {})
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
19 b = branch.Branch.open(path)
20 (revno, revid) = b.last_revision_info()
21 rev = b.repository.get_revision(revid)
24 "BZR_REVISION_ID": revid,
26 "COMMIT_DATE": osutils.format_date_with_offset_in_original_timezone(rev.timestamp,
28 "COMMIT_TIME": int(rev.timestamp),
29 "BZR_BRANCH": rev.properties.get("branch-nick", ""),
32 # If possible, retrieve the git sha
34 from bzrlib.plugins.git.object_store import get_object_store
37 ret = "BZR-%d" % revno
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
49 fields["COMMIT_IS_CLEAN"] = 1
53 def git_version_summary(path, env=None):
54 # Get version from GIT
56 return ("GIT-UNKNOWN", {})
58 environ = dict(os.environ)
59 environ["GIT_DIR"] = '%s/.git' % path
60 environ["GIT_WORK_TREE"] = path
61 git = Utils.cmd_output(env.GIT + ' show --pretty=format:"%h%n%ct%n%H%n%cd" --stat HEAD', silent=True, env=environ)
63 lines = git.splitlines()
64 if not lines or len(lines) < 4:
65 return ("GIT-UNKNOWN", {})
68 "GIT_COMMIT_ABBREV": lines[0],
69 "GIT_COMMIT_FULLREV": lines[2],
70 "COMMIT_TIME": int(lines[1]),
71 "COMMIT_DATE": lines[3],
74 ret = "GIT-" + fields["GIT_COMMIT_ABBREV"]
76 if env.GIT_LOCAL_CHANGES:
77 clean = Utils.cmd_output('git diff HEAD | wc -l', silent=True).strip()
79 fields["COMMIT_IS_CLEAN"] = 1
81 fields["COMMIT_IS_CLEAN"] = 0
87 class SambaVersion(object):
89 def __init__(self, version_dict, path, env=None):
90 '''Determine the version number of samba
92 See VERSION for the format. Entries on that file are
93 also accepted as dictionary entries here
101 self.ALPHA_RELEASE=None
102 self.PRE_RELEASE=None
104 self.IS_SNAPSHOT=True
105 self.RELEASE_NICKNAME=None
106 self.VENDOR_SUFFIX=None
107 self.VENDOR_PATCH=None
109 for a, b in version_dict.iteritems():
110 if a.startswith("SAMBA_VERSION_"):
111 setattr(self, a[14:], b)
115 if self.IS_GIT_SNAPSHOT == "yes":
116 self.IS_SNAPSHOT=True
117 elif self.IS_GIT_SNAPSHOT == "no":
118 self.IS_SNAPSHOT=False
120 raise Exception("Unknown value for IS_GIT_SNAPSHOT: %s" % self.IS_GIT_SNAPSHOT)
123 ## start with "3.0.22"
125 self.MAJOR=int(self.MAJOR)
126 self.MINOR=int(self.MINOR)
127 self.RELEASE=int(self.RELEASE)
129 SAMBA_VERSION_STRING = ("%u.%u.%u" % (self.MAJOR, self.MINOR, self.RELEASE))
132 ## maybe add "3.0.22a" or "4.0.0tp11" or "4.0.0alpha1" or "3.0.22pre1" or "3.0.22rc1"
133 ## We do not do pre or rc version on patch/letter releases
135 if self.REVISION is not None:
136 SAMBA_VERSION_STRING += self.REVISION
137 if self.TP_RELEASE is not None:
138 self.TP_RELEASE = int(self.TP_RELEASE)
139 SAMBA_VERSION_STRING += "tp%u" % self.TP_RELEASE
140 if self.ALPHA_RELEASE is not None:
141 self.ALPHA_RELEASE = int(self.ALPHA_RELEASE)
142 SAMBA_VERSION_STRING += ("alpha%u" % self.ALPHA_RELEASE)
143 if self.PRE_RELEASE is not None:
144 self.PRE_RELEASE = int(self.PRE_RELEASE)
145 SAMBA_VERSION_STRING += ("pre%u" % self.PRE_RELEASE)
146 if self.RC_RELEASE is not None:
147 self.RC_RELEASE = int(self.RC_RELEASE)
148 SAMBA_VERSION_STRING += ("rc%u" % self.RC_RELEASE)
151 if os.path.exists(os.path.join(path, ".git")):
152 suffix, self.vcs_fields = git_version_summary(path, env=env)
153 elif os.path.exists(os.path.join(path, ".bzr")):
154 suffix, self.vcs_fields = bzr_version_summary(path)
158 SAMBA_VERSION_STRING += "-" + suffix
162 self.OFFICIAL_STRING = SAMBA_VERSION_STRING
164 if self.VENDOR_SUFFIX is not None:
165 SAMBA_VERSION_STRING += ("-" + self.VENDOR_SUFFIX)
166 self.VENDOR_SUFFIX = self.VENDOR_SUFFIX
168 if self.VENDOR_PATCH is not None:
169 SAMBA_VERSION_STRING += ("-" + self.VENDOR_PATCH)
170 self.VENDOR_PATCH = self.VENDOR_PATCH
172 self.STRING = SAMBA_VERSION_STRING
174 if self.RELEASE_NICKNAME is not None:
175 self.STRING_WITH_NICKNAME = "%s (%s)" % (self.STRING, self.RELEASE_NICKNAME)
177 self.STRING_WITH_NICKNAME = self.STRING
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
187 if self.TP_RELEASE is not None:
188 string+="#define SAMBA_VERSION_TP_RELEASE %u\n" % self.TP_RELEASE
190 if self.ALPHA_RELEASE is not None:
191 string+="#define SAMBA_VERSION_ALPHA_RELEASE %u\n" % self.ALPHA_RELEASE
193 if self.PRE_RELEASE is not None:
194 string+="#define SAMBA_VERSION_PRE_RELEASE %u\n" % self.PRE_RELEASE
196 if self.RC_RELEASE is not None:
197 string+="#define SAMBA_VERSION_RC_RELEASE %u\n" % self.RC_RELEASE
199 for name in sorted(self.vcs_fields.keys()):
200 string+="#define SAMBA_VERSION_%s " % name
201 value = self.vcs_fields[name]
202 if isinstance(value, basestring):
203 string += "\"%s\"" % value
204 elif type(value) is int:
205 string += "%d" % value
207 raise Exception("Unknown type for %s: %r" % (name, value))
210 string+="#define SAMBA_VERSION_OFFICIAL_STRING \"" + self.OFFICIAL_STRING + "\"\n"
212 if self.VENDOR_SUFFIX is not None:
213 string+="#define SAMBA_VERSION_VENDOR_SUFFIX " + self.VENDOR_SUFFIX + "\n"
214 if self.VENDOR_PATCH is not None:
215 string+="#define SAMBA_VERSION_VENDOR_PATCH " + self.VENDOR_PATCH + "\n"
217 if self.RELEASE_NICKNAME is not None:
218 string+="#define SAMBA_VERSION_RELEASE_NICKNAME " + self.RELEASE_NICKNAME + "\n"
220 # We need to put this #ifdef in to the headers so that vendors can override the version with a function
222 #ifdef SAMBA_VERSION_VENDOR_FUNCTION
223 # define SAMBA_VERSION_STRING SAMBA_VERSION_VENDOR_FUNCTION
224 #else /* SAMBA_VERSION_VENDOR_FUNCTION */
225 # define SAMBA_VERSION_STRING "''' + self.STRING_WITH_NICKNAME + '''"
228 string+="/* Version for mkrelease.sh: \nSAMBA_VERSION_STRING=" + self.STRING_WITH_NICKNAME + "\n */\n"
233 def samba_version_file(version_file, path, env=None):
234 '''Parse the version information from a VERSION file'''
236 f = open(version_file, 'r')
242 if line.startswith("#"):
245 split_line = line.split("=")
246 if split_line[1] != "":
247 value = split_line[1].strip('"')
248 version_dict[split_line[0]] = value
250 print("Failed to parse line %s from %s" % (line, version_file))
253 return SambaVersion(version_dict, path, env=env)
257 def load_version(env=None):
258 '''load samba versions either from ./VERSION or git
259 return a version object for detailed breakdown'''
261 env = samba_utils.LOAD_ENVIRONMENT()
263 version = samba_version_file("./VERSION", "..", env)
264 Utils.g_module.VERSION = version.STRING