Mercurial > p > roundup > code
annotate roundup/backends/portalocker.py @ 3992:fe2af84a5ca5
allow binary data for "content" props through rawToHyperdb
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Mon, 18 Aug 2008 06:21:53 +0000 |
| parents | c648bebf021e |
| children | 13b3155869e0 |
| 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 |
|
3701
c648bebf021e
wrap long lines
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
5 # $Id: portalocker.py,v 1.9 2006-09-09 05:42:45 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()) |
|
3701
c648bebf021e
wrap long lines
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
69 # LockFileEx is not supported on all Win32 platforms (Win95, Win98, |
|
c648bebf021e
wrap long lines
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
70 # WinME). |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
71 # 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
|
72 # 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
|
73 # 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
|
74 try: |
|
1719
eeb167fb8faf
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
1459
diff
changeset
|
75 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
|
76 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
|
77 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
|
78 # 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
|
79 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
|
80 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
|
81 |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
82 # 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
|
83 # LockFile does not support shared locking -- always exclusive. |
|
3701
c648bebf021e
wrap long lines
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
84 # Care: the low/high length params are reversed compared to |
|
c648bebf021e
wrap long lines
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
85 # LockFileEx. |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
86 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
|
87 import warnings |
|
3701
c648bebf021e
wrap long lines
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
88 warnings.warn("PortaLocker does not support shared " |
|
c648bebf021e
wrap long lines
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
89 "locking on Win9x", RuntimeWarning) |
|
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 # 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
|
91 if flags & LOCK_NB: |
|
1719
eeb167fb8faf
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
1459
diff
changeset
|
92 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
|
93 else: |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
94 # 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
|
95 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
|
96 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
|
97 # 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
|
98 try: |
|
1719
eeb167fb8faf
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
1459
diff
changeset
|
99 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
|
100 break |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
101 except win32file.error, e: |
|
3701
c648bebf021e
wrap long lines
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
102 # Propagate upwards all exceptions other than lock |
|
c648bebf021e
wrap long lines
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
103 # violation. |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
104 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
|
105 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
|
106 # 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
|
107 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
|
108 # 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
|
109 |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
110 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
|
111 hfile = win32file._get_osfhandle(file.fileno()) |
|
3701
c648bebf021e
wrap long lines
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
112 # UnlockFileEx is not supported on all Win32 platforms (Win95, Win98, |
|
c648bebf021e
wrap long lines
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
113 # WinME). |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
114 # 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
|
115 try: |
|
1719
eeb167fb8faf
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
1459
diff
changeset
|
116 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
|
117 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
|
118 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
|
119 # 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
|
120 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
|
121 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
|
122 |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
123 # UnlockFileEx is not supported. Use UnlockFile. |
|
3701
c648bebf021e
wrap long lines
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
124 # Care: the low/high length params are reversed compared to |
|
c648bebf021e
wrap long lines
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
125 # UnLockFileEx. |
|
1719
eeb167fb8faf
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
1459
diff
changeset
|
126 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
|
127 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
128 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
|
129 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
|
130 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
|
131 # 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
|
132 |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
133 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
|
134 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
|
135 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
136 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
|
137 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
|
138 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
|
139 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
|
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 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
|
142 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
|
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 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
|
145 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
|
146 |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
147 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
|
148 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
|
149 |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
150 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
|
151 |
