1 import sys, os, pwd, subprocess, errno, socket, select, mmap, stat
12 if e.errno == errno.EEXIST:
29 if e.errno == errno.ENOENT:
30 pass # it doesn't exist, that's what you asked for
34 p = subprocess.Popen(argv, stdout=subprocess.PIPE)
40 # FIXME: this function isn't very generic, because it splits the filename
41 # in an odd way and depends on a terminating '/' to indicate directories.
42 # But it's used in a couple of places, so let's put it here.
45 l = list([i+'/' for i in l[:-1]]) + l[-1:]
47 l.pop() # extra blank caused by terminating '/'
51 # like os.path.realpath, but doesn't follow a symlink for the last element.
52 # (ie. if 'p' itself is itself a symlink, this one won't follow it)
58 if st and stat.S_ISLNK(st.st_mode):
59 (dir, name) = os.path.split(p)
60 dir = os.path.realpath(dir)
61 out = os.path.join(dir, name)
63 out = os.path.realpath(p)
64 #log('realpathing:%r,%r\n' % (p, out))
74 _username = pwd.getpwuid(uid)[0]
76 _username = 'user%d' % uid
86 _userfullname = pwd.getpwuid(uid)[4].split(',')[0]
88 _userfullname = 'user%d' % uid
96 _hostname = socket.getfqdn()
101 def __init__(self, inp, outp):
105 def read(self, size):
107 return self.inp.read(size)
111 return self.inp.readline()
113 def write(self, data):
114 #log('%d writing: %d bytes\n' % (os.getpid(), len(data)))
115 self.outp.write(data)
118 [rl, wl, xl] = select.select([self.inp.fileno()], [], [], 0)
120 assert(rl[0] == self.inp.fileno())
128 def drain_and_check_ok(self):
131 for rl in linereader(self.inp):
132 #log('%d got line: %r\n' % (os.getpid(), rl))
133 if not rl: # empty line
144 for rl in linereader(self.inp):
145 #log('%d got line: %r\n' % (os.getpid(), rl))
146 if not rl: # empty line
151 raise Exception('expected "ok", got %r' % rl)
152 raise Exception('server exited unexpectedly; see errors above')
163 def chunkyreader(f, count = None):
166 b = f.read(min(count, 65536))
168 raise IOError('EOF with %d bytes remaining' % count)
179 if s and not s.endswith('/'):
185 def _mmap_do(f, len, flags, prot):
187 st = os.fstat(f.fileno())
189 map = mmap.mmap(f.fileno(), len, flags, prot)
190 f.close() # map will persist beyond file close
194 def mmap_read(f, len = 0):
195 return _mmap_do(f, len, mmap.MAP_PRIVATE, mmap.PROT_READ)
198 def mmap_readwrite(f, len = 0):
199 return _mmap_do(f, len, mmap.MAP_SHARED, mmap.PROT_READ|mmap.PROT_WRITE)
202 # count the number of elements in an iterator (consumes the iterator)
204 return reduce(lambda x,y: x+1, l)
209 saved_errors.append(e)