|
@@ -1,21 +1,18 @@
|
|
|
+import os
|
|
|
from robot.api.deco import library, keyword
|
|
|
from robot.libraries.BuiltIn import BuiltIn
|
|
|
+from robot.libraries.Process import Process
|
|
|
|
|
|
@library
|
|
|
class Commander:
|
|
|
|
|
|
@property
|
|
|
- def OperatingSystem(self):
|
|
|
- self._operatingsystem = self._operatingsystem or self.BuiltIn.get_library_instance('OperatingSystem')
|
|
|
- return self._operatingsystem
|
|
|
-
|
|
|
- @property
|
|
|
- def Process(self):
|
|
|
+ def Process(self) -> Process:
|
|
|
self._process = self._process or self.BuiltIn.get_library_instance('Process')
|
|
|
return self._process
|
|
|
|
|
|
@property
|
|
|
- def BuiltIn(self):
|
|
|
+ def BuiltIn(self) -> BuiltIn:
|
|
|
self._builtin = self._builtin or BuiltIn()
|
|
|
return self._builtin
|
|
|
|
|
@@ -25,24 +22,32 @@ class Commander:
|
|
|
self._operatingsystem = None
|
|
|
|
|
|
@keyword
|
|
|
- def execute_pipeline(self, *commands, stdout=None, append=False):
|
|
|
+ def execute_pipeline(self, *commands, cwd=None, stdout=None, append=False):
|
|
|
procs = []
|
|
|
append = (stdout and append) or False
|
|
|
- if append:
|
|
|
- self.OperatingSystem.append_to_file(stdout, '')
|
|
|
- self.OperatingSystem.move_file(stdout, f'{stdout}.old')
|
|
|
-
|
|
|
- procs.append(self.Process.start_process(*commands[0]))
|
|
|
- prev = procs[-1]
|
|
|
+ procs.append(self.Process.start_process(
|
|
|
+ *commands[0], cwd=cwd,
|
|
|
+ ))
|
|
|
for command in commands[1:-1]:
|
|
|
- procs.append(self.Process.start_process(*command, stdin=prev.stdout))
|
|
|
- prev = procs[-1]
|
|
|
+ procs.append(self.Process.start_process(
|
|
|
+ *command, cwd=cwd,
|
|
|
+ stdin=procs[-1].stdout,
|
|
|
+ ))
|
|
|
procs.append(self.Process.start_process(
|
|
|
- *commands[-1], stdin=prev.stdout, stdout=f'{stdout}.new' if append else stdout))
|
|
|
- ret = self.Process.wait_for_process(procs[-1])
|
|
|
+ *commands[-1], cwd=cwd,
|
|
|
+ stdin=procs[-1].stdout,
|
|
|
+ stdout=None if append else stdout,
|
|
|
+ ))
|
|
|
if append:
|
|
|
- ret = self.Process.run_process('cat', f'{stdout}.old', f'{stdout}.new', stdout=stdout)
|
|
|
- self.OperatingSystem.remove_file(f'{stdout}.new')
|
|
|
- self.OperatingSystem.remove_file(f'{stdout}.old')
|
|
|
- return ret
|
|
|
-
|
|
|
+ # set cwd like ProcessConfiguration does
|
|
|
+ cwd = os.path.normpath(cwd) if cwd else os.path.abspath('.')
|
|
|
+ # set output filename like ProcessConfiguration does
|
|
|
+ stdout = os.path.normpath(os.path.join(cwd, stdout))
|
|
|
+ procs.append(self.Process.start_process(
|
|
|
+ 'tee', '-a', stdout,
|
|
|
+ cwd=cwd,
|
|
|
+ stdin=procs[-1].stdout,
|
|
|
+ stdout='DEVNULL',
|
|
|
+ ))
|
|
|
+
|
|
|
+ return self.Process.wait_for_process(procs[-1])
|