+++ /dev/null
-"""Extended file operations available in POSIX.\r
-\r
-f = posixfile.open(filename, [mode, [bufsize]])\r
- will create a new posixfile object\r
-\r
-f = posixfile.fileopen(fileobject)\r
- will create a posixfile object from a builtin file object\r
-\r
-f.file()\r
- will return the original builtin file object\r
-\r
-f.dup()\r
- will return a new file object based on a new filedescriptor\r
-\r
-f.dup2(fd)\r
- will return a new file object based on the given filedescriptor\r
-\r
-f.flags(mode)\r
- will turn on the associated flag (merge)\r
- mode can contain the following characters:\r
-\r
- (character representing a flag)\r
- a append only flag\r
- c close on exec flag\r
- n no delay flag\r
- s synchronization flag\r
- (modifiers)\r
- ! turn flags 'off' instead of default 'on'\r
- = copy flags 'as is' instead of default 'merge'\r
- ? return a string in which the characters represent the flags\r
- that are set\r
-\r
- note: - the '!' and '=' modifiers are mutually exclusive.\r
- - the '?' modifier will return the status of the flags after they\r
- have been changed by other characters in the mode string\r
-\r
-f.lock(mode [, len [, start [, whence]]])\r
- will (un)lock a region\r
- mode can contain the following characters:\r
-\r
- (character representing type of lock)\r
- u unlock\r
- r read lock\r
- w write lock\r
- (modifiers)\r
- | wait until the lock can be granted\r
- ? return the first lock conflicting with the requested lock\r
- or 'None' if there is no conflict. The lock returned is in the\r
- format (mode, len, start, whence, pid) where mode is a\r
- character representing the type of lock ('r' or 'w')\r
-\r
- note: - the '?' modifier prevents a region from being locked; it is\r
- query only\r
-"""\r
-import warnings\r
-warnings.warn("The posixfile module is deprecated; "\r
- "fcntl.lockf() provides better locking", DeprecationWarning, 2)\r
-\r
-class _posixfile_:\r
- """File wrapper class that provides extra POSIX file routines."""\r
-\r
- states = ['open', 'closed']\r
-\r
- #\r
- # Internal routines\r
- #\r
- def __repr__(self):\r
- file = self._file_\r
- return "<%s posixfile '%s', mode '%s' at %s>" % \\r
- (self.states[file.closed], file.name, file.mode, \\r
- hex(id(self))[2:])\r
-\r
- #\r
- # Initialization routines\r
- #\r
- def open(self, name, mode='r', bufsize=-1):\r
- import __builtin__\r
- return self.fileopen(__builtin__.open(name, mode, bufsize))\r
-\r
- def fileopen(self, file):\r
- import types\r
- if repr(type(file)) != "<type 'file'>":\r
- raise TypeError, 'posixfile.fileopen() arg must be file object'\r
- self._file_ = file\r
- # Copy basic file methods\r
- for maybemethod in dir(file):\r
- if not maybemethod.startswith('_'):\r
- attr = getattr(file, maybemethod)\r
- if isinstance(attr, types.BuiltinMethodType):\r
- setattr(self, maybemethod, attr)\r
- return self\r
-\r
- #\r
- # New methods\r
- #\r
- def file(self):\r
- return self._file_\r
-\r
- def dup(self):\r
- import posix\r
-\r
- if not hasattr(posix, 'fdopen'):\r
- raise AttributeError, 'dup() method unavailable'\r
-\r
- return posix.fdopen(posix.dup(self._file_.fileno()), self._file_.mode)\r
-\r
- def dup2(self, fd):\r
- import posix\r
-\r
- if not hasattr(posix, 'fdopen'):\r
- raise AttributeError, 'dup() method unavailable'\r
-\r
- posix.dup2(self._file_.fileno(), fd)\r
- return posix.fdopen(fd, self._file_.mode)\r
-\r
- def flags(self, *which):\r
- import fcntl, os\r
-\r
- if which:\r
- if len(which) > 1:\r
- raise TypeError, 'Too many arguments'\r
- which = which[0]\r
- else: which = '?'\r
-\r
- l_flags = 0\r
- if 'n' in which: l_flags = l_flags | os.O_NDELAY\r
- if 'a' in which: l_flags = l_flags | os.O_APPEND\r
- if 's' in which: l_flags = l_flags | os.O_SYNC\r
-\r
- file = self._file_\r
-\r
- if '=' not in which:\r
- cur_fl = fcntl.fcntl(file.fileno(), fcntl.F_GETFL, 0)\r
- if '!' in which: l_flags = cur_fl & ~ l_flags\r
- else: l_flags = cur_fl | l_flags\r
-\r
- l_flags = fcntl.fcntl(file.fileno(), fcntl.F_SETFL, l_flags)\r
-\r
- if 'c' in which:\r
- arg = ('!' not in which) # 0 is don't, 1 is do close on exec\r
- l_flags = fcntl.fcntl(file.fileno(), fcntl.F_SETFD, arg)\r
-\r
- if '?' in which:\r
- which = '' # Return current flags\r
- l_flags = fcntl.fcntl(file.fileno(), fcntl.F_GETFL, 0)\r
- if os.O_APPEND & l_flags: which = which + 'a'\r
- if fcntl.fcntl(file.fileno(), fcntl.F_GETFD, 0) & 1:\r
- which = which + 'c'\r
- if os.O_NDELAY & l_flags: which = which + 'n'\r
- if os.O_SYNC & l_flags: which = which + 's'\r
- return which\r
-\r
- def lock(self, how, *args):\r
- import struct, fcntl\r
-\r
- if 'w' in how: l_type = fcntl.F_WRLCK\r
- elif 'r' in how: l_type = fcntl.F_RDLCK\r
- elif 'u' in how: l_type = fcntl.F_UNLCK\r
- else: raise TypeError, 'no type of lock specified'\r
-\r
- if '|' in how: cmd = fcntl.F_SETLKW\r
- elif '?' in how: cmd = fcntl.F_GETLK\r
- else: cmd = fcntl.F_SETLK\r
-\r
- l_whence = 0\r
- l_start = 0\r
- l_len = 0\r
-\r
- if len(args) == 1:\r
- l_len = args[0]\r
- elif len(args) == 2:\r
- l_len, l_start = args\r
- elif len(args) == 3:\r
- l_len, l_start, l_whence = args\r
- elif len(args) > 3:\r
- raise TypeError, 'too many arguments'\r
-\r
- # Hack by davem@magnet.com to get locking to go on freebsd;\r
- # additions for AIX by Vladimir.Marangozov@imag.fr\r
- import sys, os\r
- if sys.platform in ('netbsd1',\r
- 'openbsd2',\r
- 'freebsd2', 'freebsd3', 'freebsd4', 'freebsd5',\r
- 'freebsd6', 'freebsd7', 'freebsd8',\r
- 'bsdos2', 'bsdos3', 'bsdos4'):\r
- flock = struct.pack('lxxxxlxxxxlhh', \\r
- l_start, l_len, os.getpid(), l_type, l_whence)\r
- elif sys.platform in ('aix3', 'aix4'):\r
- flock = struct.pack('hhlllii', \\r
- l_type, l_whence, l_start, l_len, 0, 0, 0)\r
- else:\r
- flock = struct.pack('hhllhh', \\r
- l_type, l_whence, l_start, l_len, 0, 0)\r
-\r
- flock = fcntl.fcntl(self._file_.fileno(), cmd, flock)\r
-\r
- if '?' in how:\r
- if sys.platform in ('netbsd1',\r
- 'openbsd2',\r
- 'freebsd2', 'freebsd3', 'freebsd4', 'freebsd5',\r
- 'bsdos2', 'bsdos3', 'bsdos4'):\r
- l_start, l_len, l_pid, l_type, l_whence = \\r
- struct.unpack('lxxxxlxxxxlhh', flock)\r
- elif sys.platform in ('aix3', 'aix4'):\r
- l_type, l_whence, l_start, l_len, l_sysid, l_pid, l_vfs = \\r
- struct.unpack('hhlllii', flock)\r
- elif sys.platform == "linux2":\r
- l_type, l_whence, l_start, l_len, l_pid, l_sysid = \\r
- struct.unpack('hhllhh', flock)\r
- else:\r
- l_type, l_whence, l_start, l_len, l_sysid, l_pid = \\r
- struct.unpack('hhllhh', flock)\r
-\r
- if l_type != fcntl.F_UNLCK:\r
- if l_type == fcntl.F_RDLCK:\r
- return 'r', l_len, l_start, l_whence, l_pid\r
- else:\r
- return 'w', l_len, l_start, l_whence, l_pid\r
-\r
-def open(name, mode='r', bufsize=-1):\r
- """Public routine to open a file as a posixfile object."""\r
- return _posixfile_().open(name, mode, bufsize)\r
-\r
-def fileopen(file):\r
- """Public routine to get a posixfile object from a Python file object."""\r
- return _posixfile_().fileopen(file)\r
-\r
-#\r
-# Constants\r
-#\r
-SEEK_SET = 0\r
-SEEK_CUR = 1\r
-SEEK_END = 2\r
-\r
-#\r
-# End of posixfile.py\r
-#\r