|
| 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