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