1
0
Fork 0
llvm-premerge-checks/scripts/phabtalk/apply_patch.py

72 lines
2.6 KiB
Python
Raw Normal View History

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-11-18 11:46:57 +01:00
print('Applying patch for Phabricator diff {} for build {}'.format(diff_id,phid))
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()
_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
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)
# Keep a copy of the Phabricator answer for later usage in a json file
with open(diff_json_path,'w') as json_file:
2019-11-18 12:06:22 +01:00
json.dump(diff.response, json_file, sort_keys=True, indent=4)
2019-10-18 13:59:58 +02:00
return diff['sourceControlBaseRevision']
2019-10-18 14:48:29 +02:00
def _git_checkout(git_hash: str):
2019-10-21 22:41:37 +02:00
try:
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:
print('WARNING: checkout of hash failed, using master branch instead.')
subprocess.check_call('git checkout master', stdout=sys.stdout, stderr=sys.stderr,
shell=True)
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):
print('running arc patch...')
2019-10-18 14:48:29 +02:00
cmd = 'arc patch --nobranch --no-ansi --diff "{}" --nocommit '\
2019-10-18 13:59:58 +02:00
'--conduit-token "{}" --conduit-uri "{}"'.format(
diff_id, conduit_token, host )
try:
subprocess.check_call(cmd, stdout=sys.stdout, stderr=sys.stderr, shell=True)
except subprocess.CalledProcessError:
print('arc patch failed!')
raise
print('Patching completed.')
2019-10-18 13:59:58 +02:00
if __name__ == "__main__":
main()