Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1151)

Side by Side Diff: git_cache.py

Issue 335253002: Clean up temp dirs when breaking locks. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/depot_tools
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 260 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 # Get the most recent version of the zipfile. 271 # Get the most recent version of the zipfile.
272 _, ls_out, _ = gsutil.check_call('ls', gs_folder) 272 _, ls_out, _ = gsutil.check_call('ls', gs_folder)
273 ls_out_sorted = sorted(ls_out.splitlines()) 273 ls_out_sorted = sorted(ls_out.splitlines())
274 if not ls_out_sorted: 274 if not ls_out_sorted:
275 # This repo is not on Google Storage. 275 # This repo is not on Google Storage.
276 return False 276 return False
277 latest_checkout = ls_out_sorted[-1] 277 latest_checkout = ls_out_sorted[-1]
278 278
279 # Download zip file to a temporary directory. 279 # Download zip file to a temporary directory.
280 try: 280 try:
281 tempdir = tempfile.mkdtemp() 281 tempdir = tempfile.mkdtemp(prefix='_cache_tmp', dir=self.GetCachePath())
282 self.print('Downloading %s' % latest_checkout) 282 self.print('Downloading %s' % latest_checkout)
283 code = gsutil.call('cp', latest_checkout, tempdir) 283 code = gsutil.call('cp', latest_checkout, tempdir)
284 if code: 284 if code:
285 return False 285 return False
286 filename = os.path.join(tempdir, latest_checkout.split('/')[-1]) 286 filename = os.path.join(tempdir, latest_checkout.split('/')[-1])
287 287
288 # Unpack the file with 7z on Windows, unzip on linux, or fallback. 288 # Unpack the file with 7z on Windows, unzip on linux, or fallback.
289 if not python_fallback: 289 if not python_fallback:
290 if sys.platform.startswith('win'): 290 if sys.platform.startswith('win'):
291 cmd = ['7z', 'x', '-o%s' % directory, '-tzip', filename] 291 cmd = ['7z', 'x', '-o%s' % directory, '-tzip', filename]
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 config_file = os.path.join(self.mirror_path, 'config') 337 config_file = os.path.join(self.mirror_path, 'config')
338 pack_dir = os.path.join(self.mirror_path, 'objects', 'pack') 338 pack_dir = os.path.join(self.mirror_path, 'objects', 'pack')
339 pack_files = [] 339 pack_files = []
340 if os.path.isdir(pack_dir): 340 if os.path.isdir(pack_dir):
341 pack_files = [f for f in os.listdir(pack_dir) if f.endswith('.pack')] 341 pack_files = [f for f in os.listdir(pack_dir) if f.endswith('.pack')]
342 342
343 should_bootstrap = (not os.path.exists(config_file) or 343 should_bootstrap = (not os.path.exists(config_file) or
344 len(pack_files) > GC_AUTOPACKLIMIT) 344 len(pack_files) > GC_AUTOPACKLIMIT)
345 if should_bootstrap: 345 if should_bootstrap:
346 tempdir = tempfile.mkdtemp( 346 tempdir = tempfile.mkdtemp(
347 suffix=self.basedir, dir=self.GetCachePath()) 347 prefix='_cache_tmp', suffix=self.basedir, dir=self.GetCachePath())
348 bootstrapped = not depth and bootstrap and self.bootstrap_repo(tempdir) 348 bootstrapped = not depth and bootstrap and self.bootstrap_repo(tempdir)
349 if bootstrapped: 349 if bootstrapped:
350 # Bootstrap succeeded; delete previous cache, if any. 350 # Bootstrap succeeded; delete previous cache, if any.
351 gclient_utils.rmtree(self.mirror_path) 351 gclient_utils.rmtree(self.mirror_path)
352 elif not os.path.exists(config_file): 352 elif not os.path.exists(config_file):
353 # Bootstrap failed, no previous cache; start with a bare git dir. 353 # Bootstrap failed, no previous cache; start with a bare git dir.
354 self.RunGit(['init', '--bare'], cwd=tempdir) 354 self.RunGit(['init', '--bare'], cwd=tempdir)
355 else: 355 else:
356 # Bootstrap failed, previous cache exists; warn and continue. 356 # Bootstrap failed, previous cache exists; warn and continue.
357 logging.warn( 357 logging.warn(
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
401 pack_files = [f for f in os.listdir(pack_dir) if 401 pack_files = [f for f in os.listdir(pack_dir) if
402 f.startswith('.tmp-') or f.startswith('tmp_pack_')] 402 f.startswith('.tmp-') or f.startswith('tmp_pack_')]
403 for f in pack_files: 403 for f in pack_files:
404 f = os.path.join(pack_dir, f) 404 f = os.path.join(pack_dir, f)
405 try: 405 try:
406 os.remove(f) 406 os.remove(f)
407 logging.warn('Deleted stale temporary pack file %s' % f) 407 logging.warn('Deleted stale temporary pack file %s' % f)
408 except OSError: 408 except OSError:
409 logging.warn('Unable to delete temporary pack file %s' % f) 409 logging.warn('Unable to delete temporary pack file %s' % f)
410 410
411 @staticmethod 411 @classmethod
412 def BreakLocks(path): 412 def BreakLocks(cls, path):
413 did_unlock = False 413 did_unlock = False
414 lf = Lockfile(path) 414 lf = Lockfile(path)
415 if lf.break_lock(): 415 if lf.break_lock():
416 did_unlock = True 416 did_unlock = True
417 # Look for lock files that might have been left behind by an interrupted 417 # Look for lock files that might have been left behind by an interrupted
418 # git process. 418 # git process.
419 lf = os.path.join(path, 'config.lock') 419 lf = os.path.join(path, 'config.lock')
420 if os.path.exists(lf): 420 if os.path.exists(lf):
421 os.remove(lf) 421 os.remove(lf)
422 did_unlock = True 422 did_unlock = True
423 cls.DeleteTmpPackFiles(path)
423 return did_unlock 424 return did_unlock
424 425
425 def unlock(self): 426 def unlock(self):
426 return self.BreakLocks(self.mirror_path) 427 return self.BreakLocks(self.mirror_path)
427 428
428 @classmethod 429 @classmethod
429 def UnlockAll(cls): 430 def UnlockAll(cls):
430 cachepath = cls.GetCachePath() 431 cachepath = cls.GetCachePath()
431 dirlist = os.listdir(cachepath) 432 dirlist = os.listdir(cachepath)
432 repo_dirs = set([os.path.join(cachepath, path) for path in dirlist 433 repo_dirs = set([os.path.join(cachepath, path) for path in dirlist
433 if os.path.isdir(os.path.join(cachepath, path))]) 434 if os.path.isdir(os.path.join(cachepath, path))])
434 for dirent in dirlist: 435 for dirent in dirlist:
435 if (dirent.endswith('.lock') and 436 if dirent.startswith('_cache_tmp') or dirent.startswith('tmp'):
437 gclient_utils.rmtree(os.path.join(cachepath, dirent))
438 elif (dirent.endswith('.lock') and
436 os.path.isfile(os.path.join(cachepath, dirent))): 439 os.path.isfile(os.path.join(cachepath, dirent))):
437 repo_dirs.add(os.path.join(cachepath, dirent[:-5])) 440 repo_dirs.add(os.path.join(cachepath, dirent[:-5]))
438 441
439 unlocked_repos = [] 442 unlocked_repos = []
440 for repo_dir in repo_dirs: 443 for repo_dir in repo_dirs:
441 if cls.BreakLocks(repo_dir): 444 if cls.BreakLocks(repo_dir):
442 unlocked_repos.append(repo_dir) 445 unlocked_repos.append(repo_dir)
443 446
444 return unlocked_repos 447 return unlocked_repos
445 448
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
573 return options, args 576 return options, args
574 577
575 578
576 def main(argv): 579 def main(argv):
577 dispatcher = subcommand.CommandDispatcher(__name__) 580 dispatcher = subcommand.CommandDispatcher(__name__)
578 return dispatcher.execute(OptionParser(), argv) 581 return dispatcher.execute(OptionParser(), argv)
579 582
580 583
581 if __name__ == '__main__': 584 if __name__ == '__main__':
582 sys.exit(main(sys.argv[1:])) 585 sys.exit(main(sys.argv[1:]))
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698