-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_python_script_launcher.py
More file actions
67 lines (50 loc) · 2.29 KB
/
example_python_script_launcher.py
File metadata and controls
67 lines (50 loc) · 2.29 KB
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
64
65
66
67
import os
import subprocess
# path to your python script
python_script_path = r"C:\code\CWMS-data-acquisition-python\src\get_USGS_measurements\get_USGS_measurements.py"
# any arguments you may need, set to None or '' if not needed
args = "-d 60"
##################################################################################################
# 1. Get the expanded pythonCWMS directory from the environment variable
pythoncwms_home = os.environ.get('PYTHON_CWMS_HOME') # Get the value of PYTHON_CWMS_HOME
if not pythoncwms_home:
print("Error: PYTHON_CWMS_HOME environment variable not set.")
exit() # Or handle the error appropriately
# 2. Construct the pythonCWMS directory path using os.path.join
pythoncwms_path = os.path.join(pythoncwms_home, "python.exe") # Path to the executable
# 3. Check if the pythonCWMS_dir is in the PATH
current_path = os.environ.get('PATH', '')
if pythoncwms_home not in current_path:
os.environ['PATH'] = pythoncwms_home + os.pathsep + current_path
print("Updated PATH:", os.environ['PATH'])
else:
print("pythonCWMS directory already in PATH.")
# 4. Test using os.system()
print("\n--- Testing pythonCWMS directly with os.system() ---")
return_code_os_system = os.system("pythonCWMS --version")
print("os.system return code:", return_code_os_system)
if return_code_os_system == 0:
print("os.system succeeded. pythonCWMS is accessible.")
else:
print("os.system failed. pythonCWMS is NOT accessible. Check the PATH.")
# 5. Subprocess Call (only if the above test succeeds)
if return_code_os_system == 0:
python_executable = pythoncwms_path # Use the constructed path
cmd = [python_executable, python_script_path]
if not args or args != '':
cmd.append(args)
print("Executing command:", cmd)
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
stdout_str = stdout.decode('utf-8')
stderr_str = stderr.decode('utf-8')
stdout_str = stdout_str.replace('\r\n', '\n')
stderr_str = stderr_str.replace('\r\n', '\n')
print("STDOUT:\n")
print(stdout_str)
print("STDERR:\n")
print(stderr_str)
return_code = process.returncode
print("Return Code:", return_code)
else:
print("\nSkipping subprocess call because os.system failed.")