summaryrefslogtreecommitdiff
path: root/src/engine/SCons/Sig/__init__.py
blob: 2a2667f348b8227186fbaf7c00860e0ccc30a19d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
"""SCons.Sig

The Signature package for the scons software construction utility.

"""

#
# Copyright (c) 2001 Steven Knight
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"

import os.path
import string


#XXX Get rid of the global array so this becomes re-entrant.
sig_files = []

def write():
    global sig_files
    for sig_file in sig_files:
        sig_file.write()

class SConsignFile:
    """
    Encapsulates reading and writing a .sconsign file.
    """

    def __init__(self, dir, module):
        """
        dir - the directory for the file
        module - the signature module being used
        """
        
        self.path = os.path.join(dir, '.sconsign')
        self.entries = {}
                    
        try:
            file = open(self.path, 'rt')
        except:
            pass
        else:
            for line in file.readlines():
                filename, rest = map(string.strip, string.split(line, ":"))
                time, signature = map(string.strip, string.split(rest, " "))
                self.entries[filename] = (int(time), module.from_string(signature))

        global sig_files
        sig_files.append(self)

    def get(self, filename):
        """
        Get the signature for a file

        filename - the filename whose signature will be returned
        returns - (timestamp, signature)
        """
        
        try:
            return self.entries[filename]
        except KeyError:
            return (0, None)

    def set(self, filename, timestamp, signature, module):
        """
        Set the signature for a file

        filename - the filename whose signature will be set
        timestamp - the timestamp
        signature - the signature
        module - the signature module being used
        """
        self.entries[filename] = (timestamp, module.to_string(signature))

    def write(self):
        """
        Write the .sconsign file to disk.
        """
        
        file = open(self.path, 'wt')
        for item in self.entries.items():
            file.write("%s: %d %s\n" % (item[0], item[1][0], item[1][1]))


class Calculator:
    """
    Encapsulates signature calculations and .sconsign file generating
    for the build engine.
    """

    def __init__(self, module):
        """
        Initialize the calculator.

        module - the signature module to use for signature calculations
        """
        self.module = module

    
    def collect(self, node):
        """
        Collect the signatures of a node's sources.

        node - the node whose sources will be collected

        This no longer handles the recursive descent of the
        node's children's signatures.  We expect that they're
        already built and updated by someone else, if that's
        what's wanted.
        """
        sigs = map(lambda n,s=self: s.get_signature(n), node.children())
        return self.module.collect(filter(lambda x: not x is None, sigs))

    def get_signature(self, node):
        """
        Get the signature for a node.

        node - the node
        returns - the signature or None if the signature could not
        be computed.

        This method does not store the signature in the node and
        in the .sconsign file.
        """

        if not node.use_signature:
            # This node type doesn't use a signature (e.g. a
            # directory) so bail right away.
            return None
        elif node.has_signature():
            sig = node.get_signature()
        elif node.builder:
            sig = self.collect(node)
        else:
            if not node.exists():
                return None
            
            # XXX handle nodes that are not under the source root
            sig = self.module.signature(node)

        return sig

    def current(self, node, newsig):
        """
        Check if a node is up to date.

        node - the node whose signature will be checked

        returns - 0 if the signature has changed since the last invocation,
        and 1 if it hasn't
        """

        c = node.current()
        if not c is None:
            # The node itself has told us whether or not it's
            # current without checking the signature.  The
            # canonical uses here are a "0" return for a file
            # that doesn't exist, or a directory.
            return c

        oldtime, oldsig = node.get_oldentry()

        newtime = node.get_timestamp()

        if not node.builder and newtime == oldtime:
            newsig = oldsig
        
        return self.module.current(newsig, oldsig)