home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 May / maximum-cd-2009-05.iso / DiscContents / XBMC_for_Windows-8.10.exe / system / python / spyce / spyceLock.py < prev    next >
Encoding:
Python Source  |  2008-11-03  |  3.2 KB  |  121 lines

  1. ##################################################
  2. # SPYCE - Python-based HTML Scripting
  3. # Copyright (c) 2002 Rimon Barr.
  4. #
  5. # Refer to spyce.py
  6. # CVS: $Id: spyceLock.py 5659 2006-04-27 16:15:15Z jwnmulder $
  7. ##################################################
  8.  
  9. import os
  10.  
  11. __doc__ = 'Spyce locking-related functions'
  12.  
  13. ##################################################
  14. # Generic lock
  15. #
  16.  
  17. class genericLock:
  18.   def lock(self, block=1):
  19.     "return true, if lock acquired"
  20.     raise 'not implemented'
  21.   def unlock(self):
  22.     raise 'not implemented'
  23.   def locked(self):
  24.     raise 'not implemented'
  25.  
  26. ##################################################
  27. # Dummy lock
  28. #
  29.  
  30. class dummyLock(genericLock):
  31.   def lock(self, block=1):
  32.     return 1
  33.   def unlock(self):
  34.     pass
  35.   def locked(self):
  36.     return 0
  37.  
  38. ##################################################
  39. # Thread locking
  40. #
  41.  
  42. class threadLock(genericLock):
  43.   def __init__(self):
  44.     import thread
  45.     self._thelock = thread.allocate_lock()
  46.   def lock(self, block=1):
  47.     if block: return self._thelock.acquire()
  48.     else: return self._thelock.acquire(0)
  49.   def unlock(self):
  50.     return self._thelock.release()
  51.   def locked(self):
  52.     return self._thelock.locked()
  53.  
  54. ##################################################
  55. # File locking
  56. #
  57.  
  58. # Adapted from portalocker.py, written by Jonathan Feinberg <jdf@pobox.com>
  59. # Used in rimap (http://rimap.sourceforge.net) before Spyce
  60. # Methods:
  61. #   file_lock(file, flags)
  62. #   file_unlock(file)
  63. # Constants: LOCK_EX, LOCK_SH, LOCK_NB
  64. # -- RB
  65.  
  66. try:
  67.   if os.name=='nt':
  68.     import win32con, win32file, pywintypes
  69.     LOCK_EX = win32con.LOCKFILE_EXCLUSIVE_LOCK
  70.     LOCK_SH = 0 # the default
  71.     LOCK_NB = win32con.LOCKFILE_FAIL_IMMEDIATELY
  72.     # is there any reason not to reuse the following structure?
  73.     __overlapped = pywintypes.OVERLAPPED()
  74.     def file_lock(file, flags):
  75.       hfile = win32file._get_osfhandle(file.fileno())
  76.       win32file.LockFileEx(hfile, flags, 0, 0xffff0000, __overlapped)
  77.     def file_unlock(file):
  78.       hfile = win32file._get_osfhandle(file.fileno())
  79.       win32file.UnlockFileEx(hfile, 0, 0xffff0000, __overlapped)
  80.   elif os.name == 'posix':
  81.     import fcntl
  82.     LOCK_EX = fcntl.LOCK_EX
  83.     LOCK_SH = fcntl.LOCK_SH
  84.     LOCK_NB = fcntl.LOCK_NB
  85.     def file_lock(file, flags):
  86.       fcntl.flock(file.fileno(), flags)
  87.     def file_unlock(file):
  88.       fcntl.flock(file.fileno(), fcntl.LOCK_UN)
  89.   else:
  90.     raise 'locking not supported on this platform'
  91. except:
  92.   LOCK_EX = 0
  93.   LOCK_SH = 0
  94.   LOCK_NB = 0
  95.   # bring on the race conditions! :)
  96.   def file_lock(file, flags): pass
  97.   def file_unlock(file): pass
  98.  
  99. class fileLock(genericLock):
  100.   f=name=None
  101.   def __init__(self, name):
  102.     self.name=name+'.lock'
  103.     self._locked = 0
  104.   def lock(self, block=1):
  105.     self.f=open(self.name, 'w')
  106.     if block: file_lock(self.f, LOCK_EX)
  107.     else: file_lock(self.f, LOCK_EX or LOCK_NB)
  108.     self._locked = 1
  109.   def unlock(self):
  110.     try:
  111.       if not self.f: return
  112.       file_unlock(self.f)
  113.       self.f.close()
  114.       os.unlink(self.name)
  115.       self.f=None
  116.       self._locked = 0
  117.     except: pass
  118.   def locked(self):
  119.     return self._locked
  120.  
  121.