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