source: rtems-tools/rtemstoolkit/check.py @ 3618a62

5
Last change on this file since 3618a62 was b0fa2ae, checked in by Chris Johns <chrisj@…>, on 03/03/16 at 05:46:18

Update rtems-tool to support Python 2 and 3.

Add solaris and netbsd.

Close #2619.

  • Property mode set to 100644
File size: 5.6 KB
Line 
1#
2# RTEMS Tools Project (http://www.rtems.org/)
3# Copyright 2010-2016 Chris Johns (chrisj@rtems.org)
4# All rights reserved.
5#
6# This file is part of the RTEMS Tools package in 'rtems-tools'.
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions are met:
10#
11# 1. Redistributions of source code must retain the above copyright notice,
12# this list of conditions and the following disclaimer.
13#
14# 2. Redistributions in binary form must reproduce the above copyright notice,
15# this list of conditions and the following disclaimer in the documentation
16# and/or other materials provided with the distribution.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28# POSSIBILITY OF SUCH DAMAGE.
29#
30
31#
32# Check the defaults for a specific host.
33#
34
35from __future__ import print_function
36
37import os
38
39#
40# Support to handle use in a package and as a unit test.
41# If there is a better way to let us know.
42#
43try:
44    from . import error
45    from . import execute
46    from . import log
47    from . import options
48    from . import path
49    from . import version
50except (ValueError, SystemError):
51    import error
52    import execute
53    import log
54    import options
55    import path
56    import version
57
58def _check_none(_opts, macro, value, constraint):
59    return True
60
61
62def _check_triplet(_opts, macro, value, constraint):
63    return True
64
65
66def _check_dir(_opts, macro, value, constraint, silent = False):
67    if constraint != 'none' and not path.isdir(value):
68        if constraint == 'required':
69            if not silent:
70                log.notice('error: dir: not found: (%s) %s' % (macro, value))
71            return False
72        if not silent and _opts.warn_all():
73            log.notice('warning: dir: not found: (%s) %s' % (macro, value))
74    return True
75
76
77def _check_exe(_opts, macro, value, constraint, silent = False):
78
79    if len(value) == 0 or constraint == 'none':
80        return True
81
82    orig_value = value
83
84    if path.isabspath(value):
85        if path.isfile(value):
86            return True
87        if os.name == 'nt':
88            if path.isfile('%s.exe' % (value)):
89                return True
90        value = path.basename(value)
91        absexe = True
92    else:
93        absexe = False
94
95    paths = os.environ['PATH'].split(os.pathsep)
96
97    if _check_paths(value, paths):
98        if absexe:
99            if not silent:
100                log.notice('warning: exe: absolute exe found in path: (%s) %s' % (macro, orig_value))
101        return True
102
103    if constraint == 'optional':
104        if not silent:
105            log.trace('warning: exe: optional exe not found: (%s) %s' % (macro, orig_value))
106        return True
107
108    if not silent:
109        log.notice('error: exe: not found: (%s) %s' % (macro, orig_value))
110    return False
111
112
113def _check_paths(name, paths):
114    for p in paths:
115        exe = path.join(p, name)
116        if path.isfile(exe):
117            return True
118        if os.name == 'nt':
119            if path.isfile('%s.exe' % (exe)):
120                return True
121    return False
122
123
124def host_setup(opts):
125    """ Basic sanity check. All executables and directories must exist."""
126
127    checks = { 'none':    _check_none,
128               'triplet': _check_triplet,
129               'dir':     _check_dir,
130               'exe':     _check_exe }
131
132    sane = True
133
134    for d in list(opts.defaults.keys()):
135        try:
136            (test, constraint, value) = opts.defaults.get(d)
137        except:
138            if opts.defaults.get(d) is None:
139                raise error.general('invalid default: %s: not found' % (d))
140            else:
141                raise error.general('invalid default: %s [%r]' % (d, opts.defaults.get(d)))
142        if test != 'none':
143            value = opts.defaults.expand(value)
144            if test not in checks:
145                raise error.general('invalid check test: %s [%r]' % (test, opts.defaults.get(d)))
146            ok = checks[test](opts, d, value, constraint)
147            if ok:
148                tag = ' '
149            else:
150                tag = '*'
151            log.trace('%c %15s: %r -> "%s"' % (tag, d, opts.defaults.get(d), value))
152            if sane and not ok:
153                sane = False
154
155    return sane
156
157
158def check_exe(label, exe):
159    return _check_exe(None, label, exe, None, True)
160
161
162def check_dir(label, path):
163    return _check_dir(None, label, path, 'required', True)
164
165
166def run():
167    import sys
168    try:
169        _opts = options.command_line(argv = sys.argv)
170        options.load(_opts)
171        log.notice('RTEMS Source Builder - Check, v%s' % (version.str()))
172        if host_setup(_opts):
173            print('Environment is ok')
174        else:
175            print('Environment is not correctly set up')
176    except error.general as gerr:
177        print(gerr)
178        sys.exit(1)
179    except error.internal as ierr:
180        print (ierr)
181        sys.exit(1)
182    except error.exit:
183        pass
184    except KeyboardInterrupt:
185        log.notice('abort: user terminated')
186        sys.exit(1)
187    sys.exit(0)
188
189
190if __name__ == '__main__':
191    run()
Note: See TracBrowser for help on using the repository browser.