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

Commit eb8b89e

Browse files
committed
add python run as module option; add support for python script args; add dryrun option; fix workspace archive name issue; fix no_logging issue
1 parent e372495 commit eb8b89e

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

paperspace/jobs.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def zip_to_tmp(files, ignore_files=[]):
2323
files_added = set()
2424
with outZipFile:
2525
for file in files:
26+
file = os.path.abspath(os.path.expanduser(file))
2627
if os.path.isdir(file):
2728
for dirpath, dirnames, filenames in os.walk(file):
2829
dirnames[:] = [d for d in dirnames if d not in ignore_files]
@@ -77,7 +78,7 @@ def method(category, method, params):
7778
if method == 'createJob' and 'workspace' in params:
7879

7980
workspace = params.get('workspace', None)
80-
ignore_files.extend(['.git', '.gitignore'])
81+
ignore_files.extend(['.git', '.gitignore', '__pycache__'])
8182
if workspace:
8283
if workspace != 'none':
8384
workspace_files.insert(0, workspace)
@@ -89,15 +90,15 @@ def method(category, method, params):
8990
file_path = os.path.expanduser(file)
9091
if not os.path.exists(file_path):
9192
message = format('error: file or directory not found: %s' % file_path)
92-
print(message)
9393
if no_logging:
9494
return { 'error': True, 'message': message }
95+
print(message)
9596
sys.exit(1)
9697
elif file_path == '/':
9798
message = 'error: cannot zip root directory'
98-
print(message)
9999
if no_logging:
100100
return { 'error': True, 'message': message }
101+
print(message)
101102
sys.exit(1)
102103

103104
if len(workspace_files) == 1 and (file_path.endswith('.zip') or file_path.endswith('.gz')):
@@ -157,7 +158,7 @@ def logs(params, tail=False, no_logging=False):
157158
elif not config.PAPERSPACE_API_KEY:
158159
config.PAPERSPACE_API_KEY = apikey()
159160
tail = params.pop('tail', False) or tail
160-
no_logging = params.pop('no_logging', False) or no_logging
161+
no_logging = no_logging or params.pop('no_logging', False)
161162

162163
last_line = 0
163164
PSEOF = False
@@ -267,7 +268,7 @@ def create(params, no_logging=False, extra_files=[]):
267268

268269
if job['state'] != 'Error' and job['state'] != 'Cancelled':
269270
print('Awaiting logs...')
270-
if logs({'jobId': jobId}, tail=True):
271+
if logs({'jobId': jobId}, tail=True, no_logging=no_logging):
271272
job = method('jobs', 'getJob', {'jobId': jobId})
272273
else:
273274
job = waitfor({'jobId': jobId, 'state': 'Stopped'})
@@ -416,8 +417,17 @@ def run(params={}, no_logging=False):
416417
python_ver = params.pop('python', str(sys.version_info[0])) # defaults locally running version
417418
# TODO validate python version; handle no version, specific version
418419

420+
run_as_module_opt = ''
421+
if params.pop('module', None):
422+
run_as_module_opt = '-m '
423+
424+
script_args = params.pop('script_args', None)
425+
args = ''
426+
if script_args:
427+
args = ' ' + ' '.join(script_args)
428+
419429
if 'command' not in params:
420-
params['command'] = 'python' + python_ver + ' ' + src_file
430+
params['command'] = 'python' + python_ver + ' ' + run_as_module_opt + src_file + args
421431

422432
if not os.path.exists(src_path):
423433
message = format('error: file not found: %s' % src_path)
@@ -472,7 +482,11 @@ def run(params={}, no_logging=False):
472482
init = 'init.sh'
473483
if os.path.exists(init):
474484
params['extraFiles'].append(init)
475-
params['command'] = '. ' + os.path.basename(init) + '\n' + params['command']
485+
params['command'] = 'source ' + os.path.basename(init) + '\n' + params['command']
486+
487+
if params.pop('dryrun', None):
488+
print(params['command'])
489+
sys.exit(1)
476490

477491
res = create(params, no_logging)
478492
if run_this:

paperspace/main.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,22 +81,25 @@ def main():
8181
sys.exit(1)
8282
elif param in ['init', 'req']:
8383
params[param] = True
84-
if args and not args[0].startswith('--') and not args[0].endswith('.py'):
84+
if args and not args[0].startswith('-') and not args[0].endswith('.py'):
8585
params[param] = args.pop(0)
8686
elif param in ['no_logging', 'nologging', 'noLogging', 'json']:
8787
params['no_logging'] = True
88-
elif param == 'pipenv':
88+
elif param in ['dryrun', 'pipenv']:
8989
params[param] = True
9090
else:
9191
print('error: invalid option: %s' % opt)
9292
print('usage: %s' % run_usage(prog))
9393
sys.exit(1)
94+
elif opt == '-m':
95+
params['module'] = True
9496
elif 'script' not in params:
9597
params['script'] = opt
9698
else:
97-
print('error: invalid argument: %s' % opt)
98-
print('usage: %s' % run_usage(prog))
99-
sys.exit(1)
99+
if 'script_args' not in params:
100+
params['script_args'] = [opt]
101+
else:
102+
params['script_args'].append(opt)
100103
if 'script' not in params:
101104
print('usage: %s' % run_usage(prog))
102105
sys.exit(1)
@@ -124,7 +127,7 @@ def apikey_usage(prog):
124127

125128

126129
def run_usage(prog):
127-
return format('%s run <python_script.py> [--python 2 | 3] [--init [<init.sh>]] [--conda <env>] [--pipenv] [--req [<requirements.txt>]' % prog)
130+
return format('%s run [-m] <python_script.py> [--python 2|3] [--init [<init.sh>]] [--pipenv] [--req [<requirements.txt>] [--workspace .|<workspace_path>] [other jobs create options...] [--dryrun] [script args]' % prog)
128131

129132

130133
def usage(prog):

0 commit comments

Comments
 (0)