forked from kdlong/SelectorTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
submitMakeHistFileToCondor.py
executable file
·225 lines (195 loc) · 9.69 KB
/
submitMakeHistFileToCondor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env python
import argparse
import makeFileList
from python import UserInput,OutputTools
from python import ConfigureJobs
import datetime
import logging
import os
import shutil
import glob
import tarfile
import math
import re
import subprocess
import logging
def getComLineArgs():
parser = UserInput.getDefaultParser(False)
parser.add_argument("-d", "--submit_dir", type=str,
required=True, help="Output directory")
parser.add_argument("--debug", action='store_true',
help="Print verbose info")
parser.add_argument("-n", "--files_per_job", type=int,
default=3, help="Number of files per job")
parser.add_argument("--submit", action='store_true',
help="Just make the directory, don't submit")
parser.add_argument("--local", action='store_true',
help="Use local files, e.g., file_path")
parser.add_argument("--input_tier", type=str,
default="", help="Selection stage of input files")
parser.add_argument("--memory", type=int,
default=2000, help="Memory to request for condor jobs")
parser.add_argument("-q", "--queue", type=str,
default="longlunch", help="lxplus queue, or 'uw' for wisconsin settings")
parser.add_argument("-m", "--merge", nargs=2, type=str,
metavar=("mergedFileName", "completeFraction"),
default=None, help="Merge outputs from all jobs to file (submit as DAG)" \
"if > completeFraction (in %%) jobs complete")
parser.add_argument("--force", action='store_true',
help="Force overwrite of existing directories")
parser.add_argument("--removeUnmerged", action='store_true',
help="Remove unmerged ROOT files (requires DAG)")
args = parser.parse_args()
if args.removeUnmerged and not args.merge:
parser.error("--removeUnmerged requires --merge")
return vars(args)
def makeSubmitDir(submit_dir, force):
log_dir = submit_dir + "/logs"
if os.path.isdir(submit_dir) and force:
logging.warning("Overwriting directory %s" % submit_dir)
shutil.rmtree(submit_dir)
elif os.path.isdir(submit_dir):
raise IOError("Submit directory %s already exists! Use --force to overrite." % submit_dir)
os.makedirs(log_dir)
def setupMergeStep(submit_dir, queue, numjobs, merge, removeUnmerged):
merge_file = merge[0]
completeFraction = float(merge[1])
if completeFraction < 0 or completeFraction > 1.:
raise InvalidArgument("completeFraction must be between 0 and 1, found %f" % completeFraction)
template_dict = {
"queue" : queue,
"merge_file" : merge_file
}
template = "Templates/CondorSubmit/merge_template.jdl"
outfile = "/".join([submit_dir, "merge.jdl"])
ConfigureJobs.fillTemplatedFile(template, outfile, template_dict)
template = "Templates/CondorSubmit/merge.sh"
outfile = "/".join([submit_dir, "merge.sh"])
ConfigureJobs.fillTemplatedFile(template, outfile, {"CMSSW_RELEASE_BASE" : os.environ["CMSSW_BASE"]})
template = "Templates/CondorSubmit/submit_and_merge_template.dag"
outfile = "/".join([submit_dir, "submit_and_merge.dag"])
ConfigureJobs.fillTemplatedFile(template, outfile,
{"minComplete" : int(completeFraction*numjobs),
"postMerge" : ("SCRIPT POST B removeRootFiles.sh %s" % merge_file) if removeUnmerged else ""})
for f in ["list_infiles.sh", "completed.sh", "removeRootFiles.sh"]:
shutil.copy("Templates/CondorSubmit/%s" % f, "/".join([submit_dir, f]))
def copyLibs():
libdir = "lib"
if os.path.isdir(libdir):
shutil.rmtree(libdir)
os.mkdir(libdir)
cmssw_libdir = "/".join([os.environ["CMSSW_BASE"], libdir, os.environ["SCRAM_ARCH"], "*SelectorTools*"])
for i in glob.glob(cmssw_libdir):
shutil.copyfile(i, '/'.join([libdir, os.path.basename(i)]))
# Needed on lxplus and uwlogin, where the afs permissions are set
# very tight and don't let condor access some dumb file it needs
# TODO: Understand what it needs and why
def modifyAFSPermissions():
if not re.findall("system:anyuser *.*l", subprocess.check_output(["fs", "la"])):
subprocess.call(["find", os.environ["CMSSW_BASE"], "-type", "d",
"-exec", "fs", "setacl", "-dir", "{}", "-acl", "system:anyuser", "rl", ";"])
raise OSError("AFS permissions have been relaxed for condor submission. You should recompile and resubmit")
def copyDatasetManagerFiles(analysis):
manager_name = "AnalysisDatasetManager"
manager_path = ConfigureJobs.getManagerPath()
if os.path.isdir(manager_name):
shutil.rmtree(manager_name)
info_dir = manager_name+"/FileInfo"
plot_dir = manager_name+"/PlotObjects"
os.makedirs(info_dir)
os.makedirs(plot_dir)
for paths in [[manager_path, manager_name, "Utilities"],
[manager_path, info_dir, "data"],
[manager_path, info_dir, "montecarlo"],
[manager_path, plot_dir, analysis]]:
path = '/'.join(paths)
if os.path.isdir(path):
shutil.copytree(path, '/'.join(paths[1:]))
else:
for d in glob.glob(path+'*'):
shutil.copy(d, d.replace(manager_path+"/", ""))
# TODO: Check if this is needed at UW. I think it isn't
def copyGridCertificate():
proxypath = "/tmp/x509up_u%s" % os.getuid() if not \
("X509_USER_PROXY" in os.environ and os.path.isfile(os.environ["X509_USER_PROXY"])) \
else os.environ["X509_USER_PROXY"]
shutil.copy(proxypath, "userproxy")
def tarAnalysisInfo(condor_dir, tarball_name):
tarname = condor_dir+"/"+tarball_name
with tarfile.open(tarname, "w:gz") as tar:
tar.add("Utilities")
tar.add("data")
tar.add("lib")
tar.add("AnalysisDatasetManager")
shutil.rmtree("lib")
shutil.rmtree("AnalysisDatasetManager")
def getUWCondorSettings():
return """# (wisconsin-specific) tell glideins to run job with access to cvmfs (via parrot)
+RequiresCVMFS = True
+RequiresSharedFS = True
+IsFastQueueJob = True
Requirements = TARGET.Arch == "X86_64" && IsSlowSlot=!=true && (MY.RequiresSharedFS=!=true || TARGET.HasAFS_OSG) && (TARGET.HasParrotCVMFS=?=true || (TARGET.UWCMS_CVMFS_Exists && TARGET.CMS_CVMFS_Exists))
"""
def writeSubmitFile(submit_dir, analysis, selection, input_tier, queue, memory, filelist, numfiles, nPerJob, selArgs):
template_dict = {
"analysis" : analysis,
"selection" : selection,
"input_tier" : input_tier,
"queue" : queue,
"memory" : memory,
"filelist" : filelist.split(".txt")[0],
"nPerJob" : nPerJob,
"nJobs" : int(math.ceil(float(numfiles)/nPerJob)),
"extraArgs" : "--debug --compress %s" % ("" if not selArgs else ("--selectorArgs "+" ".join(selArgs)))
}
template = "Templates/CondorSubmit/submit_template.jdl"
outfile = "/".join([submit_dir, "submit.jdl"])
ConfigureJobs.fillTemplatedFile(template, outfile, template_dict)
def writeWrapperFile(submit_dir, tarball_name):
template_dict = {
"CMSSW_RELEASE_BASE" : os.environ["CMSSW_RELEASE_BASE"],
"tarball" : tarball_name
}
template = "Templates/CondorSubmit/wrapRunSelector.sh"
outfile = "/".join([submit_dir, "wrapRunSelector.sh"])
ConfigureJobs.fillTemplatedFile(template, outfile, template_dict)
def writeMetaInfo(submit_dir, filename):
with open("/".join([submit_dir, filename]), "w") as metafile:
metafile.write(OutputTools.getScriptCall()+"\n\n")
metafile.write("Script called at %s \n" % datetime.datetime.now())
metafile.write("git hash: " + OutputTools.gitHash()+"\n")
metafile.write("git diff: " + OutputTools.gitDiff()+"\n")
def submitDASFilesToCondor(filenames, submit_dir, analysis, selection, input_tier, queue, memory,
numPerJob, force, das, selArgs, merge, removeUnmerged):
makeSubmitDir(submit_dir, force)
copyLibs()
copyDatasetManagerFiles(analysis)
modifyAFSPermissions()
filelist_name = '_'.join(filenames[:max(len(filenames), 4)])
filelist_name = filelist_name.replace("*", "ALL")
filelist = '/'.join([submit_dir, filelist_name+'_filelist.txt'])
numfiles = makeFileList.makeFileList(filenames, filelist, analysis, input_tier, das)
#TODO: I don't think there's any harm in addition the accounting group, but
# it doesn't do anything if you aren't a member of CMST3 group
queue = '+JobFlavour = "{0}"\n+AccountingGroup = "group_u_CMST3.all"'.format(queue) \
if queue != 'uw' else getUWCondorSettings()
writeSubmitFile(submit_dir, analysis, selection, input_tier, queue, memory, filelist_name, numfiles, numPerJob, selArgs)
if merge:
setupMergeStep(submit_dir, queue, math.ceil(numfiles/numPerJob), merge, removeUnmerged)
tarball_name = '_'.join([analysis, "AnalysisCode.tgz"])
writeWrapperFile(submit_dir, tarball_name)
tarAnalysisInfo(submit_dir, tarball_name)
writeMetaInfo(submit_dir, "metaInfo.txt")
def main():
args = getComLineArgs()
logging.basicConfig(level=(logging.DEBUG if args['debug'] else logging.INFO))
submitDASFilesToCondor(args['filenames'], args['submit_dir'], args['analysis'],
args['selection'], args['input_tier'], args['queue'], args['memory'], args['files_per_job'], args['force'],
not args['local'], args['selectorArgs'], args['merge'], args['removeUnmerged'])
if args['submit']:
command = 'condor_submit' if not args['merge'] else 'condor_submit_dag'
submitfile = 'submit.jdl' if not args['merge'] else 'submit_and_merge.dag'
os.chdir(args['submit_dir'])
subprocess.call([command, '/'.join([args['submit_dir'], submitfile])])
if __name__ == "__main__":
main()