CMS 3D CMS Logo

makeHippyCampaign.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 import argparse
4 import contextlib
5 import errno
6 import glob
7 import os
8 import re
9 import shutil
10 import stat
11 import subprocess
12 import sys
13 
14 thisfile = os.path.abspath(__file__)
15 
16 def main():
17  parser = argparse.ArgumentParser()
18  parser.add_argument("foldername", help="folder name for the campaign. Example: CRUZET20xy")
19  parser.add_argument("--cmssw", default=os.environ["CMSSW_VERSION"])
20  parser.add_argument("--scram-arch", default=os.environ["SCRAM_ARCH"])
21  parser.add_argument("--subfolder", default="", help="subfolder within basedir to make 'foldername' in.")
22  parser.add_argument("--merge-topic", action="append", help="things to cms-merge-topic within the CMSSW release created", default=[])
23  parser.add_argument("--print-sys-path", action="store_true", help=argparse.SUPPRESS) #internal, don't use this
24  parser.add_argument('--basedir', default="/afs/cern.ch/cms/CAF/CMSALCA/ALCA_TRACKERALIGN2/HipPy")
25  args = parser.parse_args()
26 
27  basedir = args.basedir
28  if not os.path.exists(basedir):
29  raise FileExistsError("Base Directory does not exist!")
30 
31  if basedir[-1] == '/':
32  basedir = basedir[:-1] #No trailing slashed allowed
33 
34  if args.print_sys_path:
35  print(repr(sys.path))
36  return
37 
38  folder = os.path.join(basedir, args.subfolder, args.foldername)
39 
40  mkdir_p(folder)
41 
42  with cd(folder):
43  if not os.path.exists(args.cmssw):
44  os.environ["SCRAM_ARCH"] = args.scram_arch
45  subprocess.check_call(["scram", "p", "CMSSW", args.cmssw])
46  with cd(args.cmssw):
47  cmsenv()
48  for _ in args.merge_topic:
49  subprocess.check_call(["git", "cms-merge-topic", _])
50  os.system("eval $(scram ru -sh) && scram b -j 10") #my cmsenv function isn't quite good enough for scram b purposes. Also, http://stackoverflow.com/a/38792806/5228524
51 
52  if os.path.exists("src/Alignment/HIPAlignmentAlgorithm"):
53  HIPAlignmentAlgorithm = os.path.abspath("src/Alignment/HIPAlignmentAlgorithm")
54  else:
55  with cd(os.environ["CMSSW_RELEASE_BASE"]):
56  HIPAlignmentAlgorithm = os.path.abspath("src/Alignment/HIPAlignmentAlgorithm")
57 
58  assert os.path.exists(HIPAlignmentAlgorithm), HIPAlignmentAlgorithm
59 
60  mkdir_p("Jobs")
61  mkdir_p("run")
62 
63  with cd("run"):
64  subprocess.check_call(["git", "init"])
65 
66  mkdir_p("Configurations")
67  with cd("Configurations"):
68  if not os.path.exists("align_tpl_py.txt"):
69  shutil.copy(os.path.join(HIPAlignmentAlgorithm, "python", "align_tpl_py.txt"), ".")
70  subprocess.check_call(["git", "add", "align_tpl_py.txt"])
71  if not os.path.exists("common_cff_py_TEMPLATE.txt"):
72  shutil.copy(os.path.join(HIPAlignmentAlgorithm, "python", "common_cff_py.txt"), "common_cff_py_TEMPLATE.txt")
73  subprocess.check_call(["git", "add", "common_cff_py_TEMPLATE.txt"])
74  mkdir_p("TrackSelection")
75  with cd("TrackSelection"):
76  for _ in glob.iglob(os.path.join(HIPAlignmentAlgorithm, "python", "*TrackSelection_cff_py.txt")):
77  if not os.path.exists(os.path.basename(_)):
78  shutil.copy(_, ".")
79  subprocess.check_call(["git", "add", os.path.basename(_)])
80 
81  mkdir_p("DataFiles")
82  with cd("DataFiles"):
83  if not os.path.exists("data_example.lst"):
84  with open("data_example.lst", "w") as f:
85  f.write(os.path.join(os.getcwd(), "minbias.txt") + ",,MBVertex,Datatype:0\n")
86  f.write(os.path.join(os.getcwd(), "cosmics.txt") + ",,COSMICS,Datatype:1 APVMode:deco Bfield:3.8T\n")
87  f.write(os.path.join(os.getcwd(), "CDCs.txt") + ",,CDCS,Datatype:1 APVMode:deco Bfield:3.8T\n")
88  subprocess.check_call(["git", "add", "data_example.lst"])
89  if not os.path.exists("baddatafiles.txt"):
90  with open("baddatafiles.txt", "w") as f:
91  f.write("If any data files are bad (e.g. not at CERN), put them here,\n")
92  f.write("separated by newlines or spaces or nothing or whatever you like.\n")
93  f.write("Anything else in this file, like these lines, will be ignored.\n")
94  f.write("You can also run hippyaddtobaddatafiles.py .../align_cfg.py to automatically\n")
95  f.write("find bad data files.\n")
96  f.write("Running jobs will automatically pick up changes here next time they resubmit.")
97 
98  mkdir_p("IOV")
99  with cd("IOV"):
100  if not os.path.exists("RunXXXXXX"):
101  with open("RunXXXXXX", "w") as f:
102  f.write("XXXXXX")
103  subprocess.check_call(["git", "add", "RunXXXXXX"])
104 
105  if not os.path.exists("submit_template.sh"):
106  shutil.copy(os.path.join(HIPAlignmentAlgorithm, "test", "hippysubmittertemplate.sh"), "submit_template.sh")
107  os.chmod("submit_template.sh", os.stat("submit_template.sh").st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
108  subprocess.check_call(["git", "add", "submit_template.sh"])
109 
110  if not os.path.exists("submit_script.sh"):
111  shutil.copy(os.path.join(HIPAlignmentAlgorithm, "test", "hippysubmitterscript.sh"), "submit_script.sh")
112  os.chmod("submit_script.sh", os.stat("submit_script.sh").st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
113  subprocess.check_call(["git", "add", "submit_script.sh"])
114 
115  print("Dumped files into ", folder)
116 
117  try:
118  subprocess.check_output(["git", "diff", "--staged", "--quiet"])
119  except subprocess.CalledProcessError:
120  subprocess.check_call(["git", "commit", "-m", "commit templates"])
121 
122 def mkdir_p(path):
123  """http://stackoverflow.com/a/600612/5228524"""
124  try:
125  os.makedirs(path)
126  except OSError as exc:
127  if exc.errno == errno.EEXIST and os.path.isdir(path):
128  pass
129  else:
130  raise
131 
132 @contextlib.contextmanager
133 def cd(newdir):
134  """http://stackoverflow.com/a/24176022/5228524"""
135  prevdir = os.getcwd()
136  os.chdir(os.path.expanduser(newdir))
137  try:
138  yield
139  finally:
140  os.chdir(prevdir)
141 
142 def cmsenv():
143  output = subprocess.check_output(["scram", "ru", "-sh"])
144  for line in output.decode('utf8').split(";\n"):
145  if not line.strip(): continue
146  match1 = re.match(r'^export (\w*)="([^"]*)"$', line)
147  match2 = re.match(r'^unset *((\w* *)*)$', line)
148  if match1:
149  variable, value = match1.groups()
150  os.environ[variable] = value
151  elif match2:
152  for variable in match2.group(1).split():
153  del os.environ[variable]
154  else:
155  raise ValueError("Bad scram ru -sh line:\n"+line)
156  sys.path[:] = eval(subprocess.check_output([thisfile, "dummy", "--print-sys-path"]))
157 
158 if __name__ == "__main__":
159  main()
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
Definition: main.py:1