Fetch version info from git or Build file

This commit is contained in:
caryoscelus 2023-12-27 11:27:15 +00:00
parent dbe283e593
commit 1a8c6aaa93
No known key found for this signature in database
GPG key ID: 254EDDB85B66CB1F
4 changed files with 126 additions and 2 deletions

View file

@ -16,3 +16,4 @@ defusedxml>=0.7
pyaes pyaes
requests requests
ipython>=8 ipython>=8
GitPython

View file

@ -1,6 +1,7 @@
import argparse import argparse
import sys import sys
import os import os
import platform
import locale import locale
import re import re
import configparser import configparser
@ -9,9 +10,21 @@ import logging.handlers
import stat import stat
import time import time
class Config(object): class Config:
def __init__(self, argv): 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.version = "0.7.10+"
self.user_agent = "conservancy" self.user_agent = "conservancy"
# DEPRECATED ; replace with git-generated commit # 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('--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('--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') self.parser.add_argument('--end', help='Stop multi value argument parsing', action='store_true')
return self.parser return self.parser

47
src/buildinfo.py Executable file
View file

@ -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 <https://www.gnu.org/licenses/>.
##
"""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]} <build-type>')
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)

62
src/util/Git.py Normal file
View file

@ -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 <https://www.gnu.org/licenses/>.
##
"""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