Package web2py :: Package gluon :: Module portalocker
[hide private]
[frames] | no frames]

Source Code for Module web2py.gluon.portalocker

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3  # portalocker.py - Cross-platform (posix/nt) API for flock-style file locking. 
  4  #                  Requires python 1.5.2 or better. 
  5   
  6  """ 
  7  Cross-platform (posix/nt) API for flock-style file locking. 
  8   
  9  Synopsis: 
 10   
 11     import portalocker 
 12     file = open(\"somefile\", \"r+\") 
 13     portalocker.lock(file, portalocker.LOCK_EX) 
 14     file.seek(12) 
 15     file.write(\"foo\") 
 16     file.close() 
 17   
 18  If you know what you're doing, you may choose to 
 19   
 20     portalocker.unlock(file) 
 21   
 22  before closing the file, but why? 
 23   
 24  Methods: 
 25   
 26     lock( file, flags ) 
 27     unlock( file ) 
 28   
 29  Constants: 
 30   
 31     LOCK_EX 
 32     LOCK_SH 
 33     LOCK_NB 
 34   
 35  I learned the win32 technique for locking files from sample code 
 36  provided by John Nielsen <nielsenjf@my-deja.com> in the documentation 
 37  that accompanies the win32 modules. 
 38   
 39  Author: Jonathan Feinberg <jdf@pobox.com> 
 40  Version: $Id: portalocker.py,v 1.3 2001/05/29 18:47:55 Administrator Exp $ 
 41  """ 
 42   
 43  import os 
 44  import logging 
 45  import platform 
 46  logger = logging.getLogger("web2py") 
 47   
 48  os_locking = None 
 49  try: 
 50      import fcntl 
 51      os_locking = 'posix' 
 52  except: 
 53      pass 
 54  try: 
 55      import win32con 
 56      import win32file 
 57      import pywintypes 
 58      os_locking = 'windows' 
 59  except: 
 60      pass 
 61   
 62  if os_locking == 'windows': 
 63      LOCK_EX = win32con.LOCKFILE_EXCLUSIVE_LOCK 
 64      LOCK_SH = 0  # the default 
 65      LOCK_NB = win32con.LOCKFILE_FAIL_IMMEDIATELY 
 66   
 67      # is there any reason not to reuse the following structure? 
 68   
 69      __overlapped = pywintypes.OVERLAPPED() 
 70   
71 - def lock(file, flags):
72 hfile = win32file._get_osfhandle(file.fileno()) 73 win32file.LockFileEx(hfile, flags, 0, 0x7fff0000, __overlapped)
74
75 - def unlock(file):
76 hfile = win32file._get_osfhandle(file.fileno()) 77 win32file.UnlockFileEx(hfile, 0, 0x7fff0000, __overlapped)
78 79 80 elif os_locking == 'posix': 81 LOCK_EX = fcntl.LOCK_EX 82 LOCK_SH = fcntl.LOCK_SH 83 LOCK_NB = fcntl.LOCK_NB 84
85 - def lock(file, flags):
86 fcntl.flock(file.fileno(), flags)
87
88 - def unlock(file):
89 fcntl.flock(file.fileno(), fcntl.LOCK_UN)
90 91 92 else: 93 if platform.system() == 'Windows': 94 logger.error('no file locking, you must install the win32 extensions from: http://sourceforge.net/projects/pywin32/files/') 95 else: 96 logger.debug('no file locking, this will cause problems') 97 98 LOCK_EX = None 99 LOCK_SH = None 100 LOCK_NB = None 101
102 - def lock(file, flags):
103 pass
104
105 - def unlock(file):
106 pass
107 108 109 if __name__ == '__main__': 110 from time import time, strftime, localtime 111 import sys 112 113 log = open('log.txt', 'a+') 114 lock(log, LOCK_EX) 115 116 timestamp = strftime('%m/%d/%Y %H:%M:%S\n', localtime(time())) 117 log.write(timestamp) 118 119 print 'Wrote lines. Hit enter to release lock.' 120 dummy = sys.stdin.readline() 121 122 log.close() 123