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)
 
bool can_parallelize (helper)
 
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 copy_condor_config (str output)
 

Variables

list __all__
 
 NPROC = multiprocessing.cpu_count()
 

Function Documentation

◆ can_parallelize()

bool can_parallelize (   helper)
Checks if the command can be parallelized.
111 def can_parallelize(helper) -> bool:
112  """Checks if the command can be parallelized."""
113  nSplit_exists = ["nSplit" in row for row in helper].count(True)
114  output_exists = ["output" in row for row in helper].count(True)
115  return nSplit_exists and output_exists
116 
117 

◆ copy_condor_config()

def copy_condor_config ( str  output)
Copy the HTC config to the running directory.
367 def copy_condor_config(output: str):
368  """Copy the HTC config to the running directory."""
369 
370  copy2(Path(__file__).parent / "condor.jdl", output + "/.condor")

◆ diagnostic()

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

◆ find_institute()

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

◆ git_hash()

None git_hash ( str  exec)
Returns the SHA of the executable at compile time.
118 def git_hash(exec: str) -> None:
119  """Returns the SHA of the executable at compile time."""
120  print(check_output(exec + " -g", shell=True).decode(), end="")
121 
122 

◆ 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)
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.absolute())]
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 

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

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:349
Darwin::Tools::split
@ split
activate -k and -j to define slice
Definition: Options.h:26
prefix.find_institute
def find_institute()
Definition: prefix.py:123
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.copy_condor_config
def copy_condor_config(str output)
Definition: prefix.py:367
prefix.can_parallelize
bool can_parallelize(helper)
Definition: prefix.py:111
prefix.git_hash
None git_hash(str exec)
Definition: prefix.py:118
prefix.tweak_helper_multi
None tweak_helper_multi(PrefixCommand prefix, bool multi_opt=True, bool dag_opt=False, bool condor=False)
Definition: prefix.py:318