0001 """Extended file operations available in POSIX. 0002 0003 f = posixfile.open(filename, [mode, [bufsize]]) 0004 will create a new posixfile object 0005 0006 f = posixfile.fileopen(fileobject) 0007 will create a posixfile object from a builtin file object 0008 0009 f.file() 0010 will return the original builtin file object 0011 0012 f.dup() 0013 will return a new file object based on a new filedescriptor 0014 0015 f.dup2(fd) 0016 will return a new file object based on the given filedescriptor 0017 0018 f.flags(mode) 0019 will turn on the associated flag (merge) 0020 mode can contain the following characters: 0021 0022 (character representing a flag) 0023 a append only flag 0024 c close on exec flag 0025 n no delay flag 0026 s synchronization flag 0027 (modifiers) 0028 ! turn flags 'off' instead of default 'on' 0029 = copy flags 'as is' instead of default 'merge' 0030 ? return a string in which the characters represent the flags 0031 that are set 0032 0033 note: - the '!' and '=' modifiers are mutually exclusive. 0034 - the '?' modifier will return the status of the flags after they 0035 have been changed by other characters in the mode string 0036 0037 f.lock(mode [, len [, start [, whence]]]) 0038 will (un)lock a region 0039 mode can contain the following characters: 0040 0041 (character representing type of lock) 0042 u unlock 0043 r read lock 0044 w write lock 0045 (modifiers) 0046 | wait until the lock can be granted 0047 ? return the first lock conflicting with the requested lock 0048 or 'None' if there is no conflict. The lock returned is in the 0049 format (mode, len, start, whence, pid) where mode is a 0050 character representing the type of lock ('r' or 'w') 0051 0052 note: - the '?' modifier prevents a region from being locked; it is 0053 query only 0054 """ 0055 0056 import warnings 0057 warnings.warn( 0058 "The posixfile module is obsolete and will disappear in the future", 0059 DeprecationWarning) 0060 del warnings 0061 0062 0063 class _posixfile_: 0064 """File wrapper class that provides extra POSIX file routines.""" 0065 0066 states = ['open', 'closed'] 0067 0068 # 0069 # Internal routines 0070 # 0071 def __repr__(self): 0072 file = self._file_ 0073 return "<%s posixfile '%s', mode '%s' at %s>" % \ 0074 (self.states[file.closed], file.name, file.mode, \ 0075 hex(id(self))[2:]) 0076 0077 # 0078 # Initialization routines 0079 # 0080 def open(self, name, mode='r', bufsize=-1): 0081 import __builtin__ 0082 return self.fileopen(__builtin__.open(name, mode, bufsize)) 0083 0084 def fileopen(self, file): 0085 import types 0086 if repr(type(file)) != "<type 'file'>": 0087 raise TypeError, 'posixfile.fileopen() arg must be file object' 0088 self._file_ = file 0089 # Copy basic file methods 0090 for maybemethod in dir(file): 0091 if not maybemethod.startswith('_'): 0092 attr = getattr(file, maybemethod) 0093 if isinstance(attr, types.BuiltinMethodType): 0094 setattr(self, maybemethod, attr) 0095 return self 0096 0097 # 0098 # New methods 0099 # 0100 def file(self): 0101 return self._file_ 0102 0103 def dup(self): 0104 import posix 0105 0106 if not hasattr(posix, 'fdopen'): 0107 raise AttributeError, 'dup() method unavailable' 0108 0109 return posix.fdopen(posix.dup(self._file_.fileno()), self._file_.mode) 0110 0111 def dup2(self, fd): 0112 import posix 0113 0114 if not hasattr(posix, 'fdopen'): 0115 raise AttributeError, 'dup() method unavailable' 0116 0117 posix.dup2(self._file_.fileno(), fd) 0118 return posix.fdopen(fd, self._file_.mode) 0119 0120 def flags(self, *which): 0121 import fcntl, os 0122 0123 if which: 0124 if len(which) > 1: 0125 raise TypeError, 'Too many arguments' 0126 which = which[0] 0127 else: which = '?' 0128 0129 l_flags = 0 0130 if 'n' in which: l_flags = l_flags | os.O_NDELAY 0131 if 'a' in which: l_flags = l_flags | os.O_APPEND 0132 if 's' in which: l_flags = l_flags | os.O_SYNC 0133 0134 file = self._file_ 0135 0136 if '=' not in which: 0137 cur_fl = fcntl.fcntl(file.fileno(), fcntl.F_GETFL, 0) 0138 if '!' in which: l_flags = cur_fl & ~ l_flags 0139 else: l_flags = cur_fl | l_flags 0140 0141 l_flags = fcntl.fcntl(file.fileno(), fcntl.F_SETFL, l_flags) 0142 0143 if 'c' in which: 0144 arg = ('!' not in which) # 0 is don't, 1 is do close on exec 0145 l_flags = fcntl.fcntl(file.fileno(), fcntl.F_SETFD, arg) 0146 0147 if '?' in which: 0148 which = '' # Return current flags 0149 l_flags = fcntl.fcntl(file.fileno(), fcntl.F_GETFL, 0) 0150 if os.O_APPEND & l_flags: which = which + 'a' 0151 if fcntl.fcntl(file.fileno(), fcntl.F_GETFD, 0) & 1: 0152 which = which + 'c' 0153 if os.O_NDELAY & l_flags: which = which + 'n' 0154 if os.O_SYNC & l_flags: which = which + 's' 0155 return which 0156 0157 def lock(self, how, *args): 0158 import struct, fcntl 0159 0160 if 'w' in how: l_type = fcntl.F_WRLCK 0161 elif 'r' in how: l_type = fcntl.F_RDLCK 0162 elif 'u' in how: l_type = fcntl.F_UNLCK 0163 else: raise TypeError, 'no type of lock specified' 0164 0165 if '|' in how: cmd = fcntl.F_SETLKW 0166 elif '?' in how: cmd = fcntl.F_GETLK 0167 else: cmd = fcntl.F_SETLK 0168 0169 l_whence = 0 0170 l_start = 0 0171 l_len = 0 0172 0173 if len(args) == 1: 0174 l_len = args[0] 0175 elif len(args) == 2: 0176 l_len, l_start = args 0177 elif len(args) == 3: 0178 l_len, l_start, l_whence = args 0179 elif len(args) > 3: 0180 raise TypeError, 'too many arguments' 0181 0182 # Hack by davem@magnet.com to get locking to go on freebsd; 0183 # additions for AIX by Vladimir.Marangozov@imag.fr 0184 import sys, os 0185 if sys.platform in ('netbsd1', 0186 'openbsd2', 0187 'freebsd2', 'freebsd3', 'freebsd4', 'freebsd5', 0188 'freebsd6', 'bsdos2', 'bsdos3', 'bsdos4'): 0189 flock = struct.pack('lxxxxlxxxxlhh', \ 0190 l_start, l_len, os.getpid(), l_type, l_whence) 0191 elif sys.platform in ['aix3', 'aix4']: 0192 flock = struct.pack('hhlllii', \ 0193 l_type, l_whence, l_start, l_len, 0, 0, 0) 0194 else: 0195 flock = struct.pack('hhllhh', \ 0196 l_type, l_whence, l_start, l_len, 0, 0) 0197 0198 flock = fcntl.fcntl(self._file_.fileno(), cmd, flock) 0199 0200 if '?' in how: 0201 if sys.platform in ('netbsd1', 0202 'openbsd2', 0203 'freebsd2', 'freebsd3', 'freebsd4', 'freebsd5', 0204 'bsdos2', 'bsdos3', 'bsdos4'): 0205 l_start, l_len, l_pid, l_type, l_whence = \ 0206 struct.unpack('lxxxxlxxxxlhh', flock) 0207 elif sys.platform in ['aix3', 'aix4']: 0208 l_type, l_whence, l_start, l_len, l_sysid, l_pid, l_vfs = \ 0209 struct.unpack('hhlllii', flock) 0210 elif sys.platform == "linux2": 0211 l_type, l_whence, l_start, l_len, l_pid, l_sysid = \ 0212 struct.unpack('hhllhh', flock) 0213 else: 0214 l_type, l_whence, l_start, l_len, l_sysid, l_pid = \ 0215 struct.unpack('hhllhh', flock) 0216 0217 if l_type != fcntl.F_UNLCK: 0218 if l_type == fcntl.F_RDLCK: 0219 return 'r', l_len, l_start, l_whence, l_pid 0220 else: 0221 return 'w', l_len, l_start, l_whence, l_pid 0222 0223 def open(name, mode='r', bufsize=-1): 0224 """Public routine to open a file as a posixfile object.""" 0225 return _posixfile_().open(name, mode, bufsize) 0226 0227 def fileopen(file): 0228 """Public routine to get a posixfile object from a Python file object.""" 0229 return _posixfile_().fileopen(file) 0230 0231 # 0232 # Constants 0233 # 0234 SEEK_SET = 0 0235 SEEK_CUR = 1 0236 SEEK_END = 2 0237 0238 # 0239 # End of posixfile.py 0240 # 0241
Generated by PyXR 0.9.4