1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
#!/usr/bin/python3
# SPDX-License-Identifier: BSD-2-Clause
# Copyright (C) 2021-2025, Raspberry Pi Ltd.
#
# Generate version information for libpisp
import os
import subprocess
import sys
import time
from datetime import datetime
from string import hexdigits
digits = 12
def generate_version():
try:
if len(sys.argv) == 2:
# Check if this is a git directory
r = subprocess.run(['git', 'rev-parse', '--git-dir'],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, universal_newlines=True)
if r.returncode:
raise RuntimeError('Invalid git directory!')
# Get commit id
r = subprocess.run(['git', 'rev-parse', '--verify', 'HEAD'],
stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, universal_newlines=True)
if r.returncode:
raise RuntimeError('Invalid git commit!')
commit = r.stdout.strip('\n')[0:digits]
# Check dirty status
r = subprocess.run(['git', 'diff-index', '--quiet', 'HEAD'],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, universal_newlines=True)
if r.returncode:
commit = commit + '-dirty'
else:
raise RuntimeError('Invalid number of command line arguments')
commit = f'v{sys.argv[1]} {commit}'
except RuntimeError as e:
commit = f'v{sys.argv[1]}'
finally:
date_str = time.strftime(
"%d-%m-%Y (%H:%M:%S)",
time.gmtime(int(os.environ.get('SOURCE_DATE_EPOCH', time.time())))
)
print(f'{commit} {date_str}', end="")
if __name__ == "__main__":
generate_version()
|