CMS 3D CMS Logo

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