Belle II Software  release-05-02-19
validationtestutil.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 import os
5 import sys
6 
7 
8 def check_execute(cmd, terminate_on_error=True):
9  """
10  Executes a shell commands and check for =! 0 return codes
11  """
12 
13  print("Executing command '{}'".format(cmd))
14  res = os.system(cmd)
15  print("Command '{}' exited with code {}".format(cmd, res))
16  if not res == 0:
17  print("FATAL: Exit code is not 0")
18  if terminate_on_error:
19  sys.exit(res)
20  return False
21 
22  return True
23 
24 
25 def check_path_exists(paths, terminate_on_error=True):
26  """
27  Checks if a path exists.
28 
29  @param paths: list of file system path (directories or files)
30  @param terminate_on_error: if true, the execution is terminated if one part
31  is not present
32  @return: None
33  """
34 
35  for p in paths:
36  print("Checking for existance of file {}".format(p))
37  if not os.path.exists(p):
38  print("Path {} does not exist".format(p))
39  if terminate_on_error:
40  sys.exit(1)
41  print("Jep !")
42 
43 
44 def create_fake_scripts(folders, scriptname):
45  """
46  Creates a fake script inside a nested path
47  :param folders: folders to hold the script
48  :param scriptname: name of the script itself
49  :return: nothing
50  """
51  if not os.path.exists(folders):
52  os.makedirs(folders)
53  with open(os.path.join(folders, scriptname), "w") as f:
54  f.write("# not content on purpose")