source: rtems-source-builder/source-builder/sb/check.py @ 649a64c

4.104.114.95
Last change on this file since 649a64c was 51b4061, checked in by Chris Johns <chrisj@…>, on 04/13/13 at 00:41:00

Provide a better error message.

  • Property mode set to 100644
File size: 4.6 KB
Line 
1#
2# RTEMS Tools Project (http://www.rtems.org/)
3# Copyright 2010-2012 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# Permission to use, copy, modify, and/or distribute this software for any
9# purpose with or without fee is hereby granted, provided that the above
10# copyright notice and this permission notice appear in all copies.
11#
12# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
20#
21# Check the defaults for a specific host.
22#
23
24import os
25
26import error
27import execute
28import log
29import options
30import path
31import version
32
33def _notice(opts, text):
34    if not opts.quiet() and log.default and not log.default.has_stdout():
35        print text
36    log.output(text)
37    log.flush()
38
39
40def _check_none(_opts, macro, value, constraint):
41    return True
42
43
44def _check_triplet(_opts, macro, value, constraint):
45    return True
46
47
48def _check_dir(_opts, macro, value, constraint):
49    if constraint != 'none' and not path.isdir(value):
50        if constraint == 'required':
51            _notice(_opts, 'error: dir: not found: (%s) %s' % (macro, value))
52            return False
53        if _opts.warn_all():
54            _notice(_opts, 'warning: dir: not found: (%s) %s' % (macro, value))
55    return True
56
57
58def _check_exe(_opts, macro, value, constraint):
59
60    if len(value) == 0 or constraint == 'none':
61        return True
62
63    orig_value = value
64
65    if path.isabspath(value):
66        if path.isfile(value):
67            return True
68        if os.name == 'nt':
69            if path.isfile('%s.exe' % (value)):
70                return True
71        value = path.basename(value)
72        absexe = True
73    else:
74        absexe = False
75
76    paths = os.environ['PATH'].split(os.pathsep)
77
78    if _check_paths(value, paths):
79        if absexe:
80            _notice(_opts,
81                    'warning: exe: absolute exe found in path: (%s) %s' % (macro, orig_value))
82        return True
83
84    if constraint == 'optional':
85        if _opts.trace():
86            _notice(_opts, 'warning: exe: optional exe not found: (%s) %s' % (macro, orig_value))
87        return True
88
89    _notice(_opts, 'error: exe: not found: (%s) %s' % (macro, orig_value))
90    return False
91
92
93def _check_paths(name, paths):
94    for p in paths:
95        exe = path.join(p, name)
96        if path.isfile(exe):
97            return True
98        if os.name == 'nt':
99            if path.isfile('%s.exe' % (exe)):
100                return True
101    return False
102
103def host_setup(opts):
104    """ Basic sanity check. All executables and directories must exist."""
105
106    checks = { 'none':    _check_none,
107               'triplet': _check_triplet,
108               'dir':     _check_dir,
109               'exe':     _check_exe }
110
111    sane = True
112
113    for d in opts.defaults.keys():
114        try:
115            (test, constraint, value) = opts.defaults.get(d)
116        except:
117            if opts.defaults.get(d) is None:
118                raise error.general('invalid default: %s: not found' % (d))
119            else:
120                raise error.general('invalid default: %s [%r]' % (d, opts.defaults.get(d)))
121        if test != 'none':
122            value = opts.defaults.expand(value)
123            if test not in checks:
124                raise error.general('invalid check test: %s [%r]' % (test, opts.defaults.get(d)))
125            ok = checks[test](opts, d, value, constraint)
126            if opts.trace():
127                if ok:
128                    tag = ' '
129                else:
130                    tag = '*'
131                _notice(opts, '%c %15s: %r -> "%s"' % (tag, d, opts.defaults.get(d), value))
132            if sane and not ok:
133                sane = False
134
135    return sane
136
137
138def run():
139    import sys
140    try:
141        _opts = options.load(args = sys.argv)
142        _notice(_opts, 'RTEMS Source Builder - Check, v%s' % (version.str()))
143        if host_setup(_opts):
144            print 'Environment is ok'
145        else:
146            print 'Environment is not correctly set up'
147    except error.general, gerr:
148        print gerr
149        sys.exit(1)
150    except error.internal, ierr:
151        print ierr
152        sys.exit(1)
153    except error.exit, eerr:
154        pass
155    except KeyboardInterrupt:
156        _notice(opts, 'abort: user terminated')
157        sys.exit(1)
158    sys.exit(0)
159
160
161if __name__ == '__main__':
162    run()
Note: See TracBrowser for help on using the repository browser.