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