-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtask.py
154 lines (121 loc) · 5.19 KB
/
runtask.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# ESA (C) 2000-2021
#
# This file is part of ESA's XMM-Newton Scientific Analysis System (SAS).
#
# SAS is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# SAS is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with SAS. If not, see <http://www.gnu.org/licenses/>.
#
""""runtask.py
It will run a Python task which has been coded into a module
named taskname.py.
It is assumed that taskname.py is part of package taskname which
will be placed below pysas.
iparsdic is a dictionary which includes all task parameters (as read
from par file) and their defaults except those modified in the
command line in the form param=value pairs.
iparsdic is passed to the task module, which will be taken in its
run function defined there.
"""
# Standard library imports
from importlib import import_module
import pkgutil, os, sys, subprocess
# Third party imports
# Local application imports
class RunTask:
"""Class RunTask
Parameters are entered in dictionary iparsdic.
Method run executes the task depending on whether
is a SAS native task or a Python task.
"""
def __init__(self, taskname, iparsdic,logFile,stdout_to_console=True):
self.taskname = taskname
self.iparsdic = iparsdic
self.logFile = logFile
self.stdout_to_console = stdout_to_console
def run(self):
""""Method run
If taskname is a Python module, therefore it is in the
list pysaspkgs, then import it and pass to its run
function the dictionary of parameters, iparsdic
If taskname is not a Python SAS task, there will not be
a run function, so we will invoke subprocess
"""
sas_path = os.environ.get('SAS_PATH')
if not sas_path:
raise Exception('SAS_PATH is undefined! SAS not initialised?')
saspath = sas_path.split(':')
sasdev = saspath[0]
pysasdevdir = os.path.join(sasdev, 'lib', 'python', 'pysas')
pysaspkgs = []
for p in pkgutil.walk_packages([pysasdevdir]):
if p.ispkg:
pysaspkgs.append(p.name)
if self.taskname in pysaspkgs:
m = import_module('pysas.' + self.taskname + '.' + self.taskname)
m.run(self.iparsdic)
else:
cmd = ''
cmd += self.taskname + ' '
for parval in self.iparsdic.items():
k, v = parval
#Remove single quotes a double quotes from the python input parameters
#SOC-SPR-7684
if v.startswith("\"") or v.endswith("\""):
v = v.replace('"','')
if v.startswith("'") or v.endswith("'"):
v = v.replace('\'','')
if ' ' or '|' in v:
cmd += k + '=' + "'"
vc = v.split(' ')
for i in range(len(vc)):
if i == len(vc) - 1:
cmd += vc[i]
else:
cmd += vc[i] + ' '
cmd += "' "
else:
cmd += k + '=' + v + ' '
print(f'Executing: \n{cmd}')
self.p= None
self.stdoutFile = None
if self.logFile == 'DEFAULT':
with subprocess.Popen(cmd,
bufsize=1,
shell=True,
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True) as p:
for line in p.stdout:
if self.stdout_to_console:
print(line, end='')
else:
self.stdoutFile=open(self.logFile,'w')
p = subprocess.Popen(cmd,
bufsize=1,
shell=True,
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True)
for line in p.stdout:
if self.stdout_to_console:
sys.stdout.write(line)
self.stdoutFile.write(line)
p.wait()
if p.returncode != 0:
if self.logFile != 'DEFAULT':
self.stdoutFile.close()
raise subprocess.CalledProcessError(p.returncode, p.args)
if self.logFile != 'DEFAULT':
self.stdoutFile.close()