|
| 1 | +#! /usr/bin/env python3 |
| 2 | + |
| 3 | +# Daemon BSD Source Code |
| 4 | +# Copyright (c) 2024, Daemon Developers |
| 5 | +# All rights reserved. |
| 6 | +# |
| 7 | +# Redistribution and use in source and binary forms, with or without |
| 8 | +# modification, are permitted provided that the following conditions are met: |
| 9 | +# * Redistributions of source code must retain the above copyright |
| 10 | +# notice, this list of conditions and the following disclaimer. |
| 11 | +# * Redistributions in binary form must reproduce the above copyright |
| 12 | +# notice, this list of conditions and the following disclaimer in the |
| 13 | +# documentation and/or other materials provided with the distribution. |
| 14 | +# * Neither the name of the <organization> nor the |
| 15 | +# names of its contributors may be used to endorse or promote products |
| 16 | +# derived from this software without specific prior written permission. |
| 17 | +# |
| 18 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 19 | +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 20 | +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 | +# DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY |
| 22 | +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 23 | +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 24 | +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 25 | +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 | +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | + |
| 29 | +import argparse |
| 30 | +import datetime |
| 31 | +import os |
| 32 | +import subprocess |
| 33 | +import sys |
| 34 | +import time |
| 35 | + |
| 36 | +class RepoVersion(): |
| 37 | + git_short_ref_length = 7 |
| 38 | + |
| 39 | + def __init__(self, repository_dir, is_permissive=False, is_quiet=False): |
| 40 | + if not os.path.isdir(repository_dir): |
| 41 | + raise(ValueError, "not a directory") |
| 42 | + |
| 43 | + self.is_permissive = is_permissive |
| 44 | + self.process_stderr = None |
| 45 | + |
| 46 | + if is_quiet: |
| 47 | + self.process_stderr = subprocess.DEVNULL |
| 48 | + |
| 49 | + self.git_command_list = ["git", "-C", repository_dir] |
| 50 | + |
| 51 | + def runGitCommand(self, command_list, is_permissive=False): |
| 52 | + command_list = self.git_command_list + command_list |
| 53 | + |
| 54 | + process_check = not ( self.is_permissive or is_permissive ) |
| 55 | + |
| 56 | + process = subprocess.run(command_list, |
| 57 | + stdout=subprocess.PIPE, stderr=self.process_stderr, check=process_check, text=True) |
| 58 | + |
| 59 | + return process.stdout.rstrip(), process.returncode |
| 60 | + |
| 61 | + def getDateString(self, timestamp): |
| 62 | + return datetime.datetime.utcfromtimestamp(timestamp).strftime('%Y%m%d-%H%M%S') |
| 63 | + |
| 64 | + def getVersionString(self): |
| 65 | + tag_string="0" |
| 66 | + date_string="-" + self.getDateString(time.time()) |
| 67 | + ref_string="-0" |
| 68 | + dirt_string="+dirty" |
| 69 | + |
| 70 | + git_last_commit_string, git_last_commit_returncode \ |
| 71 | + = self.runGitCommand(["rev-parse", "HEAD", "--"]) |
| 72 | + |
| 73 | + if git_last_commit_returncode == 0: |
| 74 | + git_last_commit_short_string = git_last_commit_string[:self.git_short_ref_length] |
| 75 | + ref_string = "-" + git_last_commit_short_string |
| 76 | + |
| 77 | + git_last_commit_timestamp_string, git_last_commit_timestamp_returncode \ |
| 78 | + = self.runGitCommand(["log", "-1", "--pretty=format:%ct"]) |
| 79 | + |
| 80 | + if git_last_commit_timestamp_returncode == 0: |
| 81 | + date_string = "-" + self.getDateString(int(git_last_commit_timestamp_string)) |
| 82 | + |
| 83 | + git_closest_tag_string, git_closest_tag_returncode \ |
| 84 | + = self.runGitCommand(["describe", "--tags", "--abbrev=0", "--match", "v[0-9].*"]) |
| 85 | + |
| 86 | + if git_closest_tag_returncode == 0: |
| 87 | + git_closest_tag_version_string = git_closest_tag_string[1:] |
| 88 | + tag_string = git_closest_tag_version_string |
| 89 | + |
| 90 | + git_describe_tag_string, git_describe_tag_returncode \ |
| 91 | + = self.runGitCommand(["describe", "--tags", "--match", "v[0-9].*"]) |
| 92 | + git_describe_version_string = git_describe_tag_string[1:] |
| 93 | + |
| 94 | + if git_describe_tag_returncode == 0: |
| 95 | + if git_closest_tag_version_string == git_describe_version_string: |
| 96 | + date_string = "" |
| 97 | + ref_string = "" |
| 98 | + |
| 99 | + git_diff_quiet_string, git_diff_quiet_returncode \ |
| 100 | + = self.runGitCommand(["diff", "--quiet"], is_permissive=True) |
| 101 | + |
| 102 | + if git_diff_quiet_returncode == 0: |
| 103 | + dirt_string = "" |
| 104 | + |
| 105 | + return tag_string + date_string + ref_string + dirt_string |
| 106 | + |
| 107 | +if __name__ == "__main__": |
| 108 | + parser = argparse.ArgumentParser(description="Print repository version string") |
| 109 | + |
| 110 | + parser.add_argument("-p", "--permissive", dest="is_permissive", help="ignore Git errors", action="store_true") |
| 111 | + parser.add_argument("-q", "--quiet", dest="is_quiet", help="silence Git errors", action="store_true") |
| 112 | + parser.add_argument(dest="repository_dir", nargs="?", metavar="DIRNAME", default=".", help="repository path") |
| 113 | + |
| 114 | + args = parser.parse_args() |
| 115 | + |
| 116 | + repoVersion = RepoVersion(args.repository_dir, is_permissive=args.is_permissive, is_quiet=args.is_quiet) |
| 117 | + print(repoVersion.getVersionString()) |
0 commit comments