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

Commit 39d7cc5

Browse files
committed
add run and apikey commands
1 parent 18d27df commit 39d7cc5

File tree

1 file changed

+81
-7
lines changed

1 file changed

+81
-7
lines changed

paperspace/main.py

Lines changed: 81 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
import os
33

44
from . import __version__
5-
from .login import login, logout
5+
from .login import login, logout, set_apikey
6+
from .jobs import run, print_json_pretty
67

78

89
def main():
@@ -15,12 +16,14 @@ def main():
1516

1617
cmd = args.pop(0)
1718

18-
if cmd in ['help', '--help', '-h']:
19+
help_opts = ['help', '--help', '-h']
20+
21+
if cmd in help_opts:
1922
usage(prog)
2023
sys.exit(0)
2124

2225
if cmd in ['version', '--version', '-v']:
23-
vers()
26+
vers(prog)
2427
sys.exit(0)
2528

2629
if cmd == 'login':
@@ -29,7 +32,10 @@ def main():
2932
apiToken = None
3033
while args:
3134
opt = args.pop(0)
32-
if opt == '--email':
35+
if opt in help_opts:
36+
print('usage: %s' % login_usage(prog))
37+
sys.exit(0)
38+
elif opt == '--email':
3339
email = args.pop(0) if args else None
3440
elif opt == '--password':
3541
password = args.pop(0) if args else None
@@ -44,14 +50,82 @@ def main():
4450
return not login(email, password, apiToken)
4551

4652
if cmd == 'logout':
53+
if args:
54+
print('usage: %s logout' % prog)
55+
sys.exit(not (args[0] in help_opts))
4756
return not logout()
4857

58+
if cmd == 'apikey' or cmd == 'apiKey':
59+
if not args or args[0] in help_opts:
60+
print('usage: %s' % apikey_usage(prog))
61+
sys.exit(not args)
62+
return not set_apikey(args[0])
63+
64+
if cmd == 'run':
65+
if not args or args[0] in help_opts:
66+
print('run usage: %s' % run_usage(prog))
67+
sys.exit(not args)
68+
params = {}
69+
while args:
70+
opt = args.pop(0)
71+
if opt.startswith('--'):
72+
param = opt[2:]
73+
if param in ['script', 'python', 'apiKey', 'container', 'machineType', 'name', 'project', 'projectId', 'command',
74+
'workspace', 'dataset', 'registryUsername', 'registryPassword', 'workspaceUsername', 'workspacePassword']:
75+
if args and not args[0].startswith('--'):
76+
params[param] = args.pop(0)
77+
else:
78+
print('error: missing argument for %s' % opt)
79+
print('usage: %s' % run_usage(prog))
80+
sys.exit(1)
81+
elif param in ['init', 'req', 'pipenv']:
82+
params[param] = True
83+
if args and not args[0].startswith('--') and not args[0].endswith('.py'):
84+
params[param] = args.pop(0)
85+
elif param in ['conda', 'no_logging']:
86+
params[param] = True
87+
else:
88+
print('error: invalid option: %s' % opt)
89+
print('usage: %s' % run_usage(prog))
90+
sys.exit(1)
91+
elif 'script' not in params:
92+
params['script'] = opt
93+
else:
94+
print('error: invalid argument: %s' % opt)
95+
print('usage: %s' % run_usage(prog))
96+
sys.exit(1)
97+
if 'script' not in params:
98+
print('usage: %s' % run_usage(prog))
99+
sys.exit(1)
100+
res = run(params)
101+
if 'error' in res:
102+
print_json_pretty(res)
103+
sys.exit(1)
104+
sys.exit(0)
105+
106+
print('error: invalid command: %s' % cmd)
49107
usage(prog)
50108
sys.exit(1)
51109

52110

53-
def vers():
54-
print('paperspace-python %s' % __version__)
111+
def vers(prog):
112+
print('%s %s' % (prog, __version__))
113+
114+
115+
def login_usage(prog):
116+
return format('%s login [[--email] <user@domain.com>] [[--password] "<secretpw>"] [[--apiToken] "<api token name>"]\n %s logout' % (prog, prog))
117+
118+
119+
def apikey_usage(prog):
120+
return format('%s apikey <api_key>' % prog)
121+
122+
123+
def run_usage(prog):
124+
return format('%s run <python_script.py> [--python 2 | 3] [--conda] [--init [<script.sh>] | --req [<requirements.txt>] | --pipenv [<Pipfile>[,<Pipfile.lock>]]]' % prog)
125+
55126

56127
def usage(prog):
57-
print('usage: %s login [[--email] <user@domain.com>] [[--password] "<secretpw>"] [[--apiToken] "<api token name>"]\n %s logout' % (prog, prog))
128+
print('usage: %s' % login_usage(prog))
129+
print(' %s' % apikey_usage(prog))
130+
print(' %s' % run_usage(prog))
131+
print(' %s version' % prog)

0 commit comments

Comments
 (0)