annotate roundup/backends/portalocker.py @ 5076:d27de8f08c31

Remove 'import *' statement from roundup/backends/portalocker.py There are various 'import *' statements scattered through the code which are generally not a good thing. These should be fairly safe changes, but I'll commit them one file at a time to make it easier to track down issues with a bisect if they crop up later.
author John Kristensen <john@jerrykan.com>
date Fri, 10 Jun 2016 18:21:50 +1000
parents 4e740f02e165
children a76fbb9f5535
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4686
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
1 #!/usr/bin/env python
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
2 #
690
509a101305da node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
3 # portalocker.py - Cross-platform (posix/nt) API for flock-style file locking.
4686
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
4 #
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
5 # http://code.activestate.com/recipes/65203-portalocker-cross-platform-posixnt-api-for-flock-s/
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
6 #
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
7 """
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
8 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
9
2005
fc52d57c6c3e documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents: 1755
diff changeset
10 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
11
509a101305da node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
12 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
13 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
14 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
15 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
16 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
17 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
18
2005
fc52d57c6c3e documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents: 1755
diff changeset
19 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
20
509a101305da node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
21 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
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 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
24
2005
fc52d57c6c3e documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents: 1755
diff changeset
25 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
26
509a101305da node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
27 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
28 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
29
2005
fc52d57c6c3e documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents: 1755
diff changeset
30 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
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 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
33 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
34 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
35
509a101305da node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
36 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
37 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
38 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
39
2005
fc52d57c6c3e documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents: 1755
diff changeset
40 :Author: Jonathan Feinberg <jdf@pobox.com>
4686
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
41
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
42 Roundup Changes
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
43 ---------------
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
44 2012-11-28 (anatoly techtonik)
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
45 - Ported to ctypes
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
46 - Dropped support for Win95, Win98 and WinME
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
47 - Added return result
690
509a101305da node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
48 """
4686
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
49
2005
fc52d57c6c3e documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents: 1755
diff changeset
50 __docformat__ = 'restructuredtext'
fc52d57c6c3e documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents: 1755
diff changeset
51
690
509a101305da node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
52 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
53
509a101305da node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
54 if os.name == 'nt':
4680
b6b600ff61e2 win32: Replace win32file._get_osfhandle with function from msvcrt module
anatoly techtonik <techtonik@gmail.com>
parents: 4679
diff changeset
55 import msvcrt
5076
d27de8f08c31 Remove 'import *' statement from roundup/backends/portalocker.py
John Kristensen <john@jerrykan.com>
parents: 4686
diff changeset
56 import ctypes
4686
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
57 from ctypes.wintypes import BOOL, DWORD, HANDLE
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
58
4679
d269f5f04f7a win32: Remove dependency on external win32con module
anatoly techtonik <techtonik@gmail.com>
parents: 4570
diff changeset
59 LOCK_SH = 0 # the default
d269f5f04f7a win32: Remove dependency on external win32con module
anatoly techtonik <techtonik@gmail.com>
parents: 4570
diff changeset
60 LOCK_NB = 0x1 # LOCKFILE_FAIL_IMMEDIATELY
d269f5f04f7a win32: Remove dependency on external win32con module
anatoly techtonik <techtonik@gmail.com>
parents: 4570
diff changeset
61 LOCK_EX = 0x2 # LOCKFILE_EXCLUSIVE_LOCK
4686
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
62
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
63 # --- the code is taken from pyserial project ---
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
64 #
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
65 # detect size of ULONG_PTR
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
66 def is_64bit():
5076
d27de8f08c31 Remove 'import *' statement from roundup/backends/portalocker.py
John Kristensen <john@jerrykan.com>
parents: 4686
diff changeset
67 return ctypes.sizeof(ctypes.c_ulong) != ctypes.sizeof(ctypes.c_void_p)
4686
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
68 if is_64bit():
5076
d27de8f08c31 Remove 'import *' statement from roundup/backends/portalocker.py
John Kristensen <john@jerrykan.com>
parents: 4686
diff changeset
69 ULONG_PTR = ctypes.c_int64
4686
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
70 else:
5076
d27de8f08c31 Remove 'import *' statement from roundup/backends/portalocker.py
John Kristensen <john@jerrykan.com>
parents: 4686
diff changeset
71 ULONG_PTR = ctypes.c_ulong
d27de8f08c31 Remove 'import *' statement from roundup/backends/portalocker.py
John Kristensen <john@jerrykan.com>
parents: 4686
diff changeset
72 PVOID = ctypes.c_void_p
4686
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
73
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
74 # --- Union inside Structure by stackoverflow:3480240 ---
5076
d27de8f08c31 Remove 'import *' statement from roundup/backends/portalocker.py
John Kristensen <john@jerrykan.com>
parents: 4686
diff changeset
75 class _OFFSET(ctypes.Structure):
4686
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
76 _fields_ = [
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
77 ('Offset', DWORD),
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
78 ('OffsetHigh', DWORD)]
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
79
5076
d27de8f08c31 Remove 'import *' statement from roundup/backends/portalocker.py
John Kristensen <john@jerrykan.com>
parents: 4686
diff changeset
80 class _OFFSET_UNION(ctypes.Union):
4686
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
81 _anonymous_ = ['_offset']
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
82 _fields_ = [
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
83 ('_offset', _OFFSET),
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
84 ('Pointer', PVOID)]
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
85
5076
d27de8f08c31 Remove 'import *' statement from roundup/backends/portalocker.py
John Kristensen <john@jerrykan.com>
parents: 4686
diff changeset
86 class OVERLAPPED(ctypes.Structure):
4686
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
87 _anonymous_ = ['_offset_union']
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
88 _fields_ = [
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
89 ('Internal', ULONG_PTR),
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
90 ('InternalHigh', ULONG_PTR),
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
91 ('_offset_union', _OFFSET_UNION),
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
92 ('hEvent', HANDLE)]
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
93
5076
d27de8f08c31 Remove 'import *' statement from roundup/backends/portalocker.py
John Kristensen <john@jerrykan.com>
parents: 4686
diff changeset
94 LPOVERLAPPED = ctypes.POINTER(OVERLAPPED)
4686
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
95
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
96 # --- Define function prototypes for extra safety ---
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
97 LockFileEx = windll.kernel32.LockFileEx
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
98 LockFileEx.restype = BOOL
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
99 LockFileEx.argtypes = [HANDLE, DWORD, DWORD, DWORD, DWORD, LPOVERLAPPED]
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
100 UnlockFileEx = windll.kernel32.UnlockFileEx
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
101 UnlockFileEx.restype = BOOL
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
102 UnlockFileEx.argtypes = [HANDLE, DWORD, DWORD, DWORD, LPOVERLAPPED]
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
103
690
509a101305da node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
104 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
105 import fcntl
4679
d269f5f04f7a win32: Remove dependency on external win32con module
anatoly techtonik <techtonik@gmail.com>
parents: 4570
diff changeset
106 LOCK_SH = fcntl.LOCK_SH # shared lock
d269f5f04f7a win32: Remove dependency on external win32con module
anatoly techtonik <techtonik@gmail.com>
parents: 4570
diff changeset
107 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
108 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
109 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
110 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
111
509a101305da node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
112 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
113 def lock(file, flags):
4686
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
114 """ Return True on success, False otherwise """
4680
b6b600ff61e2 win32: Replace win32file._get_osfhandle with function from msvcrt module
anatoly techtonik <techtonik@gmail.com>
parents: 4679
diff changeset
115 hfile = msvcrt.get_osfhandle(file.fileno())
4686
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
116 overlapped = OVERLAPPED()
5076
d27de8f08c31 Remove 'import *' statement from roundup/backends/portalocker.py
John Kristensen <john@jerrykan.com>
parents: 4686
diff changeset
117 if LockFileEx(hfile, flags, 0, 0, 0xFFFF0000, ctypes.byref(overlapped)):
4686
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
118 return True
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
119 else:
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
120 return False
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
121
1387
e975db910d9f latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents: 1244
diff changeset
122 def unlock(file):
4680
b6b600ff61e2 win32: Replace win32file._get_osfhandle with function from msvcrt module
anatoly techtonik <techtonik@gmail.com>
parents: 4679
diff changeset
123 hfile = msvcrt.get_osfhandle(file.fileno())
4686
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
124 overlapped = OVERLAPPED()
5076
d27de8f08c31 Remove 'import *' statement from roundup/backends/portalocker.py
John Kristensen <john@jerrykan.com>
parents: 4686
diff changeset
125 if UnlockFileEx(hfile, 0, 0, 0xFFFF0000, ctypes.byref(overlapped)):
4686
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
126 return True
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
127 else:
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
128 return False
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 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
131 def lock(file, flags):
4686
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
132 if fcntl.flock(file.fileno(), flags) == 0:
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
133 return True
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
134 else:
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
135 return False
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 def unlock(file):
4686
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
138 if fcntl.flock(file.fileno(), fcntl.LOCK_UN) == 0:
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
139 return True
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
140 else:
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
141 return False
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
509a101305da node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
143 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
144 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
145 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
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 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
148 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
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 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
151 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
152
1387
e975db910d9f latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents: 1244
diff changeset
153 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
154 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
155
1387
e975db910d9f latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents: 1244
diff changeset
156 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
157

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