Merge pull request #256 from caryoscelus/git

Fetch version info from git or Build file
This commit is contained in:
caryoscelus 2023-12-28 20:22:22 +00:00 committed by GitHub
commit 031dd1946f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 137 additions and 16 deletions

View file

@ -31,7 +31,7 @@ class LocalAnnouncer(BroadcastServer.BroadcastServer):
self.sender_info["peer_id"] = self.server.peer_id self.sender_info["peer_id"] = self.server.peer_id
self.sender_info["port"] = self.server.port self.sender_info["port"] = self.server.port
self.sender_info["broadcast_port"] = listen_port self.sender_info["broadcast_port"] = listen_port
self.sender_info["rev"] = config.rev self.sender_info["rev"] = config.user_agent_rev
self.known_peers = {} self.known_peers = {}
self.last_discover = 0 self.last_discover = 0

View file

@ -41,7 +41,7 @@ class UiRequestPlugin(object):
from Crypt import CryptConnection from Crypt import CryptConnection
# Memory # Memory
yield "rev%s | " % config.rev yield f'{config.version_full} | '
yield "%s | " % main.file_server.ip_external_list yield "%s | " % main.file_server.ip_external_list
yield "Port: %s | " % main.file_server.port yield "Port: %s | " % main.file_server.port
yield "Network: %s | " % main.file_server.supported_ip_types yield "Network: %s | " % main.file_server.supported_ip_types
@ -579,7 +579,7 @@ class ActionsPlugin:
yield "\n" yield "\n"
yield from self.formatTable( yield from self.formatTable(
["ZeroNet version:", "%s rev%s" % (config.version, config.rev)], ["zeronet-conservancy version:", config.version_full],
["Python:", "%s" % sys.version], ["Python:", "%s" % sys.version],
["Platform:", "%s" % sys.platform], ["Platform:", "%s" % sys.platform],
["Crypt verify lib:", "%s" % CryptBitcoin.lib_verify_best], ["Crypt verify lib:", "%s" % CryptBitcoin.lib_verify_best],

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,13 +10,25 @@ 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.version_full = f'{self.version} ({self.build_type} from {self.branch}-{self.commit})'
self.user_agent = "conservancy" self.user_agent = "conservancy"
# DEPRECATED ; replace with git-generated commit # for compatibility
self.rev = 5140
self.user_agent_rev = 8192 self.user_agent_rev = 8192
self.argv = argv self.argv = argv
self.action = None self.action = None
@ -306,7 +319,7 @@ 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_full}')
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

View file

@ -22,7 +22,6 @@ class PluginManager:
self.pluggable = {} self.pluggable = {}
self.plugin_names = [] # Loaded plugin names self.plugin_names = [] # Loaded plugin names
self.plugins_updated = {} # List of updated plugins since restart self.plugins_updated = {} # List of updated plugins since restart
self.plugins_rev = {} # Installed plugins revision numbers
self.after_load = [] # Execute functions after loaded plugins self.after_load = [] # Execute functions after loaded plugins
self.function_flags = {} # Flag function for permissions self.function_flags = {} # Flag function for permissions
self.reloading = False self.reloading = False
@ -88,7 +87,6 @@ class PluginManager:
plugin["dir_path"] = dir_path plugin["dir_path"] = dir_path
plugin["inner_path"] = plugin_name plugin["inner_path"] = plugin_name
plugin["enabled"] = is_enabled plugin["enabled"] = is_enabled
plugin["rev"] = config.rev
plugin["loaded"] = plugin_name in self.plugin_names plugin["loaded"] = plugin_name in self.plugin_names
plugins.append(plugin) plugins.append(plugin)
@ -101,7 +99,6 @@ class PluginManager:
for plugin in self.listPlugins(): for plugin in self.listPlugins():
self.log.debug("Loading plugin: %s (%s)" % (plugin["name"], plugin["source"])) self.log.debug("Loading plugin: %s (%s)" % (plugin["name"], plugin["source"]))
if plugin["source"] != "builtin": if plugin["source"] != "builtin":
self.plugins_rev[plugin["name"]] = plugin["rev"]
site_plugin_dir = os.path.dirname(plugin["dir_path"]) site_plugin_dir = os.path.dirname(plugin["dir_path"])
if site_plugin_dir not in sys.path: if site_plugin_dir not in sys.path:
sys.path.append(site_plugin_dir) sys.path.append(site_plugin_dir)

View file

@ -600,7 +600,6 @@ class Site(object):
num_connected_peers = len(peers) num_connected_peers = len(peers)
random.shuffle(peers) random.shuffle(peers)
peers = sorted(peers, key=lambda peer: peer.connection.handshake.get("rev", 0) < config.rev - 100) # Prefer newer clients
if len(peers) < limit * 2 and len(self.peers) > len(peers): # Add more, non-connected peers if necessary if len(peers) < limit * 2 and len(self.peers) > len(peers): # Add more, non-connected peers if necessary
peers += self.getRecentPeers(limit * 2) peers += self.getRecentPeers(limit * 2)

View file

@ -660,7 +660,7 @@ class UiRequest:
permissions=json.dumps(site.settings["permissions"]), permissions=json.dumps(site.settings["permissions"]),
show_loadingscreen=json.dumps(show_loadingscreen), show_loadingscreen=json.dumps(show_loadingscreen),
sandbox_permissions=sandbox_permissions, sandbox_permissions=sandbox_permissions,
rev=config.rev, rev=config.commit,
lang=config.language, lang=config.language,
homepage=homepage, homepage=homepage,
themeclass=themeclass, themeclass=themeclass,
@ -1038,7 +1038,7 @@ class UiRequest:
if details and config.debug: if details and config.debug:
details = {key: val for key, val in list(self.env.items()) if hasattr(val, "endswith") and "COOKIE" not in key} details = {key: val for key, val in list(self.env.items()) if hasattr(val, "endswith") and "COOKIE" not in key}
details["version_zeronet"] = "%s r%s" % (config.version, config.rev) details["version_zeronet"] = config.version_full
details["version_python"] = sys.version details["version_python"] = sys.version
details["version_gevent"] = gevent.__version__ details["version_gevent"] = gevent.__version__
details["plugins"] = PluginManager.plugin_manager.plugin_names details["plugins"] = PluginManager.plugin_manager.plugin_names

View file

@ -328,13 +328,15 @@ class UiWebsocket(object):
'ui_ip' : config.ui_ip, 'ui_ip' : config.ui_ip,
'ui_port' : config.ui_port, 'ui_port' : config.ui_port,
'version' : config.version, 'version' : config.version,
'rev' : config.rev, # Some legacy code relies on this being an integer, so lets return dummy one
'rev' : config.user_agent_rev,
'timecorrection' : file_server.timecorrection, 'timecorrection' : file_server.timecorrection,
'language' : config.language, 'language' : config.language,
'debug' : config.debug, 'debug' : config.debug,
'offline' : config.offline, 'offline' : config.offline,
'plugins' : PluginManager.plugin_manager.plugin_names, 'plugins' : PluginManager.plugin_manager.plugin_names,
'plugins_rev' : PluginManager.plugin_manager.plugins_rev, # For compat only
'plugins_rev' : {},
'user_settings' : self.user.settings, 'user_settings' : self.user.settings,
'lib_verify_best' : CryptBitcoin.lib_verify_best 'lib_verify_best' : CryptBitcoin.lib_verify_best
} }

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)

View file

@ -196,7 +196,7 @@ elif config.bind:
@PluginManager.acceptPlugins @PluginManager.acceptPlugins
class Actions: class Actions:
def call(self, function_name, kwargs): def call(self, function_name, kwargs):
logging.info("Version: %s r%s, Python %s, Gevent: %s" % (config.version, config.rev, sys.version, gevent.__version__)) logging.info(f'zeronet-conservancy {config.version_full} on Python {sys.version} Gevent {gevent.__version__}')
func = getattr(self, function_name, None) func = getattr(self, function_name, None)
back = func(**kwargs) back = func(**kwargs)

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