author Michael Wu <>
Thu, 03 Jun 2010 13:56:36 -0700
changeset 43066 1c416ab33c8e6cebc2c74d0f36cf76b5a0a6b2dd
parent 38959 c80eb5209859aff20b7ff598a56ccc74740abcc9
child 94475 f4157e8c410708d76703f19e4dfb61859bfe32d8
permissions -rw-r--r--
Bug 564327 - Add Android widgets backend [2/2], patch by vlad, alexp, blassey, and me. r=dougt, sr=roc

import os

class MakeError(Exception):
    def __init__(self, message, loc=None):
        self.msg = message
        self.loc = loc

    def __str__(self):
        locstr = ''
        if self.loc is not None:
            locstr = "%s:" % (self.loc,)

        return "%s%s" % (locstr, self.msg)

def normaljoin(path, suffix):
    Combine the given path with the suffix, and normalize if necessary to shrink the path to avoid hitting path length limits
    result = os.path.join(path, suffix)
    if len(result) > 255:
        result = os.path.normpath(result)
    return result

def joiniter(fd, it):
    Given an iterator that returns strings, write the words with a space in between each.
    it = iter(it)
    for i in it:

    for i in it:
        fd.write(' ')

def checkmsyscompat():
    """For msys compatibility on windows, honor the SHELL environment variable,
    and if $MSYSTEM == MINGW32, run commands through $SHELL -c instead of
    letting Python use the system shell."""
    if 'SHELL' in os.environ:
        shell = os.environ['SHELL']
    elif 'MOZILLABUILD' in os.environ:
        shell = os.environ['MOZILLABUILD'] + '/msys/bin/sh.exe'
    elif 'COMSPEC' in os.environ:
        shell = os.environ['COMSPEC']
        raise DataError("Can't find a suitable shell!")

    msys = False
    if 'MSYSTEM' in os.environ and os.environ['MSYSTEM'] == 'MINGW32':
        msys = True
        if not shell.lower().endswith(".exe"):
            shell += ".exe"
    return (shell, msys)

if hasattr(str, 'partition'):
    def strpartition(str, token):
        return str.partition(token)

    def strrpartition(str, token):
        return str.rpartition(token)

    def strpartition(str, token):
        """Python 2.4 compatible str.partition"""

        offset = str.find(token)
        if offset == -1:
            return str, '', ''

        return str[:offset], token, str[offset + len(token):]

    def strrpartition(str, token):
        """Python 2.4 compatible str.rpartition"""

        offset = str.rfind(token)
        if offset == -1:
            return '', '', str

        return str[:offset], token, str[offset + len(token):]

    from __builtin__ import any
except ImportError:
    def any(it):
        for i in it:
            if i:
                return True
        return False

class _MostUsedItem(object):
    __slots__ = ('key', 'o', 'count')

    def __init__(self, key):
        self.key = key
        self.o = None
        self.count = 1

    def __repr__(self):
        return "MostUsedItem(key=%r, count=%i, o=%r)" % (self.key, self.count, self.o)

class MostUsedCache(object):
    def __init__(self, capacity, creationfunc, verifyfunc):
        self.capacity = capacity
        self.cfunc = creationfunc
        self.vfunc = verifyfunc

        self.d = {} = [] # lazily sorted!

    def setactive(self, item):
        if item in

        if len( == self.capacity:
   i: i.count)
            old =
            old.o = None
            # print "Evicting %s" % old.key

    def get(self, key):
        item = self.d.get(key, None)
        if item is None:
            item = _MostUsedItem(key)
            self.d[key] = item
            item.count += 1

        if item.o is not None and self.vfunc(key, item.o):
            return item.o

        item.o = self.cfunc(key)
        return item.o

    def verify(self):
        for k, v in self.d.iteritems():
            if v.o:
                assert v in
                assert v not in

    def debugitems(self):
        l = [i.key for i in]
        return l