only one portalocker
This commit is contained in:
+1
-1
@@ -30,7 +30,6 @@ import hashlib
|
||||
import datetime
|
||||
import tempfile
|
||||
from gluon import recfile
|
||||
from gluon import portalocker
|
||||
from collections import defaultdict
|
||||
from collections import OrderedDict
|
||||
|
||||
@@ -40,6 +39,7 @@ try:
|
||||
except ImportError:
|
||||
have_settings = False
|
||||
|
||||
from pydal import portalocker
|
||||
from pydal._compat import pickle, thread, to_bytes, to_native, hashlib_md5
|
||||
|
||||
try:
|
||||
|
||||
+1
-1
@@ -28,7 +28,7 @@ from gluon import recfile
|
||||
from gluon.cache import CacheInRam
|
||||
from gluon.fileutils import copystream
|
||||
import hashlib
|
||||
from gluon import portalocker
|
||||
from pydal import portalocker
|
||||
from pickle import Pickler, MARK, DICT, EMPTY_DICT
|
||||
#from types import DictionaryType
|
||||
import datetime
|
||||
|
||||
+4
-3
@@ -18,10 +18,11 @@ import pkgutil
|
||||
import logging
|
||||
from cgi import escape
|
||||
from threading import RLock
|
||||
from pydal._compat import copyreg, PY2, maketrans, iterkeys, unicodeT, to_unicode, to_bytes, iteritems, to_native, pjoin
|
||||
from local_html_escape import local_html_escape
|
||||
from gluon.portalocker import read_locked, LockedFile
|
||||
from gluon.utf8 import Utf8
|
||||
from local_html_escape import local_html_escape
|
||||
|
||||
from pydal._compat import copyreg, PY2, maketrans, iterkeys, unicodeT, to_unicode, to_bytes, iteritems, to_native, pjoin
|
||||
from pydal.portalocker import read_locked, LockedFile
|
||||
|
||||
from gluon.fileutils import listdir
|
||||
from gluon.cfs import getcfs
|
||||
|
||||
+1
-1
@@ -19,7 +19,6 @@ import sched
|
||||
import re
|
||||
import datetime
|
||||
import platform
|
||||
import portalocker
|
||||
import fileutils
|
||||
from functools import reduce
|
||||
try:
|
||||
@@ -27,6 +26,7 @@ try:
|
||||
except:
|
||||
import pickle
|
||||
from gluon.settings import global_settings
|
||||
from pydal import portalocker
|
||||
|
||||
logger = logging.getLogger("web2py.cron")
|
||||
_cron_stopping = False
|
||||
|
||||
@@ -1,169 +0,0 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
Cross-platform (posix/nt) API for flock-style file locking.
|
||||
|
||||
Synopsis::
|
||||
|
||||
import portalocker
|
||||
file = open(\"somefile\", \"r+\")
|
||||
portalocker.lock(file, portalocker.LOCK_EX)
|
||||
file.seek(12)
|
||||
file.write(\"foo\")
|
||||
file.close()
|
||||
|
||||
If you know what you're doing, you may choose to::
|
||||
|
||||
portalocker.unlock(file)
|
||||
|
||||
before closing the file, but why?
|
||||
|
||||
Methods::
|
||||
|
||||
lock( file, flags )
|
||||
unlock( file )
|
||||
|
||||
Constants::
|
||||
|
||||
LOCK_EX
|
||||
LOCK_SH
|
||||
LOCK_NB
|
||||
|
||||
I learned the win32 technique for locking files from sample code
|
||||
provided by John Nielsen <nielsenjf@my-deja.com> in the documentation
|
||||
that accompanies the win32 modules.
|
||||
|
||||
Author: Jonathan Feinberg <jdf@pobox.com>
|
||||
Version: $Id: portalocker.py,v 1.3 2001/05/29 18:47:55 Administrator Exp $
|
||||
"""
|
||||
|
||||
import logging
|
||||
import platform
|
||||
logger = logging.getLogger("web2py")
|
||||
|
||||
os_locking = None
|
||||
try:
|
||||
import google.appengine
|
||||
os_locking = 'gae'
|
||||
except:
|
||||
try:
|
||||
import fcntl
|
||||
os_locking = 'posix'
|
||||
except:
|
||||
try:
|
||||
import win32con
|
||||
import pywintypes
|
||||
import win32file
|
||||
os_locking = 'windows'
|
||||
except:
|
||||
pass
|
||||
|
||||
if os_locking == 'windows':
|
||||
LOCK_EX = win32con.LOCKFILE_EXCLUSIVE_LOCK
|
||||
LOCK_SH = 0 # the default
|
||||
LOCK_NB = win32con.LOCKFILE_FAIL_IMMEDIATELY
|
||||
|
||||
# is there any reason not to reuse the following structure?
|
||||
|
||||
__overlapped = pywintypes.OVERLAPPED()
|
||||
|
||||
def lock(file, flags):
|
||||
hfile = win32file._get_osfhandle(file.fileno())
|
||||
win32file.LockFileEx(hfile, flags, 0, 0x7fff0000, __overlapped)
|
||||
|
||||
def unlock(file):
|
||||
hfile = win32file._get_osfhandle(file.fileno())
|
||||
win32file.UnlockFileEx(hfile, 0, 0x7fff0000, __overlapped)
|
||||
|
||||
|
||||
elif os_locking == 'posix':
|
||||
LOCK_EX = fcntl.LOCK_EX
|
||||
LOCK_SH = fcntl.LOCK_SH
|
||||
LOCK_NB = fcntl.LOCK_NB
|
||||
|
||||
def lock(file, flags):
|
||||
fcntl.flock(file.fileno(), flags)
|
||||
|
||||
def unlock(file):
|
||||
fcntl.flock(file.fileno(), fcntl.LOCK_UN)
|
||||
|
||||
|
||||
else:
|
||||
if platform.system() == 'Windows':
|
||||
logger.error('no file locking, you must install the win32 extensions from: http://sourceforge.net/projects/pywin32/files/')
|
||||
elif os_locking != 'gae':
|
||||
logger.debug('no file locking, this will cause problems')
|
||||
|
||||
LOCK_EX = None
|
||||
LOCK_SH = None
|
||||
LOCK_NB = None
|
||||
|
||||
def lock(file, flags):
|
||||
pass
|
||||
|
||||
def unlock(file):
|
||||
pass
|
||||
|
||||
|
||||
class LockedFile(object):
|
||||
def __init__(self, filename, mode='rb'):
|
||||
self.filename = filename
|
||||
self.mode = mode
|
||||
self.file = None
|
||||
if 'r' in mode:
|
||||
self.file = open(filename, mode)
|
||||
lock(self.file, LOCK_SH)
|
||||
elif 'w' in mode or 'a' in mode:
|
||||
self.file = open(filename, mode.replace('w', 'a'))
|
||||
lock(self.file, LOCK_EX)
|
||||
if not 'a' in mode:
|
||||
self.file.seek(0)
|
||||
self.file.truncate(0)
|
||||
else:
|
||||
raise RuntimeError("invalid LockedFile(...,mode)")
|
||||
|
||||
def read(self, size=None):
|
||||
return self.file.read() if size is None else self.file.read(size)
|
||||
|
||||
def readline(self):
|
||||
return self.file.readline()
|
||||
|
||||
def readlines(self):
|
||||
return self.file.readlines()
|
||||
|
||||
def write(self, data):
|
||||
self.file.write(data)
|
||||
self.file.flush()
|
||||
|
||||
def close(self):
|
||||
if not self.file is None:
|
||||
unlock(self.file)
|
||||
self.file.close()
|
||||
self.file = None
|
||||
|
||||
def __del__(self):
|
||||
if not self.file is None:
|
||||
self.close()
|
||||
|
||||
|
||||
def read_locked(filename):
|
||||
fp = LockedFile(filename, 'rb')
|
||||
data = fp.read()
|
||||
fp.close()
|
||||
return data
|
||||
|
||||
|
||||
def write_locked(filename, data):
|
||||
fp = LockedFile(filename, 'wb')
|
||||
data = fp.write(data)
|
||||
fp.close()
|
||||
|
||||
if __name__ == '__main__':
|
||||
import sys
|
||||
f = LockedFile('test.txt', mode='wb')
|
||||
f.write('test ok')
|
||||
f.close()
|
||||
f = LockedFile('test.txt', mode='rb')
|
||||
sys.stdout.write(f.read()+'\n')
|
||||
f.close()
|
||||
+1
-1
@@ -12,8 +12,8 @@ Provides:
|
||||
- Storage; like dictionary allowing also for `obj.foo` for `obj['foo']`
|
||||
"""
|
||||
|
||||
from pydal import portalocker
|
||||
from pydal._compat import copyreg, pickle, PY2
|
||||
import gluon.portalocker as portalocker
|
||||
|
||||
__all__ = ['List', 'Storage', 'Settings', 'Messages',
|
||||
'StorageList', 'load_storage', 'save_storage']
|
||||
|
||||
Reference in New Issue
Block a user