aboutsummaryrefslogtreecommitdiff
path: root/cros_utils
diff options
context:
space:
mode:
Diffstat (limited to 'cros_utils')
-rwxr-xr-x[-rw-r--r--]cros_utils/command_executer.py39
-rwxr-xr-xcros_utils/email_sender.py26
-rw-r--r--cros_utils/file_utils.py5
-rw-r--r--cros_utils/locks.py2
-rw-r--r--cros_utils/misc.py6
-rw-r--r--cros_utils/tabulator.py4
6 files changed, 48 insertions, 34 deletions
diff --git a/cros_utils/command_executer.py b/cros_utils/command_executer.py
index c4d3fded..39bff5ed 100644..100755
--- a/cros_utils/command_executer.py
+++ b/cros_utils/command_executer.py
@@ -1,3 +1,4 @@
+#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2011 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
@@ -17,11 +18,11 @@ import sys
import tempfile
import time
-import logger
-import misc
+from cros_utils import logger
mock_default = False
+CHROMEOS_SCRIPTS_DIR = '/mnt/host/source/src/scripts'
LOG_LEVEL = ('none', 'quiet', 'average', 'verbose')
@@ -101,6 +102,7 @@ class CommandExecuter(object):
# In this way the child cannot mess the parent's terminal.
p = None
try:
+ # pylint: disable=bad-option-value, subprocess-popen-preexec-fn
p = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
@@ -238,11 +240,14 @@ class CommandExecuter(object):
return command
def WriteToTempShFile(self, contents):
- handle, command_file = tempfile.mkstemp(prefix=os.uname()[1], suffix='.sh')
- os.write(handle, '#!/bin/bash\n')
- os.write(handle, contents)
- os.close(handle)
- return command_file
+ # TODO(crbug.com/1048938): use encoding='utf-8' when all dependencies have
+ # migrated to python 3.
+ with tempfile.NamedTemporaryFile(
+ 'w', delete=False, prefix=os.uname()[1], suffix='.sh') as f:
+ f.write('#!/bin/bash\n')
+ f.write(contents)
+ f.flush()
+ return f.name
def CrosLearnBoard(self, chromeos_root, machine):
command = self.RemoteAccessInitCommand(chromeos_root, machine)
@@ -362,15 +367,20 @@ class CommandExecuter(object):
if self.logger:
self.logger.LogCmd(command, print_to_console=print_to_console)
- handle, command_file = tempfile.mkstemp(
+ # TODO(crbug.com/1048938): use encoding='utf-8' when all dependencies have
+ # migrated to python 3.
+ with tempfile.NamedTemporaryFile(
+ 'w',
+ delete=False,
dir=os.path.join(chromeos_root, 'src/scripts'),
suffix='.sh',
- prefix='in_chroot_cmd')
- os.write(handle, '#!/bin/bash\n')
- os.write(handle, command)
- os.write(handle, '\n')
- os.close(handle)
+ prefix='in_chroot_cmd') as f:
+ f.write('#!/bin/bash\n')
+ f.write(command)
+ f.write('\n')
+ f.flush()
+ command_file = f.name
os.chmod(command_file, 0o777)
# if return_output is set, run a dummy command first to make sure that
@@ -386,7 +396,7 @@ class CommandExecuter(object):
# Run command_file inside the chroot, making sure that any "~" is expanded
# by the shell inside the chroot, not outside.
command = ("cd %s; cros_sdk %s -- bash -c '%s/%s'" %
- (chromeos_root, cros_sdk_options, misc.CHROMEOS_SCRIPTS_DIR,
+ (chromeos_root, cros_sdk_options, CHROMEOS_SCRIPTS_DIR,
os.path.basename(command_file)))
ret = self.RunCommandGeneric(
command,
@@ -601,6 +611,7 @@ class CommandExecuter(object):
# In this way the child cannot mess the parent's terminal.
pobject = None
try:
+ # pylint: disable=bad-option-value, subprocess-popen-preexec-fn
pobject = subprocess.Popen(
cmd,
cwd=cwd,
diff --git a/cros_utils/email_sender.py b/cros_utils/email_sender.py
index 3f724f64..0019982e 100755
--- a/cros_utils/email_sender.py
+++ b/cros_utils/email_sender.py
@@ -97,12 +97,14 @@ class EmailSender(object):
if not text_to_send:
text_to_send = 'Empty message body.'
- body_fd, body_filename = tempfile.mkstemp()
- to_be_deleted = [body_filename]
+ to_be_deleted = []
try:
- os.write(body_fd, text_to_send)
- os.close(body_fd)
+ with tempfile.NamedTemporaryFile(
+ 'w', encoding='utf-8', delete=False) as f:
+ f.write(text_to_send)
+ f.flush()
+ to_be_deleted.append(f.name)
# Fix single-quotes inside the subject. In bash, to escape a single quote
# (e.g 'don't') you need to replace it with '\'' (e.g. 'don'\''t'). To
@@ -113,11 +115,10 @@ class EmailSender(object):
if msg_type == 'html':
command = ("sendgmr --to='%s' --from='%s' --subject='%s' "
"--html_file='%s' --body_file=/dev/null" %
- (to_list, email_from, subject, body_filename))
+ (to_list, email_from, subject, f.name))
else:
- command = (
- "sendgmr --to='%s' --from='%s' --subject='%s' "
- "--body_file='%s'" % (to_list, email_from, subject, body_filename))
+ command = ("sendgmr --to='%s' --from='%s' --subject='%s' "
+ "--body_file='%s'" % (to_list, email_from, subject, f.name))
if email_cc:
cc_list = ','.join(email_cc)
@@ -133,10 +134,11 @@ class EmailSender(object):
report_suffix = '_report.html'
else:
report_suffix = '_report.txt'
- fd, fname = tempfile.mkstemp(suffix=report_suffix)
- os.write(fd, attachment.content)
- os.close(fd)
- attachment_files.append(fname)
+ with tempfile.NamedTemporaryFile(
+ 'w', encoding='utf-8', delete=False, suffix=report_suffix) as f:
+ f.write(attachment.content)
+ f.flush()
+ attachment_files.append(f.name)
files = ','.join(attachment_files)
command += " --attachment_files='%s'" % files
to_be_deleted += attachment_files
diff --git a/cros_utils/file_utils.py b/cros_utils/file_utils.py
index fe6fc16f..f0e4064c 100644
--- a/cros_utils/file_utils.py
+++ b/cros_utils/file_utils.py
@@ -10,7 +10,8 @@ from __future__ import print_function
import errno
import os
import shutil
-import command_executer
+
+from cros_utils import command_executer
class FileUtils(object):
@@ -65,7 +66,7 @@ class FileUtils(object):
shutil.rmtree(path, ignore_errors=True)
def WriteFile(self, path, contents):
- with open(path, 'wb') as f:
+ with open(path, 'w', encoding='utf-8') as f:
f.write(contents)
diff --git a/cros_utils/locks.py b/cros_utils/locks.py
index 4ecbe0a9..848e23fc 100644
--- a/cros_utils/locks.py
+++ b/cros_utils/locks.py
@@ -12,7 +12,7 @@ import time
import lock_machine
-import logger
+from cros_utils import logger
def AcquireLock(machines, chromeos_root, timeout=1200):
diff --git a/cros_utils/misc.py b/cros_utils/misc.py
index 48e3c724..246767f0 100644
--- a/cros_utils/misc.py
+++ b/cros_utils/misc.py
@@ -17,10 +17,10 @@ import shutil
import sys
import traceback
-import command_executer
-import logger
+from cros_utils import command_executer
+from cros_utils import logger
-CHROMEOS_SCRIPTS_DIR = '~/trunk/src/scripts'
+CHROMEOS_SCRIPTS_DIR = '/mnt/host/source/src/scripts'
TOOLCHAIN_UTILS_PATH = ('/mnt/host/source/src/third_party/toolchain-utils/'
'cros_utils/toolchain_utils.sh')
diff --git a/cros_utils/tabulator.py b/cros_utils/tabulator.py
index 9527fde0..300c2d79 100644
--- a/cros_utils/tabulator.py
+++ b/cros_utils/tabulator.py
@@ -72,8 +72,8 @@ import sys
import numpy
import scipy
-from email_sender import EmailSender
-import misc
+from cros_utils.email_sender import EmailSender
+from cros_utils import misc
def _AllFloat(values):