kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
| 3 | # |
| 4 | # Use of this source code is governed by a BSD-style license |
| 5 | # that can be found in the LICENSE file in the root of the source |
| 6 | # tree. An additional intellectual property rights grant can be found |
| 7 | # in the file PATENTS. All contributing project authors may |
| 8 | # be found in the AUTHORS file in the root of the source tree. |
| 9 | |
| 10 | """Setup links to a Chromium checkout for WebRTC. |
| 11 | |
| 12 | WebRTC standalone shares a lot of dependencies and build tools with Chromium. |
| 13 | To do this, many of the paths of a Chromium checkout is emulated by creating |
| 14 | symlinks to files and directories. This script handles the setup of symlinks to |
| 15 | achieve this. |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 16 | """ |
| 17 | |
| 18 | |
| 19 | import ctypes |
| 20 | import errno |
| 21 | import logging |
| 22 | import optparse |
| 23 | import os |
| 24 | import shelve |
| 25 | import shutil |
| 26 | import subprocess |
| 27 | import sys |
| 28 | import textwrap |
| 29 | |
| 30 | |
| 31 | DIRECTORIES = [ |
| 32 | 'build', |
| 33 | 'buildtools', |
kjellander | e26e787 | 2016-03-04 14:39:28 -0800 | [diff] [blame] | 34 | 'mojo', # TODO(kjellander): Remove, see webrtc:5629. |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 35 | 'testing', |
metzman | f89a571 | 2016-07-25 02:14:09 -0700 | [diff] [blame] | 36 | 'third_party/afl', |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 37 | 'third_party/binutils', |
| 38 | 'third_party/boringssl', |
ehmaldonado | 1249ddf | 2016-08-31 01:58:48 -0700 | [diff] [blame] | 39 | 'third_party/closure_compiler', |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 40 | 'third_party/colorama', |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 41 | 'third_party/expat', |
hbos | a9a1d2a | 2016-01-11 10:19:02 -0800 | [diff] [blame] | 42 | 'third_party/ffmpeg', |
kjellander@webrtc.org | 4e4fe4f | 2014-10-01 08:03:19 +0000 | [diff] [blame] | 43 | 'third_party/instrumented_libraries', |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 44 | 'third_party/jsoncpp', |
Henrik Kjellander | 26ab91b | 2015-11-26 10:26:32 +0100 | [diff] [blame] | 45 | 'third_party/libc++-static', |
kjellander@webrtc.org | 2addf3f | 2016-04-04 08:33:12 +0200 | [diff] [blame] | 46 | 'third_party/libFuzzer', |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 47 | 'third_party/libjpeg', |
| 48 | 'third_party/libjpeg_turbo', |
| 49 | 'third_party/libsrtp', |
kjellander | e26e787 | 2016-03-04 14:39:28 -0800 | [diff] [blame] | 50 | 'third_party/libvpx', |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 51 | 'third_party/libyuv', |
| 52 | 'third_party/llvm-build', |
Patrik Höglund | c92c23d | 2015-08-31 11:30:14 +0200 | [diff] [blame] | 53 | 'third_party/lss', |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 54 | 'third_party/nss', |
tkchin@webrtc.org | 3a63a3c | 2015-01-06 07:21:34 +0000 | [diff] [blame] | 55 | 'third_party/ocmock', |
hbos | a9a1d2a | 2016-01-11 10:19:02 -0800 | [diff] [blame] | 56 | 'third_party/openh264', |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 57 | 'third_party/openmax_dl', |
| 58 | 'third_party/opus', |
Patrik Höglund | c92c23d | 2015-08-31 11:30:14 +0200 | [diff] [blame] | 59 | 'third_party/proguard', |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 60 | 'third_party/protobuf', |
| 61 | 'third_party/sqlite', |
| 62 | 'third_party/syzygy', |
| 63 | 'third_party/usrsctp', |
| 64 | 'third_party/yasm', |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 65 | 'third_party/zlib', |
kjellander | 752f36f | 2016-03-22 16:56:01 -0700 | [diff] [blame] | 66 | 'third_party/WebKit', # TODO(kjellander): Remove, see webrtc:5629. |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 67 | 'tools/clang', |
ehmaldonado | a78213e | 2016-09-22 10:46:16 -0700 | [diff] [blame] | 68 | 'tools/determinism', |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 69 | 'tools/generate_library_loader', |
hbos | 5602f65 | 2016-01-15 01:38:34 -0800 | [diff] [blame] | 70 | 'tools/generate_stubs', |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 71 | 'tools/gn', |
ehmaldonado | 1249ddf | 2016-08-31 01:58:48 -0700 | [diff] [blame] | 72 | 'tools/grit', |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 73 | 'tools/gyp', |
kjellander | e532aec | 2016-04-17 20:08:20 -0700 | [diff] [blame] | 74 | 'tools/luci-go', |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 75 | 'tools/memory', |
| 76 | 'tools/protoc_wrapper', |
| 77 | 'tools/python', |
| 78 | 'tools/swarming_client', |
| 79 | 'tools/valgrind', |
Andrew MacDonald | 65de7d2 | 2015-05-19 11:37:34 -0700 | [diff] [blame] | 80 | 'tools/vim', |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 81 | 'tools/win', |
Henrik Kjellander | 9589e2a | 2015-10-22 06:48:21 +0200 | [diff] [blame] | 82 | 'tools/xdisplaycheck', |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 83 | ] |
| 84 | |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 85 | from sync_chromium import get_target_os_list |
Henrik Kjellander | ca84302 | 2015-06-09 10:51:22 +0200 | [diff] [blame] | 86 | target_os = get_target_os_list() |
| 87 | if 'android' in target_os: |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 88 | DIRECTORIES += [ |
| 89 | 'base', |
ehmaldonado | 9f73f7a | 2016-07-28 01:20:22 -0700 | [diff] [blame] | 90 | 'third_party/accessibility_test_framework', |
Henrik Kjellander | 94a1232 | 2015-06-09 14:56:20 +0200 | [diff] [blame] | 91 | 'third_party/android_platform', |
kjellander | b112568 | 2016-10-26 13:38:03 -0700 | [diff] [blame] | 92 | 'third_party/android_support_test_runner', |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 93 | 'third_party/android_tools', |
ehmaldonado | 9f73f7a | 2016-07-28 01:20:22 -0700 | [diff] [blame] | 94 | 'third_party/apache_velocity', |
kjellander@webrtc.org | cbe7ca8 | 2015-01-06 07:24:27 +0000 | [diff] [blame] | 95 | 'third_party/appurify-python', |
kjellander@webrtc.org | b8caf6a | 2014-09-30 18:05:02 +0000 | [diff] [blame] | 96 | 'third_party/ashmem', |
ehmaldonado | 9f73f7a | 2016-07-28 01:20:22 -0700 | [diff] [blame] | 97 | 'third_party/bouncycastle', |
ehmaldonado | cd8ae61 | 2016-08-24 02:44:14 -0700 | [diff] [blame] | 98 | 'third_party/byte_buddy', |
kjellander | 34a7054 | 2015-12-06 10:32:34 -0800 | [diff] [blame] | 99 | 'third_party/catapult', |
kjellander | f10976e | 2016-08-11 10:31:40 -0700 | [diff] [blame] | 100 | 'third_party/ced', |
ehmaldonado | 9f73f7a | 2016-07-28 01:20:22 -0700 | [diff] [blame] | 101 | 'third_party/guava', |
| 102 | 'third_party/hamcrest', |
kjellander | 5376100 | 2015-11-10 10:58:22 -0800 | [diff] [blame] | 103 | 'third_party/icu', |
ehmaldonado | 9f73f7a | 2016-07-28 01:20:22 -0700 | [diff] [blame] | 104 | 'third_party/icu4j', |
Henrik Kjellander | eecbab7 | 2015-09-16 19:19:04 +0200 | [diff] [blame] | 105 | 'third_party/ijar', |
ehmaldonado | 9f73f7a | 2016-07-28 01:20:22 -0700 | [diff] [blame] | 106 | 'third_party/intellij', |
kjellander@webrtc.org | b8caf6a | 2014-09-30 18:05:02 +0000 | [diff] [blame] | 107 | 'third_party/jsr-305', |
Henrik Kjellander | 10ba3ee | 2015-04-29 14:47:53 +0200 | [diff] [blame] | 108 | 'third_party/junit', |
kjellander@webrtc.org | b8caf6a | 2014-09-30 18:05:02 +0000 | [diff] [blame] | 109 | 'third_party/libxml', |
Henrik Kjellander | 10ba3ee | 2015-04-29 14:47:53 +0200 | [diff] [blame] | 110 | 'third_party/mockito', |
kjellander@webrtc.org | b8caf6a | 2014-09-30 18:05:02 +0000 | [diff] [blame] | 111 | 'third_party/modp_b64', |
ehmaldonado | cd8ae61 | 2016-08-24 02:44:14 -0700 | [diff] [blame] | 112 | 'third_party/objenesis', |
ehmaldonado | 9f73f7a | 2016-07-28 01:20:22 -0700 | [diff] [blame] | 113 | 'third_party/ow2_asm', |
kjellander@webrtc.org | cbe7ca8 | 2015-01-06 07:24:27 +0000 | [diff] [blame] | 114 | 'third_party/requests', |
Henrik Kjellander | 10ba3ee | 2015-04-29 14:47:53 +0200 | [diff] [blame] | 115 | 'third_party/robolectric', |
ehmaldonado | 9f73f7a | 2016-07-28 01:20:22 -0700 | [diff] [blame] | 116 | 'third_party/sqlite4java', |
primiano | b332e5d | 2016-01-25 08:13:05 -0800 | [diff] [blame] | 117 | 'third_party/tcmalloc', |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 118 | 'tools/android', |
kjellander | 34a7054 | 2015-12-06 10:32:34 -0800 | [diff] [blame] | 119 | 'tools/telemetry', |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 120 | ] |
tommi | de3b029 | 2016-04-22 01:47:02 -0700 | [diff] [blame] | 121 | else: |
| 122 | DIRECTORIES += [ |
| 123 | 'base/third_party/libevent', |
| 124 | ] |
| 125 | |
Henrik Kjellander | ca84302 | 2015-06-09 10:51:22 +0200 | [diff] [blame] | 126 | if 'ios' in target_os: |
| 127 | DIRECTORIES.append('third_party/class-dump') |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 128 | |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 129 | FILES = { |
Henrik Kjellander | d6d27e7 | 2015-09-25 22:19:11 +0200 | [diff] [blame] | 130 | 'tools/isolate_driver.py': None, |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 131 | 'third_party/BUILD.gn': None, |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 132 | } |
| 133 | |
kjellander@webrtc.org | e94f83a | 2014-09-18 13:47:23 +0000 | [diff] [blame] | 134 | ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 135 | CHROMIUM_CHECKOUT = os.path.join('chromium', 'src') |
| 136 | LINKS_DB = 'links' |
| 137 | |
| 138 | # Version management to make future upgrades/downgrades easier to support. |
| 139 | SCHEMA_VERSION = 1 |
| 140 | |
| 141 | |
| 142 | def query_yes_no(question, default=False): |
| 143 | """Ask a yes/no question via raw_input() and return their answer. |
| 144 | |
| 145 | Modified from http://stackoverflow.com/a/3041990. |
| 146 | """ |
| 147 | prompt = " [%s/%%s]: " |
| 148 | prompt = prompt % ('Y' if default is True else 'y') |
| 149 | prompt = prompt % ('N' if default is False else 'n') |
| 150 | |
| 151 | if default is None: |
| 152 | default = 'INVALID' |
| 153 | |
| 154 | while True: |
| 155 | sys.stdout.write(question + prompt) |
| 156 | choice = raw_input().lower() |
| 157 | if choice == '' and default != 'INVALID': |
| 158 | return default |
| 159 | |
| 160 | if 'yes'.startswith(choice): |
| 161 | return True |
| 162 | elif 'no'.startswith(choice): |
| 163 | return False |
| 164 | |
| 165 | print "Please respond with 'yes' or 'no' (or 'y' or 'n')." |
| 166 | |
| 167 | |
| 168 | # Actions |
| 169 | class Action(object): |
| 170 | def __init__(self, dangerous): |
| 171 | self.dangerous = dangerous |
| 172 | |
| 173 | def announce(self, planning): |
| 174 | """Log a description of this action. |
| 175 | |
| 176 | Args: |
| 177 | planning - True iff we're in the planning stage, False if we're in the |
| 178 | doit stage. |
| 179 | """ |
| 180 | pass |
| 181 | |
| 182 | def doit(self, links_db): |
| 183 | """Execute the action, recording what we did to links_db, if necessary.""" |
| 184 | pass |
| 185 | |
| 186 | |
| 187 | class Remove(Action): |
| 188 | def __init__(self, path, dangerous): |
| 189 | super(Remove, self).__init__(dangerous) |
| 190 | self._priority = 0 |
| 191 | self._path = path |
| 192 | |
| 193 | def announce(self, planning): |
| 194 | log = logging.warn |
| 195 | filesystem_type = 'file' |
| 196 | if not self.dangerous: |
| 197 | log = logging.info |
| 198 | filesystem_type = 'link' |
| 199 | if planning: |
| 200 | log('Planning to remove %s: %s', filesystem_type, self._path) |
| 201 | else: |
| 202 | log('Removing %s: %s', filesystem_type, self._path) |
| 203 | |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 204 | def doit(self, _): |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 205 | os.remove(self._path) |
| 206 | |
| 207 | |
| 208 | class Rmtree(Action): |
| 209 | def __init__(self, path): |
| 210 | super(Rmtree, self).__init__(dangerous=True) |
| 211 | self._priority = 0 |
| 212 | self._path = path |
| 213 | |
| 214 | def announce(self, planning): |
| 215 | if planning: |
| 216 | logging.warn('Planning to remove directory: %s', self._path) |
| 217 | else: |
| 218 | logging.warn('Removing directory: %s', self._path) |
| 219 | |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 220 | def doit(self, _): |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 221 | if sys.platform.startswith('win'): |
| 222 | # shutil.rmtree() doesn't work on Windows if any of the directories are |
| 223 | # read-only, which svn repositories are. |
| 224 | subprocess.check_call(['rd', '/q', '/s', self._path], shell=True) |
| 225 | else: |
| 226 | shutil.rmtree(self._path) |
| 227 | |
| 228 | |
| 229 | class Makedirs(Action): |
| 230 | def __init__(self, path): |
| 231 | super(Makedirs, self).__init__(dangerous=False) |
| 232 | self._priority = 1 |
| 233 | self._path = path |
| 234 | |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 235 | def doit(self, _): |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 236 | try: |
| 237 | os.makedirs(self._path) |
| 238 | except OSError as e: |
| 239 | if e.errno != errno.EEXIST: |
| 240 | raise |
| 241 | |
| 242 | |
| 243 | class Symlink(Action): |
| 244 | def __init__(self, source_path, link_path): |
| 245 | super(Symlink, self).__init__(dangerous=False) |
| 246 | self._priority = 2 |
| 247 | self._source_path = source_path |
| 248 | self._link_path = link_path |
| 249 | |
| 250 | def announce(self, planning): |
| 251 | if planning: |
| 252 | logging.info( |
| 253 | 'Planning to create link from %s to %s', self._link_path, |
| 254 | self._source_path) |
| 255 | else: |
| 256 | logging.debug( |
| 257 | 'Linking from %s to %s', self._link_path, self._source_path) |
| 258 | |
| 259 | def doit(self, links_db): |
| 260 | # Files not in the root directory need relative path calculation. |
| 261 | # On Windows, use absolute paths instead since NTFS doesn't seem to support |
| 262 | # relative paths for symlinks. |
| 263 | if sys.platform.startswith('win'): |
| 264 | source_path = os.path.abspath(self._source_path) |
| 265 | else: |
| 266 | if os.path.dirname(self._link_path) != self._link_path: |
| 267 | source_path = os.path.relpath(self._source_path, |
| 268 | os.path.dirname(self._link_path)) |
| 269 | |
| 270 | os.symlink(source_path, os.path.abspath(self._link_path)) |
| 271 | links_db[self._source_path] = self._link_path |
| 272 | |
| 273 | |
| 274 | class LinkError(IOError): |
| 275 | """Failed to create a link.""" |
| 276 | pass |
| 277 | |
| 278 | |
kjellander | 844dd2a | 2016-04-05 00:13:58 -0700 | [diff] [blame] | 279 | # Use junctions instead of symlinks on the Windows platform. |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 280 | if sys.platform.startswith('win'): |
| 281 | def symlink(source_path, link_path): |
kjellander | 844dd2a | 2016-04-05 00:13:58 -0700 | [diff] [blame] | 282 | if os.path.isdir(source_path): |
| 283 | subprocess.check_call(['cmd.exe', '/c', 'mklink', '/J', link_path, |
| 284 | source_path]) |
| 285 | else: |
| 286 | # Don't create symlinks to files on Windows, just copy the file instead |
| 287 | # (there's no way to create a link without administrator's privileges). |
| 288 | shutil.copy(source_path, link_path) |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 289 | os.symlink = symlink |
| 290 | |
| 291 | |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 292 | class WebRTCLinkSetup(object): |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 293 | def __init__(self, links_db, force=False, dry_run=False, prompt=False): |
| 294 | self._force = force |
| 295 | self._dry_run = dry_run |
| 296 | self._prompt = prompt |
| 297 | self._links_db = links_db |
| 298 | |
| 299 | def CreateLinks(self, on_bot): |
| 300 | logging.debug('CreateLinks') |
| 301 | # First, make a plan of action |
| 302 | actions = [] |
| 303 | |
| 304 | for source_path, link_path in FILES.iteritems(): |
| 305 | actions += self._ActionForPath( |
| 306 | source_path, link_path, check_fn=os.path.isfile, check_msg='files') |
| 307 | for source_dir in DIRECTORIES: |
| 308 | actions += self._ActionForPath( |
| 309 | source_dir, None, check_fn=os.path.isdir, |
| 310 | check_msg='directories') |
| 311 | |
kjellander@webrtc.org | e94f83a | 2014-09-18 13:47:23 +0000 | [diff] [blame] | 312 | if not on_bot and self._force: |
| 313 | # When making the manual switch from legacy SVN checkouts to the new |
| 314 | # Git-based Chromium DEPS, the .gclient_entries file that contains cached |
| 315 | # URLs for all DEPS entries must be removed to avoid future sync problems. |
| 316 | entries_file = os.path.join(os.path.dirname(ROOT_DIR), '.gclient_entries') |
| 317 | if os.path.exists(entries_file): |
| 318 | actions.append(Remove(entries_file, dangerous=True)) |
| 319 | |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 320 | actions.sort() |
| 321 | |
| 322 | if self._dry_run: |
| 323 | for action in actions: |
| 324 | action.announce(planning=True) |
| 325 | logging.info('Not doing anything because dry-run was specified.') |
| 326 | sys.exit(0) |
| 327 | |
| 328 | if any(a.dangerous for a in actions): |
| 329 | logging.warn('Dangerous actions:') |
| 330 | for action in (a for a in actions if a.dangerous): |
| 331 | action.announce(planning=True) |
| 332 | print |
| 333 | |
| 334 | if not self._force: |
| 335 | logging.error(textwrap.dedent("""\ |
| 336 | @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
| 337 | A C T I O N R E Q I R E D |
| 338 | @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
| 339 | |
kjellander | 844dd2a | 2016-04-05 00:13:58 -0700 | [diff] [blame] | 340 | Setting up the checkout requires creating symlinks to directories in the |
| 341 | Chromium checkout inside chromium/src. |
| 342 | To avoid disrupting developers, we've chosen to not delete directories |
| 343 | forcibly, in case you have some work in progress in one of them :) |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 344 | |
| 345 | ACTION REQUIRED: |
| 346 | Before running `gclient sync|runhooks` again, you must run: |
| 347 | %s%s --force |
| 348 | |
| 349 | Which will replace all directories which now must be symlinks, after |
| 350 | prompting with a summary of the work-to-be-done. |
kjellander | 844dd2a | 2016-04-05 00:13:58 -0700 | [diff] [blame] | 351 | """), 'python ' if sys.platform.startswith('win') else '', __file__) |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 352 | sys.exit(1) |
| 353 | elif self._prompt: |
| 354 | if not query_yes_no('Would you like to perform the above plan?'): |
| 355 | sys.exit(1) |
| 356 | |
| 357 | for action in actions: |
| 358 | action.announce(planning=False) |
| 359 | action.doit(self._links_db) |
| 360 | |
| 361 | if not on_bot and self._force: |
| 362 | logging.info('Completed!\n\nNow run `gclient sync|runhooks` again to ' |
| 363 | 'let the remaining hooks (that probably were interrupted) ' |
| 364 | 'execute.') |
| 365 | |
| 366 | def CleanupLinks(self): |
| 367 | logging.debug('CleanupLinks') |
| 368 | for source, link_path in self._links_db.iteritems(): |
| 369 | if source == 'SCHEMA_VERSION': |
| 370 | continue |
| 371 | if os.path.islink(link_path) or sys.platform.startswith('win'): |
| 372 | # os.path.islink() always returns false on Windows |
| 373 | # See http://bugs.python.org/issue13143. |
| 374 | logging.debug('Removing link to %s at %s', source, link_path) |
| 375 | if not self._dry_run: |
| 376 | if os.path.exists(link_path): |
| 377 | if sys.platform.startswith('win') and os.path.isdir(link_path): |
Henrik Kjellander | c444de6 | 2015-04-29 11:27:22 +0200 | [diff] [blame] | 378 | subprocess.check_call(['rmdir', '/q', '/s', link_path], |
| 379 | shell=True) |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 380 | else: |
| 381 | os.remove(link_path) |
| 382 | del self._links_db[source] |
| 383 | |
| 384 | @staticmethod |
| 385 | def _ActionForPath(source_path, link_path=None, check_fn=None, |
| 386 | check_msg=None): |
| 387 | """Create zero or more Actions to link to a file or directory. |
| 388 | |
kjellander | 844dd2a | 2016-04-05 00:13:58 -0700 | [diff] [blame] | 389 | This will be a symlink on POSIX platforms. On Windows it will result in: |
| 390 | * a junction for directories |
| 391 | * a copied file for single files. |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 392 | |
| 393 | Args: |
| 394 | source_path: Path relative to the Chromium checkout root. |
| 395 | For readability, the path may contain slashes, which will |
| 396 | automatically be converted to the right path delimiter on Windows. |
| 397 | link_path: The location for the link to create. If omitted it will be the |
| 398 | same path as source_path. |
| 399 | check_fn: A function returning true if the type of filesystem object is |
| 400 | correct for the attempted call. Otherwise an error message with |
| 401 | check_msg will be printed. |
| 402 | check_msg: String used to inform the user of an invalid attempt to create |
| 403 | a file. |
| 404 | Returns: |
| 405 | A list of Action objects. |
| 406 | """ |
| 407 | def fix_separators(path): |
| 408 | if sys.platform.startswith('win'): |
| 409 | return path.replace(os.altsep, os.sep) |
| 410 | else: |
| 411 | return path |
| 412 | |
| 413 | assert check_fn |
| 414 | assert check_msg |
| 415 | link_path = link_path or source_path |
| 416 | link_path = fix_separators(link_path) |
| 417 | |
| 418 | source_path = fix_separators(source_path) |
| 419 | source_path = os.path.join(CHROMIUM_CHECKOUT, source_path) |
| 420 | if os.path.exists(source_path) and not check_fn: |
kjellander | 844dd2a | 2016-04-05 00:13:58 -0700 | [diff] [blame] | 421 | raise LinkError('Can only to link to %s: tried to link to: %s' % |
| 422 | (check_msg, source_path)) |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 423 | |
| 424 | if not os.path.exists(source_path): |
| 425 | logging.debug('Silently ignoring missing source: %s. This is to avoid ' |
| 426 | 'errors on platform-specific dependencies.', source_path) |
| 427 | return [] |
| 428 | |
| 429 | actions = [] |
| 430 | |
| 431 | if os.path.exists(link_path) or os.path.islink(link_path): |
| 432 | if os.path.islink(link_path): |
| 433 | actions.append(Remove(link_path, dangerous=False)) |
| 434 | elif os.path.isfile(link_path): |
| 435 | actions.append(Remove(link_path, dangerous=True)) |
| 436 | elif os.path.isdir(link_path): |
| 437 | actions.append(Rmtree(link_path)) |
| 438 | else: |
| 439 | raise LinkError('Don\'t know how to plan: %s' % link_path) |
| 440 | |
| 441 | # Create parent directories to the target link if needed. |
| 442 | target_parent_dirs = os.path.dirname(link_path) |
| 443 | if (target_parent_dirs and |
| 444 | target_parent_dirs != link_path and |
| 445 | not os.path.exists(target_parent_dirs)): |
| 446 | actions.append(Makedirs(target_parent_dirs)) |
| 447 | |
| 448 | actions.append(Symlink(source_path, link_path)) |
| 449 | |
| 450 | return actions |
| 451 | |
| 452 | def _initialize_database(filename): |
| 453 | links_database = shelve.open(filename) |
| 454 | |
| 455 | # Wipe the database if this version of the script ends up looking at a |
| 456 | # newer (future) version of the links db, just to be sure. |
| 457 | version = links_database.get('SCHEMA_VERSION') |
| 458 | if version and version != SCHEMA_VERSION: |
| 459 | logging.info('Found database with schema version %s while this script only ' |
| 460 | 'supports %s. Wiping previous database contents.', version, |
| 461 | SCHEMA_VERSION) |
| 462 | links_database.clear() |
| 463 | links_database['SCHEMA_VERSION'] = SCHEMA_VERSION |
| 464 | return links_database |
| 465 | |
| 466 | |
| 467 | def main(): |
| 468 | on_bot = os.environ.get('CHROME_HEADLESS') == '1' |
| 469 | |
| 470 | parser = optparse.OptionParser() |
| 471 | parser.add_option('-d', '--dry-run', action='store_true', default=False, |
| 472 | help='Print what would be done, but don\'t perform any ' |
| 473 | 'operations. This will automatically set logging to ' |
| 474 | 'verbose.') |
| 475 | parser.add_option('-c', '--clean-only', action='store_true', default=False, |
| 476 | help='Only clean previously created links, don\'t create ' |
| 477 | 'new ones. This will automatically set logging to ' |
| 478 | 'verbose.') |
| 479 | parser.add_option('-f', '--force', action='store_true', default=on_bot, |
| 480 | help='Force link creation. CAUTION: This deletes existing ' |
| 481 | 'folders and files in the locations where links are ' |
| 482 | 'about to be created.') |
| 483 | parser.add_option('-n', '--no-prompt', action='store_false', dest='prompt', |
| 484 | default=(not on_bot), |
| 485 | help='Prompt if we\'re planning to do a dangerous action') |
| 486 | parser.add_option('-v', '--verbose', action='store_const', |
| 487 | const=logging.DEBUG, default=logging.INFO, |
| 488 | help='Print verbose output for debugging.') |
| 489 | options, _ = parser.parse_args() |
| 490 | |
| 491 | if options.dry_run or options.force or options.clean_only: |
| 492 | options.verbose = logging.DEBUG |
| 493 | logging.basicConfig(format='%(message)s', level=options.verbose) |
| 494 | |
| 495 | # Work from the root directory of the checkout. |
| 496 | script_dir = os.path.dirname(os.path.abspath(__file__)) |
| 497 | os.chdir(script_dir) |
| 498 | |
| 499 | if sys.platform.startswith('win'): |
| 500 | def is_admin(): |
| 501 | try: |
| 502 | return os.getuid() == 0 |
| 503 | except AttributeError: |
| 504 | return ctypes.windll.shell32.IsUserAnAdmin() != 0 |
kjellander | 844dd2a | 2016-04-05 00:13:58 -0700 | [diff] [blame] | 505 | if is_admin(): |
| 506 | logging.warning('WARNING: On Windows, you no longer need run as ' |
| 507 | 'administrator. Please run with user account privileges.') |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 508 | |
| 509 | if not os.path.exists(CHROMIUM_CHECKOUT): |
Henrik Kjellander | edec076 | 2016-11-15 22:39:13 +0100 | [diff] [blame] | 510 | logging.warning('Cannot find a Chromium checkout at %s. Did you run ' |
| 511 | '"gclient sync" before running this script?', |
| 512 | CHROMIUM_CHECKOUT) |
| 513 | return 0 |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 514 | |
| 515 | links_database = _initialize_database(LINKS_DB) |
| 516 | try: |
| 517 | symlink_creator = WebRTCLinkSetup(links_database, options.force, |
| 518 | options.dry_run, options.prompt) |
| 519 | symlink_creator.CleanupLinks() |
| 520 | if not options.clean_only: |
| 521 | symlink_creator.CreateLinks(on_bot) |
| 522 | except LinkError as e: |
| 523 | print >> sys.stderr, e.message |
| 524 | return 3 |
| 525 | finally: |
| 526 | links_database.close() |
| 527 | return 0 |
| 528 | |
| 529 | |
| 530 | if __name__ == '__main__': |
| 531 | sys.exit(main()) |