Belle II Software  release-05-02-19
helpers.py
1 """
2 Helper scripts needed to run tools and checks on the validation.
3 Mainly for git checkout and compilation.
4 
5  Author: The Belle II Collaboration
6  Contributors: Nils Braun, Thomas Hauth
7 """
8 
9 from subprocess import check_call, check_output, CalledProcessError
10 import basf2
11 import sys
12 import os
13 
14 try:
15  from git import Repo
16 except ImportError:
17  print("This script requires GitPython, please install it via `pip3 install --user GitPython")
18  sys.exit(1)
19 
20 
22  """
23  Return the folder name of the basf2 repository currently set up
24  """
25  git_repository_folder = os.getenv("BELLE2_LOCAL_DIR")
26  if not git_repository_folder:
27  raise ValueError("You need to setup basf2 first")
28 
29  return git_repository_folder
30 
31 
32 def get_basf2_repo(non_dirty_check=False):
33  """
34  Return a git repo object of the current basf2 repository
35 
36  :param non_dirty_check: Check if the repo is dirty and issue a printed warning if yes.
37  """
38  git_repository = get_basf2_repo_folder()
39  repo = Repo(git_repository)
40  assert not repo.bare
41 
42  if non_dirty_check and repo.is_dirty():
43  basf2.B2WARNING("Your git repo is dirty! I can not guarantee for valid results...")
44 
45  return repo
46 
47 
48 def checkout_git_revision(revision, repo=None, use_stash=False):
49  """
50  Checkout the given revision in the basf2 repository.
51  ATTENTION: this does not check for dirty files etc.
52 
53  :param revision: which revision to checkout
54  :param repo: basf2 repo object (None will use the default basf2 repo)
55  """
56  if not repo:
57  repo = get_basf2_repo()
58 
59  if use_stash:
60  repo.git.stash()
61  repo.git.checkout(revision)
62  if use_stash:
63  repo.git.stash("pop")
64 
65 
66 def compile_basf2(compile_options=None):
67  """
68  Compile basf2 with the given options as list.
69 
70  :param compile_options: List of cmd options given to scons.
71  """
72  if compile_options is None:
73  compile_options = []
74 
75  git_repository_folder = get_basf2_repo_folder()
76  check_call(["scons"] + compile_options, cwd=git_repository_folder)
77 
78 
79 def run_basf2_validation(validation_options=None):
80  """
81  Run the basf2 validation.
82  :param validation_options: List of options given to the b2validation call.
83  """
84  git_repository_folder = get_basf2_repo_folder()
85  check_call(["b2validation"] + validation_options, cwd=git_repository_folder)
86 
87 
89  """
90  Fix for ROOT to give the command line options directly to python.
91  """
92  from ROOT import PyConfig
93  PyConfig.IgnoreCommandLineOptions = True
94 
95 
96 def get_git_hashes_between(git_end_hash, git_start_hash):
97  """
98  Return list of git hashes between `git_end_hash` and `git_start_hash`
99  (but not including them).
100  """
101  try:
102  git_hashs = check_output(["git", "log", str(git_start_hash + ".." + git_end_hash),
103  "--pretty=format:%H"]).decode("utf-8").split("\n")
104  return git_hashs
105  except CalledProcessError:
106  basf2.B2FATAL("Error while receiving the git history.")
validation_tools.helpers.get_basf2_repo
def get_basf2_repo(non_dirty_check=False)
Definition: helpers.py:32
validation_tools.helpers.get_git_hashes_between
def get_git_hashes_between(git_end_hash, git_start_hash)
Definition: helpers.py:96
validation_tools.helpers.get_basf2_repo_folder
def get_basf2_repo_folder()
Definition: helpers.py:21
validation_tools.helpers.run_basf2_validation
def run_basf2_validation(validation_options=None)
Definition: helpers.py:79
validation_tools.helpers.fix_root_command_line
def fix_root_command_line()
Definition: helpers.py:88
validation_tools.helpers.compile_basf2
def compile_basf2(compile_options=None)
Definition: helpers.py:66
validation_tools.helpers.checkout_git_revision
def checkout_git_revision(revision, repo=None, use_stash=False)
Definition: helpers.py:48