@@ -375,6 +375,16 @@ def run(params={}, no_logging=False):
375
375
params = {}
376
376
params ['script' ] = script
377
377
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
+
378
388
params = params .copy ()
379
389
run_this = False
380
390
if 'script' not in params :
@@ -404,8 +414,12 @@ def run(params={}, no_logging=False):
404
414
with open (src_path , "w" ) as file :
405
415
file .write (src )
406
416
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
409
423
410
424
if 'project' not in params :
411
425
params ['project' ] = 'paperspace-python'
@@ -417,59 +431,66 @@ def run(params={}, no_logging=False):
417
431
python_ver = params .pop ('python' , str (sys .version_info [0 ])) # defaults locally running version
418
432
# TODO validate python version; handle no version, specific version
419
433
420
- run_as_module_opt = ''
421
- if params .pop ('module' , None ):
422
- run_as_module_opt = '-m '
423
-
424
434
script_args = params .pop ('script_args' , None )
425
435
args = ''
426
436
if script_args :
427
437
args = ' ' + ' ' .join (script_args )
428
438
429
439
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
444
441
445
442
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 )
448
460
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 )
450
466
451
467
if 'ignoreFiles' in params :
452
468
if isinstance (params ['ignoreFiles' ], str ):
453
469
params ['ignoreFiles' ] = params ['ignoreFiles' ].split (',' )
454
470
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
+
455
483
req = params .pop ('req' , None )
456
484
if req :
457
485
if not isinstance (req , str ):
458
486
req = 'requirements.txt'
459
487
if os .path .exists (req ):
460
488
params ['extraFiles' ].append (req )
461
489
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' ]
462
492
463
- pipenv = params .pop ('pipenv' , None )
464
493
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 '
473
494
params ['command' ] = 'pipenv ' + uses_python_ver + 'install\n ' + params ['command' ]
474
495
475
496
conda = params .pop ('conda' , None )
0 commit comments