Spyridon Mastorakis | a1d135b | 2015-08-20 20:24:59 -0700 | [diff] [blame] | 1 | # -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*- |
| 2 | |
| 3 | from waflib.Configure import conf |
| 4 | from waflib import Utils |
| 5 | import os |
| 6 | |
| 7 | def splitVersion(version): |
| 8 | base = version.split('-')[0] |
| 9 | split = [v for v in base.split('.')] |
| 10 | return base, version, split |
| 11 | |
| 12 | @conf |
| 13 | def 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) |
Alexander Afanasyev | f007a99 | 2022-05-05 15:57:08 -0400 | [diff] [blame] | 26 | out = p.communicate()[0].strip().decode('utf-8') |
Spyridon Mastorakis | a1d135b | 2015-08-20 20:24:59 -0700 | [diff] [blame] | 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 | |
Alexander Afanasyev | f007a99 | 2022-05-05 15:57:08 -0400 | [diff] [blame] | 36 | versionFile = submodule.find_node('VERSION.info') |
Spyridon Mastorakis | a1d135b | 2015-08-20 20:24:59 -0700 | [diff] [blame] | 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: |
Alexander Afanasyev | f007a99 | 2022-05-05 15:57:08 -0400 | [diff] [blame] | 52 | versionFile = submodule.make_node('VERSION.info') |
Spyridon Mastorakis | a1d135b | 2015-08-20 20:24:59 -0700 | [diff] [blame] | 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) |