Merge "repo_pull: Automatically extract gerrit instance from repo manifest" am: 9160fbc06f am: 4b1b426db2

Original change: https://android-review.googlesource.com/c/platform/development/+/1353903

Change-Id: If78c097df1649ea48a16dae2611934a45fc1bdf6
This commit is contained in:
Yo Chiang
2020-07-14 05:48:47 +00:00
committed by Automerger Merge Worker
4 changed files with 97 additions and 49 deletions

View File

@@ -25,6 +25,7 @@ import base64
import json
import os
import sys
import xml.dom.minidom
try:
from urllib.error import HTTPError # PY3
@@ -47,6 +48,47 @@ except ImportError:
from urlparse import urlparse # PY2
try:
from subprocess import PIPE, run # PY3.5
except ImportError:
from subprocess import CalledProcessError, PIPE, Popen
class CompletedProcess(object):
"""Process execution result returned by subprocess.run()."""
# pylint: disable=too-few-public-methods
def __init__(self, args, returncode, stdout, stderr):
self.args = args
self.returncode = returncode
self.stdout = stdout
self.stderr = stderr
def run(*args, **kwargs):
"""Run a command with subprocess.Popen() and redirect input/output."""
check = kwargs.pop('check', False)
try:
stdin = kwargs.pop('input')
assert 'stdin' not in kwargs
kwargs['stdin'] = PIPE
except KeyError:
stdin = None
proc = Popen(*args, **kwargs)
try:
stdout, stderr = proc.communicate(stdin)
except:
proc.kill()
proc.wait()
raise
returncode = proc.wait()
if check and returncode:
raise CalledProcessError(returncode, args, stdout)
return CompletedProcess(args, returncode, stdout, stderr)
def load_auth_credentials_from_file(cookie_file):
"""Load credentials from an opened .gitcookies file."""
credentials = {}
@@ -285,14 +327,28 @@ def get_patch(url_opener, gerrit_url, change_id, revision_id='current'):
finally:
response_file.close()
def find_gerrit_name():
"""Find the gerrit instance specified in the default remote."""
manifest_cmd = ['repo', 'manifest']
raw_manifest_xml = run(manifest_cmd, stdout=PIPE, check=True).stdout
manifest_xml = xml.dom.minidom.parseString(raw_manifest_xml)
default_remote = manifest_xml.getElementsByTagName('default')[0]
default_remote_name = default_remote.getAttribute('remote')
for remote in manifest_xml.getElementsByTagName('remote'):
name = remote.getAttribute('name')
review = remote.getAttribute('review')
if review and name == default_remote_name:
return review
raise ValueError('cannot find gerrit URL from manifest')
def _parse_args():
"""Parse command line options."""
parser = argparse.ArgumentParser()
parser.add_argument('query', help='Change list query string')
parser.add_argument('-g', '--gerrit', required=True,
help='Gerrit review URL')
parser.add_argument('-g', '--gerrit', help='Gerrit review URL')
parser.add_argument('--gitcookies',
default=os.path.expanduser('~/.gitcookies'),
@@ -307,6 +363,13 @@ def main():
"""Main function"""
args = _parse_args()
if not args.gerrit:
try:
args.gerrit = find_gerrit_name()
except:
print('gerrit instance not found, use [-g GERRIT]')
sys.exit(1)
# Query change lists
url_opener = create_url_opener_from_args(args)
change_lists = query_change_lists(

View File

@@ -24,16 +24,16 @@ from __future__ import print_function
import argparse
import os
from gerrit import create_url_opener_from_args, query_change_lists, get_patch
from gerrit import (
create_url_opener_from_args, find_gerrit_name, query_change_lists, get_patch
)
def _parse_args():
"""Parse command line options."""
parser = argparse.ArgumentParser()
parser.add_argument('query', help='Change list query string')
parser.add_argument('-g', '--gerrit', required=True,
help='Gerrit review URL')
parser.add_argument('-g', '--gerrit', help='Gerrit review URL')
parser.add_argument('--gitcookies',
default=os.path.expanduser('~/.gitcookies'),
@@ -48,6 +48,13 @@ def main():
"""Main function"""
args = _parse_args()
if not args.gerrit:
try:
args.gerrit = find_gerrit_name()
except:
print('gerrit instance not found, use [-g GERRIT]')
sys.exit(1)
# Query change lists
url_opener = create_url_opener_from_args(args)
change_lists = query_change_lists(

View File

@@ -31,7 +31,10 @@ import re
import sys
import xml.dom.minidom
from gerrit import create_url_opener_from_args, query_change_lists
from gerrit import (
create_url_opener_from_args, find_gerrit_name, query_change_lists, run
)
from subprocess import PIPE
try:
# pylint: disable=redefined-builtin
@@ -50,46 +53,6 @@ except ImportError:
"""Quote a string if it contains special characters."""
return txt if _SHELL_SIMPLE_PATTERN.match(txt) else json.dumps(txt)
try:
from subprocess import PIPE, run # PY3.5
except ImportError:
from subprocess import CalledProcessError, PIPE, Popen
class CompletedProcess(object):
"""Process execution result returned by subprocess.run()."""
# pylint: disable=too-few-public-methods
def __init__(self, args, returncode, stdout, stderr):
self.args = args
self.returncode = returncode
self.stdout = stdout
self.stderr = stderr
def run(*args, **kwargs):
"""Run a command with subprocess.Popen() and redirect input/output."""
check = kwargs.pop('check', False)
try:
stdin = kwargs.pop('input')
assert 'stdin' not in kwargs
kwargs['stdin'] = PIPE
except KeyError:
stdin = None
proc = Popen(*args, **kwargs)
try:
stdout, stderr = proc.communicate(stdin)
except:
proc.kill()
proc.wait()
raise
returncode = proc.wait()
if check and returncode:
raise CalledProcessError(returncode, args, stdout)
return CompletedProcess(args, returncode, stdout, stderr)
if bytes is str:
def write_bytes(data, file): # PY2
@@ -404,8 +367,7 @@ def _parse_args():
help='Commands')
parser.add_argument('query', help='Change list query string')
parser.add_argument('-g', '--gerrit', required=True,
help='Gerrit review URL')
parser.add_argument('-g', '--gerrit', help='Gerrit review URL')
parser.add_argument('--gitcookies',
default=os.path.expanduser('~/.gitcookies'),
@@ -451,6 +413,14 @@ def _get_local_branch_name_from_args(args):
def main():
"""Main function"""
args = _parse_args()
if not args.gerrit:
try:
args.gerrit = find_gerrit_name()
except:
print('gerrit instance not found, use [-g GERRIT]')
sys.exit(1)
if args.command == 'json':
_main_json(args)
elif args.command == 'bash':

View File

@@ -187,6 +187,14 @@ def main():
# Parse and check the command line options
args = _parse_args()
if not args.gerrit:
try:
args.gerrit = find_gerrit_name()
except:
print('gerrit instance not found, use [-g GERRIT]')
sys.exit(1)
if not _has_task(args):
print('error: Either --label, --message, --submit, --abandon, '
'--add-hashtag, --remove-hashtag, --set-topic, --delete-topic, '