summaryrefslogtreecommitdiff
path: root/bootstrap.py
blob: 1629636042ea7b5c1837f98227494d38d1eee696 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/env python
#
# Copyright (c) 2001 - 2019 The SCons Foundation
#
# 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.
#
from __future__ import print_function

import os
import os.path
import sys
import glob
import subprocess
import filecmp
import shutil

__doc__ = """bootstrap.py

Execute SCons from this source tree. It copies Python scripts and modules
from src/ subdirectory into a subdirectory named "bootstrap/" (by default),
and executes SCons from there with the supplied command-line arguments.

This is a minimal build of SCons to bootstrap the full build of all the
packages, as specified in our local SConstruct file.

Some options are specific to this bootstrap.py script and are *not* passed
on to the SCons script. All of these begin with the string "bootstrap_":

    --bootstrap_dir=DIR

        Sets the name of the directory into which the SCons files will
        be copied.  The default is "bootstrap" in the local subdirectory.

    --bootstrap_force

        Forces a copy of all necessary files.  By default, the
        bootstrap.py script only updates the bootstrap copy if the
        content of the source copy is different.

    --bootstrap_src=DIR

        Searches for the SCons files relative to the specified DIR,
        then relative to the directory in which this bootstrap.py
        script is found.

    --bootstrap_update

        Only updates the bootstrap subdirectory, and then exits.

In addition to the above, the bootstrap.py script understands
the following SCons options:

    -C, --directory

        Changes to the specified directory before invoking SCons.
        Because we change directory right away to the specified directory,
        the SCons script itself doesn't need to, so this option gets
        "eaten" by the bootstrap.py script.
"""

def parseManifestLines(basedir, manifest):
    """
    Scans a MANIFEST file, and returns the list of source files.

    Has basic support for recursive globs '**',
    filename wildcards of the form '*.xml' and
    comment lines, starting with a '#'.

    :param basedir: base path to find files in. Note this does not
                    run in an SCons context so path must not need
                    further processing (e.g. no '#' signs)
    :param manifest: path to manifest file
    :returns: list of files
    """
    sources = []
    basewd = os.path.abspath(basedir)
    with open(manifest) as m:
        lines = m.readlines()
    for l in lines:
        if l.startswith('#'):
            # Skip comments
            continue
        l = l.rstrip('\n')
        if l.endswith('**'):
            # Glob all files recursively
            globwd = os.path.dirname(os.path.join(basewd, l))
            for path, dirs, files in os.walk(globwd):
                for f in files:
                    fpath = os.path.join(globwd, path, f)
                    sources.append(os.path.relpath(fpath, basewd))
        elif '*' in l:
            # Glob file pattern
            files = glob.glob(os.path.join(basewd, l))
            for f in files:
                sources.append(os.path.relpath(f, basewd))
        else:
            sources.append(l)

    return sources

def main():
    script_dir = os.path.abspath(os.path.dirname(__file__))

    bootstrap_dir = os.path.join(script_dir, 'bootstrap')

    pass_through_args = []
    update_only = None

    requires_an_argument = 'bootstrap.py:  %s requires an argument\n'

    search = [script_dir]

    def find(filename, search=search):
        for dir_name in search:
            filepath = os.path.join(dir_name, filename)
            if os.path.exists(filepath):
                return os.path.normpath(filepath)
        sys.stderr.write("could not find `%s' in search path:\n" % filename)
        sys.stderr.write("\t" + "\n\t".join(search) + "\n")
        sys.exit(2)

    def must_copy(dst, src):
        if not os.path.exists(dst):
            return 1
        return not filecmp.cmp(dst,src)

    # Note:  We don't use the getopt module to process the command-line
    # arguments because we'd have to teach it about all of the SCons options.

    command_line_args = sys.argv[1:]

    while command_line_args:
        arg = command_line_args.pop(0)

        if arg == '--bootstrap_dir':
            try:
                bootstrap_dir = command_line_args.pop(0)
            except IndexError:
                sys.stderr.write(requires_an_argument % arg)
                sys.exit(1)
        elif arg[:16] == '--bootstrap_dir=':
            bootstrap_dir = arg[16:]
        elif arg == '--bootstrap_force':
            def must_copy(dst, src):
                return 1
        elif arg == '--bootstrap_src':
            try:
                search.insert(0, command_line_args.pop(0))
            except IndexError:
                sys.stderr.write(requires_an_argument % arg)
                sys.exit(1)
        elif arg[:16] == '--bootstrap_src=':
            search.insert(0, arg[16:])
        elif arg == '--bootstrap_update':
            update_only = 1
        elif arg in ('-C', '--directory'):
            try:
                dir = command_line_args.pop(0)
            except IndexError:
                sys.stderr.write(requires_an_argument % arg)
                sys.exit(1)
            else:
                os.chdir(dir)
        elif arg[:2] == '-C':
            os.chdir(arg[2:])
        elif arg[:12] == '--directory=':
            os.chdir(arg[12:])
        else:
            pass_through_args.append(arg)

    scons_py = os.path.join('src', 'script', 'scons.py')
    src_engine = os.path.join('src', 'engine')
    MANIFEST_in = find(os.path.join(src_engine, 'MANIFEST.in'))
    manifest_files = [os.path.join(src_engine, x)
                      for x in parseManifestLines(os.path.join(script_dir, src_engine),
                                                  MANIFEST_in)]

    files = [scons_py] + manifest_files

    for filename in files:
        src = find(filename)
        dst = os.path.join(bootstrap_dir, filename)
        if must_copy(dst, src):
            dir = os.path.split(dst)[0]
            if not os.path.isdir(dir):
                os.makedirs(dir)
            try:
                os.unlink(dst)
            except Exception as e:
                pass

            shutil.copyfile(src, dst)

    if update_only:
        sys.exit(0)

    args = [sys.executable, os.path.join(bootstrap_dir, scons_py)] + pass_through_args

    sys.stdout.write(" ".join(args) + '\n')
    sys.stdout.flush()

    os.environ['SCONS_LIB_DIR'] = os.path.join(bootstrap_dir, src_engine)

    sys.exit(subprocess.Popen(args, env=os.environ).wait())


if __name__ == "__main__":
    main()

# Local Variables:
# tab-width:4
# indent-tabs-mode:nil
# End:
# vim: set expandtab tabstop=4 shiftwidth=4: