annotate roundup/backends/portalocker.py @ 5543:bc3e00a3d24b

MySQL backend fixes for Python 3. With Python 2, text sent to and from MySQL is treated as bytes in Python. The database may be recorded by MySQL as having some other encoding (latin1 being the default in some MySQL versions - Roundup does not set an encoding explicitly, unlike in back_postgresql), but as long as MySQL's notion of the connection encoding agrees with its notion of the database encoding, no conversions actually take place and the bytes are stored and returned as-is. With Python 3, text sent to and from MySQL is treated as Python Unicode strings. When the database and connection encoding is latin1, that means the bytes stored in the database under Python 2 are interpreted as latin1 and converted from that to Unicode, producing incorrect results for any non-ASCII characters; furthermore, if trying to store new non-ASCII data in the database under Python 3, any non-latin1 characters produce errors. This patch arranges for both the connection and database character sets to be UTF-8 when using Python 3, and documents a need to export and import the database when moving from Python 2 to Python 3 with this backend.
author Joseph Myers <jsm@polyomino.org.uk>
date Sun, 16 Sep 2018 16:19:20 +0000
parents 64b05e24dbd8
children a50712b6ad56
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
5376
64b05e24dbd8 Python 3 preparation: convert print to a function.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5174
diff changeset
50 from __future__ import print_function
2005
fc52d57c6c3e documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents: 1755
diff changeset
51 __docformat__ = 'restructuredtext'
fc52d57c6c3e documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents: 1755
diff changeset
52
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 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
54
509a101305da node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
55 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
56 import msvcrt
5076
d27de8f08c31 Remove 'import *' statement from roundup/backends/portalocker.py
John Kristensen <john@jerrykan.com>
parents: 4686
diff changeset
57 import ctypes
5174
a76fbb9f5535 Fix Traceback in backends/portalocker.py (windows)
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5076
diff changeset
58 from ctypes import windll
4686
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
59 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
60
4679
d269f5f04f7a win32: Remove dependency on external win32con module
anatoly techtonik <techtonik@gmail.com>
parents: 4570
diff changeset
61 LOCK_SH = 0 # the default
d269f5f04f7a win32: Remove dependency on external win32con module
anatoly techtonik <techtonik@gmail.com>
parents: 4570
diff changeset
62 LOCK_NB = 0x1 # LOCKFILE_FAIL_IMMEDIATELY
d269f5f04f7a win32: Remove dependency on external win32con module
anatoly techtonik <techtonik@gmail.com>
parents: 4570
diff changeset
63 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
64
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
65 # --- 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
66 #
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
67 # 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
68 def is_64bit():
5076
d27de8f08c31 Remove 'import *' statement from roundup/backends/portalocker.py
John Kristensen <john@jerrykan.com>
parents: 4686
diff changeset
69 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
70 if is_64bit():
5076
d27de8f08c31 Remove 'import *' statement from roundup/backends/portalocker.py
John Kristensen <john@jerrykan.com>
parents: 4686
diff changeset
71 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
72 else:
5076
d27de8f08c31 Remove 'import *' statement from roundup/backends/portalocker.py
John Kristensen <john@jerrykan.com>
parents: 4686
diff changeset
73 ULONG_PTR = ctypes.c_ulong
d27de8f08c31 Remove 'import *' statement from roundup/backends/portalocker.py
John Kristensen <john@jerrykan.com>
parents: 4686
diff changeset
74 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
75
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
76 # --- 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
77 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
78 _fields_ = [
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
79 ('Offset', DWORD),
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
80 ('OffsetHigh', DWORD)]
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
81
5076
d27de8f08c31 Remove 'import *' statement from roundup/backends/portalocker.py
John Kristensen <john@jerrykan.com>
parents: 4686
diff changeset
82 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
83 _anonymous_ = ['_offset']
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
84 _fields_ = [
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
85 ('_offset', _OFFSET),
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
86 ('Pointer', PVOID)]
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
87
5076
d27de8f08c31 Remove 'import *' statement from roundup/backends/portalocker.py
John Kristensen <john@jerrykan.com>
parents: 4686
diff changeset
88 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
89 _anonymous_ = ['_offset_union']
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
90 _fields_ = [
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
91 ('Internal', ULONG_PTR),
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
92 ('InternalHigh', ULONG_PTR),
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
93 ('_offset_union', _OFFSET_UNION),
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
94 ('hEvent', HANDLE)]
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
95
5076
d27de8f08c31 Remove 'import *' statement from roundup/backends/portalocker.py
John Kristensen <john@jerrykan.com>
parents: 4686
diff changeset
96 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
97
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
98 # --- 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
99 LockFileEx = windll.kernel32.LockFileEx
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
100 LockFileEx.restype = BOOL
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
101 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
102 UnlockFileEx = windll.kernel32.UnlockFileEx
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
103 UnlockFileEx.restype = BOOL
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
104 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
105
690
509a101305da node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
106 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
107 import fcntl
4679
d269f5f04f7a win32: Remove dependency on external win32con module
anatoly techtonik <techtonik@gmail.com>
parents: 4570
diff changeset
108 LOCK_SH = fcntl.LOCK_SH # shared lock
d269f5f04f7a win32: Remove dependency on external win32con module
anatoly techtonik <techtonik@gmail.com>
parents: 4570
diff changeset
109 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
110 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
111 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
112 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
113
509a101305da node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
114 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
115 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
116 """ 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
117 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
118 overlapped = OVERLAPPED()
5076
d27de8f08c31 Remove 'import *' statement from roundup/backends/portalocker.py
John Kristensen <john@jerrykan.com>
parents: 4686
diff changeset
119 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
120 return True
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
121 else:
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
122 return False
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
123
1387
e975db910d9f latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents: 1244
diff changeset
124 def unlock(file):
4680
b6b600ff61e2 win32: Replace win32file._get_osfhandle with function from msvcrt module
anatoly techtonik <techtonik@gmail.com>
parents: 4679
diff changeset
125 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
126 overlapped = OVERLAPPED()
5076
d27de8f08c31 Remove 'import *' statement from roundup/backends/portalocker.py
John Kristensen <john@jerrykan.com>
parents: 4686
diff changeset
127 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
128 return True
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
129 else:
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
130 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
131
509a101305da node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
132 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
133 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
134 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
135 return True
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
136 else:
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
137 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
138
1387
e975db910d9f latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents: 1244
diff changeset
139 def unlock(file):
4686
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
140 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
141 return True
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
142 else:
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
143 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
144
509a101305da node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
145 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
146 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
147 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
148
1387
e975db910d9f latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents: 1244
diff changeset
149 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
150 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
151
1387
e975db910d9f latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents: 1244
diff changeset
152 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
153 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
154
5376
64b05e24dbd8 Python 3 preparation: convert print to a function.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5174
diff changeset
155 print("Wrote lines. Hit enter to release 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
156 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
157
1387
e975db910d9f latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents: 1244
diff changeset
158 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
159

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