Merge branch '16202-shell-suffix' refs #16202
[arvados.git] / sdk / python / 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
9 class EggInfoFromGit(egg_info):
10     """Tag the build with git commit timestamp.
11
12     If a build tag has already been set (e.g., "egg_info -b", building
13     from source package), leave it alone.
14     """
15     def git_latest_tag(self):
16         gittags = subprocess.check_output(['git', 'tag', '-l']).split()
17         gittags.sort(key=lambda s: [int(u) for u in s.split(b'.')],reverse=True)
18         return str(next(iter(gittags)).decode('utf-8'))
19
20     def git_timestamp_tag(self):
21         gitinfo = subprocess.check_output(
22             ['git', 'log', '--first-parent', '--max-count=1',
23              '--format=format:%ct', '.']).strip()
24         return time.strftime('.%Y%m%d%H%M%S', time.gmtime(int(gitinfo)))
25
26     def tags(self):
27         if self.tag_build is None:
28             self.tag_build = self.git_latest_tag()+self.git_timestamp_tag()
29         return egg_info.tags(self)