Skip to content
This repository was archived by the owner on Aug 11, 2020. It is now read-only.

Commit 1475ee1

Browse files
committed
add workspace upload and job create function
1 parent 2cd7ba8 commit 1475ee1

File tree

1 file changed

+84
-46
lines changed

1 file changed

+84
-46
lines changed

paperspace.py

Lines changed: 84 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,22 @@
99
CONFIG_HOST = 'https://api.paperspace.io'
1010
CONFIG_LOG_HOST = 'https://logs.paperspace.io'
1111

12+
def zip(obj_name):
13+
zipname = os.path.join(tempfile.gettempdir(), os.path.basename(obj_name)) + '.zip'
14+
outZipFile = zipfile.ZipFile(zipname, 'w', zipfile.ZIP_DEFLATED)
15+
if os.path.isdir(obj_name):
16+
for dirpath, dirnames, filenames in os.walk(obj_name):
17+
for filename in dirnames + filenames:
18+
filepath = os.path.join(dirpath, filename)
19+
basename = os.path.basename(filepath)
20+
if '/.git/' not in filepath and basename not in ['.git', '.gitignore']:
21+
arcname = os.path.relpath(filepath, obj_name)
22+
outZipFile.write(filepath, arcname)
23+
else:
24+
outZipFile.write(obj_name)
25+
outZipFile.close()
26+
return zipname
27+
1228
def print_json_pretty(res):
1329
print(json.dumps(res, indent = 2, sort_keys = True))
1430

@@ -30,27 +46,44 @@ def paperspace(category, method, params):
3046
http_method = 'POST'
3147
path = '/' + category + '/' + method
3248

33-
r = requests.request(http_method, CONFIG_HOST + path, headers = {'x-api-key': PAPERSPACE_API_KEY }, params = params)
49+
files = None
50+
if method == 'createJob' and 'workspace' in params:
51+
workspace = params['workspace']
52+
workspace_file = None
53+
if workspace and workspace != 'none':
54+
workspace_path = os.path.expanduser(workspace)
55+
if os.path.exists(workspace_path):
56+
if not workspace_path.endswith('.zip') and not workspace_path.endswith('.gz'):
57+
workspace_file = zip(workspace_path)
58+
else:
59+
workspace_file = workspace_path
60+
files = {'file': open(workspace_file, 'rb')}
61+
params['workspaceFileName'] = os.path.basename(workspace_file)
62+
del params['workspace']
63+
64+
r = requests.request(http_method, CONFIG_HOST + path, headers = {'x-api-key': PAPERSPACE_API_KEY }, params = params, files = files)
3465

3566
return r.json()
3667

37-
res = paperspace('jobs', 'getJobs', {'project': 'paperspace-node'})
38-
for e in res:
39-
print(e['id'])
68+
#Tests:
4069

41-
res = paperspace('jobs', 'getJob', {'jobId': 'j8eww41akg9h0'})
42-
print(res['state'])
43-
print_json_pretty(res)
70+
#jobs = paperspace('jobs', 'getJobs', {'project': 'paperspace-node'})
71+
#for job in jobs:
72+
# print(job['id'])
4473

45-
res = paperspace('jobs', 'stop', {'jobId': 'j8eww41akg9h0'})
46-
print_json_pretty(res)
74+
#job = paperspace('jobs', 'getJob', {'jobId': 'j8eww41akg9h0'})
75+
#print(job['state'])
76+
#print_json_pretty(job)
4777

48-
#res = paperspace('jobs', 'clone', {'jobId': 'j8eww41akg9h0'})
49-
#print_json_pretty(res)
78+
#job = paperspace('jobs', 'stop', {'jobId': 'j8eww41akg9h0'})
79+
#print_json_pretty(job)
5080

