~ubuntu-branches/ubuntu/quantal/enigmail/quantal-security

« back to all changes in this revision

Viewing changes to mozilla/build/pymake/pymake/util.py

  • Committer: Package Import Robot
  • Author(s): Chris Coulson
  • Date: 2013-09-13 16:02:15 UTC
  • mfrom: (0.12.16)
  • Revision ID: package-import@ubuntu.com-20130913160215-u3g8nmwa0pdwagwc
Tags: 2:1.5.2-0ubuntu0.12.10.1
* New upstream release v1.5.2 for Thunderbird 24

* Build enigmail using a stripped down Thunderbird 17 build system, as it's
  now quite difficult to build the way we were doing previously, with the
  latest Firefox build system
* Add debian/patches/no_libxpcom.patch - Don't link against libxpcom, as it
  doesn't exist anymore (but exists in the build system)
* Add debian/patches/use_sdk.patch - Use the SDK version of xpt.py and
  friends
* Drop debian/patches/ipc-pipe_rename.diff (not needed anymore)
* Drop debian/patches/makefile_depth.diff (not needed anymore)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import os
 
2
 
 
3
class 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
 
 
15
def 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
 
 
24
def 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
 
 
38
def 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
 
 
58
if 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
 
 
65
else:
 
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
 
 
84
try:
 
85
    from __builtin__ import any
 
86
except ImportError:
 
87
    def any(it):
 
88
        for i in it:
 
89
            if i:
 
90
                return True
 
91
        return False
 
92
 
 
93
class _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
 
 
104
class 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