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

Commit 6e1ac38

Browse files
committed
fix pipenv scripting; fix -m support; add -c support; fix --command support
1 parent 05ddd8b commit 6e1ac38

File tree

1 file changed

+53
-32
lines changed

1 file changed

+53
-32
lines changed

paperspace/jobs.py

Lines changed: 53 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,16 @@ def run(params={}, no_logging=False):
375375
params = {}
376376
params['script'] = script
377377

378+
python_opt = ''
379+
python_cmd_str_term = ''
380+
run_module = params.pop('run_module', None)
381+
if run_module:
382+
python_opt = '-m '
383+
run_command = params.pop('run_command', None)
384+
if run_command:
385+
python_opt = "-c '"
386+
python_cmd_str_term = "' "
387+
378388
params = params.copy()
379389
run_this = False
380390
if 'script' not in params:
@@ -404,8 +414,12 @@ def run(params={}, no_logging=False):
404414
with open(src_path, "w") as file:
405415
file.write(src)
406416
else:
407-
src_file = os.path.basename(params['script'])
408-
src_path = params.pop('script')
417+
if not run_module and not run_command:
418+
src_file = os.path.basename(params['script'])
419+
src_path = params.pop('script')
420+
else:
421+
src_file = params.pop('script')
422+
src_path = src_file
409423

410424
if 'project' not in params:
411425
params['project'] = 'paperspace-python'
@@ -417,59 +431,66 @@ def run(params={}, no_logging=False):
417431
python_ver = params.pop('python', str(sys.version_info[0])) # defaults locally running version
418432
# TODO validate python version; handle no version, specific version
419433

420-
run_as_module_opt = ''
421-
if params.pop('module', None):
422-
run_as_module_opt = '-m '
423-
424434
script_args = params.pop('script_args', None)
425435
args = ''
426436
if script_args:
427437
args = ' ' + ' '.join(script_args)
428438

429439
if 'command' not in params:
430-
params['command'] = 'python' + python_ver + ' ' + run_as_module_opt + src_file + args
431-
432-
if not os.path.exists(src_path):
433-
message = format('error: file not found: %s' % src_path)
434-
print(message)
435-
if 'no_logging' in params:
436-
return { 'error': True, 'message': message }
437-
sys.exit(1)
438-
elif os.path.isdir(src_path):
439-
message = format('error: specified file is a directory: %s' % src_path)
440-
print(message)
441-
if 'no_logging' in params:
442-
return { 'error': True, 'message': message }
443-
sys.exit(1)
440+
params['command'] = 'python' + python_ver + ' ' + python_opt + src_file + python_cmd_str_term + args
444441

445442
params['extraFiles'] = []
446-
if 'workspace' not in params:
447-
params['workspace'] = src_path
443+
if not run_module and not run_command:
444+
if not os.path.exists(src_path):
445+
message = format('error: file not found: %s' % src_path)
446+
print(message)
447+
if 'no_logging' in params:
448+
return { 'error': True, 'message': message }
449+
sys.exit(1)
450+
elif os.path.isdir(src_path):
451+
message = format('error: specified file is a directory: %s' % src_path)
452+
print(message)
453+
if 'no_logging' in params:
454+
return { 'error': True, 'message': message }
455+
sys.exit(1)
456+
if 'workspace' not in params:
457+
params['workspace'] = src_path
458+
else:
459+
params['extraFiles'].append(src_path)
448460
else:
449-
params['extraFiles'].append(src_path)
461+
if not run_command and os.path.exists(src_path) and not os.path.isdir(src_path):
462+
if 'workspace' not in params:
463+
params['workspace'] = src_path
464+
else:
465+
params['extraFiles'].append(src_path)
450466

451467
if 'ignoreFiles' in params:
452468
if isinstance(params['ignoreFiles'], str):
453469
params['ignoreFiles'] = params['ignoreFiles'].split(',')
454470

471+
pipenv = params.pop('pipenv', None)
472+
if pipenv:
473+
for pipfile in ['Pipfile', 'Pipfile.lock']:
474+
if os.path.exists(pipfile):
475+
params['extraFiles'].append(pipfile)
476+
uses_python_ver = ''
477+
if python_ver.startswith('3'):
478+
uses_python_ver = '--three '
479+
elif python_ver.startswith('2'):
480+
uses_python_ver = '--two '
481+
params['command'] = 'pipenv ' + uses_python_ver + 'run ' + params['command']
482+
455483
req = params.pop('req', None)
456484
if req:
457485
if not isinstance(req, str):
458486
req = 'requirements.txt'
459487
if os.path.exists(req):
460488
params['extraFiles'].append(req)
461489
params['command'] = 'pip' + python_ver + ' install -r ' + os.path.basename(req) + '\n' + params['command']
490+
if pipenv:
491+
params['command'] = 'pipenv ' + uses_python_ver + 'run ' + params['command']
462492

463-
pipenv = params.pop('pipenv', None)
464493
if pipenv:
465-
for pipfile in ['Pipfile', 'Pipfile.lock']:
466-
if os.path.exists(pipfile):
467-
params['extraFiles'].append(pipfile)
468-
uses_python_ver = ''
469-
if python_ver.startswith('3'):
470-
uses_python_ver == '--three '
471-
elif python_ver.startswith('2'):
472-
uses_python_ver == '--two '
473494
params['command'] = 'pipenv ' + uses_python_ver + 'install\n' + params['command']
474495

475496
conda = params.pop('conda', None)

0 commit comments

Comments
 (0)