diff options
author | Ahmad Sharif <asharif@chromium.org> | 2012-05-07 12:02:16 -0700 |
---|---|---|
committer | Ahmad Sharif <asharif@chromium.org> | 2012-05-07 12:02:16 -0700 |
commit | fd356fb31c4340851fecc750230d05c9518b125c (patch) | |
tree | 694f1a93f568d5e46655c30bb76cb71d7400f11b /utils | |
parent | 92ab7af223c82fe8897bacea8b32028828eab018 (diff) | |
download | toolchain-utils-fd356fb31c4340851fecc750230d05c9518b125c.tar.gz |
Synced repos to: 60208
Diffstat (limited to 'utils')
-rw-r--r-- | utils/command_executer.py | 28 | ||||
-rw-r--r-- | utils/logger.py | 7 | ||||
-rw-r--r-- | utils/misc.py | 158 | ||||
-rw-r--r-- | utils/misc_test.py | 22 | ||||
-rwxr-xr-x | utils/utils.py | 106 |
5 files changed, 204 insertions, 117 deletions
diff --git a/utils/command_executer.py b/utils/command_executer.py index 32d86c91..c9a19ff9 100644 --- a/utils/command_executer.py +++ b/utils/command_executer.py @@ -1,14 +1,19 @@ +#!/usr/bin/python +# +# Copyright 2011 Google Inc. All Rights Reserved. +# + import getpass -import logger import os import pty import re import select import subprocess -import sys import tempfile import time -import utils + +import logger +import misc mock_default = False @@ -155,7 +160,8 @@ class CommandExecuter: def CrosRunCommand(self, cmd, return_output=False, machine=None, username=None, command_terminator=None, chromeos_root=None, - command_timeout=None): + command_timeout=None, + terminated_timeout=10): """Run a command on a chromeos box""" self.logger.LogCmd(cmd) self.logger.LogFatalIf(not machine, "No machine provided!") @@ -176,7 +182,8 @@ class CommandExecuter: command += "\necho \"$REMOTE_OUT\"" retval = self.RunCommand(command, return_output, command_terminator=command_terminator, - command_timeout=command_timeout) + command_timeout=command_timeout, + terminated_timeout=terminated_timeout) if return_output: connect_signature = ("Initiating first contact with remote host\n" + "Connection OK\n") @@ -189,7 +196,8 @@ class CommandExecuter: return retval def ChrootRunCommand(self, chromeos_root, command, return_output=False, - command_terminator=None): + command_terminator=None, command_timeout=None, + terminated_timeout=10): self.logger.LogCmd(command) handle, command_file = tempfile.mkstemp(dir=os.path.join(chromeos_root, @@ -205,7 +213,9 @@ class CommandExecuter: command = "cd %s; cros_sdk -- ./%s" % (chromeos_root, os.path.basename(command_file)) ret = self.RunCommand(command, return_output, - command_terminator=command_terminator) + command_terminator=command_terminator, + command_timeout=command_timeout, + terminated_timeout=terminated_timeout) os.remove(command_file) return ret @@ -237,8 +247,8 @@ class CommandExecuter: cros_machine = dest_machine command = self.RemoteAccessInitCommand(chromeos_root, cros_machine) - src_parent, src_child = utils.GetRoot(src) - dest_parent, dest_child = utils.GetRoot(dest) + src_parent, src_child = misc.GetRoot(src) + dest_parent, dest_child = misc.GetRoot(dest) ssh_command = ("ssh -p ${FLAGS_ssh_port}" + " -o StrictHostKeyChecking=no" + " -o UserKnownHostsFile=$(mktemp)" + diff --git a/utils/logger.py b/utils/logger.py index 6f626407..1ef996eb 100644 --- a/utils/logger.py +++ b/utils/logger.py @@ -2,10 +2,13 @@ # # Copyright 2010 Google Inc. All Rights Reserved. +# System modules import os.path import sys import traceback -import utils + +# Local modules +import misc class Logger(object): @@ -137,7 +140,7 @@ def InitLogger(script_name, print_console=True): """Initialize a global logger. To be called only once.""" global main_logger assert not main_logger, "The logger has already been initialized" - rootdir, basefilename = utils.GetRoot(script_name) + rootdir, basefilename = misc.GetRoot(script_name) main_logger = Logger(rootdir, basefilename, print_console) diff --git a/utils/misc.py b/utils/misc.py new file mode 100644 index 00000000..9111c6b8 --- /dev/null +++ b/utils/misc.py @@ -0,0 +1,158 @@ +#!/usr/bin/python2.6 +# +# Copyright 2010 Google Inc. All Rights Reserved. + +"""Utilities for toolchain build.""" + +__author__ = "asharif@google.com (Ahmad Sharif)" + +import hashlib +import os +import re +import stat +import command_executer +import logger +import tempfile +from contextlib import contextmanager + + +def ApplySubs(string, *substitutions): + for pattern, replacement in substitutions: + string = re.sub(pattern, replacement, string) + return string + + +def GetFilenameFromString(string): + return ApplySubs(string, + ("/", "__"), + ("\s", "_"), + ("=", ""), + ("\"", "")) + + +def GetRoot(scr_name): + """Break up pathname into (dir+name).""" + abs_path = os.path.abspath(scr_name) + return (os.path.dirname(abs_path), os.path.basename(abs_path)) + + +def FormatQuotedCommand(command): + return ApplySubs(command, + ("\"", "\\\"")) + + +def FormatCommands(commands): + return ApplySubs(str(commands), + ("&&", "&&\n"), + (";", ";\n"), + ("\n+\s*", "\n")) + + +def GetImageDir(chromeos_root, board): + return os.path.join(chromeos_root, + "src", + "build", + "images", + board) + + +def LabelLatestImage(chromeos_root, board, label): + image_dir = GetImageDir(chromeos_root, board) + latest_image_dir = os.path.join(image_dir, "latest") + latest_image_dir = os.path.realpath(latest_image_dir) + latest_image_dir = os.path.basename(latest_image_dir) + with WorkingDirectory(image_dir): + command = "ln -sf -T %s %s" % (latest_image_dir, label) + ce = command_executer.GetCommandExecuter() + return ce.RunCommand(command) + + +def DoesLabelExist(chromeos_root, board, label): + image_label = os.path.join(GetImageDir(chromeos_root, board), + label) + return os.path.exists(image_label) + + +def GetBuildPackagesCommand(board, usepkg=False): + if usepkg: + usepkg_flag = "--usepkg" + else: + usepkg_flag = "--nousepkg" + return ("./build_packages %s --withdev --withtest --withautotest " + "--skip_toolchain_update --nowithdebug --board=%s" % + (usepkg_flag, board)) + + +def GetBuildImageCommand(board): + return "./build_image --board=%s test" % board + + +def GetSetupBoardCommand(board, gcc_version=None, binutils_version=None, + usepkg=None, force=None): + options = [] + + if gcc_version: + options.append("--gcc_version=%s" % gcc_version) + + if binutils_version: + options.append("--binutils_version=%s" % binutils_version) + + if usepkg: + options.append("--usepkg") + else: + options.append("--nousepkg") + + if force: + options.append("--force") + + return "./setup_board --board=%s %s" % (board, " ".join(options)) + + +def CanonicalizePath(path): + path = os.path.expanduser(path) + path = os.path.realpath(path) + return path + + +def GetCtargetFromBoard(board, chromeos_root): + base_board = board.split("_")[0] + command = ("source " + "../platform/dev/toolchain_utils.sh; get_ctarget_from_board %s" % + base_board) + ce = command_executer.GetCommandExecuter() + ret, out, err = ce.ChrootRunCommand(chromeos_root, + command, + return_output=True) + if ret != 0: + raise ValueError("Board %s is invalid!" % board) + return out.strip() + + +def GetChromeSrcDir(): + return "var/cache/distfiles/target/chrome-src/src" + + +def GetEnvStringFromDict(env_dict): + return " ".join(["%s=\"%s\"" % var for var in env_dict.items()]) + + +def GetAllImages(chromeos_root, board): + ce = command_executer.GetCommandExecuter() + command = ("find %s/src/build/images/%s -name chromiumos_test_image.bin" % + (chromeos_root, board)) + ret, out, err = ce.RunCommand(command, return_output=True) + return out.splitlines() + + +@contextmanager +def WorkingDirectory(new_dir): + old_dir = os.getcwd() + if old_dir != new_dir: + msg = "cd %s" % new_dir + logger.GetLogger().LogCmd(msg) + os.chdir(new_dir) + yield new_dir + if old_dir != new_dir: + msg = "cd %s" % old_dir + logger.GetLogger().LogCmd(msg) + os.chdir(old_dir) diff --git a/utils/misc_test.py b/utils/misc_test.py new file mode 100644 index 00000000..62742e3b --- /dev/null +++ b/utils/misc_test.py @@ -0,0 +1,22 @@ +# Copyright 2012 Google Inc. All Rights Reserved. + +"""Tests for misc.""" + +__author__ = 'asharif@google.com (Ahmad Sharif)' + +# System modules +import re +import unittest + +# Local modules +import misc + + +class UtilsTest(unittest.TestCase): + def testGetFilenameFromString(self): + string = 'a /b=c"d' + filename = misc.GetFilenameFromString(string) + self.assertTrue(filename == 'a___bcd') + +if __name__ == '__main__': + unittest.main() diff --git a/utils/utils.py b/utils/utils.py deleted file mode 100755 index c2398c54..00000000 --- a/utils/utils.py +++ /dev/null @@ -1,106 +0,0 @@ -#!/usr/bin/python2.6 -# -# Copyright 2010 Google Inc. All Rights Reserved. - -"""Utilities for toolchain build.""" - -__author__ = "asharif@google.com (Ahmad Sharif)" - -import hashlib -import os -import re -import stat -import command_executer -import logger -import tempfile -from contextlib import contextmanager - - -def GetRoot(scr_name): - """Break up pathname into (dir+name).""" - abs_path = os.path.abspath(scr_name) - return (os.path.dirname(abs_path), os.path.basename(abs_path)) - - -def FormatQuotedCommand(command): - return command.replace("\"", "\\\"") - - -def FormatCommands(commands): - output = str(commands) - output = re.sub("&&", "&&\n", output) - output = re.sub(";", ";\n", output) - output = re.sub("\n+\s*", "\n", output) - return output - - -def GetBuildPackagesCommand(board): - return "./build_packages --nousepkg --withdev --withtest --withautotest " \ - "--skip_toolchain_update --nowithdebug --board=%s" % board - - -def GetBuildImageCommand(board): - return "./build_image --withdev --board=%s" % board - - -def GetModImageForTestCommand(board): - return "./mod_image_for_test.sh --yes --board=%s" % board - - -def GetSetupBoardCommand(board, gcc_version=None, binutils_version=None, - usepkg=None, force=None): - options = [] - - if gcc_version: - options.append("--gcc_version=%s" % gcc_version) - - if binutils_version: - options.append("--binutils_version=%s" % binutils_version) - - if usepkg: - options.append("--usepkg") - else: - options.append("--nousepkg") - - if force: - options.append("--force") - - return "./setup_board --board=%s %s" % (board, " ".join(options)) - - -def CanonicalizePath(path): - path = os.path.expanduser(path) - path = os.path.realpath(path) - return path - - -def GetCtargetFromBoard(board, chromeos_root): - base_board = board.split("_")[0] - command = ("cat" - " $(cros_overlay_list --board=%s --primary_only)/toolchain.conf" % - (base_board)) - ce = command_executer.GetCommandExecuter() - ret, out, err = ce.ChrootRunCommand(chromeos_root, - command, - return_output=True) - if ret != 0: - raise ValueError("Board %s is invalid!" % board) - return out.strip() - - -def GetChromeSrcDir(): - return "var/cache/chromeos-chrome/chrome-src/src" - - -@contextmanager -def WorkingDirectory(new_dir): - old_dir = os.getcwd() - if old_dir != new_dir: - msg = "cd %s" % new_dir - logger.GetLogger().LogCmd(msg) - os.chdir(new_dir) - yield new_dir - if old_dir != new_dir: - msg = "cd %s" % old_dir - logger.GetLogger().LogCmd(msg) - os.chdir(old_dir) |