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

Commit edabe79

Browse files
committed
add paperspace login method for acquiring, caching, using api key
1 parent 39ee04f commit edabe79

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

paperspace/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
from .config import *
2+
from .login import login, logout
23
from .jobs import print_json_pretty
34
from . import jobs

paperspace/jobs.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import requests
1212

1313
from . import config
14+
from .login import apikey
1415

1516
def zip_to_tmp(obj_name):
1617
zipname = os.path.join(tempfile.gettempdir(),
@@ -38,6 +39,8 @@ def print_json_pretty(res):
3839

3940

4041
def method(category, method, params):
42+
if not config.PAPERSPACE_API_KEY:
43+
config.PAPERSPACE_API_KEY = apikey()
4144

4245
if method in ['artifactsGet', 'artifactsList', 'getJob', 'getJobs',
4346
'getLogs']:
@@ -108,6 +111,9 @@ def destroy(params):
108111

109112

110113
def logs(params, tail=False, no_logging=False):
114+
if not config.PAPERSPACE_API_KEY:
115+
config.PAPERSPACE_API_KEY = apikey()
116+
111117
last_line = 0
112118
PSEOF = False
113119
result = []

paperspace/login.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import getpass
2+
import json
3+
import os
4+
import sys
5+
6+
import requests
7+
8+
from . import config
9+
10+
11+
def errorcheck(res, *args):
12+
if 'error' in res:
13+
if 'message' in res['error']:
14+
print(res['error']['message'])
15+
return False
16+
print(json.dumps(res, indent=2, sort_keys=True))
17+
return False
18+
elif not all(key in res for key in args):
19+
if 'message' in res:
20+
print(res['message'])
21+
return False
22+
print(json.dumps(res, indent=2, sort_keys=True))
23+
return False
24+
return True
25+
26+
27+
def login(email=None, password=None, apiToken=None):
28+
paperspace_dir = os.path.expanduser('~/.paperspace')
29+
config_path = os.path.join(paperspace_dir, 'config.json')
30+
if not os.path.exists(paperspace_dir):
31+
os.makedirs(paperspace_dir)
32+
config_data = {}
33+
if os.path.exists(config_path):
34+
config_data = json.load(open(config_path))
35+
36+
if not email:
37+
email = input('Email: ')
38+
if not password:
39+
password = getpass.getpass('Password: ')
40+
41+
# get access_token
42+
params = { "email": email, "password": password }
43+
r = requests.request('post', config.CONFIG_HOST + '/users/login',
44+
json=params)
45+
res = r.json()
46+
47+
if not errorcheck(res, 'id'):
48+
return False
49+
50+
# get api key using access_token
51+
params = { 'access_token': res['id'] }
52+
if apiToken:
53+
params['apiToken'] = apiToken
54+
r = requests.request('post', config.CONFIG_HOST + '/apiTokens/createPublic',
55+
params=params)
56+
api_token = r.json()
57+
58+
if not errorcheck(api_token, 'key', 'name'):
59+
return False
60+
61+
# update config.PAPERSPACE_API_KEY
62+
config.PAPERSPACE_API_KEY = api_token['key']
63+
64+
# save api key
65+
config_data['apiKey'] = api_token['key']
66+
config_data['name'] = api_token['name']
67+
with open(config_path, 'w') as outfile:
68+
json.dump(config_data, outfile, indent=2, sort_keys=True)
69+
outfile.write('\n')
70+
71+
return True
72+
73+
74+
def apikey():
75+
paperspace_dir = os.path.expanduser('~/.paperspace')
76+
config_path = os.path.join(paperspace_dir, 'config.json')
77+
if os.path.exists(config_path):
78+
config_data = json.load(open(config_path))
79+
if config_data and 'apiKey' in config_data:
80+
return config_data['apiKey']
81+
return ''
82+
83+
84+
def logout():
85+
paperspace_dir = os.path.expanduser('~/.paperspace')
86+
config_path = os.path.join(paperspace_dir, 'config.json')
87+
if os.path.exists(config_path):
88+
config_data = {}
89+
with open(config_path, 'w') as outfile:
90+
json.dump(config_data, outfile, indent=2, sort_keys=True)
91+
outfile.write('\n')
92+
93+
if __name__ == '__main__':
94+
email, password, apiToken, *rest = sys.argv[1:] + [None] * 3
95+
if not login(email, password, apiToken):
96+
sys.exit(1)

0 commit comments

Comments
 (0)