Mercurial > p > roundup > code
annotate roundup/backends/portalocker.py @ 4680:b6b600ff61e2
win32: Replace win32file._get_osfhandle with function from msvcrt module
| author | anatoly techtonik <techtonik@gmail.com> |
|---|---|
| date | Tue, 27 Nov 2012 14:17:05 +0300 |
| parents | d269f5f04f7a |
| children | 6c74a8bff423 |
| 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 |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1755
diff
changeset
|
4 """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
|
5 |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1755
diff
changeset
|
6 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
|
7 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
8 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
|
9 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
|
10 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
|
11 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
|
12 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
|
13 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
|
14 |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1755
diff
changeset
|
15 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
|
16 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
17 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
|
18 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
19 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
|
20 |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1755
diff
changeset
|
21 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
|
22 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
23 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
|
24 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
|
25 |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1755
diff
changeset
|
26 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
|
27 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
28 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
|
29 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
|
30 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
|
31 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
32 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
|
33 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
|
34 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
|
35 |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1755
diff
changeset
|
36 :Author: Jonathan Feinberg <jdf@pobox.com> |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1755
diff
changeset
|
37 :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
|
38 **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
|
39 """ |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1755
diff
changeset
|
40 __docformat__ = 'restructuredtext' |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1755
diff
changeset
|
41 |
|
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 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
|
43 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
44 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
|
45 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
|
46 import pywintypes |
|
4680
b6b600ff61e2
win32: Replace win32file._get_osfhandle with function from msvcrt module
anatoly techtonik <techtonik@gmail.com>
parents:
4679
diff
changeset
|
47 import msvcrt |
|
4679
d269f5f04f7a
win32: Remove dependency on external win32con module
anatoly techtonik <techtonik@gmail.com>
parents:
4570
diff
changeset
|
48 LOCK_SH = 0 # the default |
|
d269f5f04f7a
win32: Remove dependency on external win32con module
anatoly techtonik <techtonik@gmail.com>
parents:
4570
diff
changeset
|
49 LOCK_NB = 0x1 # LOCKFILE_FAIL_IMMEDIATELY |
|
d269f5f04f7a
win32: Remove dependency on external win32con module
anatoly techtonik <techtonik@gmail.com>
parents:
4570
diff
changeset
|
50 LOCK_EX = 0x2 # LOCKFILE_EXCLUSIVE_LOCK |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
51 # 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
|
52 __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
|
53 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
|
54 import fcntl |
|
4679
d269f5f04f7a
win32: Remove dependency on external win32con module
anatoly techtonik <techtonik@gmail.com>
parents:
4570
diff
changeset
|
55 LOCK_SH = fcntl.LOCK_SH # shared lock |
|
d269f5f04f7a
win32: Remove dependency on external win32con module
anatoly techtonik <techtonik@gmail.com>
parents:
4570
diff
changeset
|
56 LOCK_NB = fcntl.LOCK_NB # non-blocking |
|
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 LOCK_EX = fcntl.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
|
58 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
|
59 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
|
60 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
61 if os.name == 'nt': |
|
1755
0e123e7c6ddc
fixed 0xffff0000 literal. finally.
Richard Jones <richard@users.sourceforge.net>
parents:
1719
diff
changeset
|
62 # 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
|
63 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
|
64 def lock(file, flags): |
|
4680
b6b600ff61e2
win32: Replace win32file._get_osfhandle with function from msvcrt module
anatoly techtonik <techtonik@gmail.com>
parents:
4679
diff
changeset
|
65 hfile = msvcrt.get_osfhandle(file.fileno()) |
|
3701
c648bebf021e
wrap long lines
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
66 # LockFileEx is not supported on all Win32 platforms (Win95, Win98, |
|
c648bebf021e
wrap long lines
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
67 # 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
|
68 # 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
|
69 # 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
|
70 # 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
|
71 try: |
|
1719
eeb167fb8faf
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
1459
diff
changeset
|
72 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
|
73 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
|
74 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
|
75 # 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
|
76 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
|
77 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
|
78 |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
79 # 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
|
80 # LockFile does not support shared locking -- always exclusive. |
|
3701
c648bebf021e
wrap long lines
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
81 # Care: the low/high length params are reversed compared to |
|
c648bebf021e
wrap long lines
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
82 # 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
|
83 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
|
84 import warnings |
|
3701
c648bebf021e
wrap long lines
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
85 warnings.warn("PortaLocker does not support shared " |
|
c648bebf021e
wrap long lines
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
86 "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
|
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: |
|
3701
c648bebf021e
wrap long lines
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
99 # Propagate upwards all exceptions other than lock |
|
c648bebf021e
wrap long lines
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
100 # 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
|
101 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
|
102 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
|
103 # 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
|
104 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
|
105 # 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
|
106 |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
107 def unlock(file): |
|
4680
b6b600ff61e2
win32: Replace win32file._get_osfhandle with function from msvcrt module
anatoly techtonik <techtonik@gmail.com>
parents:
4679
diff
changeset
|
108 hfile = msvcrt.get_osfhandle(file.fileno()) |
|
3701
c648bebf021e
wrap long lines
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
109 # UnlockFileEx is not supported on all Win32 platforms (Win95, Win98, |
|
c648bebf021e
wrap long lines
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
110 # 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
|
111 # 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
|
112 try: |
|
1719
eeb167fb8faf
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
1459
diff
changeset
|
113 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
|
114 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
|
115 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
|
116 # 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
|
117 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
|
118 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
|
119 |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
120 # UnlockFileEx is not supported. Use UnlockFile. |
|
3701
c648bebf021e
wrap long lines
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
121 # Care: the low/high length params are reversed compared to |
|
c648bebf021e
wrap long lines
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
122 # UnLockFileEx. |
|
1719
eeb167fb8faf
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
1459
diff
changeset
|
123 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
|
124 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
125 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
|
126 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
|
127 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
|
128 # 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
|
129 |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
130 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
|
131 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
|
132 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
133 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
|
134 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
|
135 import sys |
|
690
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
136 |
|
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 log = open('log.txt', "a+") |
|
4357
13b3155869e0
Beginnings of a big code cleanup / modernisation to make 2to3 happy
Richard Jones <richard@users.sourceforge.net>
parents:
3701
diff
changeset
|
138 lock(log, 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
|
139 |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
140 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
|
141 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
|
142 |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
143 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
|
144 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
|
145 |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
146 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
|
147 |
