3 from __future__
import print_function
13 if "CMSSW_BASE" not in os.environ:
14 print(
"You need to source the CMSSW environment first.")
17 from Alignment.MillePedeAlignmentAlgorithm.alignmentsetup.helper \
18 import checked_out_MPS
20 required_version = (2,7)
21 if sys.version_info < required_version:
22 print(
"Your Python interpreter is too old. Need version 2.7 or higher.")
30 """Main routine of the script. 33 - `argv`: arguments passed to the main routine 39 parser = argparse.ArgumentParser(
40 description=
"Setup a new alignment campaign in the MPproduction area.")
41 parser.add_argument(
"-d",
"--description", dest=
"description", required=
True,
42 help=
"comment to describe the purpose of the campaign")
43 parser.add_argument(
"-t",
"--data-type", dest=
"type", required=
True,
44 metavar=
"TYPE", choices=[
"MC",
"data"],
45 help=
"type of the input data (choices: %(choices)s)")
46 parser.add_argument(
"-c",
"--copy", dest=
"copy", metavar=
"CAMPAIGN",
47 help=
"input campaign (optional)")
48 args = parser.parse_args(argv)
51 if os.path.basename(os.path.normpath(os.getcwd())) !=
"MPproduction":
52 print(
">>> Cannot create a campaign outside of the 'MPproduction' area.")
53 print(
">>> Please change to the 'MPproduction' directory first.")
56 if len(args.description.strip()) == 0:
57 print(
">>> Please provide a non-empty description of the campaign")
60 MPS_dir = os.path.join(
"src",
"Alignment",
"MillePedeAlignmentAlgorithm")
62 if args.checked_out[0]:
63 MPS_dir = os.path.join(os.environ[
"CMSSW_BASE"], MPS_dir)
65 MPS_dir = os.path.join(os.environ[
"CMSSW_RELEASE_BASE"], MPS_dir)
66 args.MPS_dir = MPS_dir
68 mp_regex = re.compile(
r"mp([0-9]+).*")
72 if len(all_campaign_numbers) == 0
73 else sorted(all_campaign_numbers)[-1] + 1)
77 number_of_digits = len(
str(next_number))
78 number_of_digits = 4
if number_of_digits <= 4
else number_of_digits
79 next_campaign =
"mp{{0:0{0}d}}".
format(number_of_digits)
80 next_campaign = next_campaign.format(next_number)
81 os.makedirs(next_campaign)
82 print(
">>> Created new campaign:", next_campaign)
84 campaign_list =
"MP_ali_list.txt" 85 with open(campaign_list,
"a")
as f:
87 backup_dir =
".MP_ali_list" 89 os.makedirs(backup_dir)
91 if e.args == (17,
'File exists'):
95 with open(os.path.join(backup_dir, campaign_list),
"a")
as f:
96 fcntl.flock(f, fcntl.LOCK_EX)
97 f.write(campaign_info)
98 fcntl.flock(f, fcntl.LOCK_UN)
99 print(
" - updated campaign list '"+campaign_list+
"'")
101 if args.copy
is None:
105 for ext
in (
"py",
"ini",
"txt"):
106 for config_file
in glob.glob(args.copy+
"/*."+ext):
107 copied_files.append(os.path.basename(config_file))
108 shutil.copy(config_file, next_campaign)
109 if len(copied_files) == 0:
110 print(
" - no configuration files for '"+args.copy+
"'")
113 alignment_config_ini = os.path.join(next_campaign,
114 "alignment_config.ini")
115 regex_input = (
r"^(jobname\s*[=:])(\s*)"+
116 os.path.basename(args.copy.strip(
"/"))+
r"\s*$",
117 r"\1 "+next_campaign+
r"\n")
118 if os.path.isfile(alignment_config_ini):
121 print(
" - copied configuration files from", end=
' ')
122 print(
"'"+args.copy+
"':",
", ".
join(copied_files))
125 if e.args == (17,
'File exists'):
136 Checks if `directory` matches `regex` and returns the first match converted 137 to an integer. If it does not match -1 is returned. 140 - `regex`: Regular expression to be tested against 141 - `directory`: name of the directory under test 144 result = regex.search(directory)
148 return int(result.group(1))
152 """Adds a line with campaign information from `args` to `campaign_file`. 155 - `campaign_file`: output file 156 - `campaign`: name of the campaign 157 - `args`: command line arguments for this campaign 160 campaign_info = campaign.ljust(10)
161 campaign_info += os.environ[
"USER"].ljust(12)
162 campaign_info += datetime.date.today().isoformat().ljust(11)
164 version = os.environ[
"CMSSW_VERSION"]
165 if args.checked_out[1]:
166 local_area = os.path.join(os.environ[
"CMSSW_BASE"],
"src")
167 with open(os.devnull,
'w')
as devnull:
169 p = subprocess.Popen([
"git",
"tag",
"--points-at",
"HEAD"],
170 cwd = local_area, stdout=subprocess.PIPE,
172 tags = p.communicate()[0].
split()
174 p = subprocess.Popen([
"git",
"ls-files",
"-d",
"-o",
"-m",
175 "--exclude-standard"],
176 cwd = local_area, stdout=subprocess.PIPE,
178 files = p.communicate()[0].
split()
180 p = subprocess.Popen([
"git",
"diff",
"--name-only",
"--staged"],
181 cwd = local_area, stdout=subprocess.PIPE,
183 files.extend(p.communicate()[0].
split())
184 if version
not in tags
or len(files) != 0:
187 campaign_info += version.ljust(34)
188 campaign_info += args.type.ljust(17)
189 campaign_info += args.description.strip() +
"\n" 191 fcntl.flock(campaign_file, fcntl.LOCK_EX)
192 campaign_file.write(campaign_info)
193 fcntl.flock(campaign_file, fcntl.LOCK_UN)
199 """Copies the default configuration templates. 202 - `args`: container with the needed information 203 - `next_campaign`: destination for the copy operation 206 default_conf_dir = os.path.join(args.MPS_dir,
"templates")
207 template_files = (
"universalConfigTemplate.py",
"alignment_config.ini")
208 for f
in template_files:
209 shutil.copy(os.path.join(default_conf_dir, f), next_campaign)
214 auto_gt = args.type.replace(
"MC",
"phase1_2017_realistic")
215 auto_gt = auto_gt.replace(
"data",
"run2_data")
217 (
r"(jobname\s*[=:])(.*)",
r"\1 "+next_campaign),
218 (
r"(globaltag\s*[=:])(.*)",
219 r"\1 auto:"+auto_gt))
221 print(
" - copied default configuration templates from", end=
' ')
222 print(
"'"+default_conf_dir+
"'")
223 print(
" - please modify these template files according to your needs:", end=
' ')
229 Replace all lines in `file_name` matching `regex_string` with 231 Lines starting with '#' or ';' are ignored. 234 - `file_name`: path to the file to be customized 235 - `regex_replace_pairs`: tuples containing a regex string and its 239 comment = re.compile(
r"^\s*[;#]")
241 for regex_string, replace_string
in regex_replace_pairs:
242 replacements.append((re.compile(regex_string), replace_string))
245 with open(file_name,
"r") as f: 248 if not re.match(comment, custom_line):
249 for regex,replace_string
in replacements:
250 custom_line = re.sub(regex, replace_string, custom_line)
251 customized += custom_line
252 with open(file_name,
"w")
as f:
257 if __name__ ==
"__main__":
S & print(S &os, JobReport::InputFile const &f)
def copy_default_templates(args, next_campaign)
static std::string join(char **cmd)
def add_campaign(campaign_file, campaign, args)
def get_first_match(regex, directory)
def customize_default_template(file_name, regex_replace_pairs)