__init__.py :  » Installer » Zero-Install » zeroinstall-injector-0.47 » zeroinstall » support » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Installer » Zero Install 
Zero Install » zeroinstall injector 0.47 » zeroinstall » support » __init__.py
"""
Useful support routines (for internal use).

These functions aren't really Zero Install specific; they're things we might
wish were in the standard library.

@since: 0.27
"""

# Copyright (C) 2009, Thomas Leonard
# See the README file for details, or visit http://0install.net.

from zeroinstall import _
import os, logging

def find_in_path(prog):
  """Search $PATH for prog.
  If prog is an absolute path, return it unmodified.
  @param prog: name of executable to find
  @return: the full path of prog, or None if not found
  @since: 0.27
  """
  if os.path.isabs(prog): return prog
  for d in os.environ.get('PATH', '/bin:/usr/bin').split(':'):
    path = os.path.join(d, prog)
    if os.path.isfile(path):
      return path
  return None

def read_bytes(fd, nbytes, null_ok = False):
  """Read exactly nbytes from fd.
  @param fd: file descriptor to read from
  @param nbytes: number of bytes to read
  @param null_ok: if True, it's OK to receive EOF immediately (we then return None)
  @return: the bytes read
  @raise Exception: if we received less than nbytes of data
  """
  data = ''
  while nbytes:
    got = os.read(fd, nbytes)
    if not got:
      if null_ok and not data:
        return None
      raise Exception(_("Unexpected end-of-stream. Data so far %(data)s; expecting %(bytes)d bytes more.")
          % {'data': repr(data), 'bytes': nbytes})
    data += got
    nbytes -= len(got)
  logging.debug(_("Message received: %s") % repr(data))
  return data

def pretty_size(size):
  """Format a size for printing.
  @param size: the size in bytes
  @type size: int (or None)
  @return: the formatted size
  @rtype: str
  @since: 0.27"""
  if size is None:
    return '?'
  if size < 2048:
    return _('%d bytes') % size
  size = float(size)
  for unit in (_('KB'), _('MB'), _('GB'), _('TB')):
    size /= 1024
    if size < 2048:
      break
  return _('%(size).1f %(unit)s') % {'size': size, 'unit': unit}

def ro_rmtree(root):
  """Like shutil.rmtree, except that we also delete read-only items.
  @param root: the root of the subtree to remove
  @type root: str
  @since: 0.28"""
  import shutil
  for main, dirs, files in os.walk(root):
    os.chmod(main, 0700)
  shutil.rmtree(root)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.