2752: arv-put explains resumed uploads in more detail.
[arvados.git] / sdk / python / arvados / __init__.py
1 import gflags
2 import httplib
3 import httplib2
4 import logging
5 import os
6 import pprint
7 import sys
8 import types
9 import subprocess
10 import json
11 import UserDict
12 import re
13 import hashlib
14 import string
15 import bz2
16 import zlib
17 import fcntl
18 import time
19 import threading
20
21 from api import *
22 from collection import *
23 from keep import *
24 from stream import *
25 import errors
26 import util
27
28 def task_set_output(self,s):
29     api('v1').job_tasks().update(uuid=self['uuid'],
30                                  body={
31             'output':s,
32             'success':True,
33             'progress':1.0
34             }).execute()
35
36 _current_task = None
37 def current_task():
38     global _current_task
39     if _current_task:
40         return _current_task
41     t = api('v1').job_tasks().get(uuid=os.environ['TASK_UUID']).execute()
42     t = UserDict.UserDict(t)
43     t.set_output = types.MethodType(task_set_output, t)
44     t.tmpdir = os.environ['TASK_WORK']
45     _current_task = t
46     return t
47
48 _current_job = None
49 def current_job():
50     global _current_job
51     if _current_job:
52         return _current_job
53     t = api('v1').jobs().get(uuid=os.environ['JOB_UUID']).execute()
54     t = UserDict.UserDict(t)
55     t.tmpdir = os.environ['JOB_WORK']
56     _current_job = t
57     return t
58
59 def getjobparam(*args):
60     return current_job()['script_parameters'].get(*args)
61
62 def get_job_param_mount(*args):
63     return os.path.join(os.environ['TASK_KEEPMOUNT'], current_job()['script_parameters'].get(*args))
64
65 def get_task_param_mount(*args):
66     return os.path.join(os.environ['TASK_KEEPMOUNT'], current_task()['parameters'].get(*args))
67
68 class JobTask(object):
69     def __init__(self, parameters=dict(), runtime_constraints=dict()):
70         print "init jobtask %s %s" % (parameters, runtime_constraints)
71
72 class job_setup:
73     @staticmethod
74     def one_task_per_input_file(if_sequence=0, and_end_task=True, input_as_path=False):
75         if if_sequence != current_task()['sequence']:
76             return
77         job_input = current_job()['script_parameters']['input']
78         cr = CollectionReader(job_input)
79         for s in cr.all_streams():
80             for f in s.all_files():
81                 if input_as_path:
82                     task_input = os.path.join(job_input, s.name(), f.name())
83                 else:
84                     task_input = f.as_manifest()
85                 new_task_attrs = {
86                     'job_uuid': current_job()['uuid'],
87                     'created_by_job_task_uuid': current_task()['uuid'],
88                     'sequence': if_sequence + 1,
89                     'parameters': {
90                         'input':task_input
91                         }
92                     }
93                 api('v1').job_tasks().create(body=new_task_attrs).execute()
94         if and_end_task:
95             api('v1').job_tasks().update(uuid=current_task()['uuid'],
96                                        body={'success':True}
97                                        ).execute()
98             exit(0)
99
100     @staticmethod
101     def one_task_per_input_stream(if_sequence=0, and_end_task=True):
102         if if_sequence != current_task()['sequence']:
103             return
104         job_input = current_job()['script_parameters']['input']
105         cr = CollectionReader(job_input)
106         for s in cr.all_streams():
107             task_input = s.tokens()
108             new_task_attrs = {
109                 'job_uuid': current_job()['uuid'],
110                 'created_by_job_task_uuid': current_task()['uuid'],
111                 'sequence': if_sequence + 1,
112                 'parameters': {
113                     'input':task_input
114                     }
115                 }
116             api('v1').job_tasks().create(body=new_task_attrs).execute()
117         if and_end_task:
118             api('v1').job_tasks().update(uuid=current_task()['uuid'],
119                                        body={'success':True}
120                                        ).execute()
121             exit(0)
122
123