PageRenderTime 21ms CodeModel.GetById 11ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/build/pymake/pymake/util.py

http://github.com/zpao/v8monkey
Python | 150 lines | 148 code | 2 blank | 0 comment | 0 complexity | 33109c645a49c903a611e56441296427 MD5 | raw file
  1import os
  2
  3class MakeError(Exception):
  4    def __init__(self, message, loc=None):
  5        self.msg = message
  6        self.loc = loc
  7
  8    def __str__(self):
  9        locstr = ''
 10        if self.loc is not None:
 11            locstr = "%s:" % (self.loc,)
 12
 13        return "%s%s" % (locstr, self.msg)
 14
 15def normaljoin(path, suffix):
 16    """
 17    Combine the given path with the suffix, and normalize if necessary to shrink the path to avoid hitting path length limits
 18    """
 19    result = os.path.join(path, suffix)
 20    if len(result) > 255:
 21        result = os.path.normpath(result)
 22    return result
 23
 24def joiniter(fd, it):
 25    """
 26    Given an iterator that returns strings, write the words with a space in between each.
 27    """
 28    
 29    it = iter(it)
 30    for i in it:
 31        fd.write(i)
 32        break
 33
 34    for i in it:
 35        fd.write(' ')
 36        fd.write(i)
 37
 38def checkmsyscompat():
 39    """For msys compatibility on windows, honor the SHELL environment variable,
 40    and if $MSYSTEM == MINGW32, run commands through $SHELL -c instead of
 41    letting Python use the system shell."""
 42    if 'SHELL' in os.environ:
 43        shell = os.environ['SHELL']
 44    elif 'MOZILLABUILD' in os.environ:
 45        shell = os.environ['MOZILLABUILD'] + '/msys/bin/sh.exe'
 46    elif 'COMSPEC' in os.environ:
 47        shell = os.environ['COMSPEC']
 48    else:
 49        raise DataError("Can't find a suitable shell!")
 50
 51    msys = False
 52    if 'MSYSTEM' in os.environ and os.environ['MSYSTEM'] == 'MINGW32':
 53        msys = True
 54        if not shell.lower().endswith(".exe"):
 55            shell += ".exe"
 56    return (shell, msys)
 57
 58if hasattr(str, 'partition'):
 59    def strpartition(str, token):
 60        return str.partition(token)
 61
 62    def strrpartition(str, token):
 63        return str.rpartition(token)
 64
 65else:
 66    def strpartition(str, token):
 67        """Python 2.4 compatible str.partition"""
 68
 69        offset = str.find(token)
 70        if offset == -1:
 71            return str, '', ''
 72
 73        return str[:offset], token, str[offset + len(token):]
 74
 75    def strrpartition(str, token):
 76        """Python 2.4 compatible str.rpartition"""
 77
 78        offset = str.rfind(token)
 79        if offset == -1:
 80            return '', '', str
 81
 82        return str[:offset], token, str[offset + len(token):]
 83
 84try:
 85    from __builtin__ import any
 86except ImportError:
 87    def any(it):
 88        for i in it:
 89            if i:
 90                return True
 91        return False
 92
 93class _MostUsedItem(object):
 94    __slots__ = ('key', 'o', 'count')
 95
 96    def __init__(self, key):
 97        self.key = key
 98        self.o = None
 99        self.count = 1
100
101    def __repr__(self):
102        return "MostUsedItem(key=%r, count=%i, o=%r)" % (self.key, self.count, self.o)
103
104class MostUsedCache(object):
105    def __init__(self, capacity, creationfunc, verifyfunc):
106        self.capacity = capacity
107        self.cfunc = creationfunc
108        self.vfunc = verifyfunc
109
110        self.d = {}
111        self.active = [] # lazily sorted!
112
113    def setactive(self, item):
114        if item in self.active:
115            return
116
117        if len(self.active) == self.capacity:
118            self.active.sort(key=lambda i: i.count)
119            old = self.active.pop(0)
120            old.o = None
121            # print "Evicting %s" % old.key
122
123        self.active.append(item)
124
125    def get(self, key):
126        item = self.d.get(key, None)
127        if item is None:
128            item = _MostUsedItem(key)
129            self.d[key] = item
130        else:
131            item.count += 1
132
133        if item.o is not None and self.vfunc(key, item.o):
134            return item.o
135
136        item.o = self.cfunc(key)
137        self.setactive(item)
138        return item.o
139
140    def verify(self):
141        for k, v in self.d.iteritems():
142            if v.o:
143                assert v in self.active
144            else:
145                assert v not in self.active
146
147    def debugitems(self):
148        l = [i.key for i in self.active]
149        l.sort()
150        return l