aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorge Burgess IV <gbiv@google.com>2022-02-24 15:34:58 -0800
committerCommit Bot <commit-bot@chromium.org>2022-02-25 01:39:23 +0000
commitf07561c3e22d1199f44519d614eb2241efb0be3c (patch)
tree396c10e3aa653e57e47ff69a06eb0b755abf67b7
parentb968618aeec7a7c1bc1e4a6e045226e84f97152d (diff)
downloadtoolchain-utils-f07561c3e22d1199f44519d614eb2241efb0be3c.tar.gz
command_executer: reformat
`yapf` makes a lot of changes here. Split that out into another (nop) CL. BUG=b:221302420 TEST=Unittests Change-Id: Ieb1f839d6619d7d162ae6a37bff0be77bc0b91a8 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/3489848 Tested-by: George Burgess <gbiv@chromium.org> Auto-Submit: George Burgess <gbiv@chromium.org> Reviewed-by: Manoj Gupta <manojgupta@chromium.org> Commit-Queue: Manoj Gupta <manojgupta@chromium.org>
-rwxr-xr-xcros_utils/command_executer.py136
1 files changed, 67 insertions, 69 deletions
diff --git a/cros_utils/command_executer.py b/cros_utils/command_executer.py
index aeedf3ea..0bf89d4d 100755
--- a/cros_utils/command_executer.py
+++ b/cros_utils/command_executer.py
@@ -103,14 +103,13 @@ class CommandExecuter(object):
p = None
try:
# pylint: disable=bad-option-value, subprocess-popen-preexec-fn
- p = subprocess.Popen(
- cmd,
- stdout=subprocess.PIPE,
- stderr=subprocess.PIPE,
- shell=True,
- preexec_fn=os.setsid,
- executable='/bin/bash',
- env=env)
+ p = subprocess.Popen(cmd,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE,
+ shell=True,
+ preexec_fn=os.setsid,
+ executable='/bin/bash',
+ env=env)
full_stdout = ''
full_stderr = ''
@@ -159,16 +158,17 @@ class CommandExecuter(object):
if p.poll() is not None:
if terminated_time is None:
terminated_time = time.time()
- elif (terminated_timeout is not None and
- time.time() - terminated_time > terminated_timeout):
+ elif (terminated_timeout is not None
+ and time.time() - terminated_time > terminated_timeout):
if self.logger:
self.logger.LogWarning(
'Timeout of %s seconds reached since '
- 'process termination.' % terminated_timeout, print_to_console)
+ 'process termination.' % terminated_timeout,
+ print_to_console)
break
- if (command_timeout is not None and
- time.time() - started_time > command_timeout):
+ if (command_timeout is not None
+ and time.time() - started_time > command_timeout):
os.killpg(os.getpgid(p.pid), signal.SIGTERM)
if self.logger:
self.logger.LogWarning(
@@ -242,9 +242,11 @@ class CommandExecuter(object):
return command
def WriteToTempShFile(self, contents):
- with tempfile.NamedTemporaryFile(
- 'w', encoding='utf-8', delete=False, prefix=os.uname()[1],
- suffix='.sh') as f:
+ with tempfile.NamedTemporaryFile('w',
+ encoding='utf-8',
+ delete=False,
+ prefix=os.uname()[1],
+ suffix='.sh') as f:
f.write('#!/bin/bash\n')
f.write(contents)
f.flush()
@@ -292,16 +294,15 @@ class CommandExecuter(object):
machine, port = machine.split(':')
# Write all commands to a file.
command_file = self.WriteToTempShFile(cmd)
- retval = self.CopyFiles(
- command_file,
- command_file,
- dest_machine=machine,
- dest_port=port,
- command_terminator=command_terminator,
- chromeos_root=chromeos_root,
- dest_cros=True,
- recursive=False,
- print_to_console=print_to_console)
+ retval = self.CopyFiles(command_file,
+ command_file,
+ dest_machine=machine,
+ dest_port=port,
+ command_terminator=command_terminator,
+ chromeos_root=chromeos_root,
+ dest_cros=True,
+ recursive=False,
+ print_to_console=print_to_console)
if retval:
if self.logger:
self.logger.LogError('Could not run remote command on machine.'
@@ -311,13 +312,12 @@ class CommandExecuter(object):
command = self.RemoteAccessInitCommand(chromeos_root, machine, port)
command += '\nremote_sh bash %s' % command_file
command += '\nl_retval=$?; echo "$REMOTE_OUT"; exit $l_retval'
- retval = self.RunCommandGeneric(
- command,
- return_output,
- command_terminator=command_terminator,
- command_timeout=command_timeout,
- terminated_timeout=terminated_timeout,
- print_to_console=print_to_console)
+ retval = self.RunCommandGeneric(command,
+ return_output,
+ command_terminator=command_terminator,
+ command_timeout=command_timeout,
+ terminated_timeout=terminated_timeout,
+ print_to_console=print_to_console)
if return_output:
connect_signature = ('Initiating first contact with remote host\n' +
'Connection OK\n')
@@ -372,13 +372,13 @@ class CommandExecuter(object):
if self.logger:
self.logger.LogCmd(command, print_to_console=print_to_console)
- with tempfile.NamedTemporaryFile(
- 'w',
- encoding='utf-8',
- delete=False,
- dir=os.path.join(chromeos_root, 'src/scripts'),
- suffix='.sh',
- prefix='in_chroot_cmd') as f:
+ with tempfile.NamedTemporaryFile('w',
+ encoding='utf-8',
+ delete=False,
+ dir=os.path.join(chromeos_root,
+ 'src/scripts'),
+ suffix='.sh',
+ prefix='in_chroot_cmd') as f:
f.write('#!/bin/bash\n')
f.write(command)
f.write('\n')
@@ -402,14 +402,13 @@ class CommandExecuter(object):
command = ("cd %s; cros_sdk %s -- bash -c '%s/%s'" %
(chromeos_root, cros_sdk_options, CHROMEOS_SCRIPTS_DIR,
os.path.basename(command_file)))
- ret = self.RunCommandGeneric(
- command,
- return_output,
- command_terminator=command_terminator,
- command_timeout=command_timeout,
- terminated_timeout=terminated_timeout,
- print_to_console=print_to_console,
- env=env)
+ ret = self.RunCommandGeneric(command,
+ return_output,
+ command_terminator=command_terminator,
+ command_timeout=command_timeout,
+ terminated_timeout=terminated_timeout,
+ print_to_console=print_to_console,
+ env=env)
os.remove(command_file)
return ret
@@ -445,11 +444,10 @@ class CommandExecuter(object):
username=None,
command_terminator=None):
cmd = ' ;\n'.join(cmdlist)
- return self.RunCommand(
- cmd,
- machine=machine,
- username=username,
- command_terminator=command_terminator)
+ return self.RunCommand(cmd,
+ machine=machine,
+ username=username,
+ command_terminator=command_terminator)
def CopyFiles(self,
src,
@@ -505,12 +503,11 @@ class CommandExecuter(object):
else:
command += rsync_prefix + 'root@%s:%s %s' % (cros_machine, src, dest)
- return self.RunCommand(
- command,
- machine=host_machine,
- username=host_user,
- command_terminator=command_terminator,
- print_to_console=print_to_console)
+ return self.RunCommand(command,
+ machine=host_machine,
+ username=host_user,
+ command_terminator=command_terminator,
+ print_to_console=print_to_console)
if dest_machine == src_machine:
command = 'rsync -a %s %s' % (src, dest)
@@ -519,12 +516,11 @@ class CommandExecuter(object):
src_machine = os.uname()[1]
src_user = getpass.getuser()
command = 'rsync -a %s@%s:%s %s' % (src_user, src_machine, src, dest)
- return self.RunCommand(
- command,
- machine=dest_machine,
- username=dest_user,
- command_terminator=command_terminator,
- print_to_console=print_to_console)
+ return self.RunCommand(command,
+ machine=dest_machine,
+ username=dest_user,
+ command_terminator=command_terminator,
+ print_to_console=print_to_console)
def RunCommand2(self,
cmd,
@@ -593,8 +589,9 @@ class CommandExecuter(object):
def notify_line(self):
p = self._buf.find('\n')
while p >= 0:
- self._line_consumer(
- line=self._buf[:p + 1], output=self._name, pobject=self._pobject)
+ self._line_consumer(line=self._buf[:p + 1],
+ output=self._name,
+ pobject=self._pobject)
if p < len(self._buf) - 1:
self._buf = self._buf[p + 1:]
p = self._buf.find('\n')
@@ -606,8 +603,9 @@ class CommandExecuter(object):
def notify_eos(self):
# Notify end of stream. The last line may not end with a '\n'.
if self._buf != '':
- self._line_consumer(
- line=self._buf, output=self._name, pobject=self._pobject)
+ self._line_consumer(line=self._buf,
+ output=self._name,
+ pobject=self._pobject)
self._buf = ''
if self.log_level == 'verbose':