waf: fixed some python3.x portability issues
[kai/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                 clean = Utils.cmd_output('git diff HEAD | wc -l', silent=True)
82                 if clean == "0\n":
83                     self.GIT_COMMIT_IS_CLEAN = True
84                 else:
85                     self.GIT_COMMIT_IS_CLEAN = False
86                     SAMBA_VERSION_STRING += "+"
87
88         self.OFFICIAL_STRING=SAMBA_VERSION_STRING
89
90         if self.VENDOR_SUFFIX is not None:
91             SAMBA_VERSION_STRING += ("-" + self.VENDOR_SUFFIX)
92             self.VENDOR_SUFFIX = self.VENDOR_SUFFIX
93
94             if self.VENDOR_PATCH is not None:
95                 SAMBA_VERSION_STRING += ("-" + self.VENDOR_PATCH)
96                 self.VENDOR_PATCH = self.VENDOR_PATCH
97
98         self.STRING = SAMBA_VERSION_STRING
99
100         if self.RELEASE_NICKNAME is not None:
101             self.STRING_WITH_NICKNAME += (" (" + self.RELEASE_NICKNAME + ")")
102             self.RELEASE_NICKNAME = self.RELEASE_NICKNAME
103         else:
104             self.STRING_WITH_NICKNAME = self.STRING
105     
106     def __str__(self):
107         string="/* Autogenerated by waf */\n"
108         string+="#define SAMBA_VERSION_MAJOR %u\n" % self.MAJOR
109         string+="#define SAMBA_VERSION_MINOR %u\n" % self.MINOR
110         string+="#define SAMBA_VERSION_RELEASE %u\n" % self.RELEASE
111         if self.REVISION is not None:
112             string+="#define SAMBA_VERSION_REVISION %u\n" % self.REVISION
113
114         if self.TP_RELEASE is not None:
115             string+="#define SAMBA_VERSION_TP_RELEASE %u\n" % self.TP_RELEASE
116
117         if self.ALPHA_RELEASE is not None:
118             string+="#define SAMBA_VERSION_ALPHA_RELEASE %u\n" % self.ALPHA_RELEASE
119
120         if self.PRE_RELEASE is not None:
121             string+="#define SAMBA_VERSION_PRE_RELEASE %u\n" % self.PRE_RELEASE
122
123         if self.RC_RELEASE is not None:
124             string+="#define SAMBA_VERSION_RC_RELEASE %u\n" % self.RC_RELEASE
125
126         try:
127             string+="#define SAMBA_VERSION_GIT_COMMIT_ABBREV \"" + self.GIT_COMMIT_ABBREV + "\"\n"
128             string+="#define SAMBA_VERSION_GIT_COMMIT_FULLREV \"" + self.GIT_COMMIT_FULLREV + "\"\n"
129             string+="#define SAMBA_VERSION_GIT_COMMIT_DATE \"" + self.GIT_COMMIT_DATE + "\"\n"
130             string+="#define SAMBA_VERSION_GIT_COMMIT_TIME " + self.GIT_COMMIT_TIME + "\n"
131             if self.GIT_COMMIT_IS_CLEAN:
132                 string+="#define SAMBA_VERSION_GIT_COMMIT_IS_CLEAN 1\n"
133         except AttributeError:
134             pass
135
136         string+="#define SAMBA_VERSION_OFFICIAL_STRING \"" + self.OFFICIAL_STRING + "\"\n"
137
138         if self.VENDOR_SUFFIX is not None:
139             string+="#define SAMBA_VERSION_VENDOR_SUFFIX " + self.VENDOR_SUFFIX + "\n"
140             if self.VENDOR_PATCH is not None:
141                 string+="#define SAMBA_VERSION_VENDOR_PATCH " + self.VENDOR_PATCH + "\n"
142
143         if self.RELEASE_NICKNAME is not None:
144             string+="#define SAMBA_VERSION_RELEASE_NICKNAME " + self.RELEASE_NICKNAME + "\n"
145
146         # We need to put this #ifdef in to the headers so that vendors can override the version with a function
147         string+='''
148 #ifdef SAMBA_VERSION_VENDOR_FUNCTION
149 #  define SAMBA_VERSION_STRING SAMBA_VERSION_VENDOR_FUNCTION
150 #else /* SAMBA_VERSION_VENDOR_FUNCTION */
151 #  define SAMBA_VERSION_STRING "''' + self.STRING_WITH_NICKNAME + '''"
152 #endif
153 '''
154         string+="/* Version for mkrelease.sh: \nSAMBA_VERSION_STRING=" + self.STRING_WITH_NICKNAME + "\n */\n"
155
156         return string
157
158
159 class samba_version_file(samba_version):
160     def __init__(self, version_file, have_git=False):
161         '''Parse the version information from a VERSION file'''
162         f = open(version_file, 'r')
163         version_dict = {}
164         for line in f:
165             try:
166                 line = line.strip()
167                 if line == '':
168                     continue
169                 if line.startswith("#"):
170                     continue
171                 split_line=line.split("=")
172                 if split_line[1] != "":
173                     value = split_line[1].strip('"')
174                     version_dict[split_line[0]] = value
175             except:
176                 print("Failed to parse line %s from %s" % (line, version_file))
177                 raise
178             
179         super(samba_version_file, self).__init__(version_dict, have_git=have_git)