forked from oracle-samples/oci-data-science-ai-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.py
63 lines (51 loc) · 1.79 KB
/
runner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import os
import sys
import subprocess
import importlib.util
import argparse
from pathlib import Path
from subprocess import PIPE, Popen
from datetime import datetime
print(f'Job code runner {datetime.now().strftime("%m-%d-%Y-%H:%M:%S")} ...')
# set -f as command line argument on the job/job run to change which file should be ran
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--file", required=False, default="get-cpu-info.py")
parser.add_argument("positional_args", nargs="*")
args = parser.parse_args()
print(f"File to execute set to: {args.file}")
# get current code path
current_path = Path(sys.path[0])
print (f'current path: {current_path}')
def install(package):
# subprocess.run(['pip', 'install', package])
# ... or
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
# verify if the library exist, if not install it
spec = importlib.util.find_spec("cpuinfo")
if spec is None:
print("package py-cpuinfo does not exist, installing...")
install("py-cpuinfo")
else:
print("py-cpuinfo exist")
# which file to run
FILE_NAME = f'{current_path}/{args.file}'
print(f"Full path: {FILE_NAME}")
# Pass the command line arguments to the code, if any
# Notice environment variables set on the job/job run will be also avaiable in the subprocess
cmd = [sys.executable, FILE_NAME]
print(f"Positional arguments: {args.positional_args}")
# args = sys.argv[1:]
# _args = args.positional_args[1:]
if args.positional_args:
cmd += args.positional_args
# Run the python script
print('\n')
print(f"Running command: {cmd}")
process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
# Steam the outputs
while True:
output = process.stdout.readline()
if process.poll() is not None and output == b"":
break
if output:
print(output.decode().strip())