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