20
20
VERSION_RE = re .compile (r'^\d+\.\d+\.\d+$' )
21
21
BOB_REPOSITORY = 'https://github.com/idiap/bob.git'
22
22
23
- def git_remote_version_branches ():
23
+ def git_remote_version_branches (verbose ):
24
24
"""Get the branches available on the origin using git ls-remote"""
25
25
26
26
try :
@@ -34,9 +34,10 @@ def git_remote_version_branches():
34
34
return [k for k in cand if BRANCH_RE .match (k )]
35
35
36
36
except :
37
- print "Warning: could retrieve remote branch list"
37
+ if verbose :
38
+ print "Warning: could retrieve remote branch list"
38
39
39
- def git_version_branches ():
40
+ def git_version_branches (verbose ):
40
41
"""Get the branches available on the origin"""
41
42
42
43
try :
@@ -50,9 +51,10 @@ def git_version_branches():
50
51
return [k for k in cand if k and BRANCH_RE .match (k )]
51
52
52
53
except :
53
- print "Warning: could retrieve branch list"
54
+ if verbose :
55
+ print "Warning: could retrieve branch list"
54
56
55
- def git_current_branch ():
57
+ def git_current_branch (verbose ):
56
58
"""Get the current branch we are sitting on"""
57
59
58
60
try :
@@ -70,9 +72,10 @@ def git_current_branch():
70
72
raise RuntimeError
71
73
72
74
except :
73
- print "Warning: could not determine in which branch I'm on"
75
+ if verbose :
76
+ print "Warning: could not determine in which branch I'm on"
74
77
75
- def git_next_minor_version (branch ):
78
+ def git_next_minor_version (branch , verbose ):
76
79
"""Gets the next minor version"""
77
80
78
81
try :
@@ -93,19 +96,20 @@ def git_next_minor_version(branch):
93
96
return '.' .join ([str (k ) for k in next_version ])
94
97
95
98
except :
96
- print "Warning: could not determine latest tag on branch (%s). Assuming it is %s.0" % (branch , branch )
99
+ if verbose :
100
+ print "Warning: could not determine latest tag on branch (%s). Assuming it is %s.0" % (branch , branch )
97
101
return branch + '.0'
98
102
99
- def git_next_major_version ():
103
+ def git_next_major_version (verbose ):
100
104
"""Gets the next major version"""
101
105
102
106
# try local
103
- candidates = sorted ([StrictVersion (k ) for k in git_version_branches ()])
107
+ candidates = sorted ([StrictVersion (k ) for k in git_version_branches (verbose )])
104
108
105
109
if not candidates :
106
110
# try remote
107
111
candidates = \
108
- sorted ([StrictVersion (k ) for k in git_remote_version_branches ()])
112
+ sorted ([StrictVersion (k ) for k in git_remote_version_branches (verbose )])
109
113
110
114
if not candidates :
111
115
return None
@@ -118,18 +122,18 @@ def git_next_major_version():
118
122
119
123
return '.' .join ([str (k ) for k in next_version ])
120
124
121
- def git_next_version (branch ):
125
+ def git_next_version (branch , verbose ):
122
126
"""Gets the next version given the branch I'm on"""
123
127
124
128
if BRANCH_RE .match (branch ):
125
129
# we are on a stable branch
126
- return git_next_minor_version (branch )
130
+ return git_next_minor_version (branch , verbose )
127
131
128
132
else :
129
133
# we are on the master tip
130
- return git_next_major_version ()
134
+ return git_next_major_version (verbose )
131
135
132
- def git_count (branch ):
136
+ def git_count (branch , verbose ):
133
137
"""Count the number of commits in the repository.
134
138
135
139
Note: This does not work right for shallow git clones.
@@ -145,14 +149,15 @@ def git_count(branch):
145
149
return stdout .count ('\n ' )
146
150
147
151
except :
148
- print "Warning: could not determine commit count on branch '%s'" % branch
152
+ if verbose :
153
+ print "Warning: could not determine commit count on branch '%s'" % branch
149
154
150
- def package_version ():
155
+ def package_version (verbose ):
151
156
"""Interprets the package version from the directory name"""
152
157
153
158
dirname = os .path .basename (os .path .realpath (os .curdir ))
154
159
155
- match = re .match (r'^bob[-_](\d+\.\d+\.\d+[abcd]\d+)$' , dirname )
160
+ match = re .match (r'^bob[-_](\d+\.\d+\.\d+( [abcd]\d+)? )$' , dirname )
156
161
157
162
if match : return match .groups ()[0 ]
158
163
@@ -164,8 +169,9 @@ def package_version():
164
169
165
170
parser = optparse .OptionParser ()
166
171
parser .add_option ("-v" , "--version" , dest = "version" , metavar = 'VERSION' , help = "force version to a given string (format M.m.p; by default calculates the version number of the next build based on the currently existing branch and tags)" , type = 'str' )
167
- parser .add_option ("-l" , "--letter" , dest = "letter" , metavar = 'LETTER' , choices = ('a' , 'b' , 'c' ), default = 'a' , help = "force the suffix letter to be one of ([a]alpha, [b]beta, [c]candidate; defaults to 'a' )" )
172
+ parser .add_option ("-l" , "--letter" , dest = "letter" , metavar = 'LETTER' , choices = ('a' , 'b' , 'c' ), help = "force the suffix letter to be one of ([a]alpha, [b]beta, [c]candidate; defaults to None )" )
168
173
parser .add_option ("-c" , "--counter" , dest = "count" , metavar = 'COUNT' , type = 'int' , help = "force the counter after the letter (by default use number of commits in the current branch)" )
174
+ parser .add_option ("-V" , "--verbose" , dest = "verbose" , default = False , action = 'store_true' , help = "be verbose about potential issues (warnings)" )
169
175
170
176
(options , args ) = parser .parse_args ()
171
177
@@ -182,22 +188,27 @@ def package_version():
182
188
# change directories to my parent's
183
189
os .chdir (os .path .dirname (os .path .dirname (os .path .realpath (sys .argv [0 ]))))
184
190
185
- branch = git_current_branch ()
191
+ branch = git_current_branch (options . verbose )
186
192
187
193
if options .version is None :
188
- options .version = package_version ()
194
+ options .version = package_version (options . verbose )
189
195
190
196
if not options .version :
191
197
if branch is not None :
192
- options .version = git_next_version (branch )
198
+ options .version = git_next_version (branch , options . verbose )
193
199
194
200
if options .count is None :
195
- options .count = git_count (branch )
201
+ options .count = git_count (branch , options . verbose )
196
202
197
- if not options .version : print 'unknown'
198
- else :
203
+ if not options .version :
204
+ print 'unknown'
205
+ elif options .letter :
199
206
final = options .version + options .letter + str (options .count )
200
207
StrictVersion (final ) #double-checks all is good
201
208
print final
209
+ else :
210
+ final = options .version
211
+ StrictVersion (final ) #double-checks all is good
212
+ print final
202
213
203
214
sys .exit (0 )
0 commit comments