annotate roundup/backends/portalocker.py @ 691:3d8ce8e2dcee search_indexing-0-4-2-branch

[[Metadata associated with this commit was garbled during conversion from CVS to Subversion.]]
author Roche Compaan <rochecompaan@users.sourceforge.net>
date Mon, 15 Apr 2002 23:25:16 +0000
parents
children 54333751e98d 8dd4f736370b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
691
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
1 # portalocker.py - Cross-platform (posix/nt) API for flock-style file locking.
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
2 # Requires python 1.5.2 or better.
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
3
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
4 # ID line added by richard for Roundup file tracking
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
5 # $Id: portalocker.py,v 1.1 2002-04-15 23:25:15 richard Exp $
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
6
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
7 """Cross-platform (posix/nt) API for flock-style file locking.
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
8
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
9 Synopsis:
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
10
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
11 import portalocker
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
12 file = open("somefile", "r+")
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
13 portalocker.lock(file, portalocker.LOCK_EX)
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
14 file.seek(12)
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
15 file.write("foo")
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
16 file.close()
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
17
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
18 If you know what you're doing, you may choose to
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
19
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
20 portalocker.unlock(file)
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
21
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
22 before closing the file, but why?
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
23
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
24 Methods:
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
25
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
26 lock( file, flags )
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
27 unlock( file )
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
28
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
29 Constants:
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
30
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
31 LOCK_EX
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
32 LOCK_SH
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
33 LOCK_NB
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
34
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
35 I learned the win32 technique for locking files from sample code
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
36 provided by John Nielsen <nielsenjf@my-deja.com> in the documentation
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
37 that accompanies the win32 modules.
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
38
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
39 Author: Jonathan Feinberg <jdf@pobox.com>
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
40 Version: Id: portalocker.py,v 1.3 2001/05/29 18:47:55 Administrator Exp
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
41 **un-cvsified by richard so the version doesn't change**
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
42 """
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
43 import os
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
44
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
45 if os.name == 'nt':
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
46 import win32con
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
47 import win32file
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
48 import pywintypes
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
49 LOCK_EX = win32con.LOCKFILE_EXCLUSIVE_LOCK
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
50 LOCK_SH = 0 # the default
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
51 LOCK_NB = win32con.LOCKFILE_FAIL_IMMEDIATELY
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
52 # is there any reason not to reuse the following structure?
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
53 __overlapped = pywintypes.OVERLAPPED()
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
54 elif os.name == 'posix':
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
55 import fcntl
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
56 LOCK_EX = fcntl.LOCK_EX
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
57 LOCK_SH = fcntl.LOCK_SH
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
58 LOCK_NB = fcntl.LOCK_NB
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
59 else:
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
60 raise RuntimeError("PortaLocker only defined for nt and posix platforms")
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
61
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
62 if os.name == 'nt':
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
63 def lock(file, flags):
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
64 hfile = win32file._get_osfhandle(file.fileno())
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
65 win32file.LockFileEx(hfile, flags, 0, 0xffff0000, __overlapped)
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
66
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
67 def unlock(file):
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
68 hfile = win32file._get_osfhandle(file.fileno())
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
69 win32file.UnlockFileEx(hfile, 0, 0xffff0000, __overlapped)
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
70
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
71 elif os.name =='posix':
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
72 def lock(file, flags):
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
73 fcntl.flock(file.fileno(), flags)
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
74
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
75 def unlock(file):
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
76 fcntl.flock(file.fileno(), fcntl.LOCK_UN)
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
77
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
78 if __name__ == '__main__':
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
79 from time import time, strftime, localtime
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
80 import sys
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
81 import portalocker
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
82
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
83 log = open('log.txt', "a+")
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
84 portalocker.lock(log, portalocker.LOCK_EX)
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
85
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
86 timestamp = strftime("%m/%d/%Y %H:%M:%S\n", localtime(time()))
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
87 log.write( timestamp )
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
88
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
89 print "Wrote lines. Hit enter to release lock."
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
90 dummy = sys.stdin.readline()
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
91
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
92 log.close()
3d8ce8e2dcee [[Metadata associated with this commit was garbled during conversion from CVS
Roche Compaan <rochecompaan@users.sourceforge.net>
parents:
diff changeset
93

Roundup Issue Tracker: http://roundup-tracker.org/