Mercurial > hg
comparison mercurial/lock.py @ 26290:9664d32bd6cb
lock: factor code to read lock into a separate function
This is going to be needed for upcoming work with lock inheritance.
author | Siddharth Agarwal <sid0@fb.com> |
---|---|
date | Wed, 16 Sep 2015 19:26:59 -0700 |
parents | b188f60bd955 |
children | 1d33842c5b3e |
comparison
equal
deleted
inserted
replaced
26289:c4b667a7a51d | 26290:9664d32bd6cb |
---|---|
98 locker) | 98 locker) |
99 else: | 99 else: |
100 raise error.LockUnavailable(why.errno, why.strerror, | 100 raise error.LockUnavailable(why.errno, why.strerror, |
101 why.filename, self.desc) | 101 why.filename, self.desc) |
102 | 102 |
103 def _readlock(self): | |
104 """read lock and return its value | |
105 | |
106 Returns None if no lock exists, pid for old-style locks, and host:pid | |
107 for new-style locks. | |
108 """ | |
109 try: | |
110 return self.vfs.readlock(self.f) | |
111 except (OSError, IOError) as why: | |
112 if why.errno == errno.ENOENT: | |
113 return None | |
114 raise | |
115 | |
103 def testlock(self): | 116 def testlock(self): |
104 """return id of locker if lock is valid, else None. | 117 """return id of locker if lock is valid, else None. |
105 | 118 |
106 If old-style lock, we cannot tell what machine locker is on. | 119 If old-style lock, we cannot tell what machine locker is on. |
107 with new-style lock, if locker is on this machine, we can | 120 with new-style lock, if locker is on this machine, we can |
109 not alive, we can safely break lock. | 122 not alive, we can safely break lock. |
110 | 123 |
111 The lock file is only deleted when None is returned. | 124 The lock file is only deleted when None is returned. |
112 | 125 |
113 """ | 126 """ |
114 try: | 127 locker = self._readlock() |
115 locker = self.vfs.readlock(self.f) | 128 if locker is None: |
116 except (OSError, IOError) as why: | 129 return None |
117 if why.errno == errno.ENOENT: | |
118 return None | |
119 raise | |
120 try: | 130 try: |
121 host, pid = locker.split(":", 1) | 131 host, pid = locker.split(":", 1) |
122 except ValueError: | 132 except ValueError: |
123 return locker | 133 return locker |
124 if host != lock._host: | 134 if host != lock._host: |