Darwin  1.0
Event loop based prototype framework
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.
109 def can_parallelize(helper) -> bool:
110  """Checks if the command can be parallelized."""
111  nSplit_exists = ["nSplit" in row for row in helper].count(True)
112  output_exists = ["output" in row for row in helper].count(True)
113  return nSplit_exists and output_exists
114 
115 

◆ copy_condor_config()

def copy_condor_config ( str  output)
Identifies the proper HTC config for the local host.
344 def copy_condor_config(output: str):
345  """Identifies the proper HTC config for the local host."""
346 
347  institute = ""
348  try:
349  institute = find_institute()
350  except RuntimeError as e:
351  print(e)
352  institute = "default"
353 
354  copy2(Path(__file__).parent / f"{institute}.jdl", output + "/.condor")
355  copy2(Path(__file__).parent / ".run", output)

◆ diagnostic()

None diagnostic ( Exception  exception)
Common diagnostic message for all prefix commands in case of exception.
326 def diagnostic(exception: Exception) -> None:
327  """Common diagnostic message for all prefix commands in case of exception."""
328  prefix = os.path.basename(sys.argv[0])
329  versions = (
330  check_output("printDarwinSoftwareVersion", shell=True)
331  .decode()
332  .strip()
333  .split("\n")
334  )
335  versions += [f"Python {sys.version_info.major}.{sys.version_info.minor}"]
336  print(
337  f"\x1B[31m{prefix}: \x1B[1m{exception}\x1B[22m",
338  ", ".join(versions),
339  "Consider opening a GitLab issue if you don't understand the cause of the failure.\x1B[0m",
340  sep="\n",
341  )
342 
343 

◆ find_institute()

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

◆ git_hash()

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

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

◆ 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.
289  prefix: PrefixCommand,
290  multi_opt: bool = True,
291  dag_opt: bool = False,
292  condor: bool = False,
293 ) -> None:
294  """Tweak helper to use a directory as output rather than a file
295  and to reflect the number of cores of the machine."""
296 
297  print(f"\33[1m{prefix.name} \33[0m", end="")
298  for row in prefix.helper:
299  if "output" in row:
300  row = row.replace("ROOT file", "directory")
301  if "nNow" in row:
302  continue
303  if "nSplit" in row:
304  row = row.replace(
305  "(=1)" if NPROC < 10 else "(=1) ", "(=" + str(NPROC) + ")"
306  )
307  print(row)
308  if "Helper" in row:
309  if multi_opt:
310  print(" -b [ --background ] Do not wait for job to finish")
311  print(" -d [ --dry-run ] Run until the actual executation")
312  if dag_opt:
313  print(" -d [ --dag ] Indicate DAGMan directory location")
314  if condor:
315  try:
316  institute = find_institute()
317  if institute in ["DESY", "CERN"]:
318  print(
319  " -n [ --memory-needs] arg Memory needs for HTCondor job"
320  )
321  except RuntimeError:
322  pass
323  print("")
324 
325 

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:326
prefix.find_institute
def find_institute()
Definition: prefix.py:121
Darwin::Tools::split
@ split
activate -k and -j to define slice
Definition: Options.h:26
prefix.preparse
def preparse(List[str] argv, str tutorial, bool multi_opt=False, bool dag_opt=False, bool condor=False)
Definition: prefix.py:30
prefix.copy_condor_config
def copy_condor_config(str output)
Definition: prefix.py:344
prefix.can_parallelize
bool can_parallelize(helper)
Definition: prefix.py:109
prefix.git_hash
None git_hash(str exec)
Definition: prefix.py:116
prefix.tweak_helper_multi
None tweak_helper_multi(PrefixCommand prefix, bool multi_opt=True, bool dag_opt=False, bool condor=False)
Definition: prefix.py:288