Rahul Chaudhry | 8ac4ec0 | 2014-11-01 09:31:15 -0700 | [diff] [blame] | 1 | # Copyright 2013 The Chromium OS Authors. All rights reserved. |
Yunlian Jiang | b0a02f4 | 2013-08-22 09:55:54 -0700 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 4 | """Utilities for toolchain build.""" |
| 5 | |
Rahul Chaudhry | cbc5a26 | 2015-12-30 17:05:14 -0800 | [diff] [blame] | 6 | from __future__ import print_function |
| 7 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 8 | __author__ = 'asharif@google.com (Ahmad Sharif)' |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 9 | |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 10 | from contextlib import contextmanager |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 11 | import os |
| 12 | import re |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 13 | import shutil |
| 14 | import sys |
Han Shen | cc34c22 | 2014-01-03 11:39:39 -0800 | [diff] [blame] | 15 | import traceback |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 16 | |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 17 | import command_executer |
| 18 | import logger |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 19 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 20 | CHROMEOS_SCRIPTS_DIR = '~/trunk/src/scripts' |
| 21 | TOOLCHAIN_UTILS_PATH = '~/trunk/src/platform/dev/toolchain_utils.sh' |
Rahul Chaudhry | 8ac4ec0 | 2014-11-01 09:31:15 -0700 | [diff] [blame] | 22 | |
| 23 | |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 24 | def GetChromeOSVersionFromLSBVersion(lsb_version): |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 25 | """Get Chromeos version from Lsb version.""" |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 26 | ce = command_executer.GetCommandExecuter() |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 27 | command = ('git ls-remote ' |
| 28 | 'https://chromium.googlesource.com/chromiumos/manifest.git') |
Luis Lozano | 036c923 | 2015-12-10 10:47:01 -0800 | [diff] [blame] | 29 | ret, out, _ = ce.RunCommandWOutput(command, print_to_console=False) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 30 | assert ret == 0, 'Command %s failed' % command |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 31 | lower = [] |
| 32 | for line in out.splitlines(): |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 33 | mo = re.search(r'refs/heads/release-R(\d+)-(\d+)\.B', line) |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 34 | if mo: |
| 35 | revision = int(mo.group(1)) |
| 36 | build = int(mo.group(2)) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 37 | lsb_build = int(lsb_version.split('.')[0]) |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 38 | if lsb_build > build: |
| 39 | lower.append(revision) |
| 40 | lower = sorted(lower) |
| 41 | if lower: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 42 | return 'R%d-%s' % (lower[-1] + 1, lsb_version) |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 43 | else: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 44 | return 'Unknown' |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 45 | |
| 46 | |
| 47 | def ApplySubs(string, *substitutions): |
| 48 | for pattern, replacement in substitutions: |
| 49 | string = re.sub(pattern, replacement, string) |
| 50 | return string |
| 51 | |
| 52 | |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 53 | def UnitToNumber(unit_num, base=1000): |
| 54 | """Convert a number with unit to float.""" |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 55 | unit_dict = {'kilo': base, 'mega': base**2, 'giga': base**3} |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 56 | unit_num = unit_num.lower() |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 57 | mo = re.search(r'(\d*)(.+)?', unit_num) |
Ahmad Sharif | 5ae8a5c | 2012-05-18 10:59:51 -0700 | [diff] [blame] | 58 | number = mo.group(1) |
| 59 | unit = mo.group(2) |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 60 | if not unit: |
| 61 | return float(number) |
Ahmad Sharif | 5ae8a5c | 2012-05-18 10:59:51 -0700 | [diff] [blame] | 62 | for k, v in unit_dict.items(): |
| 63 | if k.startswith(unit): |
| 64 | return float(number) * v |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 65 | raise Exception('Unit: %s not found in byte: %s!' % (unit, unit_num)) |
Ahmad Sharif | 5ae8a5c | 2012-05-18 10:59:51 -0700 | [diff] [blame] | 66 | |
| 67 | |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 68 | def GetFilenameFromString(string): |
David Sharp | 3e8c219 | 2016-01-27 20:16:38 -0800 | [diff] [blame] | 69 | return ApplySubs(string, (r'/', '__'), (r'\s', '_'), (r'[\\$="?^]', ''),) |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 70 | |
| 71 | |
| 72 | def GetRoot(scr_name): |
| 73 | """Break up pathname into (dir+name).""" |
| 74 | abs_path = os.path.abspath(scr_name) |
| 75 | return (os.path.dirname(abs_path), os.path.basename(abs_path)) |
| 76 | |
| 77 | |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 78 | def GetChromeOSKeyFile(chromeos_root): |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 79 | return os.path.join(chromeos_root, 'src', 'scripts', 'mod_for_test_scripts', |
| 80 | 'ssh_keys', 'testing_rsa') |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 81 | |
| 82 | |
Ahmad Sharif | 5ae8a5c | 2012-05-18 10:59:51 -0700 | [diff] [blame] | 83 | def GetChrootPath(chromeos_root): |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 84 | return os.path.join(chromeos_root, 'chroot') |
Ahmad Sharif | 5ae8a5c | 2012-05-18 10:59:51 -0700 | [diff] [blame] | 85 | |
| 86 | |
| 87 | def GetInsideChrootPath(chromeos_root, file_path): |
| 88 | if not file_path.startswith(GetChrootPath(chromeos_root)): |
| 89 | raise Exception("File: %s doesn't seem to be in the chroot: %s" % |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 90 | (file_path, chromeos_root)) |
Ahmad Sharif | 5ae8a5c | 2012-05-18 10:59:51 -0700 | [diff] [blame] | 91 | return file_path[len(GetChrootPath(chromeos_root)):] |
| 92 | |
| 93 | |
| 94 | def GetOutsideChrootPath(chromeos_root, file_path): |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 95 | return os.path.join(GetChrootPath(chromeos_root), file_path.lstrip('/')) |
Ahmad Sharif | 5ae8a5c | 2012-05-18 10:59:51 -0700 | [diff] [blame] | 96 | |
| 97 | |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 98 | def FormatQuotedCommand(command): |
David Sharp | 3e8c219 | 2016-01-27 20:16:38 -0800 | [diff] [blame] | 99 | return ApplySubs(command, ('"', r'\"')) |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 100 | |
| 101 | |
| 102 | def FormatCommands(commands): |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 103 | return ApplySubs( |
| 104 | str(commands), ('&&', '&&\n'), (';', ';\n'), (r'\n+\s*', '\n')) |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 105 | |
| 106 | |
| 107 | def GetImageDir(chromeos_root, board): |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 108 | return os.path.join(chromeos_root, 'src', 'build', 'images', board) |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 109 | |
| 110 | |
cmtice | 56fb716 | 2014-06-18 11:32:15 -0700 | [diff] [blame] | 111 | def LabelLatestImage(chromeos_root, board, label, vanilla_path=None): |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 112 | image_dir = GetImageDir(chromeos_root, board) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 113 | latest_image_dir = os.path.join(image_dir, 'latest') |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 114 | latest_image_dir = os.path.realpath(latest_image_dir) |
| 115 | latest_image_dir = os.path.basename(latest_image_dir) |
cmtice | 56fb716 | 2014-06-18 11:32:15 -0700 | [diff] [blame] | 116 | retval = 0 |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 117 | with WorkingDirectory(image_dir): |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 118 | command = 'ln -sf -T %s %s' % (latest_image_dir, label) |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 119 | ce = command_executer.GetCommandExecuter() |
cmtice | 56fb716 | 2014-06-18 11:32:15 -0700 | [diff] [blame] | 120 | retval = ce.RunCommand(command) |
| 121 | if retval: |
| 122 | return retval |
| 123 | if vanilla_path: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 124 | command = 'ln -sf -T %s %s' % (vanilla_path, 'vanilla') |
cmtice | 56fb716 | 2014-06-18 11:32:15 -0700 | [diff] [blame] | 125 | retval2 = ce.RunCommand(command) |
| 126 | return retval2 |
| 127 | return retval |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 128 | |
| 129 | |
| 130 | def DoesLabelExist(chromeos_root, board, label): |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 131 | image_label = os.path.join(GetImageDir(chromeos_root, board), label) |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 132 | return os.path.exists(image_label) |
| 133 | |
| 134 | |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 135 | def GetBuildPackagesCommand(board, usepkg=False, debug=False): |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 136 | if usepkg: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 137 | usepkg_flag = '--usepkg' |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 138 | else: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 139 | usepkg_flag = '--nousepkg' |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 140 | if debug: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 141 | withdebug_flag = '--withdebug' |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 142 | else: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 143 | withdebug_flag = '--nowithdebug' |
| 144 | return ('%s/build_packages %s --withdev --withtest --withautotest ' |
| 145 | '--skip_toolchain_update %s --board=%s ' |
| 146 | '--accept_licenses=@CHROMEOS' % |
Rahul Chaudhry | 8ac4ec0 | 2014-11-01 09:31:15 -0700 | [diff] [blame] | 147 | (CHROMEOS_SCRIPTS_DIR, usepkg_flag, withdebug_flag, board)) |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 148 | |
| 149 | |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 150 | def GetBuildImageCommand(board, dev=False): |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 151 | dev_args = '' |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 152 | if dev: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 153 | dev_args = '--noenable_rootfs_verification --disk_layout=2gb-rootfs' |
| 154 | return ('%s/build_image --board=%s %s test' % |
Rahul Chaudhry | 8ac4ec0 | 2014-11-01 09:31:15 -0700 | [diff] [blame] | 155 | (CHROMEOS_SCRIPTS_DIR, board, dev_args)) |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 156 | |
Han Shen | e4b6f22 | 2013-11-22 10:02:38 -0800 | [diff] [blame] | 157 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 158 | def GetSetupBoardCommand(board, |
| 159 | gcc_version=None, |
| 160 | binutils_version=None, |
| 161 | usepkg=None, |
| 162 | force=None): |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 163 | """Get setup_board command.""" |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 164 | options = [] |
| 165 | |
| 166 | if gcc_version: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 167 | options.append('--gcc_version=%s' % gcc_version) |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 168 | |
| 169 | if binutils_version: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 170 | options.append('--binutils_version=%s' % binutils_version) |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 171 | |
| 172 | if usepkg: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 173 | options.append('--usepkg') |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 174 | else: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 175 | options.append('--nousepkg') |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 176 | |
| 177 | if force: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 178 | options.append('--force') |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 179 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 180 | options.append('--accept_licenses=@CHROMEOS') |
Yunlian Jiang | b0a02f4 | 2013-08-22 09:55:54 -0700 | [diff] [blame] | 181 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 182 | return ('%s/setup_board --board=%s %s' % |
| 183 | (CHROMEOS_SCRIPTS_DIR, board, ' '.join(options))) |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 184 | |
| 185 | |
| 186 | def CanonicalizePath(path): |
| 187 | path = os.path.expanduser(path) |
| 188 | path = os.path.realpath(path) |
| 189 | return path |
| 190 | |
| 191 | |
| 192 | def GetCtargetFromBoard(board, chromeos_root): |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 193 | """Get Ctarget from board.""" |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 194 | base_board = board.split('_')[0] |
| 195 | command = ('source %s; get_ctarget_from_board %s' % |
Rahul Chaudhry | 8ac4ec0 | 2014-11-01 09:31:15 -0700 | [diff] [blame] | 196 | (TOOLCHAIN_UTILS_PATH, base_board)) |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 197 | ce = command_executer.GetCommandExecuter() |
Luis Lozano | 036c923 | 2015-12-10 10:47:01 -0800 | [diff] [blame] | 198 | ret, out, _ = ce.ChrootRunCommandWOutput(chromeos_root, command) |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 199 | if ret != 0: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 200 | raise ValueError('Board %s is invalid!' % board) |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 201 | # Remove ANSI escape sequences. |
| 202 | out = StripANSIEscapeSequences(out) |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 203 | return out.strip() |
| 204 | |
| 205 | |
Rahul Chaudhry | 215c12f | 2014-11-04 15:18:47 -0800 | [diff] [blame] | 206 | def GetArchFromBoard(board, chromeos_root): |
| 207 | """Get Arch from board.""" |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 208 | base_board = board.split('_')[0] |
| 209 | command = ('source %s; get_board_arch %s' % |
Rahul Chaudhry | 215c12f | 2014-11-04 15:18:47 -0800 | [diff] [blame] | 210 | (TOOLCHAIN_UTILS_PATH, base_board)) |
| 211 | ce = command_executer.GetCommandExecuter() |
Luis Lozano | 036c923 | 2015-12-10 10:47:01 -0800 | [diff] [blame] | 212 | ret, out, _ = ce.ChrootRunCommandWOutput(chromeos_root, command) |
Rahul Chaudhry | 215c12f | 2014-11-04 15:18:47 -0800 | [diff] [blame] | 213 | if ret != 0: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 214 | raise ValueError('Board %s is invalid!' % board) |
Rahul Chaudhry | 215c12f | 2014-11-04 15:18:47 -0800 | [diff] [blame] | 215 | # Remove ANSI escape sequences. |
| 216 | out = StripANSIEscapeSequences(out) |
| 217 | return out.strip() |
| 218 | |
| 219 | |
| 220 | def GetGccLibsDestForBoard(board, chromeos_root): |
| 221 | """Get gcc libs destination from board.""" |
| 222 | arch = GetArchFromBoard(board, chromeos_root) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 223 | if arch == 'x86': |
| 224 | return '/build/%s/usr/lib/gcc/' % board |
| 225 | if arch == 'amd64': |
| 226 | return '/build/%s/usr/lib64/gcc/' % board |
| 227 | if arch == 'arm': |
| 228 | return '/build/%s/usr/lib/gcc/' % board |
| 229 | if arch == 'arm64': |
| 230 | return '/build/%s/usr/lib/gcc/' % board |
| 231 | raise ValueError('Arch %s is invalid!' % arch) |
Rahul Chaudhry | 215c12f | 2014-11-04 15:18:47 -0800 | [diff] [blame] | 232 | |
| 233 | |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 234 | def StripANSIEscapeSequences(string): |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 235 | string = re.sub(r'\x1b\[[0-9]*[a-zA-Z]', '', string) |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 236 | return string |
| 237 | |
| 238 | |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 239 | def GetChromeSrcDir(): |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 240 | return 'var/cache/distfiles/target/chrome-src/src' |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 241 | |
| 242 | |
| 243 | def GetEnvStringFromDict(env_dict): |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 244 | return ' '.join(["%s=\"%s\"" % var for var in env_dict.items()]) |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 245 | |
| 246 | |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 247 | def MergeEnvStringWithDict(env_string, env_dict, prepend=True): |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 248 | """Merge env string with dict.""" |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 249 | if not env_string.strip(): |
| 250 | return GetEnvStringFromDict(env_dict) |
| 251 | override_env_list = [] |
| 252 | ce = command_executer.GetCommandExecuter() |
| 253 | for k, v in env_dict.items(): |
| 254 | v = v.strip("\"'") |
| 255 | if prepend: |
| 256 | new_env = "%s=\"%s $%s\"" % (k, v, k) |
| 257 | else: |
| 258 | new_env = "%s=\"$%s %s\"" % (k, k, v) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 259 | command = '; '.join([env_string, new_env, 'echo $%s' % k]) |
Luis Lozano | 036c923 | 2015-12-10 10:47:01 -0800 | [diff] [blame] | 260 | ret, out, _ = ce.RunCommandWOutput(command) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 261 | override_env_list.append('%s=%r' % (k, out.strip())) |
| 262 | ret = env_string + ' ' + ' '.join(override_env_list) |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 263 | return ret.strip() |
| 264 | |
| 265 | |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 266 | def GetAllImages(chromeos_root, board): |
| 267 | ce = command_executer.GetCommandExecuter() |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 268 | command = ('find %s/src/build/images/%s -name chromiumos_test_image.bin' % |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 269 | (chromeos_root, board)) |
Luis Lozano | 036c923 | 2015-12-10 10:47:01 -0800 | [diff] [blame] | 270 | ret, out, _ = ce.RunCommandWOutput(command) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 271 | assert ret == 0, 'Could not run command: %s' % command |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 272 | return out.splitlines() |
| 273 | |
| 274 | |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 275 | def IsFloat(text): |
| 276 | if text is None: |
| 277 | return False |
| 278 | try: |
| 279 | float(text) |
| 280 | return True |
| 281 | except ValueError: |
| 282 | return False |
| 283 | |
Han Shen | e4b6f22 | 2013-11-22 10:02:38 -0800 | [diff] [blame] | 284 | |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 285 | def RemoveChromeBrowserObjectFiles(chromeos_root, board): |
Han Shen | e4b6f22 | 2013-11-22 10:02:38 -0800 | [diff] [blame] | 286 | """Remove any object files from all the posible locations.""" |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 287 | out_dir = os.path.join( |
| 288 | GetChrootPath(chromeos_root), |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 289 | 'var/cache/chromeos-chrome/chrome-src/src/out_%s' % board) |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 290 | if os.path.exists(out_dir): |
| 291 | shutil.rmtree(out_dir) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 292 | logger.GetLogger().LogCmd('rm -rf %s' % out_dir) |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 293 | out_dir = os.path.join( |
| 294 | GetChrootPath(chromeos_root), |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 295 | 'var/cache/chromeos-chrome/chrome-src-internal/src/out_%s' % board) |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 296 | if os.path.exists(out_dir): |
| 297 | shutil.rmtree(out_dir) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 298 | logger.GetLogger().LogCmd('rm -rf %s' % out_dir) |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 299 | |
Han Shen | e4b6f22 | 2013-11-22 10:02:38 -0800 | [diff] [blame] | 300 | |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 301 | @contextmanager |
| 302 | def WorkingDirectory(new_dir): |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 303 | """Get the working directory.""" |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 304 | old_dir = os.getcwd() |
| 305 | if old_dir != new_dir: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 306 | msg = 'cd %s' % new_dir |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 307 | logger.GetLogger().LogCmd(msg) |
| 308 | os.chdir(new_dir) |
| 309 | yield new_dir |
| 310 | if old_dir != new_dir: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 311 | msg = 'cd %s' % old_dir |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 312 | logger.GetLogger().LogCmd(msg) |
| 313 | os.chdir(old_dir) |
Han Shen | 9144532 | 2013-03-20 13:43:31 -0700 | [diff] [blame] | 314 | |
Han Shen | e4b6f22 | 2013-11-22 10:02:38 -0800 | [diff] [blame] | 315 | |
Han Shen | 9144532 | 2013-03-20 13:43:31 -0700 | [diff] [blame] | 316 | def HasGitStagedChanges(git_dir): |
| 317 | """Return True if git repository has staged changes.""" |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 318 | command = 'cd {0} && git diff --quiet --cached --exit-code HEAD'.format( |
Han Shen | e4b6f22 | 2013-11-22 10:02:38 -0800 | [diff] [blame] | 319 | git_dir) |
Han Shen | 9144532 | 2013-03-20 13:43:31 -0700 | [diff] [blame] | 320 | return command_executer.GetCommandExecuter().RunCommand( |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 321 | command, |
| 322 | print_to_console=False) |
Han Shen | e4b6f22 | 2013-11-22 10:02:38 -0800 | [diff] [blame] | 323 | |
Han Shen | 9144532 | 2013-03-20 13:43:31 -0700 | [diff] [blame] | 324 | |
| 325 | def HasGitUnstagedChanges(git_dir): |
| 326 | """Return True if git repository has un-staged changes.""" |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 327 | command = 'cd {0} && git diff --quiet --exit-code HEAD'.format(git_dir) |
Han Shen | 9144532 | 2013-03-20 13:43:31 -0700 | [diff] [blame] | 328 | return command_executer.GetCommandExecuter().RunCommand( |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 329 | command, |
| 330 | print_to_console=False) |
Han Shen | e4b6f22 | 2013-11-22 10:02:38 -0800 | [diff] [blame] | 331 | |
Han Shen | 9144532 | 2013-03-20 13:43:31 -0700 | [diff] [blame] | 332 | |
| 333 | def HasGitUntrackedChanges(git_dir): |
| 334 | """Return True if git repository has un-tracked changes.""" |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 335 | command = ('cd {0} && test -z ' |
| 336 | '$(git ls-files --exclude-standard --others)').format(git_dir) |
Han Shen | 9144532 | 2013-03-20 13:43:31 -0700 | [diff] [blame] | 337 | return command_executer.GetCommandExecuter().RunCommand( |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 338 | command, |
| 339 | print_to_console=False) |
Han Shen | e4b6f22 | 2013-11-22 10:02:38 -0800 | [diff] [blame] | 340 | |
Han Shen | 9144532 | 2013-03-20 13:43:31 -0700 | [diff] [blame] | 341 | |
Han Shen | 819f862 | 2014-04-08 16:57:38 -0700 | [diff] [blame] | 342 | def GitGetCommitHash(git_dir, commit_symbolic_name): |
| 343 | """Return githash for the symbolic git commit. |
| 344 | |
| 345 | For example, commit_symbolic_name could be |
| 346 | "cros/gcc.gnu.org/branches/gcc/gcc-4_8-mobile, this function returns the git |
| 347 | hash for this symbolic name. |
| 348 | |
| 349 | Args: |
| 350 | git_dir: a git working tree. |
| 351 | commit_symbolic_name: a symbolic name for a particular git commit. |
Rahul Chaudhry | 30840d7 | 2016-01-29 13:13:18 -0800 | [diff] [blame] | 352 | |
Han Shen | 819f862 | 2014-04-08 16:57:38 -0700 | [diff] [blame] | 353 | Returns: |
| 354 | The git hash for the symbolic name or None if fails. |
| 355 | """ |
| 356 | |
| 357 | command = ('cd {0} && git log -n 1 --pretty="format:%H" {1}').format( |
| 358 | git_dir, commit_symbolic_name) |
Luis Lozano | 036c923 | 2015-12-10 10:47:01 -0800 | [diff] [blame] | 359 | rv, out, _ = command_executer.GetCommandExecuter().RunCommandWOutput( |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 360 | command, |
| 361 | print_to_console=False) |
Han Shen | 819f862 | 2014-04-08 16:57:38 -0700 | [diff] [blame] | 362 | if rv == 0: |
| 363 | return out.strip() |
| 364 | return None |
| 365 | |
| 366 | |
Han Shen | 9144532 | 2013-03-20 13:43:31 -0700 | [diff] [blame] | 367 | def IsGitTreeClean(git_dir): |
Han Shen | e4b6f22 | 2013-11-22 10:02:38 -0800 | [diff] [blame] | 368 | """Test if git tree has no local changes. |
| 369 | |
| 370 | Args: |
| 371 | git_dir: git tree directory. |
Rahul Chaudhry | 30840d7 | 2016-01-29 13:13:18 -0800 | [diff] [blame] | 372 | |
Han Shen | e4b6f22 | 2013-11-22 10:02:38 -0800 | [diff] [blame] | 373 | Returns: |
| 374 | True if git dir is clean. |
| 375 | """ |
Han Shen | 9144532 | 2013-03-20 13:43:31 -0700 | [diff] [blame] | 376 | if HasGitStagedChanges(git_dir): |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 377 | logger.GetLogger().LogWarning('Git tree has staged changes.') |
Han Shen | 9144532 | 2013-03-20 13:43:31 -0700 | [diff] [blame] | 378 | return False |
| 379 | if HasGitUnstagedChanges(git_dir): |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 380 | logger.GetLogger().LogWarning('Git tree has unstaged changes.') |
Han Shen | 9144532 | 2013-03-20 13:43:31 -0700 | [diff] [blame] | 381 | return False |
| 382 | if HasGitUntrackedChanges(git_dir): |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 383 | logger.GetLogger().LogWarning('Git tree has un-tracked changes.') |
Han Shen | 9144532 | 2013-03-20 13:43:31 -0700 | [diff] [blame] | 384 | return False |
| 385 | return True |
| 386 | |
Han Shen | e4b6f22 | 2013-11-22 10:02:38 -0800 | [diff] [blame] | 387 | |
Han Shen | 9144532 | 2013-03-20 13:43:31 -0700 | [diff] [blame] | 388 | def GetGitChangesAsList(git_dir, path=None, staged=False): |
Han Shen | e4b6f22 | 2013-11-22 10:02:38 -0800 | [diff] [blame] | 389 | """Get changed files as a list. |
| 390 | |
| 391 | Args: |
| 392 | git_dir: git tree directory. |
| 393 | path: a relative path that is part of the tree directory, could be null. |
| 394 | staged: whether to include staged files as well. |
Rahul Chaudhry | 30840d7 | 2016-01-29 13:13:18 -0800 | [diff] [blame] | 395 | |
Han Shen | e4b6f22 | 2013-11-22 10:02:38 -0800 | [diff] [blame] | 396 | Returns: |
| 397 | A list containing all the changed files. |
| 398 | """ |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 399 | command = 'cd {0} && git diff --name-only'.format(git_dir) |
Han Shen | 9144532 | 2013-03-20 13:43:31 -0700 | [diff] [blame] | 400 | if staged: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 401 | command += ' --cached' |
Han Shen | 9144532 | 2013-03-20 13:43:31 -0700 | [diff] [blame] | 402 | if path: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 403 | command += ' -- ' + path |
Luis Lozano | 036c923 | 2015-12-10 10:47:01 -0800 | [diff] [blame] | 404 | _, out, _ = command_executer.GetCommandExecuter().RunCommandWOutput( |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 405 | command, |
| 406 | print_to_console=False) |
Han Shen | 9144532 | 2013-03-20 13:43:31 -0700 | [diff] [blame] | 407 | rv = [] |
| 408 | for line in out.splitlines(): |
| 409 | rv.append(line) |
| 410 | return rv |
Han Shen | e4b6f22 | 2013-11-22 10:02:38 -0800 | [diff] [blame] | 411 | |
| 412 | |
| 413 | def IsChromeOsTree(chromeos_root): |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 414 | return (os.path.isdir(os.path.join(chromeos_root, |
| 415 | 'src/third_party/chromiumos-overlay')) and |
| 416 | os.path.isdir(os.path.join(chromeos_root, 'manifest'))) |
Han Shen | e4b6f22 | 2013-11-22 10:02:38 -0800 | [diff] [blame] | 417 | |
| 418 | |
| 419 | def DeleteChromeOsTree(chromeos_root, dry_run=False): |
| 420 | """Delete a ChromeOs tree *safely*. |
| 421 | |
| 422 | Args: |
| 423 | chromeos_root: dir of the tree, could be a relative one (but be careful) |
| 424 | dry_run: only prints out the command if True |
| 425 | |
| 426 | Returns: |
| 427 | True if everything is ok. |
| 428 | """ |
| 429 | if not IsChromeOsTree(chromeos_root): |
| 430 | logger.GetLogger().LogWarning( |
| 431 | '"{0}" does not seem to be a valid chromeos tree, do nothing.'.format( |
| 432 | chromeos_root)) |
| 433 | return False |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 434 | cmd0 = 'cd {0} && cros_sdk --delete'.format(chromeos_root) |
Han Shen | e4b6f22 | 2013-11-22 10:02:38 -0800 | [diff] [blame] | 435 | if dry_run: |
Rahul Chaudhry | cbc5a26 | 2015-12-30 17:05:14 -0800 | [diff] [blame] | 436 | print(cmd0) |
Han Shen | e4b6f22 | 2013-11-22 10:02:38 -0800 | [diff] [blame] | 437 | else: |
| 438 | if command_executer.GetCommandExecuter().RunCommand( |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 439 | cmd0, |
| 440 | print_to_console=True) != 0: |
Han Shen | e4b6f22 | 2013-11-22 10:02:38 -0800 | [diff] [blame] | 441 | return False |
| 442 | |
| 443 | cmd1 = ('export CHROMEOSDIRNAME="$(dirname $(cd {0} && pwd))" && ' |
| 444 | 'export CHROMEOSBASENAME="$(basename $(cd {0} && pwd))" && ' |
| 445 | 'cd $CHROMEOSDIRNAME && sudo rm -fr $CHROMEOSBASENAME').format( |
| 446 | chromeos_root) |
| 447 | if dry_run: |
Rahul Chaudhry | cbc5a26 | 2015-12-30 17:05:14 -0800 | [diff] [blame] | 448 | print(cmd1) |
Han Shen | e4b6f22 | 2013-11-22 10:02:38 -0800 | [diff] [blame] | 449 | return True |
| 450 | |
| 451 | return command_executer.GetCommandExecuter().RunCommand( |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 452 | cmd1, |
| 453 | print_to_console=True) == 0 |
Han Shen | cc34c22 | 2014-01-03 11:39:39 -0800 | [diff] [blame] | 454 | |
| 455 | |
| 456 | def ApplyGerritPatches(chromeos_root, |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 457 | gerrit_patch_string, |
| 458 | branch='cros/master'): |
Han Shen | cc34c22 | 2014-01-03 11:39:39 -0800 | [diff] [blame] | 459 | """Apply gerrit patches on a chromeos tree. |
| 460 | |
| 461 | Args: |
| 462 | chromeos_root: chromeos tree path |
| 463 | gerrit_patch_string: a patch string just like the one gives to cbuildbot, |
| 464 | 'id1 id2 *id3 ... idn'. A prefix of '* means this is an internal patch. |
| 465 | branch: the tree based on which to apply the patches. |
Rahul Chaudhry | 30840d7 | 2016-01-29 13:13:18 -0800 | [diff] [blame] | 466 | |
Han Shen | cc34c22 | 2014-01-03 11:39:39 -0800 | [diff] [blame] | 467 | Returns: |
| 468 | True if success. |
| 469 | """ |
| 470 | |
| 471 | ### First of all, we need chromite libs |
| 472 | sys.path.append(os.path.join(chromeos_root, 'chromite')) |
Rahul Chaudhry | eff2fc1 | 2016-02-24 10:12:43 -0800 | [diff] [blame^] | 473 | # Imports below are ok after modifying path to add chromite. |
| 474 | # Pylint cannot detect that and complains. |
| 475 | # pylint: disable=import-error |
Han Shen | cc34c22 | 2014-01-03 11:39:39 -0800 | [diff] [blame] | 476 | from lib import git |
| 477 | from lib import gerrit |
| 478 | manifest = git.ManifestCheckout(chromeos_root) |
| 479 | patch_list = gerrit_patch_string.split(' ') |
| 480 | ### This takes time, print log information. |
| 481 | logger.GetLogger().LogOutput('Retrieving patch information from server ...') |
| 482 | patch_info_list = gerrit.GetGerritPatchInfo(patch_list) |
| 483 | for pi in patch_info_list: |
| 484 | project_checkout = manifest.FindCheckout(pi.project, strict=False) |
| 485 | if not project_checkout: |
| 486 | logger.GetLogger().LogError( |
| 487 | 'Failed to find patch project "{project}" in manifest.'.format( |
| 488 | project=pi.project)) |
| 489 | return False |
| 490 | |
| 491 | pi_str = '{project}:{ref}'.format(project=pi.project, ref=pi.ref) |
| 492 | try: |
| 493 | project_git_path = project_checkout.GetPath(absolute=True) |
| 494 | logger.GetLogger().LogOutput('Applying patch "{0}" in "{1}" ...'.format( |
| 495 | pi_str, project_git_path)) |
| 496 | pi.Apply(project_git_path, branch, trivial=False) |
| 497 | except Exception: |
| 498 | traceback.print_exc(file=sys.stdout) |
| 499 | logger.GetLogger().LogError('Failed to apply patch "{0}"'.format(pi_str)) |
| 500 | return False |
| 501 | return True |
cmtice | 5c09fc2 | 2015-04-22 09:25:53 -0700 | [diff] [blame] | 502 | |
| 503 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 504 | def BooleanPrompt(prompt='Do you want to continue?', |
| 505 | default=True, |
| 506 | true_value='yes', |
| 507 | false_value='no', |
| 508 | prolog=None): |
cmtice | 5c09fc2 | 2015-04-22 09:25:53 -0700 | [diff] [blame] | 509 | """Helper function for processing boolean choice prompts. |
| 510 | |
| 511 | Args: |
| 512 | prompt: The question to present to the user. |
| 513 | default: Boolean to return if the user just presses enter. |
| 514 | true_value: The text to display that represents a True returned. |
| 515 | false_value: The text to display that represents a False returned. |
| 516 | prolog: The text to display before prompt. |
| 517 | |
| 518 | Returns: |
| 519 | True or False. |
| 520 | """ |
| 521 | true_value, false_value = true_value.lower(), false_value.lower() |
| 522 | true_text, false_text = true_value, false_value |
| 523 | if true_value == false_value: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 524 | raise ValueError('true_value and false_value must differ: got %r' % |
| 525 | true_value) |
cmtice | 5c09fc2 | 2015-04-22 09:25:53 -0700 | [diff] [blame] | 526 | |
| 527 | if default: |
| 528 | true_text = true_text[0].upper() + true_text[1:] |
| 529 | else: |
| 530 | false_text = false_text[0].upper() + false_text[1:] |
| 531 | |
| 532 | prompt = ('\n%s (%s/%s)? ' % (prompt, true_text, false_text)) |
| 533 | |
| 534 | if prolog: |
| 535 | prompt = ('\n%s\n%s' % (prolog, prompt)) |
| 536 | |
| 537 | while True: |
| 538 | try: |
| 539 | response = raw_input(prompt).lower() |
| 540 | except EOFError: |
| 541 | # If the user hits CTRL+D, or stdin is disabled, use the default. |
| 542 | print() |
| 543 | response = None |
| 544 | except KeyboardInterrupt: |
| 545 | # If the user hits CTRL+C, just exit the process. |
| 546 | print() |
Rahul Chaudhry | eff2fc1 | 2016-02-24 10:12:43 -0800 | [diff] [blame^] | 547 | print('CTRL+C detected; exiting') |
| 548 | sys.exit() |
cmtice | 5c09fc2 | 2015-04-22 09:25:53 -0700 | [diff] [blame] | 549 | |
| 550 | if not response: |
| 551 | return default |
| 552 | if true_value.startswith(response): |
| 553 | if not false_value.startswith(response): |
| 554 | return True |
| 555 | # common prefix between the two... |
| 556 | elif false_value.startswith(response): |
| 557 | return False |