From ee6784d268567bb82a47b00ab7f35802f878c1a9 Mon Sep 17 00:00:00 2001 From: Yuqian Li Date: Tue, 31 Oct 2017 10:14:25 -0400 Subject: Reuse more upload_to_android and allow removing flag on Android Bug: skia: Change-Id: I8c84b63d01ead32bf4aa71550f6fa84ddfcf480f Reviewed-on: https://skia-review.googlesource.com/63743 Reviewed-by: Ravi Mistry Commit-Queue: Yuqian Li --- tools/android/upload_to_android.py | 89 ++++++++++++++++++++++++++++++------- tools/rebaseline/add_legacy_flag.py | 30 +------------ 2 files changed, 76 insertions(+), 43 deletions(-) (limited to 'tools') diff --git a/tools/android/upload_to_android.py b/tools/android/upload_to_android.py index 76e4640fb0..dba0c1692c 100644 --- a/tools/android/upload_to_android.py +++ b/tools/android/upload_to_android.py @@ -108,26 +108,32 @@ About to run repo init. If it hangs asking you to run glogin then please: return repo_binary -def upload_to_android(work_dir, change_num, debug): - repo_binary = init_work_dir(work_dir) - # Create repo branch. - subprocess.check_call('%s start %s .' % (repo_binary, REPO_BRANCH_NAME), - shell=True) - try: - change_details = get_change_details(change_num) +class Modifier: + def modify(self): + raise NotImplementedError + def get_user_msg(self): + raise NotImplementedError + +class FetchModifier(Modifier): + def __init__(self, change_num, debug): + self.change_num = change_num + self.debug = debug + + def modify(self): # Download and cherry-pick the patch. + change_details = get_change_details(self.change_num) latest_patchset = len(change_details['revisions']) - mod = int(change_num) % 100 + mod = int(self.change_num) % 100 download_ref = 'refs/changes/%s/%s/%s' % ( - str(mod).zfill(2), change_num, latest_patchset) + str(mod).zfill(2), self.change_num, latest_patchset) subprocess.check_call( 'git fetch https://skia.googlesource.com/skia %s' % download_ref, shell=True) subprocess.check_call('git cherry-pick FETCH_HEAD', shell=True) - if debug: + if self.debug: # Add SK_DEBUG to SkUserConfig.h. with open(SK_USER_CONFIG_PATH, 'a') as f: f.write('#ifndef SK_DEBUG\n') @@ -145,17 +151,70 @@ def upload_to_android(work_dir, change_num, debug): '[DO ' + 'NOT ' + 'SUBMIT] %s\n\n' 'Test: Presubmit checks will test this change.' % ( original_commit_message)) + subprocess.check_call('git commit --amend -m "%s"' % new_commit_message, shell=True) - # Upload to Android Gerrit. - subprocess.check_call('%s upload --verify' % repo_binary, shell=True) - - print """ + def get_user_msg(self): + return """ Open the above URL and trigger TH by checking 'Presubmit-Ready'. You can download binaries (if required) from the TH link after it completes. """ + + +# Add a legacy flag if it doesn't exist, or remove it if it exists. +class AndroidLegacyFlagModifier(Modifier): + def __init__(self, flag): + self.flag = flag + self.verb = "Unknown" + + def modify(self): + flag_line = " #define %s\n" % self.flag + + config_file = os.path.join('include', 'config', 'SkUserConfigManual.h') + + with open(config_file) as f: + lines = f.readlines() + + if flag_line not in lines: + lines.insert( + lines.index("#endif // SkUserConfigManual_DEFINED\n"), flag_line) + verb = "Add" + else: + lines.remove(flag_line) + verb = "Remove" + + with open(config_file, 'w') as f: + for line in lines: + f.write(line) + + subprocess.check_call('git add %s' % config_file, shell=True) + message = '%s %s\n\nTest: Presubmit checks will test this change.' % ( + verb, self.flag) + + subprocess.check_call('git commit -m "%s"' % message, shell=True) + + def get_user_msg(self): + return """ + + Please open the above URL to review and land the change. +""" + + +def upload_to_android(work_dir, modifier): + repo_binary = init_work_dir(work_dir) + + # Create repo branch. + subprocess.check_call('%s start %s .' % (repo_binary, REPO_BRANCH_NAME), + shell=True) + try: + modifier.modify() + + # Upload to Android Gerrit. + subprocess.check_call('%s upload --verify' % repo_binary, shell=True) + + print modifier.get_user_msg() finally: # Abandon repo branch. subprocess.call('%s abandon %s' % (repo_binary, REPO_BRANCH_NAME), @@ -176,7 +235,7 @@ def main(): '--debug', '-d', action='store_true', default=False, help='Adds SK_DEBUG to SkUserConfig.h.') args = parser.parse_args() - upload_to_android(args.work_dir, args.change_num, args.debug) + upload_to_android(args.work_dir, FetchModifier(args.change_num, args.debug)) if __name__ == '__main__': diff --git a/tools/rebaseline/add_legacy_flag.py b/tools/rebaseline/add_legacy_flag.py index 0775d8ac5d..8a99e517b6 100644 --- a/tools/rebaseline/add_legacy_flag.py +++ b/tools/rebaseline/add_legacy_flag.py @@ -43,37 +43,11 @@ def insert_at(filename, pattern, offset, content): def add_to_android(args): - REPO_BRANCH_NAME = "flag" sys.path.append(ANDROID_TOOLS_DIR) import upload_to_android - repo_binary = upload_to_android.init_work_dir(args.android_dir); - - # Create repo branch. - subprocess.check_call('%s start %s .' % (repo_binary, REPO_BRANCH_NAME), - shell=True) - - try: - # Add flag to SkUserConfigManual.h. - config_file = os.path.join('include', 'config', 'SkUserConfigManual.h') - - insert_at(config_file, - "#endif // SkUserConfigManual_DEFINED\n", - 0, - " #define %s\n" % args.flag) - - subprocess.check_call('git add %s' % config_file, shell=True) - - message = ('Add %s\n\n' - 'Test: Presubmit checks will test this change.' % args.flag) - - subprocess.check_call('git commit -m "%s"' % message, shell=True) - - # Upload to Android Gerrit. - subprocess.check_call('%s upload --verify' % repo_binary, shell=True) - finally: - # Remove repo branch - subprocess.check_call('%s abandon flag' % repo_binary, shell=True) + modifier = upload_to_android.AndroidLegacyFlagModifier(args.flag) + upload_to_android.upload_to_android(args.android_dir, modifier) def add_to_chromium(args): -- cgit v1.2.3