• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
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 toggle_android(args):
34  sys.path.append(ANDROID_TOOLS_DIR)
35  import upload_to_android
36
37  modifier = upload_to_android.AndroidLegacyFlagModifier(args.flag)
38  upload_to_android.upload_to_android(args.android_dir, modifier)
39
40
41def toggle_chromium(args):
42  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
57  # Update the repository to avoid conflicts
58  subprocess.check_call(['git', 'pull'])
59  subprocess.check_call(['gclient', 'sync']);
60
61  # 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')
68    with open(config_file) as f:
69      lines = f.readlines()
70
71    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,
94                          shell=True)
95  finally:
96    subprocess.check_call(['git', 'checkout', 'master'])
97    subprocess.check_call(['git', 'branch', '-D', 'legacyflag_%d' % random])
98
99
100def toggle_google3(args):
101  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
111  citc_flag.toggle_google3(args.google3, args.flag)
112
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)
145    toggle_chromium(args)
146    end_message += " * https://chromium-review.googlesource.com\n"
147  if args.google3:
148    toggle_google3(args)
149    end_message += " * http://goto.google.com/cl\n"
150  if args.android_dir:
151    args.android_dir = os.path.expanduser(args.android_dir)
152    toggle_android(args)
153    end_message += " * http://goto.google.com/androidcl\n"
154
155  print end_message
156
157
158if __name__ == '__main__':
159  main()
160