blob: ef734ddb6d1b2cb7cf8feddcb8ad08c931deddde [file] [log] [blame]
Spyridon Mastorakisa1d135b2015-08-20 20:24:59 -07001# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
2
3from waflib.Configure import conf
4from waflib import Utils
5import os
6
7def splitVersion(version):
8 base = version.split('-')[0]
9 split = [v for v in base.split('.')]
10 return base, version, split
11
12@conf
13def getVersion(conf, submodule, **kw):
14 tagPrefix = kw.get('tag', '%s-' % submodule)
15 baseVersion = kw.get('base_version', '0.0.0')
16 submodule = conf.path.find_node(submodule)
17
18 gitVersion = baseVersion
19
20 didGetVersion = False
21 try:
22 cmd = ['git', 'describe', '--always', '--match', '%s*' % tagPrefix]
23 p = Utils.subprocess.Popen(cmd, stdout=Utils.subprocess.PIPE,
24 cwd=submodule.abspath(),
25 stderr=None, stdin=None)
26 out = str(p.communicate()[0].strip())
27 didGetVersion = (p.returncode == 0 and out != "")
28 if didGetVersion:
29 if out.startswith(tagPrefix):
30 gitVersion = out[len(tagPrefix):]
31 else:
32 gitVersion = "%s-commit-%s" % (baseVersion, out)
33 except OSError:
34 pass
35
36 versionFile = submodule.find_node('VERSION')
37
38 if not didGetVersion and versionFile is not None:
39 try:
40 return splitVersion(versionFile.read())
41 except (OSError, IOError):
42 pass
43
44 # version was obtained from git, update VERSION file if necessary
45 if versionFile is not None:
46 try:
47 version = versionFile.read()
48 versionFile = None # no need to update
49 except (OSError, IOError):
50 Logs.warn("VERSION file exists, but not readable")
51 else:
52 versionFile = submodule.make_node('VERSION')
53
54 if versionFile:
55 try:
56 versionFile.write(gitVersion)
57 except (OSError, IOError):
58 Logs.warn("VERSION file is not writeable")
59
60 return splitVersion(gitVersion)