waf/samba_version: Simplify git show command.
[amitay/samba.git] / buildtools / wafsamba / samba_version.py
1 import os
2 import Utils
3
4 def bzr_version_summary(path):
5     try:
6         from bzrlib import branch, osutils, workingtree
7     except ImportError:
8         return ("BZR-UNKNOWN", {})
9
10     from bzrlib.plugin import load_plugins
11     load_plugins()
12
13     b = branch.Branch.open(path)
14     (revno, revid) = b.last_revision_info()
15     rev = b.repository.get_revision(revid)
16
17     fields = {
18         "BZR_REVISION_ID": revid,
19         "BZR_REVNO": revno,
20         "COMMIT_DATE": osutils.format_date_with_offset_in_original_timezone(rev.timestamp,
21             rev.timezone or 0),
22         "COMMIT_TIME": int(rev.timestamp),
23         "BZR_BRANCH": rev.properties.get("branch-nick", ""),
24         }
25
26     # If possible, retrieve the git sha
27     try:
28         from bzrlib.plugins.git.object_store import get_object_store
29     except ImportError:
30         # No git plugin
31         ret = "BZR-%d" % revno
32     else:
33         store = get_object_store(b.repository)
34         full_rev = store._lookup_revision_sha1(revid)
35         fields["GIT_COMMIT_ABBREV"] = full_rev[:7]
36         fields["GIT_COMMIT_FULLREV"] = full_rev
37         ret = "GIT-" + fields["GIT_COMMIT_ABBREV"]
38
39     if workingtree.WorkingTree.open(path).has_changes():
40         fields["COMMIT_IS_CLEAN"] = 0
41         ret += "+"
42     else:
43         fields["COMMIT_IS_CLEAN"] = 1
44     return (ret, fields)
45
46
47 def git_version_summary(path, have_git):
48     # Get version from GIT
49     if not have_git:
50         return ("GIT-UNKNOWN", {})
51
52     git = Utils.cmd_output(env={"GIT_DIR": "%s/.git" % path}, cmd=["git", "show", '--pretty=format:%h%n%ct%n%H%n%cd', "--stat", "HEAD"])
53
54     lines = git.splitlines()
55     fields = {
56             "GIT_COMMIT_ABBREV": lines[0],
57             "GIT_COMMIT_FULLREV": lines[2],
58             "COMMIT_TIME": int(lines[1]),
59             "COMMIT_DATE": lines[3],
60             }
61
62     ret = "GIT-" + fields["GIT_COMMIT_ABBREV"]
63
64     clean = Utils.cmd_output('git diff HEAD | wc -l', silent=True)
65     if clean == "0\n":
66         fields["COMMIT_IS_CLEAN"] = 1
67     else:
68         fields["COMMIT_IS_CLEAN"] = 0
69         ret += "+"
70     return (ret, fields)
71
72
73 class SambaVersion(object):
74
75     def __init__(self, version_dict, path, have_git=False):
76         '''Determine the version number of samba
77
78 See VERSION for the format.  Entries on that file are 
79 also accepted as dictionary entries here
80         '''
81
82         self.MAJOR=None
83         self.MINOR=None
84         self.RELEASE=None
85         self.REVISION=None
86         self.TP_RELEASE=None
87         self.ALPHA_RELEASE=None
88         self.PRE_RELEASE=None
89         self.RC_RELEASE=None
90         self.IS_SNAPSHOT=True
91         self.RELEASE_NICKNAME=None
92         self.VENDOR_SUFFIX=None
93         self.VENDOR_PATCH=None
94
95         for a, b in version_dict.iteritems():
96             if a.startswith("SAMBA_VERSION_"):
97                 setattr(self, a[14:], b)
98             else:
99                 setattr(self, a, b)
100
101         if self.IS_GIT_SNAPSHOT == "yes":
102             self.IS_SNAPSHOT=True
103         elif self.IS_GIT_SNAPSHOT == "no":
104             self.IS_SNAPSHOT=False
105         else:
106             raise Exception("Unknown value for IS_GIT_SNAPSHOT: %s" % self.IS_GIT_SNAPSHOT)
107
108  ##
109  ## start with "3.0.22"
110  ##
111         self.MAJOR=int(self.MAJOR)
112         self.MINOR=int(self.MINOR)
113         self.RELEASE=int(self.RELEASE)
114
115         SAMBA_VERSION_STRING = ("%u.%u.%u" % (self.MAJOR, self.MINOR, self.RELEASE))
116
117 ##
118 ## maybe add "3.0.22a" or "4.0.0tp11" or "4.0.0alpha1" or "3.0.22pre1" or "3.0.22rc1"
119 ## We do not do pre or rc version on patch/letter releases
120 ##
121         if self.REVISION is not None:
122             SAMBA_VERSION_STRING += self.REVISION
123         if self.TP_RELEASE is not None:
124             self.TP_RELEASE = int(self.TP_RELEASE)
125             SAMBA_VERSION_STRING += "tp%u" % self.TP_RELEASE
126         if self.ALPHA_RELEASE is not None:
127             self.ALPHA_RELEASE = int(self.ALPHA_RELEASE)
128             SAMBA_VERSION_STRING += ("alpha%u" % self.ALPHA_RELEASE)
129         if self.PRE_RELEASE is not None:
130             self.PRE_RELEASE = int(self.PRE_RELEASE)
131             SAMBA_VERSION_STRING += ("pre%u" % self.PRE_RELEASE)
132         if self.RC_RELEASE is not None:
133             self.RC_RELEASE = int(self.RC_RELEASE)
134             SAMBA_VERSION_STRING += ("rc%u" % self.RC_RELEASE)
135
136         if self.IS_SNAPSHOT:
137             if os.path.exists(os.path.join(path, ".git")):
138                 suffix, self.vcs_fields = git_version_summary(path, have_git)
139             elif os.path.exists(os.path.join(path, ".bzr")):
140                 suffix, self.vcs_fields = bzr_version_summary(path)
141             else:
142                 suffix = "UNKNOWN"
143                 self.vcs_fields = {}
144             SAMBA_VERSION_STRING += "-" + suffix
145         else:
146             self.vcs_fields = {}
147
148         self.OFFICIAL_STRING = SAMBA_VERSION_STRING
149
150         if self.VENDOR_SUFFIX is not None:
151             SAMBA_VERSION_STRING += ("-" + self.VENDOR_SUFFIX)
152             self.VENDOR_SUFFIX = self.VENDOR_SUFFIX
153
154             if self.VENDOR_PATCH is not None:
155                 SAMBA_VERSION_STRING += ("-" + self.VENDOR_PATCH)
156                 self.VENDOR_PATCH = self.VENDOR_PATCH
157
158         self.STRING = SAMBA_VERSION_STRING
159
160         if self.RELEASE_NICKNAME is not None:
161             self.STRING_WITH_NICKNAME += (" (" + self.RELEASE_NICKNAME + ")")
162             self.RELEASE_NICKNAME = self.RELEASE_NICKNAME
163         else:
164             self.STRING_WITH_NICKNAME = self.STRING
165
166     def __str__(self):
167         string="/* Autogenerated by waf */\n"
168         string+="#define SAMBA_VERSION_MAJOR %u\n" % self.MAJOR
169         string+="#define SAMBA_VERSION_MINOR %u\n" % self.MINOR
170         string+="#define SAMBA_VERSION_RELEASE %u\n" % self.RELEASE
171         if self.REVISION is not None:
172             string+="#define SAMBA_VERSION_REVISION %u\n" % self.REVISION
173
174         if self.TP_RELEASE is not None:
175             string+="#define SAMBA_VERSION_TP_RELEASE %u\n" % self.TP_RELEASE
176
177         if self.ALPHA_RELEASE is not None:
178             string+="#define SAMBA_VERSION_ALPHA_RELEASE %u\n" % self.ALPHA_RELEASE
179
180         if self.PRE_RELEASE is not None:
181             string+="#define SAMBA_VERSION_PRE_RELEASE %u\n" % self.PRE_RELEASE
182
183         if self.RC_RELEASE is not None:
184             string+="#define SAMBA_VERSION_RC_RELEASE %u\n" % self.RC_RELEASE
185
186         for name in sorted(self.vcs_fields.keys()):
187             string+="#define SAMBA_VERSION_%s " % name
188             value = self.vcs_fields[name]
189             if isinstance(value, basestring):
190                 string += "\"%s\"" % value
191             elif type(value) is int:
192                 string += "%d" % value
193             else:
194                 raise Exception("Unknown type for %s: %r" % (name, value))
195             string += "\n"
196
197         string+="#define SAMBA_VERSION_OFFICIAL_STRING \"" + self.OFFICIAL_STRING + "\"\n"
198
199         if self.VENDOR_SUFFIX is not None:
200             string+="#define SAMBA_VERSION_VENDOR_SUFFIX " + self.VENDOR_SUFFIX + "\n"
201             if self.VENDOR_PATCH is not None:
202                 string+="#define SAMBA_VERSION_VENDOR_PATCH " + self.VENDOR_PATCH + "\n"
203
204         if self.RELEASE_NICKNAME is not None:
205             string+="#define SAMBA_VERSION_RELEASE_NICKNAME " + self.RELEASE_NICKNAME + "\n"
206
207         # We need to put this #ifdef in to the headers so that vendors can override the version with a function
208         string+='''
209 #ifdef SAMBA_VERSION_VENDOR_FUNCTION
210 #  define SAMBA_VERSION_STRING SAMBA_VERSION_VENDOR_FUNCTION
211 #else /* SAMBA_VERSION_VENDOR_FUNCTION */
212 #  define SAMBA_VERSION_STRING "''' + self.STRING_WITH_NICKNAME + '''"
213 #endif
214 '''
215         string+="/* Version for mkrelease.sh: \nSAMBA_VERSION_STRING=" + self.STRING_WITH_NICKNAME + "\n */\n"
216
217         return string
218
219
220 def samba_version_file(version_file, path, have_git=False):
221     '''Parse the version information from a VERSION file'''
222
223     f = open(version_file, 'r')
224     version_dict = {}
225     for line in f:
226         line = line.strip()
227         if line == '':
228             continue
229         if line.startswith("#"):
230             continue
231         try:
232             split_line = line.split("=")
233             if split_line[1] != "":
234                 value = split_line[1].strip('"')
235                 version_dict[split_line[0]] = value
236         except:
237             print("Failed to parse line %s from %s" % (line, version_file))
238             raise
239
240     return SambaVersion(version_dict, path, have_git=have_git)