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