def DeleteChromeOsTree(chromeos_root, dry_run=False): """Delete a ChromeOs tree *safely*. Args: chromeos_root: dir of the tree, could be a relative one (but be careful) dry_run: only prints out the command if True Returns: True if everything is ok. """ if not IsChromeOsTree(chromeos_root): logger.GetLogger().LogWarning( '"{0}" does not seem to be a valid chromeos tree, do nothing.'. format(chromeos_root)) return False cmd0 = 'cd {0} && cros_sdk --delete'.format(chromeos_root) if dry_run: print(cmd0) else: if command_executer.GetCommandExecuter().RunCommand( cmd0, print_to_console=True) != 0: return False cmd1 = ('export CHROMEOSDIRNAME="$(dirname $(cd {0} && pwd))" && ' 'export CHROMEOSBASENAME="$(basename $(cd {0} && pwd))" && ' 'cd $CHROMEOSDIRNAME && sudo rm -fr $CHROMEOSBASENAME' ).format(chromeos_root) if dry_run: print(cmd1) return True return command_executer.GetCommandExecuter().RunCommand( cmd1, print_to_console=True) == 0
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, _ = ce.RunCommandWOutput(command) assert ret == 0, 'Could not run command: %s' % command return out.splitlines()
def Md5File(self, filename, log_level='verbose', _block_size=2**10): command = 'md5sum %s' % filename ce = command_executer.GetCommandExecuter(log_level=log_level) ret, out, _ = ce.RunCommandWOutput(command) if ret: raise RuntimeError('Could not run md5sum on: %s' % filename) return out.strip().split()[0]
def GetArchFromBoard(board, chromeos_root): """Get Arch from board.""" base_board = board.split('_')[0] command = ('source %s; get_board_arch %s' % (TOOLCHAIN_UTILS_PATH, base_board)) ce = command_executer.GetCommandExecuter() ret, out, _ = ce.ChrootRunCommandWOutput(chromeos_root, command) if ret != 0: raise ValueError('Board %s is invalid!' % board) # Remove ANSI escape sequences. out = StripANSIEscapeSequences(out) return out.strip()
def __init__(self, internal=True): self.internal = internal self.clone_location = tempfile.mkdtemp() self.ce = command_executer.GetCommandExecuter() if internal: versions_git = ('https://chrome-internal.googlesource.com/' 'chromeos/manifest-versions.git') else: versions_git = ( 'https://chromium.googlesource.com/chromiumos/manifest-versions.git') commands = [ 'cd {0}'.format(self.clone_location), 'git clone {0}'.format(versions_git) ] ret = self.ce.RunCommands(commands) if ret: logger.GetLogger().LogFatal('Failed to clone manifest-versions.')
def MergeEnvStringWithDict(env_string, env_dict, prepend=True): """Merge env string with dict.""" if not env_string.strip(): return GetEnvStringFromDict(env_dict) override_env_list = [] ce = command_executer.GetCommandExecuter() for k, v in env_dict.items(): v = v.strip("\"'") if prepend: new_env = "%s=\"%s $%s\"" % (k, v, k) else: new_env = "%s=\"$%s %s\"" % (k, k, v) command = '; '.join([env_string, new_env, 'echo $%s' % k]) ret, out, _ = ce.RunCommandWOutput(command) override_env_list.append('%s=%r' % (k, out.strip())) ret = env_string + ' ' + ' '.join(override_env_list) return ret.strip()
def LabelLatestImage(chromeos_root, board, label, vanilla_path=None): 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) retval = 0 with WorkingDirectory(image_dir): command = 'ln -sf -T %s %s' % (latest_image_dir, label) ce = command_executer.GetCommandExecuter() retval = ce.RunCommand(command) if retval: return retval if vanilla_path: command = 'ln -sf -T %s %s' % (vanilla_path, 'vanilla') retval2 = ce.RunCommand(command) return retval2 return retval
def GetChromeOSVersionFromLSBVersion(lsb_version): """Get Chromeos version from Lsb version.""" ce = command_executer.GetCommandExecuter() command = ('git ls-remote ' 'https://chromium.googlesource.com/chromiumos/manifest.git') ret, out, _ = ce.RunCommandWOutput(command, print_to_console=False) assert ret == 0, 'Command %s failed' % command lower = [] for line in out.splitlines(): mo = re.search(r'refs/heads/release-R(\d+)-(\d+)\.B', line) if mo: revision = int(mo.group(1)) build = int(mo.group(2)) lsb_build = int(lsb_version.split('.')[0]) if lsb_build > build: lower.append(revision) lower = sorted(lower) if lower: return 'R%d-%s' % (lower[-1] + 1, lsb_version) else: return 'Unknown'
def GetGitChangesAsList(git_dir, path=None, staged=False): """Get changed files as a list. Args: git_dir: git tree directory. path: a relative path that is part of the tree directory, could be null. staged: whether to include staged files as well. Returns: A list containing all the changed files. """ command = 'cd {0} && git diff --name-only'.format(git_dir) if staged: command += ' --cached' if path: command += ' -- ' + path _, out, _ = command_executer.GetCommandExecuter().RunCommandWOutput( command, print_to_console=False) rv = [] for line in out.splitlines(): rv.append(line) return rv
def GitGetCommitHash(git_dir, commit_symbolic_name): """Return githash for the symbolic git commit. For example, commit_symbolic_name could be "cros/gcc.gnu.org/branches/gcc/gcc-4_8-mobile, this function returns the git hash for this symbolic name. Args: git_dir: a git working tree. commit_symbolic_name: a symbolic name for a particular git commit. Returns: The git hash for the symbolic name or None if fails. """ command = ('cd {0} && git log -n 1 --pretty="format:%H" {1}').format( git_dir, commit_symbolic_name) rv, out, _ = command_executer.GetCommandExecuter().RunCommandWOutput( command, print_to_console=False) if rv == 0: return out.strip() return None
def HasGitUntrackedChanges(git_dir): """Return True if git repository has un-tracked changes.""" command = ('cd {0} && test -z ' '$(git ls-files --exclude-standard --others)').format(git_dir) return command_executer.GetCommandExecuter().RunCommand( command, print_to_console=False)
def HasGitUnstagedChanges(git_dir): """Return True if git repository has un-staged changes.""" command = 'cd {0} && git diff --quiet --exit-code HEAD'.format(git_dir) return command_executer.GetCommandExecuter().RunCommand( command, print_to_console=False)