submitter.py 2.81 KB
Newer Older
Michal Simon's avatar
Michal Simon committed
1
#   Copyright notice:
2
#   Copyright  Members of the EMI Collaboration, 2013.
3
#
Michal Simon's avatar
Michal Simon committed
4
#   See www.eu-emi.eu for details on the copyright holders
5
#
Michal Simon's avatar
Michal Simon committed
6
7
8
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
9
#
Michal Simon's avatar
Michal Simon committed
10
#       http://www.apache.org/licenses/LICENSE-2.0
11
#
Michal Simon's avatar
Michal Simon committed
12
13
14
15
16
17
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

18
19
try:
    import simplejson as json
20
21
except:
    import json
22
23


ayllon's avatar
ayllon committed
24
class Submitter(object):
ayllon's avatar
ayllon committed
25
26
27
28

    def __init__(self, context):
        self.context = context

29
    @staticmethod
30
    def build_submission(transfers=None, delete=None, staging=None, params=None, **kwargs):
ayllon's avatar
ayllon committed
31
        job = dict()
32
        job['params'] = dict()
33
        if params:
34
            job['params'].update(params)
ayllon's avatar
ayllon committed
35
        job['params'].update(kwargs)
36
37
38

        if delete:
            job['delete'] = delete
39
40
        if staging:
            job['staging'] = staging
41
42
        if transfers:
            job['files'] = transfers
43
            if 'checksum' in job['params']:
44
45
                for f in job['files']:
                    if 'checksum' not in f:
46
                        f['checksum'] = job['params']['checksum']
47
                del job['params']['checksum']
48
            if 'filesize' in job['params']:
49
                for f in job['files']:
50
                    f['filesize'] = job['params']['filesize']
51
                del job['params']['filesize']
52
            if 'file_metadata' in job['params']:
53
                for f in job['files']:
54
                    f['metadata'] = job['params']['file_metadata']
55
                del job['params']['file_metadata']
ayllon's avatar
ayllon committed
56
57
58

        return json.dumps(job, indent=2)

59
60
61
62
63
    def submit(self, transfers=None, delete=None, params=None, **kwargs):
        job = Submitter.build_submission(transfers=transfers,
                                         delete=delete,
                                         params=params,
                                         **kwargs)
ayllon's avatar
ayllon committed
64
65
66
        r = json.loads(self.context.post_json('/jobs', job))
        return r['job_id']

67
68
69
70
71
72
    def cancel(self, job_id, file_ids = None):
        if file_ids is not None:
            file_ids_str = ','.join(map(str, file_ids))
            return json.loads(self.context.delete('/jobs/%s/files/%s' % (job_id, file_ids_str)))
        else:
            return json.loads(self.context.delete('/jobs/%s' % job_id))
73
74
75
76
77
78

    def cancel_all(self, vo = None):
        if vo is None:
            return json.loads(self.context.delete('/jobs/all'))
        else:
            return json.loads(self.context.delete('/jobs/vo/%s' % vo))