1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#-------------------------------------------------------------------------------
# This file is part of Code_Saturne, a general-purpose CFD tool.
#
# Copyright (C) 1998-2013 EDF S.A.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
# Street, Fifth Floor, Boston, MA 02110-1301, USA.
#-------------------------------------------------------------------------------
import sys
import os.path
#-------------------------------------------------------------------------------
# Generate version strings, using local and optionally version control info.
# Notes:
#-------
# Currently, basic version information is determined from the NEWS file.
# This is not quite standard (though methods and automation level used by other
# tools vary widely), but we want to minimize files that need to be edited
# when releasing a version, but use a robust mechanism which will work
# for versions pulled by different version control tools (at least
# Subversion, used for the main repository, git svn clones, which may or
# may not have up-to-date fetched remotes information, and clones of those
# clones...). As we need to maintain the NEWS file anyways to provide a clear
# user-readable summary of changes between versions, using a consistent
# presentation, using this file to determine version information is a
# practical solution, which requires enforcing that the NEWS file is
# maintained correctly, which is good practice.
#===============================================================================
# Utility functions
#===============================================================================
#-------------------------------------------------------------------------------
def version_from_news(srcdir):
"""
Determine version information from NEWS file.
"""
major = 'unknown'
minor = ''
release = ''
extra = ''
trunk = -1
version = [-1]
release = [-1, '', '', '']
f = open(os.path.join(srcdir, "NEWS"))
lines = f.readlines()
f.close()
for i in range(1, len(lines)):
# Release names are underlined by '==='
if lines[i][0:7] == '=======':
j = i-1
l = lines[j]
if l[0:6] == 'Trunk ' and trunk < 0:
trunk = j
# If release appears, at which line, and is there a date,
# or is it not yet released ?
elif l[0:8] == 'Release ' or l[0:8] == 'Version ':
v, n, info = l.split(' ', 2)
if version[0] < 0:
version = [j] + n.split('.')
if release[0] < 0:
try:
dateinfo = info.split('(')[1].split(')')[0].split()
num_count = 0
for d in dateinfo:
try:
if int(d) > 0:
num_count += 1
except Exception:
pass
if num_count > 1:
release = [j] + n.split('.')
break
except Exception:
pass
if trunk > -1:
if version[0] > -1:
major, minor = trunk_major_minor(version[1], version[2])
release = ''
extra = '-alpha'
elif version[0] > -1:
major, minor, release, extra \
= branch_release_extra(version[1], version[2], version[3],
release[1], release[2], release[3])
return major, minor, release, extra
#-------------------------------------------------------------------------------
def trunk_major_minor(prev_major, prev_minor):
"""
For trunk, determine next version based on previous branches
"""
# Current release cycle plans for 4 minor releases per major release
major = int(prev_major)
minor = int(prev_minor) + 1
if minor > 3:
major += 1
minor = 0
return str(major), str(minor)
#-------------------------------------------------------------------------------
def branch_release_extra(cur_major, cur_minor, cur_release,
prev_major, prev_minor, prev_release):
"""
For branch, determine next version based on released versions
"""
major = cur_major
minor = cur_minor
release = cur_release
extra = ''
# Same version, next release
if major == prev_major and minor == prev_minor:
release = prev_release
if cur_release > prev_release:
extra = '-patch'
# Different version, or, no previous release
else:
try:
if int(cur_release) == 0:
release = ''
extra = '-beta'
except Exception:
pass
return major, minor, release, extra
#-------------------------------------------------------------------------------
def svn_version_from_branch(branch):
"""
Determine branch version number from Subversion branch
"""
major = ''
minor = ''
if branch[0:7] == 'Version':
v = branch[7:]
sep = v.find('_')
if (sep > -1):
major = v[0:sep]
minor = v[sep+1:]
return major, minor
#-------------------------------------------------------------------------------
def svn_version_from_tag(tag):
"""
Determine branch version number from Subversion tag
"""
major = ''
minor = ''
release = ''
extra = ''
if tag[0] == 'V':
v = tag[1:].split('_', 3)
if len(v) >= 1:
major = v[0]
if len(v) >= 2:
minor = v[1]
if len(v) >= 3:
if v[2] > '-1':
release = v[2]
if len(v) >= 4:
extra = v[3]
return major, minor, release, extra
#-------------------------------------------------------------------------------
def svn_version(srcdir, defaults):
"""
Determine version information from Subversion.
"""
# Use default info to pre-determine next version
major, minor, release, extra = defaults
import subprocess
# Use the XML output from Subversion, to avoid formatting/language
# environment parsing issues
try:
from xml.dom import minidom
except Exception:
return major, minor, release, extra, revision
# Get base info from 'svn info'
url = None
revision = ''
cmd = ['svn', 'info', '--xml', srcdir]
p = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
output = p.communicate()
if p.returncode != 0:
return major, minor, release, extra, revision
else:
try:
svn_info = minidom.parseString(output[0]).documentElement
url = svn_info.getElementsByTagName("url").item(0).firstChild.data
entry_node = svn_info.getElementsByTagName("entry")[0]
revision = entry_node.getAttribute("revision")
except Exception:
pass
if not url:
return major, minor, release, extra, revision
# If we are in trunk, use previous local info to determine next version
# If we are in a branch, use tags to determine next version
if url.find('/branches/') > -1:
url_id_0 = url.find('/branches/')
url_id_1 = url_id_0 + len('/branches/')
url_root = url[:url_id_0]
if url[url_id_1:url_id_1+7] == 'Version':
major, minor = svn_version_from_branch(url[url_id_1:])
cmd = ['svn', 'ls', '--xml', url_root + '/tags']
p = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
output = p.communicate()
if p.returncode != 0:
return major, minor, release, extra, revision
else:
try:
tag_ref = 'V' + str(major) + '_' + str(minor) + '_'
tag_ref_l = len(tag_ref)
name_max = tag_ref + '-1'
release = ''
svn_info = minidom.parseString(output[0]).documentElement
for tag in svn_info.getElementsByTagName("name"):
name = tag.firstChild.data
if name[0:tag_ref_l] == tag_ref and name > name_max:
rev_node = tag.parentNode.getElementsByTagName("commit")[0]
tag_rev = rev_node.getAttribute("revision")
if (tag_rev == revision):
release = tag[tag_ref_l:]
break
elif tag_rev < revision:
name_max = name
major, minor, release, extra = svn_version_from_tag(name_max)
if not release:
extra += '-beta'
if tag_rev == revision:
revision = ''
except Exception:
pass
# If we are at a tag, simply use if
elif url.find('/tags/') > -1:
url_id_0 = url.find('/tags/')
url_id_1 = url_id_0 + len('/tags/')
tag = url[url_id_1:]
major, minor, release, extra = svn_version_from_tag(url[url_id_1:])
revision = ''
if revision:
revision = '-r' + revision
else:
revision = ''
return major, minor, release, extra, revision
#-------------------------------------------------------------------------------
def svn_version_is_modified(srcdir):
"""
Determine if version is modifed relative to Subversion revision
(requires a local operation only).
"""
import subprocess
p = subprocess.Popen(['svn', 'status', '--xml', srcdir],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
output = p.communicate()
if p.returncode == 0:
if output[0].find('entry') > -1:
return True
return False
#-------------------------------------------------------------------------------
def git_svn_version(srcdir, url, revision, defaults):
"""
Determine partial version information from Subversion.
"""
# Use known info to pre-determine next version
major, minor, release, extra = defaults
import subprocess
# If we are in a branch, use tags if available to determine next version
if url.find('/branches/') > -1:
url_id_0 = url.find('/branches/')
url_id_1 = url_id_0 + len('/branches/')
url_root = url[:url_id_0]
if url[url_id_1:url_id_1+7] == 'Version':
major, minor = svn_version_from_branch(url[url_id_1:])
revision = '-svn' + revision
# If we are at a tag, simply use if
elif url.find('/tags/') > -1:
url_id_0 = url.find('/tags/')
url_id_1 = url_id_0 + len('/tags/')
tag = url[url_id_1:]
major, minor, release, extra = svn_version_from_tag(url[url_id_1:])
revision = ''
# If we are in trunk, branch data might not be available to determine
# next version, so use defaults
else:
revision = '-svn' + revision
return major, minor, release, extra, revision
#-------------------------------------------------------------------------------
def git_version(srcdir, defaults):
"""
Determine version information from Git.
"""
# Use known info to pre-determine next version
major, minor, release, extra = defaults
import subprocess
# Get base info from 'svn info'
url = None
revision = ''
head_is_svn = False
cmd = ['git', '--no-pager', '--git-dir='+os.path.join(srcdir, '.git'),
'--work-tree='+srcdir, 'log', '-n', '200']
p = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
output = p.communicate()
if p.returncode != 0:
return major, minor, release, extra
else:
svn_id = output[0].find('git-svn-id:')
if svn_id > 1:
svn_rev = output[0][svn_id:].split(' ', 2)[1]
svn_url, svn_revision = svn_rev.split('@')
major, minor, release, extra, revision \
= git_svn_version(srcdir, svn_url, svn_revision, defaults)
# Try to count commits before the one with svn
svn_log = output[0][:svn_id].split()
c1 = svn_log.count('commit')
c2 = svn_log.count('Author:')
if c1 == 1 and c2 == 1:
head_is_svn = True
if not head_is_svn:
commit_id = output[0].find('commit')
commit_rev = output[0][commit_id:].split(' ', 2)[1].split('\n')[0]
revision += '-git' + commit_rev
return major, minor, release, extra, revision
#-------------------------------------------------------------------------------
def git_version_is_modified(srcdir):
"""
Determine if version is modifed relative to Git commit
(requires a local operation only).
"""
import subprocess
cmd = ['git', '--no-pager', '--git-dir='+os.path.join(srcdir, '.git'),
'--work-tree='+srcdir, 'status', '-s', '-uno']
p = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
output = p.communicate()
if p.returncode == 0:
if len(output[0].split('\n')) > 1:
return True
return False
#-------------------------------------------------------------------------------
def build_version_string(major, minor, release, extra, revision = None):
"""
Build version string from substrings.
"""
version = major + '.' + minor
if release:
version += '.' + release
if extra:
version += extra
if revision:
version += revision
return version
#-------------------------------------------------------------------------------
# Main
#-------------------------------------------------------------------------------
if __name__ == '__main__':
import sys
srcdir = os.path.split(os.path.abspath(os.path.dirname(sys.argv[0])))[0]
# Default command: return simple string based on local information
if len(sys.argv) == 1:
major, minor, release, extra = version_from_news(srcdir)
print(build_version_string(major, minor, release, extra))
elif sys.argv[1] == '--short':
major, minor, release, extra = version_from_news(srcdir)
print(build_version_string(major, minor, '', extra))
# Call tools
elif sys.argv[1] in ['--full', '--revision-only', '--verbose']:
defaults = version_from_news(srcdir)
modified = ''
revision = ''
if os.path.isdir(os.path.join(srcdir, ".svn")):
major, minor, release, extra, revision \
= svn_version(srcdir, defaults)
modified = svn_version_is_modified(srcdir)
elif os.path.isdir(os.path.join(srcdir, ".git")):
major, minor, release, extra, revision \
= git_version(srcdir, defaults)
modified = git_version_is_modified(srcdir)
else:
major, minor, release, extra = version_from_news(srcdir)
if sys.argv[1] == '--full':
if modified:
revision += '-m'
print(build_version_string(major, minor, release, extra, revision))
elif sys.argv[1] == '--revision-only':
if modified:
revision += '-m'
print(revision[1:]) # remove first '-' character
elif sys.argv[1] == '--verbose':
print('major: ' + major)
print('minor: ' + minor)
print('release: ' + release)
print('extra: ' + extra)
print('revision: ' + revision[1:])
print('modified: ' + str(modified))
else:
usage = \
"""Usage: %(prog)s [option]
Default: print version string
Options:
--full print full version string with available version control info
--short print short version string with major, minor, and extras
--revision-only print version string with version control info only
--verbose print fields on different lines
Options:
-h, --help show this help message and exit"""
print(usage % {'prog':sys.argv[0]})
sys.exit(1)
sys.exit(0)
#-------------------------------------------------------------------------------
# End
#-------------------------------------------------------------------------------
|