| 1 | n/a | """Extended file operations available in POSIX. |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | f = posixfile.open(filename, [mode, [bufsize]]) |
|---|
| 4 | n/a | will create a new posixfile object |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | f = posixfile.fileopen(fileobject) |
|---|
| 7 | n/a | will create a posixfile object from a builtin file object |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | f.file() |
|---|
| 10 | n/a | will return the original builtin file object |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | f.dup() |
|---|
| 13 | n/a | will return a new file object based on a new filedescriptor |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | f.dup2(fd) |
|---|
| 16 | n/a | will return a new file object based on the given filedescriptor |
|---|
| 17 | n/a | |
|---|
| 18 | n/a | f.flags(mode) |
|---|
| 19 | n/a | will turn on the associated flag (merge) |
|---|
| 20 | n/a | mode can contain the following characters: |
|---|
| 21 | n/a | |
|---|
| 22 | n/a | (character representing a flag) |
|---|
| 23 | n/a | a append only flag |
|---|
| 24 | n/a | c close on exec flag |
|---|
| 25 | n/a | n no delay flag |
|---|
| 26 | n/a | s synchronization flag |
|---|
| 27 | n/a | (modifiers) |
|---|
| 28 | n/a | ! turn flags 'off' instead of default 'on' |
|---|
| 29 | n/a | = copy flags 'as is' instead of default 'merge' |
|---|
| 30 | n/a | ? return a string in which the characters represent the flags |
|---|
| 31 | n/a | that are set |
|---|
| 32 | n/a | |
|---|
| 33 | n/a | note: - the '!' and '=' modifiers are mutually exclusive. |
|---|
| 34 | n/a | - the '?' modifier will return the status of the flags after they |
|---|
| 35 | n/a | have been changed by other characters in the mode string |
|---|
| 36 | n/a | |
|---|
| 37 | n/a | f.lock(mode [, len [, start [, whence]]]) |
|---|
| 38 | n/a | will (un)lock a region |
|---|
| 39 | n/a | mode can contain the following characters: |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | (character representing type of lock) |
|---|
| 42 | n/a | u unlock |
|---|
| 43 | n/a | r read lock |
|---|
| 44 | n/a | w write lock |
|---|
| 45 | n/a | (modifiers) |
|---|
| 46 | n/a | | wait until the lock can be granted |
|---|
| 47 | n/a | ? return the first lock conflicting with the requested lock |
|---|
| 48 | n/a | or 'None' if there is no conflict. The lock returned is in the |
|---|
| 49 | n/a | format (mode, len, start, whence, pid) where mode is a |
|---|
| 50 | n/a | character representing the type of lock ('r' or 'w') |
|---|
| 51 | n/a | |
|---|
| 52 | n/a | note: - the '?' modifier prevents a region from being locked; it is |
|---|
| 53 | n/a | query only |
|---|
| 54 | 1 | """ |
|---|
| 55 | 1 | import warnings |
|---|
| 56 | 1 | warnings.warn("The posixfile module is deprecated; " |
|---|
| 57 | 1 | "fcntl.lockf() provides better locking", DeprecationWarning, 2) |
|---|
| 58 | n/a | |
|---|
| 59 | 2 | class _posixfile_: |
|---|
| 60 | 1 | """File wrapper class that provides extra POSIX file routines.""" |
|---|
| 61 | n/a | |
|---|
| 62 | 1 | states = ['open', 'closed'] |
|---|
| 63 | n/a | |
|---|
| 64 | n/a | # |
|---|
| 65 | n/a | # Internal routines |
|---|
| 66 | n/a | # |
|---|
| 67 | 1 | def __repr__(self): |
|---|
| 68 | 0 | file = self._file_ |
|---|
| 69 | 0 | return "<%s posixfile '%s', mode '%s' at %s>" % \ |
|---|
| 70 | 0 | (self.states[file.closed], file.name, file.mode, \ |
|---|
| 71 | 0 | hex(id(self))[2:]) |
|---|
| 72 | n/a | |
|---|
| 73 | n/a | # |
|---|
| 74 | n/a | # Initialization routines |
|---|
| 75 | n/a | # |
|---|
| 76 | 1 | def open(self, name, mode='r', bufsize=-1): |
|---|
| 77 | 0 | import __builtin__ |
|---|
| 78 | 0 | return self.fileopen(__builtin__.open(name, mode, bufsize)) |
|---|
| 79 | n/a | |
|---|
| 80 | 1 | def fileopen(self, file): |
|---|
| 81 | 0 | import types |
|---|
| 82 | 0 | if repr(type(file)) != "<type 'file'>": |
|---|
| 83 | 0 | raise TypeError, 'posixfile.fileopen() arg must be file object' |
|---|
| 84 | 0 | self._file_ = file |
|---|
| 85 | n/a | # Copy basic file methods |
|---|
| 86 | 0 | for maybemethod in dir(file): |
|---|
| 87 | 0 | if not maybemethod.startswith('_'): |
|---|
| 88 | 0 | attr = getattr(file, maybemethod) |
|---|
| 89 | 0 | if isinstance(attr, types.BuiltinMethodType): |
|---|
| 90 | 0 | setattr(self, maybemethod, attr) |
|---|
| 91 | 0 | return self |
|---|
| 92 | n/a | |
|---|
| 93 | n/a | # |
|---|
| 94 | n/a | # New methods |
|---|
| 95 | n/a | # |
|---|
| 96 | 1 | def file(self): |
|---|
| 97 | 0 | return self._file_ |
|---|
| 98 | n/a | |
|---|
| 99 | 1 | def dup(self): |
|---|
| 100 | 0 | import posix |
|---|
| 101 | n/a | |
|---|
| 102 | 0 | if not hasattr(posix, 'fdopen'): |
|---|
| 103 | 0 | raise AttributeError, 'dup() method unavailable' |
|---|
| 104 | n/a | |
|---|
| 105 | 0 | return posix.fdopen(posix.dup(self._file_.fileno()), self._file_.mode) |
|---|
| 106 | n/a | |
|---|
| 107 | 1 | def dup2(self, fd): |
|---|
| 108 | 0 | import posix |
|---|
| 109 | n/a | |
|---|
| 110 | 0 | if not hasattr(posix, 'fdopen'): |
|---|
| 111 | 0 | raise AttributeError, 'dup() method unavailable' |
|---|
| 112 | n/a | |
|---|
| 113 | 0 | posix.dup2(self._file_.fileno(), fd) |
|---|
| 114 | 0 | return posix.fdopen(fd, self._file_.mode) |
|---|
| 115 | n/a | |
|---|
| 116 | 1 | def flags(self, *which): |
|---|
| 117 | 0 | import fcntl, os |
|---|
| 118 | n/a | |
|---|
| 119 | 0 | if which: |
|---|
| 120 | 0 | if len(which) > 1: |
|---|
| 121 | 0 | raise TypeError, 'Too many arguments' |
|---|
| 122 | 0 | which = which[0] |
|---|
| 123 | 0 | else: which = '?' |
|---|
| 124 | n/a | |
|---|
| 125 | 0 | l_flags = 0 |
|---|
| 126 | 0 | if 'n' in which: l_flags = l_flags | os.O_NDELAY |
|---|
| 127 | 0 | if 'a' in which: l_flags = l_flags | os.O_APPEND |
|---|
| 128 | 0 | if 's' in which: l_flags = l_flags | os.O_SYNC |
|---|
| 129 | n/a | |
|---|
| 130 | 0 | file = self._file_ |
|---|
| 131 | n/a | |
|---|
| 132 | 0 | if '=' not in which: |
|---|
| 133 | 0 | cur_fl = fcntl.fcntl(file.fileno(), fcntl.F_GETFL, 0) |
|---|
| 134 | 0 | if '!' in which: l_flags = cur_fl & ~ l_flags |
|---|
| 135 | 0 | else: l_flags = cur_fl | l_flags |
|---|
| 136 | n/a | |
|---|
| 137 | 0 | l_flags = fcntl.fcntl(file.fileno(), fcntl.F_SETFL, l_flags) |
|---|
| 138 | n/a | |
|---|
| 139 | 0 | if 'c' in which: |
|---|
| 140 | 0 | arg = ('!' not in which) # 0 is don't, 1 is do close on exec |
|---|
| 141 | 0 | l_flags = fcntl.fcntl(file.fileno(), fcntl.F_SETFD, arg) |
|---|
| 142 | n/a | |
|---|
| 143 | 0 | if '?' in which: |
|---|
| 144 | 0 | which = '' # Return current flags |
|---|
| 145 | 0 | l_flags = fcntl.fcntl(file.fileno(), fcntl.F_GETFL, 0) |
|---|
| 146 | 0 | if os.O_APPEND & l_flags: which = which + 'a' |
|---|
| 147 | 0 | if fcntl.fcntl(file.fileno(), fcntl.F_GETFD, 0) & 1: |
|---|
| 148 | 0 | which = which + 'c' |
|---|
| 149 | 0 | if os.O_NDELAY & l_flags: which = which + 'n' |
|---|
| 150 | 0 | if os.O_SYNC & l_flags: which = which + 's' |
|---|
| 151 | 0 | return which |
|---|
| 152 | n/a | |
|---|
| 153 | 1 | def lock(self, how, *args): |
|---|
| 154 | 0 | import struct, fcntl |
|---|
| 155 | n/a | |
|---|
| 156 | 0 | if 'w' in how: l_type = fcntl.F_WRLCK |
|---|
| 157 | 0 | elif 'r' in how: l_type = fcntl.F_RDLCK |
|---|
| 158 | 0 | elif 'u' in how: l_type = fcntl.F_UNLCK |
|---|
| 159 | 0 | else: raise TypeError, 'no type of lock specified' |
|---|
| 160 | n/a | |
|---|
| 161 | 0 | if '|' in how: cmd = fcntl.F_SETLKW |
|---|
| 162 | 0 | elif '?' in how: cmd = fcntl.F_GETLK |
|---|
| 163 | 0 | else: cmd = fcntl.F_SETLK |
|---|
| 164 | n/a | |
|---|
| 165 | 0 | l_whence = 0 |
|---|
| 166 | 0 | l_start = 0 |
|---|
| 167 | 0 | l_len = 0 |
|---|
| 168 | n/a | |
|---|
| 169 | 0 | if len(args) == 1: |
|---|
| 170 | 0 | l_len = args[0] |
|---|
| 171 | 0 | elif len(args) == 2: |
|---|
| 172 | 0 | l_len, l_start = args |
|---|
| 173 | 0 | elif len(args) == 3: |
|---|
| 174 | 0 | l_len, l_start, l_whence = args |
|---|
| 175 | 0 | elif len(args) > 3: |
|---|
| 176 | 0 | raise TypeError, 'too many arguments' |
|---|
| 177 | n/a | |
|---|
| 178 | n/a | # Hack by davem@magnet.com to get locking to go on freebsd; |
|---|
| 179 | n/a | # additions for AIX by Vladimir.Marangozov@imag.fr |
|---|
| 180 | 0 | import sys, os |
|---|
| 181 | 0 | if sys.platform in ('netbsd1', |
|---|
| 182 | 0 | 'openbsd2', |
|---|
| 183 | 0 | 'freebsd2', 'freebsd3', 'freebsd4', 'freebsd5', |
|---|
| 184 | 0 | 'freebsd6', 'freebsd7', 'freebsd8', |
|---|
| 185 | 0 | 'bsdos2', 'bsdos3', 'bsdos4'): |
|---|
| 186 | 0 | flock = struct.pack('lxxxxlxxxxlhh', \ |
|---|
| 187 | 0 | l_start, l_len, os.getpid(), l_type, l_whence) |
|---|
| 188 | 0 | elif sys.platform in ('aix3', 'aix4'): |
|---|
| 189 | 0 | flock = struct.pack('hhlllii', \ |
|---|
| 190 | 0 | l_type, l_whence, l_start, l_len, 0, 0, 0) |
|---|
| 191 | n/a | else: |
|---|
| 192 | 0 | flock = struct.pack('hhllhh', \ |
|---|
| 193 | 0 | l_type, l_whence, l_start, l_len, 0, 0) |
|---|
| 194 | n/a | |
|---|
| 195 | 0 | flock = fcntl.fcntl(self._file_.fileno(), cmd, flock) |
|---|
| 196 | n/a | |
|---|
| 197 | 0 | if '?' in how: |
|---|
| 198 | 0 | if sys.platform in ('netbsd1', |
|---|
| 199 | 0 | 'openbsd2', |
|---|
| 200 | 0 | 'freebsd2', 'freebsd3', 'freebsd4', 'freebsd5', |
|---|
| 201 | 0 | 'bsdos2', 'bsdos3', 'bsdos4'): |
|---|
| 202 | n/a | l_start, l_len, l_pid, l_type, l_whence = \ |
|---|
| 203 | 0 | struct.unpack('lxxxxlxxxxlhh', flock) |
|---|
| 204 | 0 | elif sys.platform in ('aix3', 'aix4'): |
|---|
| 205 | n/a | l_type, l_whence, l_start, l_len, l_sysid, l_pid, l_vfs = \ |
|---|
| 206 | 0 | struct.unpack('hhlllii', flock) |
|---|
| 207 | 0 | elif sys.platform == "linux2": |
|---|
| 208 | n/a | l_type, l_whence, l_start, l_len, l_pid, l_sysid = \ |
|---|
| 209 | 0 | struct.unpack('hhllhh', flock) |
|---|
| 210 | n/a | else: |
|---|
| 211 | n/a | l_type, l_whence, l_start, l_len, l_sysid, l_pid = \ |
|---|
| 212 | 0 | struct.unpack('hhllhh', flock) |
|---|
| 213 | n/a | |
|---|
| 214 | 0 | if l_type != fcntl.F_UNLCK: |
|---|
| 215 | 0 | if l_type == fcntl.F_RDLCK: |
|---|
| 216 | 0 | return 'r', l_len, l_start, l_whence, l_pid |
|---|
| 217 | n/a | else: |
|---|
| 218 | 0 | return 'w', l_len, l_start, l_whence, l_pid |
|---|
| 219 | n/a | |
|---|
| 220 | 1 | def open(name, mode='r', bufsize=-1): |
|---|
| 221 | n/a | """Public routine to open a file as a posixfile object.""" |
|---|
| 222 | 0 | return _posixfile_().open(name, mode, bufsize) |
|---|
| 223 | n/a | |
|---|
| 224 | 1 | def fileopen(file): |
|---|
| 225 | n/a | """Public routine to get a posixfile object from a Python file object.""" |
|---|
| 226 | 0 | return _posixfile_().fileopen(file) |
|---|
| 227 | n/a | |
|---|
| 228 | n/a | # |
|---|
| 229 | n/a | # Constants |
|---|
| 230 | n/a | # |
|---|
| 231 | 1 | SEEK_SET = 0 |
|---|
| 232 | 1 | SEEK_CUR = 1 |
|---|
| 233 | 1 | SEEK_END = 2 |
|---|
| 234 | n/a | |
|---|
| 235 | n/a | # |
|---|
| 236 | n/a | # End of posixfile.py |
|---|
| 237 | n/a | # |
|---|