1cb93a386Sopenharmony_ci#!/usr/bin/env python 2cb93a386Sopenharmony_ci# Copyright (c) 2017 The Chromium Authors. All rights reserved. 3cb93a386Sopenharmony_ci# Use of this source code is governed by a BSD-style license that can be 4cb93a386Sopenharmony_ci# found in the LICENSE file. 5cb93a386Sopenharmony_ci 6cb93a386Sopenharmony_ciREADME = """ 7cb93a386Sopenharmony_ciAutomatically add or remove a specific legacy flag to multiple Skia client repos. 8cb93a386Sopenharmony_ci 9cb93a386Sopenharmony_ciThis would only work on Google desktop. 10cb93a386Sopenharmony_ci 11cb93a386Sopenharmony_ciExample usage: 12cb93a386Sopenharmony_ci $ python toggle_legacy_flag.py SK_SUPPORT_LEGACY_SOMETHING \\ 13cb93a386Sopenharmony_ci -a /data/android -c ~/chromium/src -g legacyflag 14cb93a386Sopenharmony_ci 15cb93a386Sopenharmony_ciIf you only need to add the flag to one repo, for example, Android, please give 16cb93a386Sopenharmony_cionly -a (--android-dir) argument: 17cb93a386Sopenharmony_ci $ python toggle_legacy_flag.py SK_SUPPORT_LEGACY_SOMETHING -a /data/android 18cb93a386Sopenharmony_ci 19cb93a386Sopenharmony_ci""" 20cb93a386Sopenharmony_ci 21cb93a386Sopenharmony_cifrom __future__ import print_function 22cb93a386Sopenharmony_ciimport os, sys 23cb93a386Sopenharmony_ciimport argparse 24cb93a386Sopenharmony_ciimport subprocess 25cb93a386Sopenharmony_ciimport getpass 26cb93a386Sopenharmony_cifrom random import randint 27cb93a386Sopenharmony_ci 28cb93a386Sopenharmony_ci 29cb93a386Sopenharmony_ciANDROID_TOOLS_DIR = os.path.join( 30cb93a386Sopenharmony_ci os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 31cb93a386Sopenharmony_ci 'android') 32cb93a386Sopenharmony_ci 33cb93a386Sopenharmony_ci 34cb93a386Sopenharmony_cidef toggle_android(args): 35cb93a386Sopenharmony_ci sys.path.append(ANDROID_TOOLS_DIR) 36cb93a386Sopenharmony_ci import upload_to_android 37cb93a386Sopenharmony_ci 38cb93a386Sopenharmony_ci modifier = upload_to_android.AndroidLegacyFlagModifier(args.flag) 39cb93a386Sopenharmony_ci upload_to_android.upload_to_android(args.android_dir, modifier) 40cb93a386Sopenharmony_ci 41cb93a386Sopenharmony_ci 42cb93a386Sopenharmony_cidef toggle_chromium(args): 43cb93a386Sopenharmony_ci os.chdir(args.chromium_dir) 44cb93a386Sopenharmony_ci 45cb93a386Sopenharmony_ci branch = subprocess.check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD']) 46cb93a386Sopenharmony_ci branch = branch.strip() 47cb93a386Sopenharmony_ci 48cb93a386Sopenharmony_ci EXPECTED_STASH_OUT = "No local changes to save" 49cb93a386Sopenharmony_ci stash_output = subprocess.check_output(['git', 'stash']).strip() 50cb93a386Sopenharmony_ci 51cb93a386Sopenharmony_ci if branch != "master" or stash_output != EXPECTED_STASH_OUT: 52cb93a386Sopenharmony_ci print ("Please checkout a clean master branch at your chromium repo (%s) " 53cb93a386Sopenharmony_ci "before running this script") % args.chromium_dir 54cb93a386Sopenharmony_ci if stash_output != EXPECTED_STASH_OUT: 55cb93a386Sopenharmony_ci subprocess.check_call(['git', 'stash', 'pop']) 56cb93a386Sopenharmony_ci exit(1) 57cb93a386Sopenharmony_ci 58cb93a386Sopenharmony_ci # Update the repository to avoid conflicts 59cb93a386Sopenharmony_ci subprocess.check_call(['git', 'pull']) 60cb93a386Sopenharmony_ci subprocess.check_call(['gclient', 'sync']); 61cb93a386Sopenharmony_ci 62cb93a386Sopenharmony_ci # Use random number to avoid branch name collision. 63cb93a386Sopenharmony_ci # We'll delete the branch in the end. 64cb93a386Sopenharmony_ci random = randint(1, 10000) 65cb93a386Sopenharmony_ci subprocess.check_call(['git', 'checkout', '-b', 'legacyflag_%d' % random]) 66cb93a386Sopenharmony_ci 67cb93a386Sopenharmony_ci try: 68cb93a386Sopenharmony_ci config_file = os.path.join('skia', 'config', 'SkUserConfig.h') 69cb93a386Sopenharmony_ci with open(config_file) as f: 70cb93a386Sopenharmony_ci lines = f.readlines() 71cb93a386Sopenharmony_ci 72cb93a386Sopenharmony_ci flag_line = "#define %s\n" % args.flag 73cb93a386Sopenharmony_ci if flag_line in lines: 74cb93a386Sopenharmony_ci index = lines.index(flag_line) 75cb93a386Sopenharmony_ci del lines[index-1 : index +2] 76cb93a386Sopenharmony_ci verb = "Remove" 77cb93a386Sopenharmony_ci else: 78cb93a386Sopenharmony_ci separator = ( 79cb93a386Sopenharmony_ci "/////////////////////////" 80cb93a386Sopenharmony_ci " Imported from BUILD.gn and skia_common.gypi\n") 81cb93a386Sopenharmony_ci content = ("#ifndef {0}\n" 82cb93a386Sopenharmony_ci "#define {0}\n" 83cb93a386Sopenharmony_ci "#endif\n\n").format(args.flag) 84cb93a386Sopenharmony_ci lines.insert(lines.index(separator), content) 85cb93a386Sopenharmony_ci verb = "Add" 86cb93a386Sopenharmony_ci 87cb93a386Sopenharmony_ci with open(config_file, 'w') as f: 88cb93a386Sopenharmony_ci for line in lines: 89cb93a386Sopenharmony_ci f.write(line) 90cb93a386Sopenharmony_ci 91cb93a386Sopenharmony_ci message = "%s %s" % (verb, args.flag) 92cb93a386Sopenharmony_ci 93cb93a386Sopenharmony_ci subprocess.check_call('git commit -a -m "%s"' % message, shell=True) 94cb93a386Sopenharmony_ci subprocess.check_call('git cl upload -m "%s" -f' % message, 95cb93a386Sopenharmony_ci shell=True) 96cb93a386Sopenharmony_ci finally: 97cb93a386Sopenharmony_ci subprocess.check_call(['git', 'checkout', 'master']) 98cb93a386Sopenharmony_ci subprocess.check_call(['git', 'branch', '-D', 'legacyflag_%d' % random]) 99cb93a386Sopenharmony_ci 100cb93a386Sopenharmony_ci 101cb93a386Sopenharmony_cidef toggle_google3(args): 102cb93a386Sopenharmony_ci G3_SCRIPT_DIR = os.path.expanduser("~/skia-g3/scripts") 103cb93a386Sopenharmony_ci if not os.path.isdir(G3_SCRIPT_DIR): 104cb93a386Sopenharmony_ci print ("Google3 directory unavailable.\n" 105cb93a386Sopenharmony_ci "Please see " 106cb93a386Sopenharmony_ci "https://sites.google.com/a/google.com/skia/rebaseline#g3_flag " 107cb93a386Sopenharmony_ci "for Google3 setup.") 108cb93a386Sopenharmony_ci exit(1) 109cb93a386Sopenharmony_ci sys.path.append(G3_SCRIPT_DIR) 110cb93a386Sopenharmony_ci import citc_flag 111cb93a386Sopenharmony_ci 112cb93a386Sopenharmony_ci citc_flag.toggle_google3(args.google3, args.flag) 113cb93a386Sopenharmony_ci 114cb93a386Sopenharmony_ci 115cb93a386Sopenharmony_cidef main(): 116cb93a386Sopenharmony_ci if len(sys.argv) <= 1 or sys.argv[1] == '-h' or sys.argv[1] == '--help': 117cb93a386Sopenharmony_ci print(README) 118cb93a386Sopenharmony_ci 119cb93a386Sopenharmony_ci parser = argparse.ArgumentParser() 120cb93a386Sopenharmony_ci parser.add_argument( 121cb93a386Sopenharmony_ci '--android-dir', '-a', required=False, 122cb93a386Sopenharmony_ci help='Directory where an Android checkout will be created (if it does ' 123cb93a386Sopenharmony_ci 'not already exist). Note: ~1GB space will be used.') 124cb93a386Sopenharmony_ci parser.add_argument( 125cb93a386Sopenharmony_ci '--chromium-dir', '-c', required=False, 126cb93a386Sopenharmony_ci help='Directory of an EXISTING Chromium checkout (e.g., ~/chromium/src)') 127cb93a386Sopenharmony_ci parser.add_argument( 128cb93a386Sopenharmony_ci '--google3', '-g', required=False, 129cb93a386Sopenharmony_ci help='Google3 workspace to be created (if it does not already exist).') 130cb93a386Sopenharmony_ci parser.add_argument('flag', type=str, help='legacy flag name') 131cb93a386Sopenharmony_ci 132cb93a386Sopenharmony_ci args = parser.parse_args() 133cb93a386Sopenharmony_ci 134cb93a386Sopenharmony_ci if not args.android_dir and not args.chromium_dir and not args.google3: 135cb93a386Sopenharmony_ci print(""" 136cb93a386Sopenharmony_ciNothing to do. Please give me at least one of these three arguments: 137cb93a386Sopenharmony_ci -a (--android-dir) 138cb93a386Sopenharmony_ci -c (--chromium-dir) 139cb93a386Sopenharmony_ci -g (--google3) 140cb93a386Sopenharmony_ci""") 141cb93a386Sopenharmony_ci exit(1) 142cb93a386Sopenharmony_ci 143cb93a386Sopenharmony_ci end_message = "CLs generated. Now go review and land them:\n" 144cb93a386Sopenharmony_ci if args.chromium_dir: 145cb93a386Sopenharmony_ci args.chromium_dir = os.path.expanduser(args.chromium_dir) 146cb93a386Sopenharmony_ci toggle_chromium(args) 147cb93a386Sopenharmony_ci end_message += " * https://chromium-review.googlesource.com\n" 148cb93a386Sopenharmony_ci if args.google3: 149cb93a386Sopenharmony_ci toggle_google3(args) 150cb93a386Sopenharmony_ci end_message += " * http://goto.google.com/cl\n" 151cb93a386Sopenharmony_ci if args.android_dir: 152cb93a386Sopenharmony_ci args.android_dir = os.path.expanduser(args.android_dir) 153cb93a386Sopenharmony_ci toggle_android(args) 154cb93a386Sopenharmony_ci end_message += " * http://goto.google.com/androidcl\n" 155cb93a386Sopenharmony_ci 156cb93a386Sopenharmony_ci print(end_message) 157cb93a386Sopenharmony_ci 158cb93a386Sopenharmony_ci 159cb93a386Sopenharmony_ciif __name__ == '__main__': 160cb93a386Sopenharmony_ci main() 161