1bd8cbf6dbe1abb1c790d8b62ff83b1f0036bbb9
7 _logger
= logging
.getLogger("Runtime")
10 class Runtime(object):
11 def __init__(self
, runtime_dir
):
13 A dictionary of popen object eg. lttng-sessiond, relayd,
14 anything really. Key is a uuid.
16 self
.__subprocess
= {}
17 self
.__stdout
_stderr
= {}
20 self
.__runtime
_log
= os
.path
.join(runtime_dir
, "log")
21 self
.__runtime
_log
_sub
= os
.path
.join(self
.__runtime
_log
, "subprocess")
23 self
._runtime
_log
_aggregation
= os
.path
.join(self
.__runtime
_log
, "runtime.log")
25 self
._run
_command
_count
= 0
27 self
.lttng_home
= os
.path
.join(runtime_dir
, "lttng_home")
29 # TODO move exist_ok to false !!!! ONLY for testing
30 os
.makedirs(self
.__runtime
_log
, exist_ok
=True)
31 os
.makedirs(self
.__runtime
_log
_sub
, exist_ok
=True)
32 os
.makedirs(self
.lttng_home
, exist_ok
=True)
34 def add_project(self
, project
):
35 self
.__projects
.append(project
)
37 def subprocess_signal(self
, subprocess_uuid
, signal
):
38 self
.__subproces
[subprocess_uuid
].send_signal(signal
)
40 def subprocess_terminate(self
, subprocess_uuid
, timeout
=60):
41 process
= self
.__subprocess
[subprocess_uuid
]
44 stdout
, stderr
= self
.__stdout
_stderr
[subprocess_uuid
]
48 def subprocess_kill(self
, subprocess_uuid
):
49 process
= self
.__subprocess
[subprocess_uuid
]
52 stdout
, stderr
= self
.__stdout
_stderr
[subprocess_uuid
]
56 def get_subprocess_stdout_path(self
, subprocess_uuid
):
57 stdout
, stderr
= self
.__stdout
_stderr
[subprocess_uuid
]
60 def get_subprocess_stderr_path(self
, subprocess_uuid
):
61 stdout
, stderr
= self
.__stdout
_stderr
[subprocess_uuid
]
64 def spawn_subprocess(self
, command_line
):
65 args
= shlex
.split(command_line
)
69 out_path
= os
.path
.join(self
.__runtime
_log
_sub
, str(tmp_id
) + ".out")
70 err_path
= os
.path
.join(self
.__runtime
_log
_sub
, str(tmp_id
) + ".err")
71 stdout
= open(out_path
, "w")
72 stderr
= open(err_path
, "w")
74 p
= subprocess
.Popen(args
, stdout
=stdout
, stderr
=stderr
, env
=env
)
75 self
.__subprocess
[tmp_id
] = p
76 self
.__stdout
_stderr
[tmp_id
] = (stdout
, stderr
)
77 _logger
.debug("Spawned sub pid: {} args: {} stdout: {} stderr{}".format(p
.pid
, p
.args
, out_path
, err_path
))
79 def run(self
, command_line
):
81 Run the command and return a tuple of a (CompletedProcess, stdout_path,
82 stderr_path). The subprocess is already executed and returned. The
83 callecaller is responsible for checking for errors.
85 args
= shlex
.split(command_line
)
88 tmp_id
= self
._run
_command
_count
89 self
._run
_command
_count
+= 1
91 out_path
= os
.path
.join(self
.__runtime
_log
, str(tmp_id
) + ".out")
92 err_path
= os
.path
.join(self
.__runtime
_log
, str(tmp_id
) + ".err")
93 stdout
= open(out_path
, "w")
94 stderr
= open(err_path
, "w")
96 stdout
.write("Output for command #{} {}\n".format(tmp_id
, command_line
))
97 stdout
.write("Start >>>>>>>>>>>>>>>>\n")
100 stderr
.write("Output for command #{} {}\n".format(tmp_id
, command_line
))
101 stderr
.write("Start >>>>>>>>>>>>>>>>\n")
104 cp
= subprocess
.run(args
, stdout
=stdout
, stderr
=stderr
, env
=env
)
105 _logger
.debug("Command #{} args: {} stdout: {} stderr{}".format(tmp_id
, cp
.args
, out_path
, err_path
))
107 stdout
.write("End <<<<<<<<<<<<<<<<\n")
110 stderr
.write("End <<<<<<<<<<<<<<<<\n")
113 # Add to the global log file. This can help a little. Leave the other
114 # file available for per-run analysis
115 with
open(self
._runtime
_log
_aggregation
, "a") as log
:
116 with
open(out_path
, "r") as out
:
117 log
.write(out
.read())
118 with
open(err_path
, "r") as out
:
119 log
.write(out
.read())
121 return (cp
, out_path
, err_path
)
123 def get_cppflags(self
):
125 for project
in self
.__projects
:
126 cppflags
.append(project
.get_cppflags())
127 return " ".join(cppflags
)
129 def get_ldflags(self
):
131 for project
in self
.__projects
:
132 ldflags
.append(project
.get_ldflags())
133 return " ".join(ldflags
)
135 def get_ld_library_path(self
):
137 for project
in self
.__projects
:
138 library_path
.append(project
.get_ld_library_path())
139 return " ".join(library_path
)
141 def get_bin_path(self
):
143 for project
in self
.__projects
:
144 path
.append(project
.get_bin_path())
145 return ":".join(path
)
148 env
= os
.environ
.copy()
150 env
["LTTNG_HOME"] = self
.lttng_home
152 env_fetch
= {"CPPFLAGS": (self
.get_cppflags(), " "),
153 "LDFLAGS": (self
.get_ldflags(), " "),
154 "LD_LIRABRY_PATH": (self
.get_ld_library_path(), ":"),
155 "PATH": (self
.get_bin_path(), ":"),
157 for key
, (value
, delimiter
) in env_fetch
.items():
161 env
[key
] = delimiter
.join([value
, tmp_var
])
163 for project
in self
.__projects
:
164 for var
, value
in project
.special_env_variables
.items():
166 # Raise for now since no special cases is known
167 _logger
.warning("% Special var % is already defined",
169 raise Exception("Multiple definition of a special environment variable")
175 for key
, subp
in self
.__subprocess
.items():
177 for key
, subp
in self
.__subprocess
.items():
179 # TODO move timeout to settings
180 subp
.wait(timeout
=60)
181 except subprocess
.TimeoutExpired
as e
:
185 for key
, (stdout
, stderr
) in self
.__stdout
_stderr
.items():
This page took 0.038319 seconds and 5 git commands to generate.