blob: 8a99e517b67f59c6996c622d2a1c56061abe1cf6 [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
69 # Use random number to avoid branch name collision.
70 # We'll delete the branch in the end.
71 random = randint(1, 10000)
72 subprocess.check_call(['git', 'checkout', '-b', 'legacyflag_%d' % random])
73
74 try:
75 config_file = os.path.join('skia', 'config', 'SkUserConfig.h')
76 separator = (
77 "///////////////////////// Imported from BUILD.gn and skia_common.gypi\n")
78 content = ("#ifndef {0}\n"
79 "#define {0}\n"
80 "#endif\n\n").format(args.flag)
81 insert_at(config_file, separator, 0, content)
82
83 subprocess.check_call('git commit -a -m "Add %s"' % args.flag, shell=True)
84 subprocess.check_call('git cl upload -m "Add %s" -f' % args.flag,
85 shell=True)
86 finally:
87 subprocess.check_call(['git', 'checkout', 'master'])
88 subprocess.check_call(['git', 'branch', '-D', 'legacyflag_%d' % random])
89
90
91def add_to_google3(args):
92 G3_SCRIPT_DIR = os.path.expanduser("~/skia-g3/scripts")
93 if not os.path.isdir(G3_SCRIPT_DIR):
94 print ("Google3 directory unavailable.\n"
95 "Please see "
96 "https://sites.google.com/a/google.com/skia/rebaseline#g3_flag "
97 "for Google3 setup.")
98 exit(1)
99 sys.path.append(G3_SCRIPT_DIR)
100 import citc_flag
101
102 citc_flag.add_to_google3(args.google3, args.flag)
103
104
105def main():
106 if len(sys.argv) <= 1 or sys.argv[1] == '-h' or sys.argv[1] == '--help':
107 print README
108
109 parser = argparse.ArgumentParser()
110 parser.add_argument(
111 '--android-dir', '-a', required=False,
112 help='Directory where an Android checkout will be created (if it does '
113 'not already exist). Note: ~1GB space will be used.')
114 parser.add_argument(
115 '--chromium-dir', '-c', required=False,
116 help='Directory of an EXISTING Chromium checkout (e.g., ~/chromium/src)')
117 parser.add_argument(
118 '--google3', '-g', required=False,
119 help='Google3 workspace to be created (if it does not already exist).')
120 parser.add_argument('flag', type=str, help='legacy flag name')
121
122 args = parser.parse_args()
123
124 if not args.android_dir and not args.chromium_dir and not args.google3:
125 print """
126Nothing to do. Please give me at least one of these three arguments:
127 -a (--android-dir)
128 -c (--chromium-dir)
129 -g (--google3)
130"""
131 exit(1)
132
133 end_message = "CLs generated. Now go review and land them:\n"
134 if args.chromium_dir:
135 args.chromium_dir = os.path.expanduser(args.chromium_dir)
136 add_to_chromium(args)
137 end_message += " * https://chromium-review.googlesource.com\n"
138 if args.google3:
139 add_to_google3(args)
140 end_message += " * http://goto.google.com/cl\n"
141 if args.android_dir:
142 args.android_dir = os.path.expanduser(args.android_dir)
143 add_to_android(args)
144 end_message += " * http://goto.google.com/androidcl\n"
145
146 print end_message
147
148
149if __name__ == '__main__':
150 main()