| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2014 The Chromium Authors. All rights reserved. | 2 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """A utility script for downloading versioned Syzygy binaries.""" | 6 """A utility script for downloading versioned Syzygy binaries.""" |
| 7 | 7 |
| 8 import cStringIO | 8 import cStringIO |
| 9 import hashlib | 9 import hashlib |
| 10 import errno | |
| 11 import json | 10 import json |
| 12 import logging | 11 import logging |
| 13 import optparse | 12 import optparse |
| 14 import os | 13 import os |
| 15 import re | 14 import re |
| 16 import shutil | 15 import shutil |
| 17 import stat | |
| 18 import subprocess | 16 import subprocess |
| 19 import urllib2 | 17 import urllib2 |
| 20 import zipfile | 18 import zipfile |
| 21 | 19 |
| 22 | 20 |
| 23 _LOGGER = logging.getLogger(os.path.basename(__file__)) | 21 _LOGGER = logging.getLogger(os.path.basename(__file__)) |
| 24 | 22 |
| 25 # The URL where official builds are archived. | 23 # The URL where official builds are archived. |
| 26 _SYZYGY_ARCHIVE_URL = ('http://syzygy-archive.commondatastorage.googleapis.com/' | 24 _SYZYGY_ARCHIVE_URL = ('http://syzygy-archive.commondatastorage.googleapis.com/' |
| 27 'builds/official/%(revision)s') | 25 'builds/official/%(revision)s') |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 return (stored, False) | 165 return (stored, False) |
| 168 return (stored, True) | 166 return (stored, True) |
| 169 | 167 |
| 170 | 168 |
| 171 def _DirIsEmpty(path): | 169 def _DirIsEmpty(path): |
| 172 """Returns true if the given directory is empty, false otherwise.""" | 170 """Returns true if the given directory is empty, false otherwise.""" |
| 173 for root, dirs, files in os.walk(path): | 171 for root, dirs, files in os.walk(path): |
| 174 return not dirs and not files | 172 return not dirs and not files |
| 175 | 173 |
| 176 | 174 |
| 177 def _RmTreeHandleReadOnly(func, path, exc): | |
| 178 """An error handling function for use with shutil.rmtree. This will | |
| 179 detect failures to remove read-only files, and will change their properties | |
| 180 prior to removing them. This is necessary on Windows as os.remove will return | |
| 181 an access error for read-only files, and git repos contain read-only | |
| 182 pack/index files. | |
| 183 """ | |
| 184 excvalue = exc[1] | |
| 185 if func in (os.rmdir, os.remove) and excvalue.errno == errno.EACCES: | |
| 186 _LOGGER.debug('Removing read-only path: %s', path) | |
| 187 os.chmod(path, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) | |
| 188 func(path) | |
| 189 else: | |
| 190 raise | |
| 191 | |
| 192 | |
| 193 def _RmTree(path): | |
| 194 """A wrapper of shutil.rmtree that handles read-only files.""" | |
| 195 shutil.rmtree(path, ignore_errors=False, onerror=_RmTreeHandleReadOnly) | |
| 196 | |
| 197 | |
| 198 def _CleanState(output_dir, state, dry_run=False): | 175 def _CleanState(output_dir, state, dry_run=False): |
| 199 """Cleans up files/directories in |output_dir| that are referenced by | 176 """Cleans up files/directories in |output_dir| that are referenced by |
| 200 the given |state|. Raises an error if there are local changes. Returns a | 177 the given |state|. Raises an error if there are local changes. Returns a |
| 201 dictionary of files that were deleted. | 178 dictionary of files that were deleted. |
| 202 """ | 179 """ |
| 203 _LOGGER.debug('Deleting files from previous installation.') | 180 _LOGGER.debug('Deleting files from previous installation.') |
| 204 deleted = {} | 181 deleted = {} |
| 205 | 182 |
| 206 # Generate a list of files to delete, relative to |output_dir|. | 183 # Generate a list of files to delete, relative to |output_dir|. |
| 207 contents = state['contents'] | 184 contents = state['contents'] |
| (...skipping 24 matching lines...) Expand all Loading... |
| 232 if not dry_run: | 209 if not dry_run: |
| 233 os.unlink(fullpath) | 210 os.unlink(fullpath) |
| 234 | 211 |
| 235 # Sort directories from longest name to shortest. This lets us remove empty | 212 # Sort directories from longest name to shortest. This lets us remove empty |
| 236 # directories from the most nested paths first. | 213 # directories from the most nested paths first. |
| 237 dirs = sorted(dirs.keys(), key=lambda x: len(x), reverse=True) | 214 dirs = sorted(dirs.keys(), key=lambda x: len(x), reverse=True) |
| 238 for p in dirs: | 215 for p in dirs: |
| 239 if os.path.exists(p) and _DirIsEmpty(p): | 216 if os.path.exists(p) and _DirIsEmpty(p): |
| 240 _LOGGER.debug('Deleting empty directory "%s".', p) | 217 _LOGGER.debug('Deleting empty directory "%s".', p) |
| 241 if not dry_run: | 218 if not dry_run: |
| 242 _RmTree(p) | 219 shutil.rmtree(p, False) |
| 243 | 220 |
| 244 return deleted | 221 return deleted |
| 245 | 222 |
| 246 | 223 |
| 247 def _Download(url): | 224 def _Download(url): |
| 248 """Downloads the given URL and returns the contents as a string.""" | 225 """Downloads the given URL and returns the contents as a string.""" |
| 249 response = urllib2.urlopen(url) | 226 response = urllib2.urlopen(url) |
| 250 if response.code != 200: | 227 if response.code != 200: |
| 251 raise RuntimeError('Failed to download "%s".' % url) | 228 raise RuntimeError('Failed to download "%s".' % url) |
| 252 return response.read() | 229 return response.read() |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 372 _LOGGER.info('Installing revision %s Syzygy binaries.', | 349 _LOGGER.info('Installing revision %s Syzygy binaries.', |
| 373 options.revision[0:12]) | 350 options.revision[0:12]) |
| 374 | 351 |
| 375 # Clean up the old state to begin with. | 352 # Clean up the old state to begin with. |
| 376 deleted = [] | 353 deleted = [] |
| 377 if options.overwrite: | 354 if options.overwrite: |
| 378 if os.path.exists(options.output_dir): | 355 if os.path.exists(options.output_dir): |
| 379 # If overwrite was specified then take a heavy-handed approach. | 356 # If overwrite was specified then take a heavy-handed approach. |
| 380 _LOGGER.debug('Deleting entire installation directory.') | 357 _LOGGER.debug('Deleting entire installation directory.') |
| 381 if not options.dry_run: | 358 if not options.dry_run: |
| 382 _RmTree(options.output_dir) | 359 shutil.rmtree(options.output_dir, False) |
| 383 else: | 360 else: |
| 384 # Otherwise only delete things that the previous installation put in place, | 361 # Otherwise only delete things that the previous installation put in place, |
| 385 # and take care to preserve any local changes. | 362 # and take care to preserve any local changes. |
| 386 deleted = _CleanState(options.output_dir, state, options.dry_run) | 363 deleted = _CleanState(options.output_dir, state, options.dry_run) |
| 387 | 364 |
| 388 # Install the new binaries. In a dry-run this will actually download the | 365 # Install the new binaries. In a dry-run this will actually download the |
| 389 # archives, but it won't write anything to disk. | 366 # archives, but it won't write anything to disk. |
| 390 state = _InstallBinaries(options, deleted) | 367 state = _InstallBinaries(options, deleted) |
| 391 | 368 |
| 392 # Build and save the state for the directory. | 369 # Build and save the state for the directory. |
| 393 _SaveState(options.output_dir, state, options.dry_run) | 370 _SaveState(options.output_dir, state, options.dry_run) |
| 394 | 371 |
| 395 | 372 |
| 396 if __name__ == '__main__': | 373 if __name__ == '__main__': |
| 397 main() | 374 main() |
| OLD | NEW |