wafsamba: Fix parsing of IS_GIT_VERSION.
[nivanova/samba-autobuild/.git] / buildtools / wafsamba / samba_version.py
1 import Utils;
2
3 class samba_version(object):
4     def __init__(self, version_dict, have_git=False):
5         '''Determine the version number of samba
6
7 See VERSION for the format.  Entries on that file are 
8 also accepted as dictionary entries here
9         '''
10
11         self.MAJOR=None
12         self.MINOR=None
13         self.RELEASE=None
14         self.REVISION=None
15         self.TP_RELEASE=None
16         self.ALPHA_RELEASE=None
17         self.PRE_RELEASE=None
18         self.RC_RELEASE=None
19         self.IS_GIT_SNAPSHOT=True
20         self.RELEASE_NICKNAME=None
21         self.VENDOR_SUFFIX=None
22         self.VENDOR_PATCH=None
23
24         for a, b in version_dict.iteritems():
25             if a.startswith("SAMBA_VERSION_"):
26                 setattr(self, a[14:], b)
27             else:
28                 setattr(self, a, b)
29
30         if self.IS_GIT_SNAPSHOT == "yes":
31             self.IS_GIT_SNAPSHOT=True
32         elif self.IS_GIT_SNAPSHOT == "no":
33             self.IS_GIT_SNAPSHOT=False
34                 
35  ##
36  ## start with "3.0.22"
37  ##
38         self.MAJOR=int(self.MAJOR)
39         self.MINOR=int(self.MINOR)
40         self.RELEASE=int(self.RELEASE)
41
42         SAMBA_VERSION_STRING = ("%u.%u.%u" % (self.MAJOR, self.MINOR, self.RELEASE))
43
44 ##
45 ## maybe add "3.0.22a" or "4.0.0tp11" or "4.0.0alpha1" or "3.0.22pre1" or "3.0.22rc1"
46 ## We do not do pre or rc version on patch/letter releases
47 ##
48         if self.REVISION is not None:
49             SAMBA_VERSION_STRING += self.REVISION
50         if self.TP_RELEASE is not None:
51             self.TP_RELEASE = int(self.TP_RELEASE)
52             SAMBA_VERSION_STRING += ("tp%u" % self.TP_RELEASE)
53         if self.ALPHA_RELEASE is not None:
54             self.ALPHA_RELEASE = int(self.ALPHA_RELEASE)
55             SAMBA_VERSION_STRING += ("alpha%u" % self.ALPHA_RELEASE)
56         if self.PRE_RELEASE is not None:
57             self.PRE_RELEASE = int(self.PRE_RELEASE)
58             SAMBA_VERSION_STRING += ("pre%u" % self.PRE_RELEASE)
59         if self.RC_RELEASE is not None:
60             self.RC_RELEASE = int(self.RC_RELEASE)
61             SAMBA_VERSION_STRING += ("rc%u" % self.RC_RELEASE)
62
63         if self.IS_GIT_SNAPSHOT:
64             #Get version from GIT
65             if have_git:
66                 git = Utils.cmd_output('git show --pretty=format:"%h%n%ct%n%H%n%cd" --stat HEAD', silent=True)
67             else:
68                 git = ''
69
70             if git == '':
71                 SAMBA_VERSION_STRING += "-GIT-UNKNOWN"
72             else:
73                 lines = git.splitlines();
74                 self.GIT_COMMIT_ABBREV = lines[0]
75                 self.GIT_COMMIT_TIME = lines[1]
76                 self.GIT_COMMIT_FULLREV = lines[2]
77                 self.GIT_COMMIT_DATE = lines[3]
78
79                 SAMBA_VERSION_STRING += ("-GIT-" + self.GIT_COMMIT_ABBREV)
80
81         self.OFFICIAL_STRING=SAMBA_VERSION_STRING
82
83         if self.VENDOR_SUFFIX is not None:
84             SAMBA_VERSION_STRING += ("-" + self.VENDOR_SUFFIX)
85             self.VENDOR_SUFFIX = self.VENDOR_SUFFIX
86
87             if self.VENDOR_PATCH is not None:
88                 SAMBA_VERSION_STRING += ("-" + self.VENDOR_PATCH)
89                 self.VENDOR_PATCH = self.VENDOR_PATCH
90
91         self.STRING = SAMBA_VERSION_STRING
92
93         if self.RELEASE_NICKNAME is not None:
94             self.STRING_WITH_NICKNAME += (" (" + self.RELEASE_NICKNAME + ")")
95             self.RELEASE_NICKNAME = self.RELEASE_NICKNAME
96         else:
97             self.STRING_WITH_NICKNAME = self.STRING
98     
99     def __str__(self):
100         string="/* Autogenerated by waf */\n"
101         string+="#define SAMBA_VERSION_MAJOR %u\n" % self.MAJOR
102         string+="#define SAMBA_VERSION_MINOR %u\n" % self.MINOR
103         string+="#define SAMBA_VERSION_RELEASE %u\n" % self.RELEASE
104         if self.REVISION is not None:
105             string+="#define SAMBA_VERSION_REVISION %u\n" % self.REVISION
106
107         if self.TP_RELEASE is not None:
108             string+="#define SAMBA_VERSION_TP_RELEASE %u\n" % self.TP_RELEASE
109
110         if self.ALPHA_RELEASE is not None:
111             string+="#define SAMBA_VERSION_ALPHA_RELEASE %u\n" % self.ALPHA_RELEASE
112
113         if self.PRE_RELEASE is not None:
114             string+="#define SAMBA_VERSION_PRE_RELEASE %u\n" % self.PRE_RELEASE
115
116         if self.RC_RELEASE is not None:
117             string+="#define SAMBA_VERSION_RC_RELEASE %u\n" % self.RC_RELEASE
118
119         try:
120             string+="#define SAMBA_VERSION_GIT_COMMIT_ABBREV " + self.GIT_COMMIT_ABBREV + "\n"
121             string+="#define SAMBA_VERSION_GIT_COMMIT_TIME " + self.GIT_COMMIT_TIME + "\n"
122             string+="#define SAMBA_VERSION_GIT_COMMIT_FULLREV " + self.GIT_COMMIT_TIME + "\n"
123             string+="#define SAMBA_VERSION_GIT_COMMIT_DATE " + self.GIT_COMMIT_DATA + "\n"
124         except AttributeError:
125             pass
126
127         string+="#define SAMBA_VERSION_OFFICIAL_STRING \"" + self.OFFICIAL_STRING + "\"\n"
128
129         if self.VENDOR_SUFFIX is not None:
130             string+="#define SAMBA_VERSION_VENDOR_SUFFIX " + self.VENDOR_SUFFIX + "\n"
131             if self.VENDOR_PATCH is not None:
132                 string+="#define SAMBA_VERSION_VENDOR_PATCH " + self.VENDOR_PATCH + "\n"
133
134         if self.RELEASE_NICKNAME is not None:
135             string+="#define SAMBA_VERSION_RELEASE_NICKNAME " + self.RELEASE_NICKNAME + "\n"
136
137         # We need to put this #ifdef in to the headers so that vendors can override the version with a function
138         string+='''
139 #ifdef SAMBA_VERSION_VENDOR_FUNCTION
140 #  define SAMBA_VERSION_STRING SAMBA_VERSION_VENDOR_FUNCTION
141 #else /* SAMBA_VERSION_VENDOR_FUNCTION */
142 #  define SAMBA_VERSION_STRING "''' + self.STRING_WITH_NICKNAME + '''"
143 #endif
144 '''
145         string+="/* Version for mkrelease.sh: \nSAMBA_VERSION_STRING=" + self.STRING_WITH_NICKNAME + "\n */\n"
146
147         return string
148
149
150 class samba_version_file(samba_version):
151     def __init__(self, version_file, have_git=False):
152         '''Parse the version information from a VERSION file'''
153         f = open(version_file, 'r')
154         version_dict = {}
155         for line in f:
156             try:
157                 line = line.strip()
158                 if line == '':
159                     continue
160                 if line.startswith("#"):
161                     continue
162                 split_line=line.split("=")
163                 if split_line[1] != "":
164                     value = split_line[1].strip('"')
165                     version_dict[split_line[0]] = value
166             except:
167                 print "Failed to parse line %s from %s" % (line, version_file)
168                 raise
169             
170         super(samba_version_file, self).__init__(version_dict, have_git=have_git)