blob: 0775d8ac5d86facc4ed85b8f0cb0fa9348506905 [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 = """
7Automatically add a specific legacy flag to multiple Skia client repos.
8
9This would only work on Google desktop.
10
11Example usage:
12 $ python add_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 add_legacy_flag.py SK_SUPPORT_LEGACY_SOMETHING -a /data/android
18
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
33def insert_at(filename, pattern, offset, content):
34 with open(filename) as f:
35 lines = f.readlines()
36
37 line_index = lines.index(pattern)
38 lines.insert(line_index + offset, content)
39
40 with open(filename, 'w') as f:
41 for line in lines:
42 f.write(line)
43
44
45def add_to_android(args):
46 REPO_BRANCH_NAME = "flag"
47 sys.path.append(ANDROID_TOOLS_DIR)
48 import upload_to_android
49
50 repo_binary = upload_to_android.init_work_dir(args.android_dir);
51
52 # Create repo branch.
53 subprocess.check_call('%s start %s .' % (repo_binary, REPO_BRANCH_NAME),
54 shell=True)
55
56 try:
57 # Add flag to SkUserConfigManual.h.
58 config_file = os.path.join('include', 'config', 'SkUserConfigManual.h')
59
60 insert_at(config_file,
61 "#endif // SkUserConfigManual_DEFINED\n",
62 0,
63 " #define %s\n" % args.flag)
64
65 subprocess.check_call('git add %s' % config_file, shell=True)
66
67 message = ('Add %s\n\n'
68 'Test: Presubmit checks will test this change.' % args.flag)
69
70 subprocess.check_call('git commit -m "%s"' % message, shell=True)
71
72 # Upload to Android Gerrit.
73 subprocess.check_call('%s upload --verify' % repo_binary, shell=True)
74 finally:
75 # Remove repo branch
76 subprocess.check_call('%s abandon flag' % repo_binary, shell=True)
77
78
79def add_to_chromium(args):
80 os.chdir(args.chromium_dir)
81
82 branch = subprocess.check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD'])
83 branch = branch.strip()
84
85 EXPECTED_STASH_OUT = "No local changes to save"
86 stash_output = subprocess.check_output(['git', 'stash']).strip()
87
88 if branch != "master" or stash_output != EXPECTED_STASH_OUT:
89 print ("Please checkout a clean master branch at your chromium repo (%s) "
90 "before running this script") % args.chromium_dir
91 if stash_output != EXPECTED_STASH_OUT:
92 subprocess.check_call(['git', 'stash', 'pop'])
93 exit(1)
94
95 # Use random number to avoid branch name collision.
96 # We'll delete the branch in the end.
97 random = randint(1, 10000)
98 subprocess.check_call(['git', 'checkout', '-b', 'legacyflag_%d' % random])
99
100 try:
101 config_file = os.path.join('skia', 'config', 'SkUserConfig.h')
102 separator = (
103 "///////////////////////// Imported from BUILD.gn and skia_common.gypi\n")
104 content = ("#ifndef {0}\n"
105 "#define {0}\n"
106 "#endif\n\n").format(args.flag)
107 insert_at(config_file, separator, 0, content)
108
109 subprocess.check_call('git commit -a -m "Add %s"' % args.flag, shell=True)
110 subprocess.check_call('git cl upload -m "Add %s" -f' % args.flag,
111 shell=True)
112 finally:
113 subprocess.check_call(['git', 'checkout', 'master'])
114 subprocess.check_call(['git', 'branch', '-D', 'legacyflag_%d' % random])
115
116
117def add_to_google3(args):
118 G3_SCRIPT_DIR = os.path.expanduser("~/skia-g3/scripts")
119 if not os.path.isdir(G3_SCRIPT_DIR):
120 print ("Google3 directory unavailable.\n"
121 "Please see "
122 "https://sites.google.com/a/google.com/skia/rebaseline#g3_flag "
123 "for Google3 setup.")
124 exit(1)
125 sys.path.append(G3_SCRIPT_DIR)
126 import citc_flag
127
128 citc_flag.add_to_google3(args.google3, args.flag)
129
130
131def main():
132 if len(sys.argv) <= 1 or sys.argv[1] == '-h' or sys.argv[1] == '--help':
133 print README
134
135 parser = argparse.ArgumentParser()
136 parser.add_argument(
137 '--android-dir', '-a', required=False,
138 help='Directory where an Android checkout will be created (if it does '
139 'not already exist). Note: ~1GB space will be used.')
140 parser.add_argument(
141 '--chromium-dir', '-c', required=False,
142 help='Directory of an EXISTING Chromium checkout (e.g., ~/chromium/src)')
143 parser.add_argument(
144 '--google3', '-g', required=False,
145 help='Google3 workspace to be created (if it does not already exist).')
146 parser.add_argument('flag', type=str, help='legacy flag name')
147
148 args = parser.parse_args()
149
150 if not args.android_dir and not args.chromium_dir and not args.google3:
151 print """
152Nothing to do. Please give me at least one of these three arguments:
153 -a (--android-dir)
154 -c (--chromium-dir)
155 -g (--google3)
156"""
157 exit(1)
158
159 end_message = "CLs generated. Now go review and land them:\n"
160 if args.chromium_dir:
161 args.chromium_dir = os.path.expanduser(args.chromium_dir)
162 add_to_chromium(args)
163 end_message += " * https://chromium-review.googlesource.com\n"
164 if args.google3:
165 add_to_google3(args)
166 end_message += " * http://goto.google.com/cl\n"
167 if args.android_dir:
168 args.android_dir = os.path.expanduser(args.android_dir)
169 add_to_android(args)
170 end_message += " * http://goto.google.com/androidcl\n"
171
172 print end_message
173
174
175if __name__ == '__main__':
176 main()