2019-10-18 13:59:58 +02:00
|
|
|
#!/bin/env python3
|
|
|
|
# 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-10-18 13:59:58 +02:00
|
|
|
from phabricator import Phabricator
|
|
|
|
|
|
|
|
def main():
|
|
|
|
diff_id = os.environ['DIFF_ID']
|
|
|
|
phid = os.environ['PHID']
|
2019-10-18 14:57:28 +02:00
|
|
|
conduit_token = os.environ['CONDUIT_TOKEN']
|
|
|
|
host = os.environ['PHABRICATOR_HOST']
|
2019-11-18 11:51:10 +01:00
|
|
|
diff_json_path = os.environ['DIFF_JSON']
|
2019-10-18 14:57:28 +02:00
|
|
|
phab = Phabricator(token=conduit_token, host=host+'/api/')
|
2019-10-18 13:59:58 +02:00
|
|
|
phab.update_interfaces()
|
|
|
|
|
2019-11-18 11:45:08 +01:00
|
|
|
_git_checkout(_get_parent_hash(diff_id, phab, diff_json_path))
|
2019-10-18 14:57:28 +02:00
|
|
|
_apply_patch(diff_id, conduit_token, host)
|
2019-10-18 13:59:58 +02:00
|
|
|
|
|
|
|
|
2019-11-18 11:45:08 +01:00
|
|
|
def _get_parent_hash(diff_id: str, phab: Phabricator, diff_json_path: str) -> str:
|
2019-10-18 13:59:58 +02:00
|
|
|
diff = phab.differential.getdiff(diff_id=diff_id)
|
2019-11-18 11:45:08 +01:00
|
|
|
# Keep a copy of the Phabricator answer for later usage in a json file
|
2019-11-18 17:19:13 +01:00
|
|
|
try:
|
|
|
|
with open(diff_json_path,'w') as json_file:
|
|
|
|
json.dump(diff.response, json_file, sort_keys=True, indent=4)
|
|
|
|
except Exception:
|
|
|
|
print('WARNING: could not write build/diff.json log file')
|
2019-10-18 13:59:58 +02:00
|
|
|
return diff['sourceControlBaseRevision']
|
|
|
|
|
2019-10-18 14:48:29 +02:00
|
|
|
|
2019-11-18 11:45:08 +01:00
|
|
|
def _git_checkout(git_hash: str):
|
2019-10-21 22:41:37 +02:00
|
|
|
try:
|
2019-11-18 11:45:08 +01:00
|
|
|
print('Checking out git hash {}'.format(git_hash))
|
|
|
|
subprocess.check_call('git reset --hard {}'.format(git_hash), stdout=sys.stdout,
|
|
|
|
stderr=sys.stderr, shell=True)
|
2019-10-23 15:39:24 +02:00
|
|
|
except subprocess.CalledProcessError:
|
2019-11-18 11:45:08 +01:00
|
|
|
print('WARNING: checkout of hash failed, using master branch instead.')
|
|
|
|
subprocess.check_call('git checkout master', stdout=sys.stdout, stderr=sys.stderr,
|
2019-11-18 16:36:23 +01:00
|
|
|
shell=True)
|
2019-11-18 11:45:08 +01:00
|
|
|
print('git checkout completed.')
|
2019-10-18 13:59:58 +02:00
|
|
|
|
2019-10-18 14:48:29 +02:00
|
|
|
|
2019-10-18 13:59:58 +02:00
|
|
|
def _apply_patch(diff_id: str, conduit_token: str, host: str):
|
2019-11-18 11:45:08 +01:00
|
|
|
print('running arc patch...')
|
2019-11-18 17:02:24 +01:00
|
|
|
cmd = 'arc patch --force --nobranch --no-ansi --diff "{}" --nocommit '\
|
2019-10-18 13:59:58 +02:00
|
|
|
'--conduit-token "{}" --conduit-uri "{}"'.format(
|
|
|
|
diff_id, conduit_token, host )
|
2019-11-18 16:38:55 +01:00
|
|
|
result = subprocess.run(cmd, capture_output=True, shell=True, text=True)
|
2019-11-18 17:07:31 +01:00
|
|
|
print(result.stdout + result.stderr)
|
2019-11-18 16:36:23 +01:00
|
|
|
if result.returncode != 0:
|
2019-11-18 17:07:31 +01:00
|
|
|
print('ERROR: arc patch failed with error code {}.'.format(result.returncode))
|
2019-11-21 15:00:48 +01:00
|
|
|
raise Exception('ERROR: arc patch failed')
|
2019-11-18 11:45:08 +01:00
|
|
|
print('Patching completed.')
|
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__":
|
|
|
|
main()
|
|
|
|
|