annotate rhodecode/lib/vcs/utils/lockfiles.py @ 4186:7e5f8c12a3fc kallithea-2.2.5-rebrand

First step in two-part process to rename directories to kallithea. This first step is to change all references in the files where they refer to the old directory name.
author Bradley M. Kuhn <bkuhn@sfconservancy.org>
date Wed, 02 Jul 2014 19:04:28 -0400
parents 95a226b35b91
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2007
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1 import os
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
2
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
3
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
4 class LockFile(object):
3449
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
5 """Provides methods to obtain, check for, and release a file based lock which
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
6 should be used to handle concurrent access to the same file.
2007
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
7
3449
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
8 As we are a utility class to be derived from, we only use protected methods.
2007
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
9
3449
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
10 Locks will automatically be released on destruction"""
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
11 __slots__ = ("_file_path", "_owns_lock")
2007
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
12
3449
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
13 def __init__(self, file_path):
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
14 self._file_path = file_path
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
15 self._owns_lock = False
2007
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
16
3449
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
17 def __del__(self):
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
18 self._release_lock()
2007
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
19
3449
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
20 def _lock_file_path(self):
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
21 """:return: Path to lockfile"""
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
22 return "%s.lock" % (self._file_path)
2007
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
23
3449
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
24 def _has_lock(self):
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
25 """:return: True if we have a lock and if the lockfile still exists
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
26 :raise AssertionError: if our lock-file does not exist"""
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
27 if not self._owns_lock:
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
28 return False
2007
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
29
3449
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
30 return True
2007
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
31
3449
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
32 def _obtain_lock_or_raise(self):
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
33 """Create a lock file as flag for other instances, mark our instance as lock-holder
2007
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
34
3449
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
35 :raise IOError: if a lock was already present or a lock file could not be written"""
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
36 if self._has_lock():
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
37 return
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
38 lock_file = self._lock_file_path()
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
39 if os.path.isfile(lock_file):
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
40 raise IOError("Lock for file %r did already exist, delete %r in case the lock is illegal" % (self._file_path, lock_file))
2007
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
41
3449
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
42 try:
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
43 fd = os.open(lock_file, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0)
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
44 os.close(fd)
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
45 except OSError,e:
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
46 raise IOError(str(e))
2007
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
47
3449
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
48 self._owns_lock = True
2007
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
49
3449
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
50 def _obtain_lock(self):
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
51 """The default implementation will raise if a lock cannot be obtained.
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
52 Subclasses may override this method to provide a different implementation"""
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
53 return self._obtain_lock_or_raise()
2007
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
54
3449
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
55 def _release_lock(self):
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
56 """Release our lock if we have one"""
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
57 if not self._has_lock():
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
58 return
2007
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
59
3449
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
60 # if someone removed our file beforhand, lets just flag this issue
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
61 # instead of failing, to make it more usable.
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
62 lfp = self._lock_file_path()
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
63 try:
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
64 # on bloody windows, the file needs write permissions to be removable.
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
65 # Why ...
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
66 if os.name == 'nt':
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
67 os.chmod(lfp, 0777)
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
68 # END handle win32
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
69 os.remove(lfp)
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
70 except OSError:
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
71 pass
95a226b35b91 codecleaner, fix tabs -> spaces
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
72 self._owns_lock = False