5175: Do not add +sha1 tag unless --sha1-tag flag given.
[arvados.git] / sdk / python / gittaggers.py
1 from setuptools.command.egg_info import egg_info
2 import subprocess
3 import time
4
5 class GitTagger(egg_info):
6     """Tag the build with git commit info.
7
8     Exact choice and format is determined by subclass's tags_to_add
9     method.
10
11     If a build tag has already been set (e.g., "egg_info -b", building
12     from source package), leave it alone.
13     """
14     def git_commit_info(self):
15         gitinfo = subprocess.check_output(
16             ['git', 'log', '--first-parent', '--max-count=1',
17              '--format=format:%ct %h', '.']).split()
18         assert len(gitinfo) == 2
19         return {
20             'commit_utc': time.strftime(
21                 '%Y%m%d%H%M%S', time.gmtime(int(gitinfo[0]))),
22             'commit_sha1': gitinfo[1],
23         }
24
25     def tags(self):
26         if self.tag_build is None:
27             self.tag_build = self.tags_to_add()
28         return egg_info.tags(self)
29
30
31 class TagBuildWithCommitDateAndSha1(GitTagger):
32     """Tag the build with the sha1 and date of the last git commit."""
33     def tags_to_add(self):
34         return '.{commit_utc}+{commit_sha1}'.format(**self.git_commit_info())
35
36
37 class TagBuildWithCommitDate(GitTagger):
38     """Tag the build with the date of the last git commit."""
39     def tags_to_add(self):
40         return '.{commit_utc}'.format(**self.git_commit_info())