1 import sys, os, pwd, subprocess, errno, socket, select, mmap, stat, re
4 # Write (blockingly) to sockets that may or may not be in blocking mode.
5 # We need this because our stderr is sometimes eaten by subprocesses
6 # (probably ssh) that sometimes make it nonblocking, if only temporarily,
7 # leading to race conditions. Ick. We'll do it the hard way.
8 def _hard_write(fd, buf):
10 (r,w,x) = select.select([], [fd], [], None)
12 raise IOError('select(fd) returned without being writable')
14 sz = os.write(fd, buf)
16 if e.errno != errno.EAGAIN:
23 _hard_write(sys.stderr.fileno(), s)
30 if e.errno == errno.EEXIST:
47 if e.errno == errno.ENOENT:
48 pass # it doesn't exist, that's what you asked for
52 p = subprocess.Popen(argv, stdout=subprocess.PIPE)
58 # FIXME: this function isn't very generic, because it splits the filename
59 # in an odd way and depends on a terminating '/' to indicate directories.
60 # But it's used in a couple of places, so let's put it here.
63 l = [i+'/' for i in l[:-1]] + l[-1:]
65 l.pop() # extra blank caused by terminating '/'
69 # like os.path.realpath, but doesn't follow a symlink for the last element.
70 # (ie. if 'p' itself is itself a symlink, this one won't follow it)
76 if st and stat.S_ISLNK(st.st_mode):
77 (dir, name) = os.path.split(p)
78 dir = os.path.realpath(dir)
79 out = os.path.join(dir, name)
81 out = os.path.realpath(p)
82 #log('realpathing:%r,%r\n' % (p, out))
92 _username = pwd.getpwuid(uid)[0]
94 _username = 'user%d' % uid
101 if not _userfullname:
104 _userfullname = pwd.getpwuid(uid)[4].split(',')[0]
106 _userfullname = 'user%d' % uid
114 _hostname = socket.getfqdn()
118 class NotOk(Exception):
122 def __init__(self, inp, outp):
126 def read(self, size):
128 return self.inp.read(size)
132 return self.inp.readline()
134 def write(self, data):
135 #log('%d writing: %d bytes\n' % (os.getpid(), len(data)))
136 self.outp.write(data)
139 [rl, wl, xl] = select.select([self.inp.fileno()], [], [], 0)
141 assert(rl[0] == self.inp.fileno())
150 s = re.sub(r'\s+', ' ', str(s))
151 self.write('\nerror %s\n' % s)
153 def _check_ok(self, onempty):
156 for rl in linereader(self.inp):
157 #log('%d got line: %r\n' % (os.getpid(), rl))
158 if not rl: # empty line
162 elif rl.startswith('error '):
163 #log('client: error: %s\n' % rl[6:])
167 raise Exception('server exited unexpectedly; see errors above')
169 def drain_and_check_ok(self):
172 return self._check_ok(onempty)
176 raise Exception('expected "ok", got %r' % rl)
177 return self._check_ok(onempty)
188 def chunkyreader(f, count = None):
191 b = f.read(min(count, 65536))
193 raise IOError('EOF with %d bytes remaining' % count)
204 def __init__(self, it, ondone = None):
212 return self.it.next()
222 if s and not s.endswith('/'):
228 def _mmap_do(f, len, flags, prot):
230 st = os.fstat(f.fileno())
232 map = mmap.mmap(f.fileno(), len, flags, prot)
233 f.close() # map will persist beyond file close
237 def mmap_read(f, len = 0):
238 return _mmap_do(f, len, mmap.MAP_PRIVATE, mmap.PROT_READ)
241 def mmap_readwrite(f, len = 0):
242 return _mmap_do(f, len, mmap.MAP_SHARED, mmap.PROT_READ|mmap.PROT_WRITE)
246 g = re.match(r'([-+\d.e]+)\s*(\w*)', str(s))
248 raise ValueError("can't parse %r as a number" % s)
249 (val, unit) = g.groups()
252 if unit in ['t', 'tb']:
253 mult = 1024*1024*1024*1024
254 elif unit in ['g', 'gb']:
255 mult = 1024*1024*1024
256 elif unit in ['m', 'mb']:
258 elif unit in ['k', 'kb']:
260 elif unit in ['', 'b']:
263 raise ValueError("invalid unit %r in number %r" % (unit, s))
267 # count the number of elements in an iterator (consumes the iterator)
269 return reduce(lambda x,y: x+1, l)
281 saved_errors.append(e)
284 istty = os.isatty(2) or atoi(os.environ.get('BUP_FORCE_TTY'))
291 oldhook = sys.excepthook
292 def newhook(exctype, value, traceback):
293 if exctype == KeyboardInterrupt:
294 log('Interrupted.\n')
296 return oldhook(exctype, value, traceback)
297 sys.excepthook = newhook
300 # hashlib is only available in python 2.5 or higher, but the 'sha' module
301 # produces a DeprecationWarning in python 2.6 or higher. We want to support
302 # python 2.4 and above without any stupid warnings, so let's try using hashlib
303 # first, and downgrade if it fails.