2020-01-22 13:23:07 +01:00
|
|
|
#!/usr/bin/env python3
|
2019-10-18 13:59:58 +02:00
|
|
|
# Copyright 2019 Google LLC
|
|
|
|
#
|
|
|
|
# Licensed under the the Apache License v2.0 with LLVM Exceptions (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# https://llvm.org/LICENSE.txt
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
import argparse
|
2019-11-18 12:17:06 +01:00
|
|
|
import json
|
2019-10-18 13:59:58 +02:00
|
|
|
import os
|
|
|
|
import subprocess
|
2019-11-18 12:17:06 +01:00
|
|
|
import sys
|
2019-11-22 10:42:51 +01:00
|
|
|
from typing import List, Optional
|
2020-10-12 16:25:23 +02:00
|
|
|
|
|
|
|
import backoff
|
2019-10-18 13:59:58 +02:00
|
|
|
from phabricator import Phabricator
|
|
|
|
|
2020-02-03 13:35:13 +01:00
|
|
|
|
2019-11-22 10:42:51 +01:00
|
|
|
class ApplyPatch:
|
|
|
|
|
2020-02-03 13:35:13 +01:00
|
|
|
def __init__(self, comment_file_path: str, git_hash: str):
|
2019-11-22 10:42:51 +01:00
|
|
|
# TODO: turn os.environ parameter into command line arguments
|
|
|
|
# this would be much clearer and easier for testing
|
|
|
|
self.comment_file_path = comment_file_path
|
2020-10-12 16:25:23 +02:00
|
|
|
self.conduit_token = os.environ.get('CONDUIT_TOKEN') # type: Optional[str]
|
2019-11-22 10:42:51 +01:00
|
|
|
self.host = os.environ.get('PHABRICATOR_HOST') # type: Optional[str]
|
|
|
|
self._load_arcrc()
|
|
|
|
self.diff_id = os.environ['DIFF_ID'] # type: str
|
2020-10-12 16:25:23 +02:00
|
|
|
self.diff_json_path = os.environ['DIFF_JSON'] # type: str
|
2019-11-22 10:42:51 +01:00
|
|
|
if not self.host.endswith('/api/'):
|
|
|
|
self.host += '/api/'
|
|
|
|
self.phab = Phabricator(token=self.conduit_token, host=self.host)
|
2020-02-03 13:35:13 +01:00
|
|
|
self.git_hash = git_hash # type: Optional[str]
|
2019-11-22 10:42:51 +01:00
|
|
|
self.msg = [] # type: List[str]
|
|
|
|
|
|
|
|
def _load_arcrc(self):
|
|
|
|
"""Load arc configuration from file if not set."""
|
|
|
|
if self.conduit_token is not None or self.host is not None:
|
|
|
|
return
|
|
|
|
print('Loading configuration from ~/.arcrc file')
|
|
|
|
with open(os.path.expanduser('~/.arcrc'), 'r') as arcrc_file:
|
|
|
|
arcrc = json.load(arcrc_file)
|
|
|
|
# use the first host configured in the file
|
|
|
|
self.host = next(iter(arcrc['hosts']))
|
|
|
|
self.conduit_token = arcrc['hosts'][self.host]['token']
|
2019-10-18 13:59:58 +02:00
|
|
|
|
2020-10-12 16:25:23 +02:00
|
|
|
@backoff.on_exception(backoff.expo, Exception, max_tries=5, logger='', factor=3)
|
|
|
|
def update_interfaces(self):
|
|
|
|
self.phab.update_interfaces()
|
|
|
|
|
2019-11-22 10:42:51 +01:00
|
|
|
def run(self):
|
|
|
|
"""try to apply the patch from phabricator
|
|
|
|
"""
|
2020-10-12 16:25:23 +02:00
|
|
|
self.update_interfaces()
|
2019-10-18 13:59:58 +02:00
|
|
|
|
2019-11-22 10:42:51 +01:00
|
|
|
try:
|
2020-02-03 13:35:13 +01:00
|
|
|
if self.git_hash is None:
|
|
|
|
self._get_parent_hash()
|
|
|
|
else:
|
|
|
|
print('Use provided commit {}'.format(self.git_hash))
|
2019-11-22 10:42:51 +01:00
|
|
|
self._git_checkout()
|
|
|
|
self._apply_patch()
|
|
|
|
finally:
|
|
|
|
self._write_error_message()
|
2019-10-18 13:59:58 +02:00
|
|
|
|
2020-02-03 13:35:13 +01:00
|
|
|
def _get_parent_hash(self):
|
2019-11-22 10:42:51 +01:00
|
|
|
diff = self.phab.differential.getdiff(diff_id=self.diff_id)
|
|
|
|
# Keep a copy of the Phabricator answer for later usage in a json file
|
|
|
|
try:
|
2020-02-03 13:35:13 +01:00
|
|
|
with open(self.diff_json_path, 'w') as json_file:
|
2019-11-22 10:42:51 +01:00
|
|
|
json.dump(diff.response, json_file, sort_keys=True, indent=4)
|
2019-11-22 11:03:05 +01:00
|
|
|
print('Wrote diff details to "{}".'.format(self.diff_json_path))
|
2019-11-22 10:42:51 +01:00
|
|
|
except Exception:
|
|
|
|
print('WARNING: could not write build/diff.json log file')
|
|
|
|
self.git_hash = diff['sourceControlBaseRevision']
|
2019-10-18 13:59:58 +02:00
|
|
|
|
2020-10-12 16:25:23 +02:00
|
|
|
@backoff.on_exception(backoff.expo, Exception, max_tries=5, logger='', factor=3)
|
2019-11-22 10:42:51 +01:00
|
|
|
def _git_checkout(self):
|
|
|
|
try:
|
|
|
|
print('Checking out git hash {}'.format(self.git_hash))
|
2020-10-12 16:25:23 +02:00
|
|
|
subprocess.check_call('git reset --hard {}'.format(self.git_hash),
|
|
|
|
stdout=sys.stdout, stderr=sys.stderr, shell=True)
|
2019-11-22 10:42:51 +01:00
|
|
|
except subprocess.CalledProcessError:
|
2020-12-09 17:23:01 +01:00
|
|
|
print('WARNING: checkout of hash failed, using main branch instead.')
|
2019-11-22 10:42:51 +01:00
|
|
|
self.msg += [
|
|
|
|
'Could not check out parent git hash "{}". It was not found in '
|
|
|
|
'the repository. Did you configure the "Parent Revision" in '
|
|
|
|
'Phabricator properly? Trying to apply the patch to the '
|
2020-12-09 17:23:01 +01:00
|
|
|
'main branch instead...'.format(self.git_hash)]
|
|
|
|
subprocess.check_call('git checkout main', stdout=sys.stdout,
|
2020-10-12 16:25:23 +02:00
|
|
|
stderr=sys.stderr, shell=True)
|
2020-01-23 09:32:45 +01:00
|
|
|
subprocess.check_call('git show -s', stdout=sys.stdout,
|
|
|
|
stderr=sys.stderr, shell=True)
|
2019-11-22 10:42:51 +01:00
|
|
|
print('git checkout completed.')
|
2019-10-18 14:48:29 +02:00
|
|
|
|
2020-10-12 16:25:23 +02:00
|
|
|
@backoff.on_exception(backoff.expo, Exception, max_tries=5, logger='', factor=3)
|
2019-11-22 10:42:51 +01:00
|
|
|
def _apply_patch(self):
|
|
|
|
print('running arc patch...')
|
2020-10-12 16:25:23 +02:00
|
|
|
cmd = 'arc patch --force --nobranch --no-ansi --diff "{}" --nocommit ' \
|
|
|
|
'--conduit-token "{}" --conduit-uri "{}"'.format(
|
|
|
|
self.diff_id, self.conduit_token, self.host)
|
2019-11-22 10:42:51 +01:00
|
|
|
result = subprocess.run(cmd, capture_output=True, shell=True, text=True)
|
|
|
|
print(result.stdout + result.stderr)
|
2020-10-12 16:25:23 +02:00
|
|
|
if result.returncode != 0:
|
2019-11-22 10:42:51 +01:00
|
|
|
msg = (
|
|
|
|
'ERROR: arc patch failed with error code {}. '
|
|
|
|
'Check build log for details.'.format(result.returncode))
|
|
|
|
self.msg += [msg]
|
|
|
|
raise Exception(msg)
|
|
|
|
print('Patching completed.')
|
2019-10-18 13:59:58 +02:00
|
|
|
|
2019-11-22 10:42:51 +01:00
|
|
|
def _write_error_message(self):
|
|
|
|
"""Write the log message to a file."""
|
|
|
|
if self.comment_file_path is None:
|
|
|
|
return
|
2019-10-18 14:48:29 +02:00
|
|
|
|
2019-11-22 10:42:51 +01:00
|
|
|
if len(self.msg) == 0:
|
|
|
|
return
|
|
|
|
print('writing error message to {}'.format(self.comment_file_path))
|
|
|
|
with open(self.comment_file_path, 'a') as comment_file:
|
|
|
|
text = '\n\n'.join(self.msg)
|
|
|
|
comment_file.write(text)
|
2019-10-18 13:59:58 +02:00
|
|
|
|
2019-11-18 17:07:31 +01:00
|
|
|
|
2019-10-18 13:59:58 +02:00
|
|
|
if __name__ == "__main__":
|
2019-11-22 10:42:51 +01:00
|
|
|
parser = argparse.ArgumentParser(description='Apply Phabricator patch to working directory.')
|
|
|
|
parser.add_argument('--comment-file', type=str, dest='comment_file_path', default=None)
|
2020-02-03 13:35:13 +01:00
|
|
|
parser.add_argument('--commit', type=str, dest='commit', default=None, help='use explicitly specified base commit')
|
2019-11-22 10:42:51 +01:00
|
|
|
args = parser.parse_args()
|
2020-02-03 13:35:13 +01:00
|
|
|
patcher = ApplyPatch(args.comment_file_path, args.commit)
|
2019-11-22 10:42:51 +01:00
|
|
|
patcher.run()
|