diff options
author | Jordan R Abrahams-Whitehead <ajordanr@google.com> | 2022-03-04 01:22:45 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2022-03-04 01:22:45 +0000 |
commit | 882a18888febb9cb0b9d6c6069498cbc4aa30f88 (patch) | |
tree | b0522edde1d3c5356c95eb1ee2eae3e87befa1f3 /cros_utils | |
parent | 9d023268dcf468f9d4bbd874545c9c230e7549b3 (diff) | |
parent | 178e686da38f18bbc35b0f1903c93effa94bc6b4 (diff) | |
download | toolchain-utils-882a18888febb9cb0b9d6c6069498cbc4aa30f88.tar.gz |
Merging 25 commit(s) from Chromium's toolchain-utils am: 9090b1a17b am: c9cb1157f1 am: 8a12172a24 am: 178e686da3aml_uwb_331910010aml_uwb_331820070aml_uwb_331613010aml_uwb_331611010aml_uwb_331410010aml_uwb_331310030aml_uwb_331115000aml_uwb_331015040aml_uwb_330810010aml_tz4_332714070aml_tz4_332714050aml_tz4_332714010aml_tz4_331910000aml_tz4_331314030aml_tz4_331314020aml_tz4_331314010aml_tz4_331012050aml_tz4_331012040aml_tz4_331012000aml_ase_331311020aml_ase_331112000aml_ase_331011020android13-mainline-uwb-releaseandroid13-mainline-tzdata4-releaseandroid13-mainline-appsearch-releaseaml_tz4_332714010
Original change: https://android-review.googlesource.com/c/platform/external/toolchain-utils/+/2006491
Change-Id: I9dc6817d5526f5565c964f386d415954804de104
Diffstat (limited to 'cros_utils')
-rwxr-xr-x | cros_utils/command_executer.py | 142 |
1 files changed, 72 insertions, 70 deletions
diff --git a/cros_utils/command_executer.py b/cros_utils/command_executer.py index aeedf3ea..cc0f3372 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') @@ -393,7 +393,11 @@ class CommandExecuter(object): if return_output: ret = self.RunCommand( 'cd %s; cros_sdk %s -- true' % (chromeos_root, cros_sdk_options), - env=env) + env=env, + # Give this command a long time to execute; it might involve setting + # the chroot up, or running fstrim on its image file. Both of these + # operations can take well over the timeout default of 10 seconds. + terminated_timeout=5 * 60) if ret: return (ret, '', '') @@ -402,14 +406,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 +448,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 +507,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 +520,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 +593,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 +607,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': |