File indexing completed on 2023-03-17 10:39:13
0001
0002
0003 import argparse
0004 import contextlib
0005 import errno
0006 import glob
0007 import os
0008 import re
0009 import shutil
0010 import stat
0011 import subprocess
0012 import sys
0013
0014 basedir = "/afs/cern.ch/cms/CAF/CMSALCA/ALCA_TRACKERALIGN2/HipPy"
0015
0016 thisfile = os.path.abspath(__file__)
0017
0018 def main():
0019 parser = argparse.ArgumentParser()
0020 parser.add_argument("foldername", help="folder name for the campaign. Example: CRUZET20xy")
0021 parser.add_argument("--cmssw", default=os.environ["CMSSW_VERSION"])
0022 parser.add_argument("--scram-arch", default=os.environ["SCRAM_ARCH"])
0023 parser.add_argument("--subfolder", default="", help="subfolder within "+basedir+" to make 'foldername' in.")
0024 parser.add_argument("--merge-topic", action="append", help="things to cms-merge-topic within the CMSSW release created", default=[])
0025 parser.add_argument("--print-sys-path", action="store_true", help=argparse.SUPPRESS)
0026 args = parser.parse_args()
0027
0028 if args.print_sys_path:
0029 print repr(sys.path)
0030 return
0031
0032 folder = os.path.join(basedir, args.subfolder, args.foldername)
0033
0034 mkdir_p(folder)
0035
0036 with cd(folder):
0037 if not os.path.exists(args.cmssw):
0038 os.environ["SCRAM_ARCH"] = args.scram_arch
0039 subprocess.check_call(["scram", "p", "CMSSW", args.cmssw])
0040 with cd(args.cmssw):
0041 cmsenv()
0042 for _ in args.merge_topic:
0043 subprocess.check_call(["git", "cms-merge-topic", _])
0044 os.system("eval $(scram ru -sh) && scram b -j 10")
0045
0046 if os.path.exists("src/Alignment/HIPAlignmentAlgorithm"):
0047 HIPAlignmentAlgorithm = os.path.abspath("src/Alignment/HIPAlignmentAlgorithm")
0048 else:
0049 with cd(os.environ["CMSSW_RELEASE_BASE"]):
0050 HIPAlignmentAlgorithm = os.path.abspath("src/Alignment/HIPAlignmentAlgorithm")
0051
0052 assert os.path.exists(HIPAlignmentAlgorithm), HIPAlignmentAlgorithm
0053
0054 mkdir_p("Jobs")
0055 mkdir_p("run")
0056
0057 with cd("run"):
0058 subprocess.check_call(["git", "init"])
0059
0060 mkdir_p("Configurations")
0061 with cd("Configurations"):
0062 if not os.path.exists("align_tpl_py.txt"):
0063 shutil.copy(os.path.join(HIPAlignmentAlgorithm, "python", "align_tpl_py.txt"), ".")
0064 subprocess.check_call(["git", "add", "align_tpl_py.txt"])
0065 if not os.path.exists("common_cff_py_TEMPLATE.txt"):
0066 shutil.copy(os.path.join(HIPAlignmentAlgorithm, "python", "common_cff_py.txt"), "common_cff_py_TEMPLATE.txt")
0067 subprocess.check_call(["git", "add", "common_cff_py_TEMPLATE.txt"])
0068 mkdir_p("TrackSelection")
0069 with cd("TrackSelection"):
0070 for _ in glob.iglob(os.path.join(HIPAlignmentAlgorithm, "python", "*TrackSelection_cff_py.txt")):
0071 if not os.path.exists(os.path.basename(_)):
0072 shutil.copy(_, ".")
0073 subprocess.check_call(["git", "add", os.path.basename(_)])
0074
0075 mkdir_p("DataFiles")
0076 with cd("DataFiles"):
0077 if not os.path.exists("data_example.lst"):
0078 with open("data_example.lst", "w") as f:
0079 f.write(os.path.join(os.getcwd(), "minbias.txt") + ",,MBVertex,Datatype:0\n")
0080 f.write(os.path.join(os.getcwd(), "cosmics.txt") + ",,COSMICS,Datatype:1 APVMode:deco Bfield:3.8T\n")
0081 f.write(os.path.join(os.getcwd(), "CDCs.txt") + ",,CDCS,Datatype:1 APVMode:deco Bfield:3.8T\n")
0082 subprocess.check_call(["git", "add", "data_example.lst"])
0083 if not os.path.exists("baddatafiles.txt"):
0084 with open("baddatafiles.txt", "w") as f:
0085 f.write("If any data files are bad (e.g. not at CERN), put them here,\n")
0086 f.write("separated by newlines or spaces or nothing or whatever you like.\n")
0087 f.write("Anything else in this file, like these lines, will be ignored.\n")
0088 f.write("You can also run hippyaddtobaddatafiles.py .../align_cfg.py to automatically\n")
0089 f.write("find bad data files.\n")
0090 f.write("Running jobs will automatically pick up changes here next time they resubmit.")
0091
0092 mkdir_p("IOV")
0093 with cd("IOV"):
0094 if not os.path.exists("RunXXXXXX"):
0095 with open("RunXXXXXX", "w") as f:
0096 f.write("XXXXXX")
0097 subprocess.check_call(["git", "add", "RunXXXXXX"])
0098
0099 if not os.path.exists("submit_template.sh"):
0100 shutil.copy(os.path.join(HIPAlignmentAlgorithm, "test", "hippysubmittertemplate.sh"), "submit_template.sh")
0101 os.chmod("submit_template.sh", os.stat("submit_template.sh").st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
0102 subprocess.check_call(["git", "add", "submit_template.sh"])
0103
0104 try:
0105 subprocess.check_output(["git", "diff", "--staged", "--quiet"])
0106 except subprocess.CalledProcessError:
0107 subprocess.check_call(["git", "commit", "-m", "commit templates"])
0108
0109 def mkdir_p(path):
0110 """http://stackoverflow.com/a/600612/5228524"""
0111 try:
0112 os.makedirs(path)
0113 except OSError as exc:
0114 if exc.errno == errno.EEXIST and os.path.isdir(path):
0115 pass
0116 else:
0117 raise
0118
0119 @contextlib.contextmanager
0120 def cd(newdir):
0121 """http://stackoverflow.com/a/24176022/5228524"""
0122 prevdir = os.getcwd()
0123 os.chdir(os.path.expanduser(newdir))
0124 try:
0125 yield
0126 finally:
0127 os.chdir(prevdir)
0128
0129 def cmsenv():
0130 output = subprocess.check_output(["scram", "ru", "-sh"])
0131 for line in output.split(";\n"):
0132 if not line.strip(): continue
0133 match1 = re.match(r'^export (\w*)="([^"]*)"$', line)
0134 match2 = re.match(r'^unset *((\w* *)*)$', line)
0135 if match1:
0136 variable, value = match1.groups()
0137 os.environ[variable] = value
0138 elif match2:
0139 for variable in match2.group(1).split():
0140 del os.environ[variable]
0141 else:
0142 raise ValueError("Bad scram ru -sh line:\n"+line)
0143 sys.path[:] = eval(subprocess.check_output([thisfile, "dummy", "--print-sys-path"]))
0144
0145 if __name__ == "__main__":
0146 main()