/usr/lib/python2.7/dist-packages/mx/Misc/FileLock.py is in python-egenix-mxtools 3.2.7-1build1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 | #! /usr/bin/python -u
""" FileLock - Implements a file lock mechanism that does not depend
on fcntl.
Copyright (c) 1997-2000, Marc-Andre Lemburg; mailto:mal@lemburg.com
Copyright (c) 2000-2013, eGenix.com Software GmbH; mailto:info@egenix.com
See the documentation for further information on copyrights,
or contact the author. All Rights Reserved.
"""
from mx.Misc.ExitFunctions import ExitFunctions
import os,exceptions,time
# Version
__version__ = '1.0'
# Get fully qualified hostname
def _fqhostname(hostname=None,default=('localhost','127.0.0.1')):
""" Returns fully qualified (hostname, ip) for the given hostname.
If hostname is not given, the default name of the local host
is chosen.
Defaults to default in case an error occurs while trying to
determine the data.
"""
try:
import socket
except ImportError:
return default
try:
if hostname is None:
hostname = socket.gethostname()
ip = socket.gethostbyname(hostname)
hostname = socket.gethostbyaddr(ip)[0]
except socket.error:
return default
else:
return hostname,ip
hostname,ip = _fqhostname()
### Errors
class Error(exceptions.StandardError):
pass
# Backward compatibility:
FileLockError = Error
### File lock using symbolic links
class SymbolicFileLock:
""" Implements a file lock mechanism.
The base class implements the locking mechanism using symbolic
links.
Note that since the mechanism does not use file system
function calls this may not always work in the desired
way.
The lock is acquired per process, not per thread.
Instancevariables:
filename - file the lock applies to
lockfilename - name of the lock file
locked - indicator if the lock is in position (1) or not (0)
"""
# Do we hold the lock ?
locked = 0
# Lock timeout in seconds (0 = never)
locktimeout = 0
def __init__(self, filename):
self.filename = filename
self.lockfilename = filename + '.locked'
self.locked = 0
# Avoid deadlocks
ExitFunctions.register(self.unlock)
def __del__(self):
if self.locked:
self.unlock(0)
try:
ExitFunctions.deregister(self.unlock)
except:
pass
def lock(self,timeout=500,sleeptime=0.0001,
sleep=time.sleep,Error=Error,time=time.time,error=os.error,
hostname=hostname,ip=ip):
""" Try to lock the file for this process, waiting
timeout ms if necessary.
Raises an exception if a timeout occurs. Multiple locking
by the same process is not an error.
Note that a non existent path to the file will also result
in a timeout.
If the lock is held by a process running on our host, a
timeout will first invoke a check of the locking
process. If it is not alive anymore, the lock is removed
and granted to the current process.
"""
if self.locked:
return
lockfilename = self.lockfilename
lockinfo = '%s:%i' % (hostname,os.getpid())
stop = time() + timeout * 0.001
# Localize these for speed
islink=os.path.islink
makelink=os.symlink
readlink=os.readlink
while 1:
# These are rather time-critical
if not islink(lockfilename):
try:
makelink(lockinfo,lockfilename)
except error:
# A non existent path will result in a time out.
pass
else:
break
sleep(sleeptime)
if time() > stop:
# Timeout... check whether it's a valid lock
if not self.validate_lock():
continue
host, locking_pid = self.lock_info()
raise Error,\
'file "%s" is locked by process %s:%i' % \
(self.filename, host, locking_pid)
self.locked = 1
def unlock(self,sleeptime=0.0001,
unlink=os.unlink,Error=Error,sleep=time.sleep,error=os.error):
""" Release the lock, letting other processes using this
mechanism access the file.
Multiple unlocking is not an error. Raises an exception if
the lock file was already deleted by another process.
After having unlocked the file the process sleeps for
sleeptime seconds to give other processes a chance to
acquire the lock too. If the lock will only be used every
once in a while by the process, it is safe to set it to 0.
"""
if not self.locked:
return
self.locked = 0
try:
unlink(self.lockfilename)
except error:
raise Error,'file lock "%s" is already gone' % \
self.lockfilename
# Give other processes a chance too
if sleeptime:
sleep(sleeptime)
return 1
def has_lock(self):
""" Returns the current state of the file lock: 1 - a lock
exists, 0 - no lock exists.
Note that in case a lock exists, this lock is not checked
for being valid.
"""
if self.locked:
return 1
if os.path.islink(self.lockfilename):
return 1
return 0
def lock_info(self):
""" Returns a tuple (hostname, PID integer) indicating the
host and process id currently holding the lock.
An Error is raised if no lock exists.
"""
try:
host,locking_pid = os.readlink(self.lockfilename).split(':')
except os.error,why:
raise Error,\
'file "%s" could not be locked: %s' % \
(self.filename,why)
locking_pid = int(locking_pid)
return (host, locking_pid)
def validate_lock(self):
""" Validates a lock on the file and return 1 for a valid lock,
0 for an invalid one.
Note that it is only possible to check for valid locks
which are owned by the same host. This method removes any
invalid locks it may find.
An Error is raised if no lock exists.
"""
# Check for lock timeouts
if self.locktimeout:
ctime = self.lock_time()
if ctime < time.time() - self.locktimeout:
# Timed out
try:
os.unlink(self.lockfilename)
except os.error, why:
# We probably don't have proper permissions.
return 1
else:
return 0
# Check process
host, locking_pid = self.lock_info()
if host != hostname:
# Ok, then compare via IPs
other_ip = _fqhostname(host, default=('???','???'))[1]
samehost = (ip == other_ip)
else:
samehost = 1
if samehost:
# Check whether the locking process is still alive
try:
os.kill(locking_pid, 0)
except os.error, why:
# It's gone without a trace...
try:
os.unlink(self.lockfilename)
except os.error:
# We probably don't have proper permissions.
pass
else:
return 0
return 1
def lock_time(self):
""" Returns a Unix time value indicating the time when the
current lock was created.
An Error is raised if no lock exists.
"""
try:
ctime = os.lstat(self.lockfilename)[9]
except os.error, why:
raise Error,\
'could not read file lock info for "%s": %s' % \
(self.filename, why)
return ctime
def remove_lock(self,
unlink=os.unlink):
""" Remove any existing lock on the file.
"""
self.locked = 0
try:
unlink(self.lockfilename)
except:
pass
def __repr__(self):
return '<%s for "%s" at %x>' % (self.__class__.__name__,
self.filename,
id(self))
# Alias
BaseFileLock = SymbolicFileLock
### File lock using directories
class DirectoryFileLock(BaseFileLock):
""" This class implements a file lock mechanism that uses
temporary directories for locking.
See FileLock for documentation of the various methods.
Thanks to Thomas Heller for this idea !
"""
def lock(self,timeout=500,sleeptime=0.0001,
sleep=time.sleep,Error=Error,time=time.time,error=os.error,
hostname=hostname,ip=ip,mkdir=os.mkdir):
if self.locked:
return
lockfilename = self.lockfilename
lockinfo = '%s:%i' % (hostname,os.getpid())
stop = time() + timeout * 0.001
while 1:
# These are rather time-critical
try:
mkdir(lockfilename)
except error:
# A non existent path will result in a time out.
pass
else:
break
sleep(sleeptime)
if time() > stop:
# Timeout... check whether it's a valid lock
if not self.validate_lock():
continue
raise Error,\
'file "%s" is currently locked' % self.filename
self.locked = 1
def unlock(self,sleeptime=0.0001,
rmdir=os.rmdir,Error=Error,sleep=time.sleep,error=os.error):
if not self.locked:
return
self.locked = 0
try:
rmdir(self.lockfilename)
except error:
raise Error,'file lock "%s" is already gone' % \
self.lockfilename
# Give other processes a chance too
if sleeptime:
sleep(sleeptime)
return 1
def has_lock(self):
if self.locked:
return 1
if os.path.isdir(self.lockfilename):
return 1
return 0
def validate_lock(self):
# Check for lock timeouts
if self.locktimeout:
ctime = self.lock_time()
if ctime < time.time() - self.locktimeout:
# Timed out
try:
os.rmdir(self.lockfilename)
except os.error, why:
# We probably don't have proper permissions.
return 1
else:
return 0
return 1
def lock_info(self):
""" Locking info is not available for DirectoryFileLocks.
A TypeError is raised in case this method is called.
"""
raise TypeError, \
'.lock_info() is not implemented for DirectoryFileLocks'
def lock_time(self):
try:
ctime = os.stat(self.lockfilename)[9]
except os.error, why:
raise Error,\
'could not read file lock info for "%s": %s' % \
(self.filename, why)
return ctime
def remove_lock(self,
rmdir=os.rmdir):
self.locked = 0
try:
rmdir(self.lockfilename)
except:
pass
# For b/w compatibility
DirectyFileLock = DirectoryFileLock
### Generic file lock mechanism
if hasattr(os, 'symlink'):
FileLock = SymbolicFileLock
else:
FileLock = DirectoryFileLock
def _test():
#lock = SymbolicFileLock('test-lock')
#lock = DirectoryFileLock('test-lock')
lock = FileLock('test-lock')
starttime = time.time()
try:
for i in range(10000):
print '%i\r'%i,
lock.lock()
time.sleep(i/100000.0)
lock.unlock()
#time.sleep(i/100000.0)
except KeyboardInterrupt:
lock.unlock()
totaltime = time.time() - starttime
print '%i lock/release cycles in %5.2f sec. = %i cycles/sec.' % \
(i, totaltime, i / totaltime)
if __name__ == '__main__':
_test()
|