Merge branch '16080-github' refs #16080
[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 builtins import str
6 from builtins import next
7
8 from setuptools.command.egg_info import egg_info
9 import subprocess
10 import time
11 import os
12
13 SETUP_DIR = os.path.dirname(__file__) or '.'
14
15 def choose_version_from():
16     sdk_ts = subprocess.check_output(
17         ['git', 'log', '--first-parent', '--max-count=1',
18          '--format=format:%ct', os.path.join(SETUP_DIR, "../python")]).strip()
19     cwl_ts = subprocess.check_output(
20         ['git', 'log', '--first-parent', '--max-count=1',
21          '--format=format:%ct', SETUP_DIR]).strip()
22     if int(sdk_ts) > int(cwl_ts):
23         getver = os.path.join(SETUP_DIR, "../python")
24     else:
25         getver = SETUP_DIR
26     return getver
27
28 class EggInfoFromGit(egg_info):
29     """Tag the build with git commit timestamp.
30
31     If a build tag has already been set (e.g., "egg_info -b", building
32     from source package), leave it alone.
33     """
34     def git_latest_tag(self):
35         gittags = subprocess.check_output(['git', 'tag', '-l']).split()
36         gittags.sort(key=lambda s: [int(u) for u in s.split(b'.')],reverse=True)
37         return str(next(iter(gittags)).decode('utf-8'))
38
39     def git_timestamp_tag(self):
40         gitinfo = subprocess.check_output(
41             ['git', 'log', '--first-parent', '--max-count=1',
42              '--format=format:%ct', choose_version_from()]).strip()
43         return time.strftime('.%Y%m%d%H%M%S', time.gmtime(int(gitinfo)))
44
45     def tags(self):
46         if self.tag_build is None:
47             self.tag_build = self.git_latest_tag() + self.git_timestamp_tag()
48         return egg_info.tags(self)