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 |
11 import optparse | 11 import optparse |
12 import os | 12 import os |
13 import tempfile | 13 import tempfile |
14 import time | |
14 import subprocess | 15 import subprocess |
15 import sys | 16 import sys |
16 import urlparse | 17 import urlparse |
17 | 18 |
18 from download_from_google_storage import Gsutil | 19 from download_from_google_storage import Gsutil |
19 import gclient_utils | 20 import gclient_utils |
20 import subcommand | 21 import subcommand |
21 | 22 |
22 try: | 23 try: |
23 # pylint: disable=E0602 | 24 # pylint: disable=E0602 |
(...skipping 29 matching lines...) Expand all Loading... | |
53 | 54 |
54 def _make_lockfile(self): | 55 def _make_lockfile(self): |
55 """Safely creates a lockfile containing the current pid.""" | 56 """Safely creates a lockfile containing the current pid.""" |
56 open_flags = (os.O_CREAT | os.O_EXCL | os.O_WRONLY) | 57 open_flags = (os.O_CREAT | os.O_EXCL | os.O_WRONLY) |
57 fd = os.open(self.lockfile, open_flags, 0o644) | 58 fd = os.open(self.lockfile, open_flags, 0o644) |
58 f = os.fdopen(fd, 'w') | 59 f = os.fdopen(fd, 'w') |
59 print(self.pid, file=f) | 60 print(self.pid, file=f) |
60 f.close() | 61 f.close() |
61 | 62 |
62 def _remove_lockfile(self): | 63 def _remove_lockfile(self): |
63 """Delete the lockfile. Complains (implicitly) if it doesn't exist.""" | 64 """Delete the lockfile. Complains (implicitly) if it doesn't exist. |
64 os.remove(self.lockfile) | 65 |
66 See gclient_utils.py:rmtree docstring for more explanation on the | |
67 windows case. | |
68 """ | |
69 if sys.platform == 'win32': | |
70 lockfile = os.path.normcase(self.lockfile) | |
71 for _ in xrange(3): | |
72 exitcode = subprocess.call(['cmd.exe', '/c', | |
73 'del', '/f', '/q', lockfile]) | |
74 if exitcode == 0: | |
75 return | |
76 time.sleep(3) | |
77 raise LockError('Failed to remove lock: %s' % lockfile) | |
Ryan Tseng
2014/04/17 08:20:22
I think you want to put this outside the for loop
| |
78 else: | |
79 os.remove(self.lockfile) | |
65 | 80 |
66 def lock(self): | 81 def lock(self): |
67 """Acquire the lock. | 82 """Acquire the lock. |
68 | 83 |
69 Note: This is a NON-BLOCKING FAIL-FAST operation. | 84 Note: This is a NON-BLOCKING FAIL-FAST operation. |
70 Do. Or do not. There is no try. | 85 Do. Or do not. There is no try. |
71 """ | 86 """ |
72 try: | 87 try: |
73 self._make_lockfile() | 88 self._make_lockfile() |
74 except OSError as e: | 89 except OSError as e: |
(...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
490 return options, args | 505 return options, args |
491 | 506 |
492 | 507 |
493 def main(argv): | 508 def main(argv): |
494 dispatcher = subcommand.CommandDispatcher(__name__) | 509 dispatcher = subcommand.CommandDispatcher(__name__) |
495 return dispatcher.execute(OptionParser(), argv) | 510 return dispatcher.execute(OptionParser(), argv) |
496 | 511 |
497 | 512 |
498 if __name__ == '__main__': | 513 if __name__ == '__main__': |
499 sys.exit(main(sys.argv[1:])) | 514 sys.exit(main(sys.argv[1:])) |
OLD | NEW |