blob: 9ccfd8cad7260e9b1480e16ea2349a5a6f4767a1 [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):
Yuqian Li33e2fad2017-10-24 14:42:57 -040046 sys.path.append(ANDROID_TOOLS_DIR)
47 import upload_to_android
48
Yuqian Liee6784d2017-10-31 10:14:25 -040049 modifier = upload_to_android.AndroidLegacyFlagModifier(args.flag)
50 upload_to_android.upload_to_android(args.android_dir, modifier)
Yuqian Li33e2fad2017-10-24 14:42:57 -040051
52
53def add_to_chromium(args):
54 os.chdir(args.chromium_dir)
55
56 branch = subprocess.check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD'])
57 branch = branch.strip()
58
59 EXPECTED_STASH_OUT = "No local changes to save"
60 stash_output = subprocess.check_output(['git', 'stash']).strip()
61
62 if branch != "master" or stash_output != EXPECTED_STASH_OUT:
63 print ("Please checkout a clean master branch at your chromium repo (%s) "
64 "before running this script") % args.chromium_dir
65 if stash_output != EXPECTED_STASH_OUT:
66 subprocess.check_call(['git', 'stash', 'pop'])
67 exit(1)
68
Yuqian Li427293c2017-11-06 17:46:40 -050069 # Update the repository to avoid conflicts
70 subprocess.check_call(['git', 'pull'])
71 subprocess.check_call(['gclient', 'sync']);
72
Yuqian Li33e2fad2017-10-24 14:42:57 -040073 # Use random number to avoid branch name collision.
74 # We'll delete the branch in the end.
75 random = randint(1, 10000)
76 subprocess.check_call(['git', 'checkout', '-b', 'legacyflag_%d' % random])
77
78 try:
79 config_file = os.path.join('skia', 'config', 'SkUserConfig.h')
80 separator = (
81 "///////////////////////// Imported from BUILD.gn and skia_common.gypi\n")
82 content = ("#ifndef {0}\n"
83 "#define {0}\n"
84 "#endif\n\n").format(args.flag)
85 insert_at(config_file, separator, 0, content)
86
87 subprocess.check_call('git commit -a -m "Add %s"' % args.flag, shell=True)
88 subprocess.check_call('git cl upload -m "Add %s" -f' % args.flag,
89 shell=True)
90 finally:
91 subprocess.check_call(['git', 'checkout', 'master'])
92 subprocess.check_call(['git', 'branch', '-D', 'legacyflag_%d' % random])
93
94
95def add_to_google3(args):
96 G3_SCRIPT_DIR = os.path.expanduser("~/skia-g3/scripts")
97 if not os.path.isdir(G3_SCRIPT_DIR):
98 print ("Google3 directory unavailable.\n"
99 "Please see "
100 "https://sites.google.com/a/google.com/skia/rebaseline#g3_flag "
101 "for Google3 setup.")
102 exit(1)
103 sys.path.append(G3_SCRIPT_DIR)
104 import citc_flag
105
106 citc_flag.add_to_google3(args.google3, args.flag)
107
108
109def main():
110 if len(sys.argv) <= 1 or sys.argv[1] == '-h' or sys.argv[1] == '--help':
111 print README
112
113 parser = argparse.ArgumentParser()
114 parser.add_argument(
115 '--android-dir', '-a', required=False,
116 help='Directory where an Android checkout will be created (if it does '
117 'not already exist). Note: ~1GB space will be used.')
118 parser.add_argument(
119 '--chromium-dir', '-c', required=False,
120 help='Directory of an EXISTING Chromium checkout (e.g., ~/chromium/src)')
121 parser.add_argument(
122 '--google3', '-g', required=False,
123 help='Google3 workspace to be created (if it does not already exist).')
124 parser.add_argument('flag', type=str, help='legacy flag name')
125
126 args = parser.parse_args()
127
128 if not args.android_dir and not args.chromium_dir and not args.google3:
129 print """
130Nothing to do. Please give me at least one of these three arguments:
131 -a (--android-dir)
132 -c (--chromium-dir)
133 -g (--google3)
134"""
135 exit(1)
136
137 end_message = "CLs generated. Now go review and land them:\n"
138 if args.chromium_dir:
139 args.chromium_dir = os.path.expanduser(args.chromium_dir)
140 add_to_chromium(args)
141 end_message += " * https://chromium-review.googlesource.com\n"
142 if args.google3:
143 add_to_google3(args)
144 end_message += " * http://goto.google.com/cl\n"
145 if args.android_dir:
146 args.android_dir = os.path.expanduser(args.android_dir)
147 add_to_android(args)
148 end_message += " * http://goto.google.com/androidcl\n"
149
150 print end_message
151
152
153if __name__ == '__main__':
154 main()