Mercurial > p > roundup > code
annotate roundup/backends/portalocker.py @ 2929:7a8a02646d4e
backend is an attribute of tracker instances
| author | Alexander Smishlajev <a1s@users.sourceforge.net> |
|---|---|
| date | Thu, 18 Nov 2004 16:23:58 +0000 |
| parents | fc52d57c6c3e |
| children | c648bebf021e |
| rev | line source |
|---|---|
|
690
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1 # portalocker.py - Cross-platform (posix/nt) API for flock-style file locking. |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
2 # Requires python 1.5.2 or better. |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
3 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
4 # ID line added by richard for Roundup file tracking |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1755
diff
changeset
|
5 # $Id: portalocker.py,v 1.8 2004-02-11 23:55:09 richard Exp $ |
|
690
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
6 |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1755
diff
changeset
|
7 """Cross-platform (posix/nt) API for flock-style file locking. |
|
690
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
8 |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1755
diff
changeset
|
9 Synopsis:: |
|
690
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
10 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
11 import portalocker |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
12 file = open("somefile", "r+") |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
13 portalocker.lock(file, portalocker.LOCK_EX) |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
14 file.seek(12) |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
15 file.write("foo") |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
16 file.close() |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
17 |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1755
diff
changeset
|
18 If you know what you're doing, you may choose to:: |
|
690
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
19 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
20 portalocker.unlock(file) |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
21 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
22 before closing the file, but why? |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
23 |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1755
diff
changeset
|
24 Methods:: |
|
690
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
25 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
26 lock( file, flags ) |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
27 unlock( file ) |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
28 |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1755
diff
changeset
|
29 Constants:: |
|
690
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
30 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
31 LOCK_EX |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
32 LOCK_SH |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
33 LOCK_NB |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
34 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
35 I learned the win32 technique for locking files from sample code |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
36 provided by John Nielsen <nielsenjf@my-deja.com> in the documentation |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
37 that accompanies the win32 modules. |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
38 |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1755
diff
changeset
|
39 :Author: Jonathan Feinberg <jdf@pobox.com> |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1755
diff
changeset
|
40 :Version: Id: portalocker.py,v 1.3 2001/05/29 18:47:55 Administrator Exp |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1755
diff
changeset
|
41 **un-cvsified by richard so the version doesn't change** |
|
690
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
42 """ |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1755
diff
changeset
|
43 __docformat__ = 'restructuredtext' |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1755
diff
changeset
|
44 |
|
690
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
45 import os |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
46 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
47 if os.name == 'nt': |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
48 import win32con |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
49 import win32file |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
50 import pywintypes |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
51 LOCK_EX = win32con.LOCKFILE_EXCLUSIVE_LOCK |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
52 LOCK_SH = 0 # the default |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
53 LOCK_NB = win32con.LOCKFILE_FAIL_IMMEDIATELY |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
54 # is there any reason not to reuse the following structure? |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
55 __overlapped = pywintypes.OVERLAPPED() |
|
690
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
56 elif os.name == 'posix': |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
57 import fcntl |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
58 LOCK_EX = fcntl.LOCK_EX |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
59 LOCK_SH = fcntl.LOCK_SH |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
60 LOCK_NB = fcntl.LOCK_NB |
|
690
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
61 else: |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
62 raise RuntimeError("PortaLocker only defined for nt and posix platforms") |
|
690
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
63 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
64 if os.name == 'nt': |
|
1755
0e123e7c6ddc
fixed 0xffff0000 literal. finally.
Richard Jones <richard@users.sourceforge.net>
parents:
1719
diff
changeset
|
65 # eugh, we want 0xffff0000 here, but python 2.3 won't let us :( |
|
0e123e7c6ddc
fixed 0xffff0000 literal. finally.
Richard Jones <richard@users.sourceforge.net>
parents:
1719
diff
changeset
|
66 FFFF0000 = -65536 |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
67 def lock(file, flags): |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
68 hfile = win32file._get_osfhandle(file.fileno()) |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
69 # LockFileEx is not supported on all Win32 platforms (Win95, Win98, WinME). |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
70 # If it's not supported, win32file will raise an exception. |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
71 # Try LockFileEx first, as it has more functionality and handles |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
72 # blocking locks more efficiently. |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
73 try: |
|
1719
eeb167fb8faf
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
1459
diff
changeset
|
74 win32file.LockFileEx(hfile, flags, 0, FFFF0000, __overlapped) |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
75 except win32file.error, e: |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
76 import winerror |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
77 # Propagate upwards all exceptions other than not-implemented. |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
78 if e[0] != winerror.ERROR_CALL_NOT_IMPLEMENTED: |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
79 raise e |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
80 |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
81 # LockFileEx is not supported. Use LockFile. |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
82 # LockFile does not support shared locking -- always exclusive. |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
83 # Care: the low/high length params are reversed compared to LockFileEx. |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
84 if not flags & LOCK_EX: |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
85 import warnings |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
86 warnings.warn("PortaLocker does not support shared locking on Win9x", RuntimeWarning) |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
87 # LockFile only supports immediate-fail locking. |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
88 if flags & LOCK_NB: |
|
1719
eeb167fb8faf
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
1459
diff
changeset
|
89 win32file.LockFile(hfile, 0, 0, FFFF0000, 0) |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
90 else: |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
91 # Emulate a blocking lock with a polling loop. |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
92 import time |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
93 while 1: |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
94 # Attempt a lock. |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
95 try: |
|
1719
eeb167fb8faf
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
1459
diff
changeset
|
96 win32file.LockFile(hfile, 0, 0, FFFF0000, 0) |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
97 break |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
98 except win32file.error, e: |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
99 # Propagate upwards all exceptions other than lock violation. |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
100 if e[0] != winerror.ERROR_LOCK_VIOLATION: |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
101 raise e |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
102 # Sleep and poll again. |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
103 time.sleep(0.1) |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
104 # TODO: should this return the result of the lock? |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
105 |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
106 def unlock(file): |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
107 hfile = win32file._get_osfhandle(file.fileno()) |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
108 # UnlockFileEx is not supported on all Win32 platforms (Win95, Win98, WinME). |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
109 # If it's not supported, win32file will raise an api_error exception. |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
110 try: |
|
1719
eeb167fb8faf
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
1459
diff
changeset
|
111 win32file.UnlockFileEx(hfile, 0, FFFF0000, __overlapped) |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
112 except win32file.error, e: |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
113 import winerror |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
114 # Propagate upwards all exceptions other than not-implemented. |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
115 if e[0] != winerror.ERROR_CALL_NOT_IMPLEMENTED: |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
116 raise e |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
117 |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
118 # UnlockFileEx is not supported. Use UnlockFile. |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
119 # Care: the low/high length params are reversed compared to UnLockFileEx. |
|
1719
eeb167fb8faf
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
1459
diff
changeset
|
120 win32file.UnlockFile(hfile, 0, 0, FFFF0000, 0) |
|
690
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
121 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
122 elif os.name =='posix': |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
123 def lock(file, flags): |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
124 fcntl.flock(file.fileno(), flags) |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
125 # TODO: should this return the result of the lock? |
|
690
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
126 |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
127 def unlock(file): |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
128 fcntl.flock(file.fileno(), fcntl.LOCK_UN) |
|
690
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
129 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
130 if __name__ == '__main__': |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
131 from time import time, strftime, localtime |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
132 import sys |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
133 import portalocker |
|
690
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
134 |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
135 log = open('log.txt', "a+") |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
136 portalocker.lock(log, portalocker.LOCK_EX) |
|
690
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
137 |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
138 timestamp = strftime("%m/%d/%Y %H:%M:%S\n", localtime(time())) |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
139 log.write( timestamp ) |
|
690
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
140 |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
141 print "Wrote lines. Hit enter to release lock." |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
142 dummy = sys.stdin.readline() |
|
690
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
143 |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
144 log.close() |
|
690
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
145 |
