| 
									
										
										
										
											1996-07-22 15:23:25 +00:00
										 |  |  | # | 
					
						
							|  |  |  | # Start of posixfile.py | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Extended file operations | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # f = posixfile.open(filename, [mode, [bufsize]]) | 
					
						
							| 
									
										
										
										
											1998-03-26 22:14:20 +00:00
										 |  |  | #       will create a new posixfile object | 
					
						
							| 
									
										
										
										
											1996-07-22 15:23:25 +00:00
										 |  |  | # | 
					
						
							|  |  |  | # f = posixfile.fileopen(fileobject) | 
					
						
							| 
									
										
										
										
											1998-03-26 22:14:20 +00:00
										 |  |  | #       will create a posixfile object from a builtin file object | 
					
						
							| 
									
										
										
										
											1996-07-22 15:23:25 +00:00
										 |  |  | # | 
					
						
							|  |  |  | # f.file() | 
					
						
							| 
									
										
										
										
											1998-03-26 22:14:20 +00:00
										 |  |  | #       will return the original builtin file object | 
					
						
							| 
									
										
										
										
											1996-07-22 15:23:25 +00:00
										 |  |  | # | 
					
						
							|  |  |  | # f.dup() | 
					
						
							| 
									
										
										
										
											1998-03-26 22:14:20 +00:00
										 |  |  | #       will return a new file object based on a new filedescriptor | 
					
						
							| 
									
										
										
										
											1996-07-22 15:23:25 +00:00
										 |  |  | # | 
					
						
							|  |  |  | # f.dup2(fd) | 
					
						
							| 
									
										
										
										
											1998-03-26 22:14:20 +00:00
										 |  |  | #       will return a new file object based on the given filedescriptor | 
					
						
							| 
									
										
										
										
											1996-07-22 15:23:25 +00:00
										 |  |  | # | 
					
						
							|  |  |  | # f.flags(mode) | 
					
						
							| 
									
										
										
										
											1998-03-26 22:14:20 +00:00
										 |  |  | #       will turn on the associated flag (merge) | 
					
						
							|  |  |  | #       mode can contain the following characters: | 
					
						
							| 
									
										
										
										
											1996-07-22 15:23:25 +00:00
										 |  |  | # | 
					
						
							|  |  |  | #   (character representing a flag) | 
					
						
							| 
									
										
										
										
											1998-03-26 22:14:20 +00:00
										 |  |  | #       a       append only flag | 
					
						
							|  |  |  | #       c       close on exec flag | 
					
						
							|  |  |  | #       n       no delay flag | 
					
						
							|  |  |  | #       s       synchronization flag | 
					
						
							| 
									
										
										
										
											1996-07-22 15:23:25 +00:00
										 |  |  | #   (modifiers) | 
					
						
							| 
									
										
										
										
											1998-03-26 22:14:20 +00:00
										 |  |  | #       !       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 | 
					
						
							| 
									
										
										
										
											1996-07-22 15:23:25 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											1998-03-26 22:14:20 +00:00
										 |  |  | #       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 | 
					
						
							| 
									
										
										
										
											1996-07-22 15:23:25 +00:00
										 |  |  | # | 
					
						
							|  |  |  | # f.lock(mode [, len [, start [, whence]]]) | 
					
						
							| 
									
										
										
										
											1998-03-26 22:14:20 +00:00
										 |  |  | #       will (un)lock a region | 
					
						
							|  |  |  | #       mode can contain the following characters: | 
					
						
							| 
									
										
										
										
											1996-07-22 15:23:25 +00:00
										 |  |  | # | 
					
						
							|  |  |  | #   (character representing type of lock) | 
					
						
							| 
									
										
										
										
											1998-03-26 22:14:20 +00:00
										 |  |  | #       u       unlock | 
					
						
							|  |  |  | #       r       read lock | 
					
						
							|  |  |  | #       w       write lock | 
					
						
							| 
									
										
										
										
											1996-07-22 15:23:25 +00:00
										 |  |  | #   (modifiers) | 
					
						
							| 
									
										
										
										
											1998-03-26 22:14:20 +00:00
										 |  |  | #       |       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') | 
					
						
							| 
									
										
										
										
											1996-07-22 15:23:25 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											1998-03-26 22:14:20 +00:00
										 |  |  | #       note: - the '?' modifier prevents a region from being locked; it is | 
					
						
							|  |  |  | #               query only | 
					
						
							| 
									
										
										
										
											1996-07-22 15:23:25 +00:00
										 |  |  | # | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class _posixfile_: | 
					
						
							|  |  |  |     states = ['open', 'closed'] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     # Internal routines | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     def __repr__(self): | 
					
						
							| 
									
										
										
										
											1998-03-26 22:14:20 +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:]) | 
					
						
							| 
									
										
										
										
											1996-07-22 15:23:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def __del__(self): | 
					
						
							| 
									
										
										
										
											1998-03-26 22:14:20 +00:00
										 |  |  |         self._file_.close() | 
					
						
							| 
									
										
										
										
											1996-07-22 15:23:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     # Initialization routines | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     def open(self, name, mode='r', bufsize=-1): | 
					
						
							| 
									
										
										
										
											1998-03-26 22:14:20 +00:00
										 |  |  |         import __builtin__ | 
					
						
							|  |  |  |         return self.fileopen(__builtin__.open(name, mode, bufsize)) | 
					
						
							| 
									
										
										
										
											1996-07-22 15:23:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def fileopen(self, file): | 
					
						
							| 
									
										
										
										
											1998-03-26 22:14:20 +00:00
										 |  |  |         if `type(file)` != "<type 'file'>": | 
					
						
							|  |  |  |             raise TypeError, 'posixfile.fileopen() arg must be file object' | 
					
						
							|  |  |  |         self._file_  = file | 
					
						
							|  |  |  |         # Copy basic file methods | 
					
						
							|  |  |  |         for method in file.__methods__: | 
					
						
							|  |  |  |             setattr(self, method, getattr(file, method)) | 
					
						
							|  |  |  |         return self | 
					
						
							| 
									
										
										
										
											1996-07-22 15:23:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     # New methods | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     def file(self): | 
					
						
							| 
									
										
										
										
											1998-03-26 22:14:20 +00:00
										 |  |  |         return self._file_ | 
					
						
							| 
									
										
										
										
											1996-07-22 15:23:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def dup(self): | 
					
						
							| 
									
										
										
										
											1998-03-26 22:14:20 +00:00
										 |  |  |         import posix | 
					
						
							| 
									
										
										
										
											1996-07-22 15:23:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 22:14:20 +00:00
										 |  |  |         try: ignore = posix.fdopen | 
					
						
							|  |  |  |         except: raise AttributeError, 'dup() method unavailable' | 
					
						
							| 
									
										
										
										
											1996-07-22 15:23:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 22:14:20 +00:00
										 |  |  |         return posix.fdopen(posix.dup(self._file_.fileno()), self._file_.mode) | 
					
						
							| 
									
										
										
										
											1996-07-22 15:23:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def dup2(self, fd): | 
					
						
							| 
									
										
										
										
											1998-03-26 22:14:20 +00:00
										 |  |  |         import posix | 
					
						
							| 
									
										
										
										
											1996-07-22 15:23:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 22:14:20 +00:00
										 |  |  |         try: ignore = posix.fdopen | 
					
						
							|  |  |  |         except: raise AttributeError, 'dup() method unavailable' | 
					
						
							| 
									
										
										
										
											1996-07-22 15:23:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 22:14:20 +00:00
										 |  |  |         posix.dup2(self._file_.fileno(), fd) | 
					
						
							|  |  |  |         return posix.fdopen(fd, self._file_.mode) | 
					
						
							| 
									
										
										
										
											1996-07-22 15:23:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def flags(self, *which): | 
					
						
							| 
									
										
										
										
											1998-03-26 22:14:20 +00:00
										 |  |  |         import fcntl, FCNTL | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if which: | 
					
						
							|  |  |  |             if len(which) > 1: | 
					
						
							|  |  |  |                 raise TypeError, 'Too many arguments' | 
					
						
							|  |  |  |             which = which[0] | 
					
						
							|  |  |  |         else: which = '?' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         l_flags = 0 | 
					
						
							|  |  |  |         if 'n' in which: l_flags = l_flags | FCNTL.O_NDELAY | 
					
						
							|  |  |  |         if 'a' in which: l_flags = l_flags | FCNTL.O_APPEND | 
					
						
							|  |  |  |         if 's' in which: l_flags = l_flags | FCNTL.O_SYNC | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         file = self._file_ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if '=' not in which: | 
					
						
							|  |  |  |             cur_fl = fcntl.fcntl(file.fileno(), FCNTL.F_GETFL, 0) | 
					
						
							|  |  |  |             if '!' in which: l_flags = cur_fl & ~ l_flags | 
					
						
							|  |  |  |             else: l_flags = cur_fl | l_flags | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         l_flags = fcntl.fcntl(file.fileno(), FCNTL.F_SETFL, l_flags) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if 'c' in which:         | 
					
						
							|  |  |  |             arg = ('!' not in which)    # 0 is don't, 1 is do close on exec | 
					
						
							|  |  |  |             l_flags = fcntl.fcntl(file.fileno(), FCNTL.F_SETFD, arg) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if '?' in which: | 
					
						
							|  |  |  |             which = ''                  # Return current flags | 
					
						
							|  |  |  |             l_flags = fcntl.fcntl(file.fileno(), FCNTL.F_GETFL, 0) | 
					
						
							|  |  |  |             if FCNTL.O_APPEND & l_flags: which = which + 'a' | 
					
						
							|  |  |  |             if fcntl.fcntl(file.fileno(), FCNTL.F_GETFD, 0) & 1: | 
					
						
							|  |  |  |                 which = which + 'c' | 
					
						
							|  |  |  |             if FCNTL.O_NDELAY & l_flags: which = which + 'n' | 
					
						
							|  |  |  |             if FCNTL.O_SYNC & l_flags: which = which + 's' | 
					
						
							|  |  |  |             return which | 
					
						
							|  |  |  |          | 
					
						
							| 
									
										
										
										
											1996-07-22 15:23:25 +00:00
										 |  |  |     def lock(self, how, *args): | 
					
						
							| 
									
										
										
										
											1998-03-26 22:14:20 +00:00
										 |  |  |         import struct, fcntl, FCNTL | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         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 | 
					
						
							|  |  |  |         else: raise TypeError, 'no type of lock specified' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if '|' in how: cmd = FCNTL.F_SETLKW | 
					
						
							|  |  |  |         elif '?' in how: cmd = FCNTL.F_GETLK | 
					
						
							|  |  |  |         else: cmd = FCNTL.F_SETLK | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         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:35:26 +00:00
										 |  |  |         import sys, os | 
					
						
							| 
									
										
										
										
											1999-04-08 20:27:54 +00:00
										 |  |  |         if sys.platform in ('netbsd1', | 
					
						
							|  |  |  |                             'freebsd2', 'freebsd3', | 
					
						
							|  |  |  |                             'bsdos2', 'bsdos3', 'bsdos4'): | 
					
						
							| 
									
										
										
										
											1998-03-26 22:14:20 +00:00
										 |  |  |             flock = struct.pack('lxxxxlxxxxlhh', \ | 
					
						
							|  |  |  |                   l_start, l_len, os.getpid(), l_type, l_whence)  | 
					
						
							| 
									
										
										
										
											1996-08-26 18:33:32 +00:00
										 |  |  |         elif sys.platform in ['aix3', 'aix4']: | 
					
						
							|  |  |  |             flock = struct.pack('hhlllii', \ | 
					
						
							|  |  |  |                   l_type, l_whence, l_start, l_len, 0, 0, 0) | 
					
						
							| 
									
										
										
										
											1998-03-26 22:14:20 +00:00
										 |  |  |         else: | 
					
						
							|  |  |  |             flock = struct.pack('hhllhh', \ | 
					
						
							|  |  |  |                   l_type, l_whence, l_start, l_len, 0, 0) | 
					
						
							| 
									
										
										
										
											1996-07-30 16:35:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 22:14:20 +00:00
										 |  |  |         flock = fcntl.fcntl(self._file_.fileno(), cmd, flock) | 
					
						
							| 
									
										
										
										
											1996-07-22 15:23:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 22:14:20 +00:00
										 |  |  |         if '?' in how: | 
					
						
							| 
									
										
										
										
											1999-04-08 20:27:54 +00:00
										 |  |  |             if sys.platform in ('netbsd1', | 
					
						
							|  |  |  |                                 'freebsd2', 'freebsd3', | 
					
						
							|  |  |  |                                 'bsdos2', 'bsdos3', 'bsdos4'): | 
					
						
							| 
									
										
										
										
											1998-03-26 22:14:20 +00:00
										 |  |  |                 l_start, l_len, l_pid, l_type, l_whence = \ | 
					
						
							|  |  |  |                     struct.unpack('lxxxxlxxxxlhh', flock) | 
					
						
							| 
									
										
										
										
											1996-08-26 18:33:32 +00:00
										 |  |  |             elif sys.platform in ['aix3', 'aix4']: | 
					
						
							|  |  |  |                 l_type, l_whence, l_start, l_len, l_sysid, l_pid, l_vfs = \ | 
					
						
							|  |  |  |                     struct.unpack('hhlllii', flock) | 
					
						
							| 
									
										
										
										
											1998-03-26 22:14:20 +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) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if l_type != FCNTL.F_UNLCK: | 
					
						
							|  |  |  |                 if l_type == FCNTL.F_RDLCK: | 
					
						
							|  |  |  |                     return 'r', l_len, l_start, l_whence, l_pid | 
					
						
							|  |  |  |                 else: | 
					
						
							|  |  |  |                     return 'w', l_len, l_start, l_whence, l_pid | 
					
						
							| 
									
										
										
										
											1996-07-22 15:23:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Public routine to obtain a posixfile object | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | def open(name, mode='r', bufsize=-1): | 
					
						
							|  |  |  |     return _posixfile_().open(name, mode, bufsize) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def fileopen(file): | 
					
						
							|  |  |  |     return _posixfile_().fileopen(file) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Constants | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | SEEK_SET = 0 | 
					
						
							|  |  |  | SEEK_CUR = 1 | 
					
						
							|  |  |  | SEEK_END = 2 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # End of posixfile.py | 
					
						
							|  |  |  | # |