diff options
Diffstat (limited to 'cros_utils/command_executer.py')
-rwxr-xr-x | cros_utils/command_executer.py | 142 |
1 files changed, 70 insertions, 72 deletions
diff --git a/cros_utils/command_executer.py b/cros_utils/command_executer.py index cc0f3372..aeedf3ea 100755 --- a/cros_utils/command_executer.py +++ b/cros_utils/command_executer.py @@ -103,13 +103,14 @@ 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 = '' @@ -158,17 +159,16 @@ 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,11 +242,9 @@ 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() @@ -294,15 +292,16 @@ 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.' @@ -312,12 +311,13 @@ 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,11 +393,7 @@ class CommandExecuter(object): if return_output: ret = self.RunCommand( 'cd %s; cros_sdk %s -- true' % (chromeos_root, cros_sdk_options), - 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) + env=env) if ret: return (ret, '', '') @@ -406,13 +402,14 @@ 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 @@ -448,10 +445,11 @@ 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, @@ -507,11 +505,12 @@ 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) @@ -520,11 +519,12 @@ 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,9 +593,8 @@ 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') @@ -607,9 +606,8 @@ 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': |