Skip to content

Commit

Permalink
Add small feature for printing versions of git (#665)
Browse files Browse the repository at this point in the history
* add small feature for printing versions of git

* allow detached heads

* Update misc.py

* Update bootstrax

* Update test_misc.py
  • Loading branch information
JoranAngevaare authored Sep 16, 2021
1 parent 890e3ea commit d983e77
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 7 deletions.
7 changes: 7 additions & 0 deletions bin/bootstrax
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,13 @@ except ModuleNotFoundError:
log = logging.getLogger()

log.info(f'---\n bootstrax version {__version__}\n---')
log.info(
straxen.print_versions(
modules='strax straxen utilix daqnt numpy tensorflow numba'.split(),
include_git=True,
return_string=True,
))


# Set the output folder
output_folder = '/data/xenonnt_processed/' if args.production else test_data_folder
Expand Down
1 change: 1 addition & 0 deletions extra_requirements/requirements-tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ coveralls==3.2.0
commentjson==0.9.0
coverage==5.5
flake8==3.9.2
gitpython==3.1.18
holoviews==1.14.5
ipywidgets==7.6.4
hypothesis==6.17.4
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ numba>=0.50.0
requests
commentjson
matplotlib
gitpython
36 changes: 30 additions & 6 deletions straxen/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import pytz
from os import environ as os_environ
from importlib import import_module
from git import Repo, InvalidGitRepositoryError
from configparser import NoSectionError

export, __all__ = strax.exporter()
Expand Down Expand Up @@ -41,7 +42,9 @@ def do_round(x):


@export
def print_versions(modules=('strax', 'straxen', 'cutax'), return_string=False):
def print_versions(modules=('strax', 'straxen', 'cutax'),
return_string=False,
include_git=True):
"""
Print versions of modules installed.
Expand All @@ -50,6 +53,8 @@ def print_versions(modules=('strax', 'straxen', 'cutax'), return_string=False):
'cutax', 'pema'))
:param return_string: optional. Instead of printing the message,
return a string
:param include_git_details: Include the current branch and latest
commit hash
:return: optional, the message that would have been printed
"""
message = (f'Working on {socket.getfqdn()} with the following '
Expand All @@ -59,13 +64,32 @@ def print_versions(modules=('strax', 'straxen', 'cutax'), return_string=False):
for m in strax.to_str_tuple(modules):
try:
mod = import_module(m)
message += f'\n{m}'
if hasattr(mod, '__version__'):
message += f'\tv{mod.__version__}'
if hasattr(mod, '__path__'):
message += f'\t{mod.__path__[0]}'
except (ModuleNotFoundError, ImportError):
print(f'{m} is not installed')
continue

message += f'\n{m}'
if hasattr(mod, '__version__'):
message += f'\tv{mod.__version__}'
if hasattr(mod, '__path__'):
module_path = mod.__path__[0]
message += f'\t{module_path}'
if include_git:
try:
repo = Repo(module_path, search_parent_directories=True)
except InvalidGitRepositoryError:
# not a git repo
pass
else:
try:
branch = repo.active_branch
except TypeError:
branch = 'unknown'
try:
commit_hash = repo.head.object.hexsha
except TypeError:
commit_hash = 'unknown'
message += f'\tgit branch:{branch} | {commit_hash[:7]}'
if return_string:
return message
print(message)
Expand Down
12 changes: 11 additions & 1 deletion tests/test_misc.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from straxen.misc import TimeWidgets
from straxen.misc import TimeWidgets, print_versions


def test_widgets():
Expand Down Expand Up @@ -41,3 +41,13 @@ def test_change_in_fields():

start00, _ = tw.get_start_end()
assert start20 - start00 == minutes, 'Time field did not update its value!'


def test_print_versions(modules=('numpy', 'straxen', 'non_existing_module')):
for return_string in [True, False]:
for include_git in [True, False]:
res = print_versions(modules,
return_string=return_string,
include_git=include_git)
if return_string:
assert res is not None

0 comments on commit d983e77

Please sign in to comment.