DAS  3.0
Das Analysis System
prefix Namespace Reference

Classes

class  PrefixCommand
 

Functions

def preparse (List[str] argv, str tutorial, bool multi_opt=False, bool dag_opt=False, bool condor=False)
 
None git_hash (str exec)
 
def find_institute ()
 
None tweak_helper_multi (PrefixCommand prefix, bool multi_opt=True, bool dag_opt=False, bool condor=False)
 
None diagnostic (Exception exception)
 
def print_slice (formatting, line, nNow, nSplit, end)
 
def copy_condor_config (output)
 

Variables

list __all__
 
 NPROC = multiprocessing.cpu_count()
 

Function Documentation

◆ copy_condor_config()

def copy_condor_config (   output)
Copy the HTC config to the running directory.
396 def copy_condor_config(output):
397  """Copy the HTC config to the running directory."""
398 
399  copy2(Path(__file__).parent / "condor.jdl", str(output) + "/.condor")

◆ diagnostic()

None diagnostic ( Exception  exception)
Common diagnostic message for all prefix commands in case of exception.
356 def diagnostic(exception: Exception) -> None:
357  """Common diagnostic message for all prefix commands in case of exception."""
358  prefix = os.path.basename(sys.argv[0])
359  versions = (
360  check_output("printDarwinSoftwareVersion", shell=True)
361  .decode()
362  .strip()
363  .split("\n")
364  )
365  versions += [f"Python {sys.version_info.major}.{sys.version_info.minor}"]
366  print(
367  f"\x1B[31m{prefix}: \x1B[1m{exception}\x1B[22m",
368  ", ".join(versions),
369  "Consider opening a GitLab issue if you don't understand the cause of the failure.\x1B[0m",
370  sep="\n",
371  )
372 
373 

◆ find_institute()

def find_institute ( )
Identifies the host from the name of the machine
116 def find_institute():
117  """Identifies the host from the name of the machine"""
118 
119  clusters = {
120  r"naf-cms[1-2][0-9].desy.de": "DESY",
121  r".phy.pku.edu.cn": "PKU",
122  r"lxplus[0-9]*.cern.ch": "CERN",
123  }
124 
125  hostname = socket.gethostname()
126  for key, value in clusters.items():
127  if re.search(key, hostname):
128  return value
129 
130  raise RuntimeError("The host could not be identified")
131 
132 

◆ git_hash()

None git_hash ( str  exec)
Returns the SHA of the executable at compile time.
111 def git_hash(exec: str) -> None:
112  """Returns the SHA of the executable at compile time."""
113  print(check_output(exec + " -g", shell=True).decode(), end="")
114 
115 

◆ preparse()

def preparse ( List[str]  argv,
str  tutorial,
bool   multi_opt = False,
bool   dag_opt = False,
bool   condor = False 
)
Parser for the prefix command itself, relying on ArgumentParser.
32 def preparse(
33  argv: List[str],
34  tutorial: str,
35  multi_opt: bool = False,
36  dag_opt: bool = False,
37  condor: bool = False,
38 ):
39  """Parser for the prefix command itself, relying on ArgumentParser."""
40 
41  parser = ArgumentParser(add_help=False)
42 
43  parser.add_argument("exec", nargs="?")
44  if multi_opt:
45  parser.add_argument("-b", "--background", action="store_true")
46  parser.add_argument("-d", "--dry-run", action="store_true")
47  if dag_opt:
48  parser.add_argument("-D", "--dag", type=Path, nargs='?', default="dag")
49  if condor:
50  parser.add_argument("-n", "--memory-needs", type=int, default=1024)
51 
52  # helper
53  parser.add_argument("-h", "--help", action="store_true")
54  parser.add_argument("-t", "--tutorial", action="store_true")
55  parser.add_argument("-g", "--git", action="store_true")
56  if not dag_opt:
57  parser.add_argument("-e", "--example", action="store_true")
58 
59  # common
60  parser.add_argument("-v", "--verbose", action="store_true")
61  parser.add_argument("-m", "--mute", action="store_true")
62  parser.add_argument("-f", "--fill", action="store_true")
63  parser.add_argument("-F", "--Friend", action="store_true")
64  parser.add_argument("-s", "--syst", action="store_true")
65  parser.add_argument("-c", "--config", type=Path)
66  parser.add_argument("-j", "--nSplit", type=int, default=NPROC)
67 
68  # custom will be in args
69  cmds, args = parser.parse_known_intermixed_args(argv[1:])
70 
71  if any(x in args for x in ["-k", "--nNow"]):
72  raise ValueError("Giving `-k` or `--nNow` is meaningless in this context.")
73  unknown_explicit_args = [arg for arg in args if arg[0] == "-"]
74  if len(unknown_explicit_args) > 0:
75  raise ValueError(
76  f"No unknown explicit option is allowed: {unknown_explicit_args}"
77  )
78 
79  # restore explicit options
80  if cmds.verbose:
81  args += ["-v"]
82  if cmds.mute:
83  args += ["-m"]
84  if cmds.fill:
85  args += ["-f"]
86  if cmds.Friend:
87  args += ["-F"]
88  if cmds.syst:
89  args += ["-s"]
90  if cmds.config:
91  args += ["-c", str(cmds.config.resolve())]
92 
93  if not cmds.exec:
94  if cmds.help or len(argv) == 1:
95  prefix = os.path.basename(sys.argv[0])
96  print(
97  f"\33[1m{prefix} exec args [...]\33[0m",
98  "where\texec = a command using `Darwin::Tools::Options` with `Darwin::Tools::split`",
99  "\targs = the arguments of the same command, one being called exactly `output`",
100  sep="\n",
101  )
102  if cmds.tutorial:
103  # boost::program_options::options_description::m_default_line_length = 80
104  lines = textwrap.TextWrapper(width=80).wrap(text=tutorial)
105  print(*lines, sep="\n")
106  parser.exit()
107 
108  return cmds, args
109 
110 

