blob: 80edeb2bc460fe494c31d7fdd283752c22e5088a [file] [log] [blame]
Yuqian Li33e2fad2017-10-24 14:42:57 -04001#!/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 = """
Yuqian Li5e9dfdb2017-11-10 11:58:01 -05007Automatically add or remove a specific legacy flag to multiple Skia client repos.
Yuqian Li33e2fad2017-10-24 14:42:57 -04008
9This would only work on Google desktop.
10
11Example usage:
Yuqian Li5e9dfdb2017-11-10 11:58:01 -050012 $ python toggle_legacy_flag.py SK_SUPPORT_LEGACY_SOMETHING \\
Yuqian Li33e2fad2017-10-24 14:42:57 -040013 -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:
Yuqian Li5e9dfdb2017-11-10 11:58:01 -050017 $ python toggle_legacy_flag.py SK_SUPPORT_LEGACY_SOMETHING -a /data/android
Yuqian Li33e2fad2017-10-24 14:42:57 -040018
19"""
20
21import os, sys
22import argparse
23import subprocess
24import getpass
25from random import randint
26
27
28ANDROID_TOOLS_DIR = os.path.join(
29 os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
30 'android')
31
32
Yuqian Libb4b4892017-11-08 13:00:08 -050033def toggle_android(args):
Yuqian Li33e2fad2017-10-24 14:42:57 -040034 sys.path.append(ANDROID_TOOLS_DIR)
35 import upload_to_android
36
Yuqian Liee6784d2017-10-31 10:14:25 -040037 modifier = upload_to_android.AndroidLegacyFlagModifier(args.flag)
38 upload_to_android.upload_to_android(args.android_dir, modifier)
Yuqian Li33e2fad2017-10-24 14:42:57 -040039
40
Yuqian Libb4b4892017-11-08 13:00:08 -050041def toggle_chromium(args):
Yuqian Li33e2fad2017-10-24 14:42:57 -040042 os.chdir(args.chromium_dir)
43
44 branch = subprocess.check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD'])
45 branch = branch.strip()
46
47 EXPECTED_STASH_OUT = "No local changes to save"
48 stash_output = subprocess.check_output(['git', 'stash']).strip()
49
50 if branch != "master" or stash_output != EXPECTED_STASH_OUT:
51 print ("Please checkout a clean master branch at your chromium repo (%s) "
52 "before running this script") % args.chromium_dir
53 if stash_output != EXPECTED_STASH_OUT:
54 subprocess.check_call(['git', 'stash', 'pop'])
55 exit(1)
56
Yuqian Li427293c2017-11-06 17:46:40 -050057 # Update the repository to avoid conflicts
58 subprocess.check_call(['git', 'pull'])
59 subprocess.check_call(['gclient', 'sync']);
60
Yuqian Li33e2fad2017-10-24 14:42:57 -040061 # Use random number to avoid branch name collision.
62 # We'll delete the branch in the end.
63 random = randint(1, 10000)
64 subprocess.check_call(['git', 'checkout', '-b', 'legacyflag_%d' % random])
65
66 try:
67 config_file = os.path.join('skia', 'config', 'SkUserConfig.h')
Yuqian Libb4b4892017-11-08 13:00:08 -050068 with open(config_file) as f:
69 lines = f.readlines()
Yuqian Li33e2fad2017-10-24 14:42:57 -040070
Yuqian Libb4b4892017-11-08 13:00:08 -050071 flag_line = "#define %s\n" % args.flag
72 if flag_line in lines:
73 index = lines.index(flag_line)
74 del lines[index-1 : index +2]
75 verb = "Remove"
76 else:
77 separator = (
78 "/////////////////////////"
79 " Imported from BUILD.gn and skia_common.gypi\n")
80 content = ("#ifndef {0}\n"
81 "#define {0}\n"
82 "#endif\n\n").format(args.flag)
83 lines.insert(lines.index(separator), content)
84 verb = "Add"
85
86 with open(config_file, 'w') as f:
87 for line in lines:
88 f.write(line)
89
90 message = "%s %s" % (verb, args.flag)
91
92 subprocess.check_call('git commit -a -m "%s"' % message, shell=True)
93 subprocess.check_call('git cl upload -m "%s" -f' % message,
Yuqian Li33e2fad2017-10-24 14:42:57 -040094 shell=True)
95 finally:
96 subprocess.check_call(['git', 'checkout', 'master'])
97 subprocess.check_call(['git', 'branch', '-D', 'legacyflag_%d' % random])
98
99
Yuqian Libb4b4892017-11-08 13:00:08 -0500100def toggle_google3(args):
Yuqian Li33e2fad2017-10-24 14:42:57 -0400101 G3_SCRIPT_DIR = os.path.expanduser("~/skia-g3/scripts")
102 if not os.path.isdir(G3_SCRIPT_DIR):
103 print ("Google3 directory unavailable.\n"
104 "Please see "
105 "https://sites.google.com/a/google.com/skia/rebaseline#g3_flag "
106 "for Google3 setup.")
107 exit(1)
108 sys.path.append(G3_SCRIPT_DIR)
109 import citc_flag
110
Yuqian Libb4b4892017-11-08 13:00:08 -0500111 citc_flag.toggle_google3(args.google3, args.flag)
Yuqian Li33e2fad2017-10-24 14:42:57 -0400112
113
114def main():
115 if len(sys.argv) <= 1 or sys.argv[1] == '-h' or sys.argv[1] == '--help':
116 print README
117
118 parser = argparse.ArgumentParser()
119 parser.add_argument(
120 '--android-dir', '-a', required=False,
121 help='Directory where an Android checkout will be created (if it does '
122 'not already exist). Note: ~1GB space will be used.')
123 parser.add_argument(
124 '--chromium-dir', '-c', required=False,
125 help='Directory of an EXISTING Chromium checkout (e.g., ~/chromium/src)')
126 parser.add_argument(
127 '--google3', '-g', required=False,
128 help='Google3 workspace to be created (if it does not already exist).')
129 parser.add_argument('flag', type=str, help='legacy flag name')
130
131 args = parser.parse_args()
132
133 if not args.android_dir and not args.chromium_dir and not args.google3:
134 print """
135Nothing to do. Please give me at least one of these three arguments:
136 -a (--android-dir)
137 -c (--chromium-dir)
138 -g (--google3)
139"""
140 exit(1)
141
142 end_message = "CLs generated. Now go review and land them:\n"
143 if args.chromium_dir:
144 args.chromium_dir = os.path.expanduser(args.chromium_dir)
Yuqian Libb4b4892017-11-08 13:00:08 -0500145 toggle_chromium(args)
Yuqian Li33e2fad2017-10-24 14:42:57 -0400146 end_message += " * https://chromium-review.googlesource.com\n"
147 if args.google3:
Yuqian Libb4b4892017-11-08 13:00:08 -0500148 toggle_google3(args)
Yuqian Li33e2fad2017-10-24 14:42:57 -0400149 end_message += " * http://goto.google.com/cl\n"
150 if args.android_dir:
151 args.android_dir = os.path.expanduser(args.android_dir)
Yuqian Libb4b4892017-11-08 13:00:08 -0500152 toggle_android(args)
Yuqian Li33e2fad2017-10-24 14:42:57 -0400153 end_message += " * http://goto.google.com/androidcl\n"
154
155 print end_message
156
157
158if __name__ == '__main__':
159 main()