From c16daa1eb92cba9781a557798b065747128acfb1 Mon Sep 17 00:00:00 2001 From: Jian Cai Date: Wed, 15 Apr 2020 17:53:41 -0700 Subject: llvm_tools: move common functions into standalone modules Move common functions into separate modules and update dependencies accordingly. BUG=chromium:1057428 TEST=local tests. Change-Id: I40f1b613f0a41f1fc478c811379c851479aff7c3 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/2151708 Reviewed-by: Manoj Gupta Reviewed-by: George Burgess Tested-by: Jian Cai --- llvm_tools/git.py | 136 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100755 llvm_tools/git.py (limited to 'llvm_tools/git.py') diff --git a/llvm_tools/git.py b/llvm_tools/git.py new file mode 100755 index 00000000..778d9695 --- /dev/null +++ b/llvm_tools/git.py @@ -0,0 +1,136 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# Copyright 2020 The Chromium OS Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +"""Git helper functions.""" + +from __future__ import print_function + +import collections +import os +import re +import subprocess +import tempfile + +CommitContents = collections.namedtuple('CommitContents', ['url', 'cl_number']) + + +def InChroot(): + """Returns True if currently in the chroot.""" + return 'CROS_WORKON_SRCROOT' in os.environ + + +def VerifyOutsideChroot(): + """Checks whether the script invoked was executed in the chroot. + + Raises: + AssertionError: The script was run inside the chroot. + """ + + assert not InChroot(), 'Script should be run outside the chroot.' + + +def CreateBranch(repo, branch): + """Creates a branch in the given repo. + + Args: + repo: The absolute path to the repo. + branch: The name of the branch to create. + + Raises: + ValueError: Failed to create a repo in that directory. + """ + + if not os.path.isdir(repo): + raise ValueError('Invalid directory path provided: %s' % repo) + + subprocess.check_output(['git', '-C', repo, 'reset', 'HEAD', '--hard'], + encoding='utf-8') + + subprocess.check_output(['repo', 'start', branch], cwd=repo, encoding='utf-8') + + +def DeleteBranch(repo, branch): + """Deletes a branch in the given repo. + + Args: + repo: The absolute path of the repo. + branch: The name of the branch to delete. + + Raises: + ValueError: Failed to delete the repo in that directory. + """ + + if not os.path.isdir(repo): + raise ValueError('Invalid directory path provided: %s' % repo) + + subprocess.check_output(['git', '-C', repo, 'checkout', 'cros/master'], + encoding='utf-8') + + subprocess.check_output(['git', '-C', repo, 'reset', 'HEAD', '--hard'], + encoding='utf-8') + + subprocess.check_output(['git', '-C', repo, 'branch', '-D', branch], + encoding='utf-8') + + +def UploadChanges(repo, branch, commit_messages): + """Uploads the changes in the specifed branch of the given repo for review. + + Args: + repo: The absolute path to the repo where changes were made. + branch: The name of the branch to upload. + commit_messages: A string of commit message(s) (i.e. '[message]' + of the changes made. + + Returns: + A nametuple that has two (key, value) pairs, where the first pair is the + Gerrit commit URL and the second pair is the change list number. + + Raises: + ValueError: Failed to create a commit or failed to upload the + changes for review. + """ + + if not os.path.isdir(repo): + raise ValueError('Invalid path provided: %s' % repo) + + # Create a git commit. + with tempfile.NamedTemporaryFile(mode='w+t') as f: + f.write('\n'.join(commit_messages)) + f.flush() + + subprocess.check_output(['git', 'commit', '-F', f.name], + cwd=repo, + encoding='utf-8') + + # Upload the changes for review. + # Pylint currently doesn't lint things in py3 mode, and py2 didn't allow + # users to specify `encoding`s for Popen. Hence, pylint is "wrong" here. + # pylint: disable=unexpected-keyword-arg + # The CL URL is sent to 'stderr', so need to redirect 'stderr' to 'stdout'. + upload_changes_obj = subprocess.Popen( + ['repo', 'upload', '--yes', '--ne', '--no-verify', + '--br=%s' % branch], + cwd=repo, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + encoding='utf-8') + + out, _ = upload_changes_obj.communicate() + + if upload_changes_obj.returncode: # Failed to upload changes. + print(out) + raise ValueError('Failed to upload changes for review') + + found_url = re.search( + r'https://chromium-review.googlesource.com/c/' + r'chromiumos/overlays/chromiumos-overlay/\+/([0-9]+)', out.rstrip()) + + if not found_url: + raise ValueError('Failed to find change list URL.') + + return CommitContents( + url=found_url.group(0), cl_number=int(found_url.group(1))) -- cgit v1.2.3 From fbefdc4647a046010844aa2ea2551f15c80ef3aa Mon Sep 17 00:00:00 2001 From: Jian Cai Date: Thu, 30 Apr 2020 15:29:03 -0700 Subject: llvm_tool: cherry-pick multiple patches at once This change will allow cherrypick_cl.py to take in multiple SHAs at once and create local patches accordingly. All these SHAs will be applied to the same starting SHA. The package a patch applies to will be inferred automatically, so users no longer have to specify the package name. If a patch changes files in more than one package, it will be split into smaller patches by package and applied accordingly. The patch information will be added to PATCHES.json of each affected package. BUG=chromium:1057428 TEST=local tests. Change-Id: I8c4d93716b7682b42c8202e8b939ca2175775fdf Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/2175675 Reviewed-by: Manoj Gupta Tested-by: Jian Cai --- llvm_tools/git.py | 33 +++++++++------------------------ 1 file changed, 9 insertions(+), 24 deletions(-) (limited to 'llvm_tools/git.py') diff --git a/llvm_tools/git.py b/llvm_tools/git.py index 778d9695..f38d5e72 100755 --- a/llvm_tools/git.py +++ b/llvm_tools/git.py @@ -46,10 +46,9 @@ def CreateBranch(repo, branch): if not os.path.isdir(repo): raise ValueError('Invalid directory path provided: %s' % repo) - subprocess.check_output(['git', '-C', repo, 'reset', 'HEAD', '--hard'], - encoding='utf-8') + subprocess.check_output(['git', '-C', repo, 'reset', 'HEAD', '--hard']) - subprocess.check_output(['repo', 'start', branch], cwd=repo, encoding='utf-8') + subprocess.check_output(['repo', 'start', branch], cwd=repo) def DeleteBranch(repo, branch): @@ -66,14 +65,11 @@ def DeleteBranch(repo, branch): if not os.path.isdir(repo): raise ValueError('Invalid directory path provided: %s' % repo) - subprocess.check_output(['git', '-C', repo, 'checkout', 'cros/master'], - encoding='utf-8') + subprocess.check_output(['git', '-C', repo, 'checkout', 'cros/master']) - subprocess.check_output(['git', '-C', repo, 'reset', 'HEAD', '--hard'], - encoding='utf-8') + subprocess.check_output(['git', '-C', repo, 'reset', 'HEAD', '--hard']) - subprocess.check_output(['git', '-C', repo, 'branch', '-D', branch], - encoding='utf-8') + subprocess.check_output(['git', '-C', repo, 'branch', '-D', branch]) def UploadChanges(repo, branch, commit_messages): @@ -102,28 +98,17 @@ def UploadChanges(repo, branch, commit_messages): f.write('\n'.join(commit_messages)) f.flush() - subprocess.check_output(['git', 'commit', '-F', f.name], - cwd=repo, - encoding='utf-8') + subprocess.check_output(['git', 'commit', '-F', f.name], cwd=repo) # Upload the changes for review. - # Pylint currently doesn't lint things in py3 mode, and py2 didn't allow - # users to specify `encoding`s for Popen. Hence, pylint is "wrong" here. - # pylint: disable=unexpected-keyword-arg - # The CL URL is sent to 'stderr', so need to redirect 'stderr' to 'stdout'. - upload_changes_obj = subprocess.Popen( + out = subprocess.check_output( ['repo', 'upload', '--yes', '--ne', '--no-verify', '--br=%s' % branch], - cwd=repo, - stdout=subprocess.PIPE, stderr=subprocess.STDOUT, + cwd=repo, encoding='utf-8') - out, _ = upload_changes_obj.communicate() - - if upload_changes_obj.returncode: # Failed to upload changes. - print(out) - raise ValueError('Failed to upload changes for review') + print(out) found_url = re.search( r'https://chromium-review.googlesource.com/c/' -- cgit v1.2.3