Make sure the dev version prefix is based on the latest tag in the repo, not
[arvados.git] / sdk / cwl / gittaggers.py
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: Apache-2.0
4
5 from setuptools.command.egg_info import egg_info
6 import subprocess
7 import time
8 import os
9
10 SETUP_DIR = os.path.dirname(__file__) or '.'
11
12 def choose_version_from():
13     sdk_ts = subprocess.check_output(
14         ['git', 'log', '--first-parent', '--max-count=1',
15          '--format=format:%ct', os.path.join(SETUP_DIR, "../python")]).strip()
16     cwl_ts = subprocess.check_output(
17         ['git', 'log', '--first-parent', '--max-count=1',
18          '--format=format:%ct', SETUP_DIR]).strip()
19     if int(sdk_ts) > int(cwl_ts):
20         getver = os.path.join(SETUP_DIR, "../python")
21     else:
22         getver = SETUP_DIR
23     return getver
24
25 class EggInfoFromGit(egg_info):
26     """Tag the build with git commit timestamp.
27
28     If a build tag has already been set (e.g., "egg_info -b", building
29     from source package), leave it alone.
30     """
31     def git_latest_tag(self):
32         gittags = subprocess.check_output(['git', 'tag', '-l']).split()
33         gittags.sort(key=lambda s: [int(u) for u in s.split(b'.')],reverse=True)
34         return str(next(iter(gittags)).decode('utf-8'))
35
36     def git_timestamp_tag(self):
37         gitinfo = subprocess.check_output(
38             ['git', 'log', '--first-parent', '--max-count=1',
39              '--format=format:%ct', choose_version_from()]).strip()
40         return time.strftime('.%Y%m%d%H%M%S', time.gmtime(int(gitinfo)))
41
42     def tags(self):
43         if self.tag_build is None:
44             self.tag_build = self.git_latest_tag() + self.git_timestamp_tag()
45         return egg_info.tags(self)