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