From 1a8c6aaa939fb97591944b04c6ecea48d46f5a72 Mon Sep 17 00:00:00 2001 From: caryoscelus Date: Wed, 27 Dec 2023 11:27:15 +0000 Subject: [PATCH] Fetch version info from git or Build file --- requirements.txt | 1 + src/Config.py | 18 ++++++++++++-- src/buildinfo.py | 47 ++++++++++++++++++++++++++++++++++++ src/util/Git.py | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 126 insertions(+), 2 deletions(-) create mode 100755 src/buildinfo.py create mode 100644 src/util/Git.py diff --git a/requirements.txt b/requirements.txt index b9eb9fc9..c5ecfae2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -16,3 +16,4 @@ defusedxml>=0.7 pyaes requests ipython>=8 +GitPython diff --git a/src/Config.py b/src/Config.py index fee378d0..768443e5 100644 --- a/src/Config.py +++ b/src/Config.py @@ -1,6 +1,7 @@ import argparse import sys import os +import platform import locale import re import configparser @@ -9,9 +10,21 @@ import logging.handlers import stat import time -class Config(object): +class Config: def __init__(self, argv): + try: + from . import Build + except ImportError: + print('cannot find build') + from .util import Git + self.build_type = 'source' + self.branch = Git.branch() or 'unknown' + self.commit = Git.commit() or 'unknown' + else: + self.build_type = Build.build_type + self.branch = Build.branch + self.commit = Build.commit self.version = "0.7.10+" self.user_agent = "conservancy" # DEPRECATED ; replace with git-generated commit @@ -306,7 +319,8 @@ class Config(object): self.parser.add_argument('--tor-hs-port', help='Hidden service port in Tor always mode', metavar='limit', type=int, default=15441) self.parser.add_argument('--repl', help='Instead of printing logs in console, drop into REPL after initialization', action='store_true') - self.parser.add_argument('--version', action='version', version=f'zeronet-conservancy {self.version} r{self.rev}') + self.parser.add_argument('--version', action='version', + version=f'zeronet-conservancy {self.version} ({self.build_type} from {self.branch}-{self.commit})') self.parser.add_argument('--end', help='Stop multi value argument parsing', action='store_true') return self.parser diff --git a/src/buildinfo.py b/src/buildinfo.py new file mode 100755 index 00000000..38de4cae --- /dev/null +++ b/src/buildinfo.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python3 + +## Copyright (c) 2023 caryoscelus +## +## zeronet-conservancy is free software: you can redistribute it and/or modify it under the +## terms of the GNU General Public License as published by the Free Software +## Foundation, either version 3 of the License, or (at your option) any later version. +## +## zeronet-conservancy is distributed in the hope that it will be useful, but +## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +## details. +## +## You should have received a copy of the GNU General Public License along with +## zeronet-conservancy. If not, see . +## + +"""A small script to generate build info""" + +from util import Git + +import sys + +def main(argv): + if len(argv) < 2: + print(f'Useage: {argv[0]} ') + print('Known build types:') + sys.exit(1) + else: + writeBuildInfo(argv[1]) + +def writeBuildInfo(build_type): + bvars = { + 'build_type': build_type, + 'branch': Git.branch(), + 'commit': Git.commit(), + } + code = '\n'.join(f'{var} = {repr(val)}' for var, val in bvars.items()) + content = \ + '# auto-generated by buildinfo.py\n' \ + '# This file should not persist in git tree\n' + code + '\n' + with open('src/Build.py', 'w') as f: + f.write(content) + +if __name__ == '__main__': + from sys import argv + main(argv) diff --git a/src/util/Git.py b/src/util/Git.py new file mode 100644 index 00000000..7b60d396 --- /dev/null +++ b/src/util/Git.py @@ -0,0 +1,62 @@ +## Copyright (c) 2023 caryoscelus +## +## zeronet-conservancy is free software: you can redistribute it and/or modify it under the +## terms of the GNU General Public License as published by the Free Software +## Foundation, either version 3 of the License, or (at your option) any later version. +## +## zeronet-conservancy is distributed in the hope that it will be useful, but +## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +## details. +## +## You should have received a copy of the GNU General Public License along with +## zeronet-conservancy. If not, see . +## + +"""Git-related operations + +Currently this is only to retrieve git revision for debug purposes, but later on we might +also want to use it for updates. +""" + +import os + +from typing import Optional + +global git + +try: + import git +except ImportError: + git = None +else: + try: + global _repo + up = os.path.dirname + root = up(up(up(__file__))) + print(root) + _repo = git.Repo(root) + except Exception as exc: + print("Caught exception while trying to detect git repo.") + traceback.print_exc() + git = None + +def _gitted(f): + if git: + return f + else: + return lambda *args, **kwargs: None + +@_gitted +def commit() -> str: + """Returns git revision, possibly suffixed with -dirty""" + dirty = '-dirty' if _repo.is_dirty() else '' + return f'{_repo.head.commit}{dirty}' + +@_gitted +def branch() -> Optional[str]: + """Returns current git branch if any""" + try: + return str(_repo.active_branch) + except TypeError: + return None