51-
#res = paperspace('jobs', 'createJob', {'project': 'paperspace-python',
81+
#job = paperspace('jobs', 'clone', {'jobId': 'j8eww41akg9h0'})
82+
#print_json_pretty(job)
83+
84+
#job = paperspace('jobs', 'createJob', {'project': 'paperspace-python',
5285
# 'workspace':'none', 'machineType': 'GPU+', 'container': 'Test-Container'})
53-
#print_json_pretty(res)
86+
#print_json_pretty(job)
5487

5588
def paperspace_jobs_logs(params, tail = False, json = False):
5689
last_line = 0
@@ -100,43 +133,48 @@ def paperspace_jobs_logs(params, tail = False, json = False):
100133
if json:
101134
print_json_pretty(json_res)
102135

103-
paperspace_jobs_logs({'jobId': 'jsjo03tmsh6kzy', 'limit': 4}, tail = True)
136+
#Test
137+
#paperspace_jobs_logs({'jobId': 'jsjo03tmsh6kzy', 'limit': 4}, tail = True)
104138

105-
def zip(obj_name):
106-
zipname = os.path.join(tempfile.gettempdir(), os.path.basename(obj_name)) + '.zip'
107-
outZipFile = zipfile.ZipFile(zipname, 'w', zipfile.ZIP_DEFLATED)
108-
if os.path.isdir(obj_name):
109-
for dirpath, dirnames, filenames in os.walk(obj_name):
110-
for filename in dirnames + filenames:
111-
filepath = os.path.join(dirpath, filename)
112-
basename = os.path.basename(filepath)
113-
if '/.git/' not in filepath and basename not in ['.git', '.gitignore']:
114-
arcname = os.path.relpath(filepath, obj_name)
115-
outZipFile.write(filepath, arcname)
139+
def paperspace_jobs_waitfor(params):
140+
while True:
141+
job = paperspace('jobs', 'getJob', params)
142+
state = job['state']
143+
if state == params['state'] or \
144+
(state == 'Running' and params['state'] == 'Pending') or \
145+
state == 'Error' or \
146+
state == 'Stopped' or \
147+
state == 'Failed':
148+
return job
149+
time.sleep(5)
150+
151+
def paperspace_jobs_create(params):
152+
job = paperspace('jobs', 'createJob', params)
153+
jobId = job['id']
154+
print_json_pretty(job)
155+
print("New jobId: %s" % jobId)
156+
print("Job %s" % job['state'])
157+
158+
if job['state'] == 'Pending':
159+
print('Waiting for job to run...')
160+
job = paperspace_jobs_waitfor({'jobId': jobId, 'state': 'Running'})
161+
162+
if job['state'] != 'Error':
163+
print('Awaiting logs...')
164+
paperspace_jobs_logs({'jobId': jobId}, tail = True)
165+
job = paperspace('jobs', 'getJob', {'jobId': jobId})
166+
167+
if job['state'] != 'Error':
168+
print("Job %s; exitCode %d" % (job['state'], job['exitCode']))
116169
else:
117-
outZipFile.write(obj_name)
118-
outZipFile.close()
119-
return zipname
120-
121-
workspace = '~/myproject3'
122-
#workspace = 'paperspace.py'
123-
#workspace = None
124-
125-
workspace_file = None
126-
if workspace and workspace != 'none':
127-
workspace_path = os.path.expanduser(workspace)
128-
if os.path.exists(workspace_path):
129-
if not workspace_path.endswith('.zip') and not workspace_path.endswith('.gz'):
130-
workspace_file = zip(workspace_path)
131-
else:
132-
workspace_file = workspace_path
170+
print("Job %s: %s" % (job['state'], job['jobError'])
133171

134-
if workspace_file:
135-
# add workspace_file to POST
136-
print(workspace_file)
172+
#Test
173+
paperspace_jobs_create({'project': 'myproject3',
174+
'machineType': 'GPU+', 'container': 'Test-Container',
175+
'command': './do.sh', 'workspace': '~/myproject3'})
137176

138-
# TODO:
139-
# upload a workspace folder
177+
# TODO:
140178
# download artifacts
141179
# create/use project config
142180
# deal with timeouts/server unreachable

0 commit comments

Comments
 (0)