annotate roundup/backends/portalocker.py @ 8543:1ffa1f42e1da

refactor: rework mime type comparison and clean code rest.py: accept application/* as match for application/json in non /binary_context rest path. allow defining default mime type to return when file/message is missing mime type. Make it a class variable to it can be changed from text/plain to text/markdown or whatever. extract code from determine_output_format() to create create_valid_content_types() method which returns a list of matching mime types for a given type/subtype. Eliminate mostly duplicate return statements by introducing a variable to specify valid mime types in error message. rest_common.py: Fix error messages that now return application/* as valid mime type. CHANGES.txt upgrading.txt rest.txt: top level notes and corrections. Also correct rst syntax on earlier change.
author John Rouillard <rouilj@ieee.org>
date Tue, 24 Mar 2026 21:30:47 -0400
parents a50712b6ad56
children 9c3ec0a5c7fc
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 #
6035
a50712b6ad56 flake8 fixes whitespace
John Rouillard <rouilj@ieee.org>
parents: 5376
diff changeset
67 # detect size of ULONG_PTR
4686
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]
6035
a50712b6ad56 flake8 fixes whitespace
John Rouillard <rouilj@ieee.org>
parents: 5376
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()
6035
a50712b6ad56 flake8 fixes whitespace
John Rouillard <rouilj@ieee.org>
parents: 5376
diff changeset
119 if LockFileEx(hfile, flags, 0, 0, 0xFFFF0000,
a50712b6ad56 flake8 fixes whitespace
John Rouillard <rouilj@ieee.org>
parents: 5376
diff changeset
120 ctypes.byref(overlapped)):
4686
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
121 return True
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
122 else:
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
123 return False
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
124
1387
e975db910d9f latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents: 1244
diff changeset
125 def unlock(file):
4680
b6b600ff61e2 win32: Replace win32file._get_osfhandle with function from msvcrt module
anatoly techtonik <techtonik@gmail.com>
parents: 4679
diff changeset
126 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
127 overlapped = OVERLAPPED()
5076
d27de8f08c31 Remove 'import *' statement from roundup/backends/portalocker.py
John Kristensen <john@jerrykan.com>
parents: 4686
diff changeset
128 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
129 return True
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
130 else:
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
131 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
132
6035
a50712b6ad56 flake8 fixes whitespace
John Rouillard <rouilj@ieee.org>
parents: 5376
diff changeset
133 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
134 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
135 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
136 return True
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
137 else:
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
138 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
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 def unlock(file):
4686
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
141 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
142 return True
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
143 else:
4e740f02e165 Remove pywin32 installation dependency by porting portalocker.py to ctypes.
anatoly techtonik <techtonik@gmail.com>
parents: 4681
diff changeset
144 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
145
509a101305da node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
146 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
147 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
148 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
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 = 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
151 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
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 timestamp = strftime("%m/%d/%Y %H:%M:%S\n", localtime(time()))
6035
a50712b6ad56 flake8 fixes whitespace
John Rouillard <rouilj@ieee.org>
parents: 5376
diff changeset
154 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
155
5376
64b05e24dbd8 Python 3 preparation: convert print to a function.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5174
diff changeset
156 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
157 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
158
1387
e975db910d9f latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents: 1244
diff changeset
159 log.close()

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