Start development series 0.42.1-post
[zeroinstall/zeroinstall-rsl.git] / zeroinstall / support / __init__.py
blob5e5aa1e43b0e92ecf8637728664d7750f13f284a
1 """
2 Useful support routines (for internal use).
4 These functions aren't really Zero Install specific; they're things we might
5 wish were in the standard library.
7 @since: 0.27
8 """
10 # Copyright (C) 2009, Thomas Leonard
11 # See the README file for details, or visit http://0install.net.
13 from zeroinstall import _
14 import os, logging
16 def find_in_path(prog):
17 """Search $PATH for prog.
18 If prog is an absolute path, return it unmodified.
19 @param prog: name of executable to find
20 @return: the full path of prog, or None if not found
21 @since: 0.27
22 """
23 if os.path.isabs(prog): return prog
24 for d in os.environ.get('PATH', '/bin:/usr/bin').split(':'):
25 path = os.path.join(d, prog)
26 if os.path.isfile(path):
27 return path
28 return None
30 def read_bytes(fd, nbytes, null_ok = False):
31 """Read exactly nbytes from fd.
32 @param fd: file descriptor to read from
33 @param nbytes: number of bytes to read
34 @param null_ok: if True, it's OK to receive EOF immediately (we then return None)
35 @return: the bytes read
36 @raise Exception: if we received less than nbytes of data
37 """
38 data = ''
39 while nbytes:
40 got = os.read(fd, nbytes)
41 if not got:
42 if null_ok and not data:
43 return None
44 raise Exception(_("Unexpected end-of-stream. Data so far %(data)s; expecting %(bytes)d bytes more.")
45 % {'data': repr(data), 'bytes': nbytes})
46 data += got
47 nbytes -= len(got)
48 logging.debug(_("Message received: %s") % repr(data))
49 return data
51 def pretty_size(size):
52 """Format a size for printing.
53 @param size: the size in bytes
54 @type size: int (or None)
55 @return: the formatted size
56 @rtype: str
57 @since: 0.27"""
58 if size is None:
59 return '?'
60 if size < 2048:
61 return _('%d bytes') % size
62 size = float(size)
63 for unit in (_('KB'), _('MB'), _('GB'), _('TB')):
64 size /= 1024
65 if size < 2048:
66 break
67 return _('%(size).1f %(unit)s') % {'size': size, 'unit': unit}
69 def ro_rmtree(root):
70 """Like shutil.rmtree, except that we also delete read-only items.
71 @param root: the root of the subtree to remove
72 @type root: str
73 @since: 0.28"""
74 import shutil
75 for main, dirs, files in os.walk(root):
76 os.chmod(main, 0700)
77 shutil.rmtree(root)