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 git command for managing a local cache of git repositories.""" | 6 """A git command for managing a local cache of git repositories.""" |
7 | 7 |
8 from __future__ import print_function | 8 from __future__ import print_function |
9 import errno | 9 import errno |
10 import logging | 10 import logging |
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
342 subprocess.call(['zip', '-r', tmp_zipfile, '.'], cwd=self.mirror_path) | 342 subprocess.call(['zip', '-r', tmp_zipfile, '.'], cwd=self.mirror_path) |
343 gsutil = Gsutil(path=self.gsutil_exe, boto_path=None) | 343 gsutil = Gsutil(path=self.gsutil_exe, boto_path=None) |
344 dest_name = 'gs://%s/%s/%s.zip' % ( | 344 dest_name = 'gs://%s/%s/%s.zip' % ( |
345 self.bootstrap_bucket, self.basedir, gen_number) | 345 self.bootstrap_bucket, self.basedir, gen_number) |
346 gsutil.call('cp', tmp_zipfile, dest_name) | 346 gsutil.call('cp', tmp_zipfile, dest_name) |
347 os.remove(tmp_zipfile) | 347 os.remove(tmp_zipfile) |
348 | 348 |
349 def unlock(self): | 349 def unlock(self): |
350 lf = Lockfile(self.mirror_path) | 350 lf = Lockfile(self.mirror_path) |
351 config_lock = os.path.join(self.mirror_path, 'config.lock') | 351 config_lock = os.path.join(self.mirror_path, 'config.lock') |
352 did_unlock = False | |
352 if os.path.exists(config_lock): | 353 if os.path.exists(config_lock): |
353 os.remove(config_lock) | 354 os.remove(config_lock) |
354 lf.break_lock() | 355 did_unlock = True |
356 if lf.break_lock(): | |
357 did_unlock = True | |
358 return did_unlock | |
359 | |
360 @classmethod | |
361 def UnlockAll(cls): | |
362 cachepath = cls.GetCachePath() | |
363 dirlist = os.listdir(cachepath) | |
364 repo_dirs = set([os.path.join(cachepath, path) for path in dirlist | |
365 if os.path.isdir(os.path.join(cachepath, path))]) | |
366 for dirent in dirlist: | |
367 if (not dirent.endswith('.lock') or | |
Ryan Tseng
2014/05/12 18:45:18
opinion: I think the positive version looks more r
szager1
2014/05/13 20:17:21
Done.
| |
368 not os.path.isfile(os.path.join(cachepath, dirent))): | |
369 continue | |
370 repo_dirs.add(os.path.join(cachepath, dirent[:-5])) | |
371 | |
372 unlocked_repos = [] | |
373 for repo_dir in repo_dirs: | |
374 lf = Lockfile(repo_dir) | |
375 config_lock = os.path.join(repo_dir, 'config.lock') | |
Ryan Tseng
2014/05/12 18:45:18
Theres repeated code here. Lets consolidate them.
szager1
2014/05/13 20:17:21
Done.
| |
376 unlocked = False | |
377 if os.path.exists(config_lock): | |
378 os.remove(config_lock) | |
379 unlocked = True | |
380 if lf.break_lock(): | |
381 unlocked = True | |
382 if unlocked: | |
383 unlocked_repos.append(repo_dir) | |
384 | |
385 return unlocked_repos | |
355 | 386 |
356 @subcommand.usage('[url of repo to check for caching]') | 387 @subcommand.usage('[url of repo to check for caching]') |
357 def CMDexists(parser, args): | 388 def CMDexists(parser, args): |
358 """Check to see if there already is a cache of the given repo.""" | 389 """Check to see if there already is a cache of the given repo.""" |
359 _, args = parser.parse_args(args) | 390 _, args = parser.parse_args(args) |
360 if not len(args) == 1: | 391 if not len(args) == 1: |
361 parser.error('git cache exists only takes exactly one repo url.') | 392 parser.error('git cache exists only takes exactly one repo url.') |
362 url = args[0] | 393 url = args[0] |
363 mirror = Mirror(url) | 394 mirror = Mirror(url) |
364 if mirror.exists(): | 395 if mirror.exists(): |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
420 def CMDunlock(parser, args): | 451 def CMDunlock(parser, args): |
421 """Unlock one or all repos if their lock files are still around.""" | 452 """Unlock one or all repos if their lock files are still around.""" |
422 parser.add_option('--force', '-f', action='store_true', | 453 parser.add_option('--force', '-f', action='store_true', |
423 help='Actually perform the action') | 454 help='Actually perform the action') |
424 parser.add_option('--all', '-a', action='store_true', | 455 parser.add_option('--all', '-a', action='store_true', |
425 help='Unlock all repository caches') | 456 help='Unlock all repository caches') |
426 options, args = parser.parse_args(args) | 457 options, args = parser.parse_args(args) |
427 if len(args) > 1 or (len(args) == 0 and not options.all): | 458 if len(args) > 1 or (len(args) == 0 and not options.all): |
428 parser.error('git cache unlock takes exactly one repo url, or --all') | 459 parser.error('git cache unlock takes exactly one repo url, or --all') |
429 | 460 |
430 repo_dirs = [] | 461 if not options.force: |
431 if not options.all: | |
432 url = args[0] | |
433 repo_dirs.append(Mirror(url).mirror_path) | |
434 else: | |
435 cachepath = Mirror.GetCachePath() | 462 cachepath = Mirror.GetCachePath() |
436 repo_dirs = [os.path.join(cachepath, path) | 463 lockfiles = [os.path.join(cachepath, path) |
437 for path in os.listdir(cachepath) | 464 for path in os.listdir(cachepath) |
438 if os.path.isdir(os.path.join(cachepath, path))] | 465 if path.endswith('.lock') and os.path.isfile(path)] |
439 repo_dirs.extend([os.path.join(cachepath, | |
440 lockfile.replace('.lock', '')) | |
441 for lockfile in os.listdir(cachepath) | |
442 if os.path.isfile(os.path.join(cachepath, | |
443 lockfile)) | |
444 and lockfile.endswith('.lock') | |
445 and os.path.join(cachepath, lockfile) | |
446 not in repo_dirs]) | |
447 lockfiles = [repo_dir + '.lock' for repo_dir in repo_dirs | |
448 if os.path.exists(repo_dir + '.lock')] | |
449 | |
450 if not options.force: | |
451 parser.error('git cache unlock requires -f|--force to do anything. ' | 466 parser.error('git cache unlock requires -f|--force to do anything. ' |
452 'Refusing to unlock the following repo caches: ' | 467 'Refusing to unlock the following repo caches: ' |
453 ', '.join(lockfiles)) | 468 ', '.join(lockfiles)) |
454 | 469 |
455 unlocked_repos = [] | 470 unlocked_repos = [] |
456 untouched_repos = [] | 471 if options.all: |
457 for repo_dir in repo_dirs: | 472 unlocked_repos.extend(Mirror.UnlockAll()) |
458 lf = Lockfile(repo_dir) | 473 else: |
459 config_lock = os.path.join(repo_dir, 'config.lock') | 474 m = Mirror(args[0]) |
460 unlocked = False | 475 if m.unlock(): |
461 if os.path.exists(config_lock): | 476 unlocked_repos.append(m.mirror_path) |
462 os.remove(config_lock) | |
463 unlocked = True | |
464 if lf.break_lock(): | |
465 unlocked = True | |
466 | |
467 if unlocked: | |
468 unlocked_repos.append(repo_dir) | |
469 else: | |
470 untouched_repos.append(repo_dir) | |
471 | 477 |
472 if unlocked_repos: | 478 if unlocked_repos: |
473 logging.info('Broke locks on these caches:\n %s' % '\n '.join( | 479 logging.info('Broke locks on these caches:\n %s' % '\n '.join( |
474 unlocked_repos)) | 480 unlocked_repos)) |
475 if untouched_repos: | |
476 logging.debug('Did not touch these caches:\n %s' % '\n '.join( | |
477 untouched_repos)) | |
478 | 481 |
479 | 482 |
480 class OptionParser(optparse.OptionParser): | 483 class OptionParser(optparse.OptionParser): |
481 """Wrapper class for OptionParser to handle global options.""" | 484 """Wrapper class for OptionParser to handle global options.""" |
482 | 485 |
483 def __init__(self, *args, **kwargs): | 486 def __init__(self, *args, **kwargs): |
484 optparse.OptionParser.__init__(self, *args, prog='git cache', **kwargs) | 487 optparse.OptionParser.__init__(self, *args, prog='git cache', **kwargs) |
485 self.add_option('-c', '--cache-dir', | 488 self.add_option('-c', '--cache-dir', |
486 help='Path to the directory containing the cache') | 489 help='Path to the directory containing the cache') |
487 self.add_option('-v', '--verbose', action='count', default=0, | 490 self.add_option('-v', '--verbose', action='count', default=0, |
(...skipping 19 matching lines...) Expand all Loading... | |
507 return options, args | 510 return options, args |
508 | 511 |
509 | 512 |
510 def main(argv): | 513 def main(argv): |
511 dispatcher = subcommand.CommandDispatcher(__name__) | 514 dispatcher = subcommand.CommandDispatcher(__name__) |
512 return dispatcher.execute(OptionParser(), argv) | 515 return dispatcher.execute(OptionParser(), argv) |
513 | 516 |
514 | 517 |
515 if __name__ == '__main__': | 518 if __name__ == '__main__': |
516 sys.exit(main(sys.argv[1:])) | 519 sys.exit(main(sys.argv[1:])) |
OLD | NEW |