inquirer.py 2.86 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
ayllon's avatar
ayllon committed
22
import urllib
ayllon's avatar
ayllon committed
23

24
25
from exceptions import *

ayllon's avatar
ayllon committed
26

ayllon's avatar
ayllon committed
27
class Inquirer(object):
ayllon's avatar
ayllon committed
28

ayllon's avatar
ayllon committed
29
30
    def __init__(self, context):
        self.context = context
ayllon's avatar
ayllon committed
31

32
    def get_job_status(self, job_id, list_files=False):
33

Oliver Keeble's avatar
Oliver Keeble committed
34
        if not isinstance(job_id, basestring):
35
36
            raise Exception('The job_id provided is not a string!')

ayllon's avatar
ayllon committed
37
        try:
38
            job_info = json.loads(self.context.get("/jobs/%s" % job_id))
39

40
41
            if list_files:
                job_info['files'] = json.loads(self.context.get("/jobs/%s/files" % job_id))
42
                job_info['dm'] = json.loads(self.context.get("/jobs/%s/dm" % job_id))
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

            return job_info

        except NotFound:
            raise NotFound(job_id)

    def get_jobs_statuses(self, job_ids, list_files=False):

        if isinstance(job_ids, list):
            xfer_ids = ','.join(job_ids)
        else:
            raise Exception('The input provided is not a list of ids!')

        try:
            if not list_files:
                job_info = json.loads(self.context.get("/jobs/%s" % xfer_ids))
            else:
                job_info = json.loads(self.context.get("/jobs/%s?files=file_state,dest_surl,finish_time,start_time,reason,source_surl,file_metadata" % xfer_ids))

62
            return job_info
ayllon's avatar
ayllon committed
63
        except NotFound:
64
            raise NotFound(job_id)
ayllon's avatar
ayllon committed
65

66
    def get_job_list(self, user_dn=None, vo_name=None, source_se=None, dest_se=None, delegation_id=None, state_in=None):
ayllon's avatar
ayllon committed
67
68
        url = "/jobs?"
        args = {}
69
70
71
72
        if user_dn:
            args['user_dn'] = user_dn
        if vo_name:
            args['vo_name'] = vo_name
73
74
75
76
        if source_se:
            args['source_se'] = source_se
        if dest_se:
            args['dest_se'] = dest_se
77
78
79
80
        if delegation_id:
            args['dlg_id'] = delegation_id
        if state_in:
            args['state_in'] = ','.join(state_in)
ayllon's avatar
ayllon committed
81

ayllon's avatar
ayllon committed
82
83
84
85
86
        query = '&'.join(map(lambda (k, v): "%s=%s" % (k, urllib.quote(v, '')),
                             args.iteritems()))
        url += query

        return json.loads(self.context.get(url))
87
88
89

    def whoami(self):
        return json.loads(self.context.get("/whoami"))
90

Maria Arsuaga Rios's avatar
Maria Arsuaga Rios committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105