forked from johnmarcampbell/starSubmit
/
request.py
98 lines (79 loc) · 3.4 KB
/
request.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
import xml.etree.ElementTree as et
import xml.dom.minidom as md
from .job import Job
from argparse import ArgumentParser as ap
import subprocess
class Request(object):
"""An object that represents a single scheduler request"""
def __init__(self, job = Job()):
"""Initialize some defaults"""
self.job = job
def get_job_tree(self, job):
"""Turn a Job object into an xml element tree"""
# The root of the job element tree
root = et.Element('job')
for attribute, value in job.config['attributes'].items():
root.set(attribute, value)
# Add commands
commands = et.SubElement(root, 'command')
commands.text = ''
for com in job.config['commands']:
commands.text += '\r\n\t\t' + com
commands.text += '\r\n\t'
# Add Generator stuff
generator = et.SubElement(root, 'Generator')
generator_location = et.SubElement(generator, 'Location')
generator_location.text = job.config['generator_location']
generator_report_location = et.SubElement(generator, 'ReportLocation')
generator_report_location.text = job.config['generator_report_location']
# Add Sandbox stuff
sandbox = et.SubElement(root, 'SandBox')
if job.config['sandbox_installer_option']:
sandbox.set('installer', job.config['sandbox_installer_option'])
sandbox_package = et.SubElement(sandbox, 'Package')
sandbox_package.set('name', job.config['sandbox_package_name'])
for f in job.config['sandbox_files']:
sandbox_files = et.SubElement(sandbox_package, 'File')
sandbox_files.text = f
# Add input files
for url, n_files in job.config['input_files']:
input_files = et.SubElement(root, 'input')
input_files.set('URL', url)
input_files.set('nFiles', n_files)
# Add output files
for toUrl, from_scratch in job.config['output_files']:
output_files = et.SubElement(root, 'output')
output_files.set('toURL', toUrl)
output_files.set('fromScratch', from_scratch)
# Specify stdout and stderr
stdout = et.SubElement(root, 'stdout')
stdout.set('discard', 'true')
stderr = et.SubElement(root, 'stderr')
stderr.set('URL', job.config['stderr_path'])
return root
def make_xml(self):
"""Iterate over jobs and make and xml document out of them"""
self.tree = self.get_job_tree(self.job)
def __str__(self):
self.make_xml()
dom = md.parseString( et.tostring(self.tree) )
bytes_str = dom.toprettyxml(encoding = 'utf-8')
return bytes_str.decode('utf-8')
if __name__ == '__main__':
argparser = ap()
argparser.add_argument('config_file')
argparser.add_argument('-f', '--file', help='Name of xml file to write to')
argparser.add_argument('-s', '--submit', help='Submit xml file after writing',
action='store_true')
args = argparser.parse_args()
job = Job(config_file=args.config_file)
req = Request(job)
if args.file:
with open(args.file, 'w') as f:
f.write(req.__str__())
else:
print(req)
if args.submit and args.file:
subprocess.call(['star-submit', args.file])
elif args.submit:
print('User invoked --submit but did not specify an xml file with --file')