◆ print_slice()

def print_slice (   formatting,
  line,
  nNow,
  nSplit,
  end 
)
Prefix the output stream with the slice. The formatting of the
printout is preserved but the slice is printed in default formatting.
374 def print_slice(formatting, line, nNow, nSplit, end):
375  """
376  Prefix the output stream with the slice. The formatting of the
377  printout is preserved but the slice is printed in default formatting.
378  """
379 
380  RESET = "\x1B[0m"
381 
382  # find the special characters to encode the colour and the highlighting
383  special_chars = re.findall(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])", line)
384 
385  # if any was found, check if it should be reset to default
386  if special_chars and special_chars[-1] == RESET:
387  formatting = RESET
388  else:
389  formatting += "".join(special_chars)
390 
391  print(f"{nNow}/{nSplit}\t{formatting}{line}{RESET}", end=end)
392 
393  return formatting
394 
395 

◆ tweak_helper_multi()

None tweak_helper_multi ( PrefixCommand  prefix,
bool   multi_opt = True,
bool   dag_opt = False,
bool   condor = False 
)
Tweak helper to use a directory as output rather than a file
and to reflect the number of cores of the machine.
326  prefix: PrefixCommand,
327  multi_opt: bool = True,
328  dag_opt: bool = False,
329  condor: bool = False,
330 ) -> None:
331  """Tweak helper to use a directory as output rather than a file
332  and to reflect the number of cores of the machine."""
333 
334  print(f"\33[1m{prefix.name} \33[0m", end="")
335  for row in prefix.helper:
336  if "output" in row:
337  row = row.replace("ROOT file", "directory")
338  if "nNow" in row:
339  continue
340  if "nSplit" in row:
341  row = row.replace(
342  "(=1)" if NPROC < 10 else "(=1) ", "(=" + str(NPROC) + ")"
343  )
344  print(row)
345  if "Helper" in row:
346  if multi_opt:
347  print(" -b [ --background ] Do not wait for job to finish")
348  print(" -d [ --dry-run ] Run until the actual executation")
349  if dag_opt:
350  print(" -D [ --dag ] Indicate DAG file")
351  if condor:
352  print(" -n [ --memory-needs] arg Memory needs for HTCondor job")
353  print("")
354 
355 

Variable Documentation

◆ __all__

list __all__
private
Initial value:
1 = [
2  "PrefixCommand",
3  "preparse",
4  "tweak_helper_multi",
5  "diagnostic",
6  "git_hash",
7  "copy_condor_config",
8  "find_institute",
9 ]

◆ NPROC

NPROC = multiprocessing.cpu_count()
prefix.diagnostic
None diagnostic(Exception exception)
Definition: prefix.py:356
Darwin::Tools::split
@ split
activate -k and -j to define slice
Definition: Options.h:26
prefix.copy_condor_config
def copy_condor_config(output)
Definition: prefix.py:396
prefix.find_institute
def find_institute()
Definition: prefix.py:116
prefix.preparse
def preparse(List[str] argv, str tutorial, bool multi_opt=False, bool dag_opt=False, bool condor=False)
Definition: prefix.py:32
prefix.print_slice
def print_slice(formatting, line, nNow, nSplit, end)
Definition: prefix.py:374
prefix.git_hash
None git_hash(str exec)
Definition: prefix.py:111
prefix.tweak_helper_multi
None tweak_helper_multi(PrefixCommand prefix, bool multi_opt=True, bool dag_opt=False, bool condor=False)
Definition: prefix.py:325