source: rtems-source-builder/source-builder/sb/check.py @ 1b40c77

4.104.114.95
Last change on this file since 1b40c77 was 1b40c77, checked in by Chris Johns <chrisj@…>, on May 15, 2013 at 3:23:41 AM

Make outputing errors optional.

  • 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 _check_none(_opts, macro, value, constraint):
34    return True
35
36
37def _check_triplet(_opts, macro, value, constraint):
38    return True
39
40
41def _check_dir(_opts, macro, value, constraint, silent = False):
42    if constraint != 'none' and not path.isdir(value):
43        if constraint == 'required':
44            if not silent:
45                log.notice('error: dir: not found: (%s) %s' % (macro, value))
46            return False
47        if not silent and _opts.warn_all():
48            log.notice('warning: dir: not found: (%s) %s' % (macro, value))
49    return True
50
51
52def _check_exe(_opts, macro, value, constraint, silent = False):
53
54    if len(value) == 0 or constraint == 'none':
55        return True
56
57    orig_value = value
58
59    if path.isabspath(value):
60        if path.isfile(value):
61            return True
62        if os.name == 'nt':
63            if path.isfile('%s.exe' % (value)):
64                return True
65        value = path.basename(value)
66        absexe = True
67    else:
68        absexe = False
69
70    paths = os.environ['PATH'].split(os.pathsep)
71
72    if _check_paths(value, paths):
73        if absexe:
74            if not silent:
75                log.notice('warning: exe: absolute exe found in path: (%s) %s' % (macro, orig_value))
76        return True
77
78    if constraint == 'optional':
79        if not silent:
80            log.trace('warning: exe: optional exe not found: (%s) %s' % (macro, orig_value))
81        return True
82
83    if not silent:
84        log.notice('error: exe: not found: (%s) %s' % (macro, orig_value))
85    return False
86
87
88def _check_paths(name, paths):
89    for p in paths:
90        exe = path.join(p, name)
91        if path.isfile(exe):
92            return True
93        if os.name == 'nt':
94            if path.isfile('%s.exe' % (exe)):
95                return True
96    return False
97
98
99def host_setup(opts):
100    """ Basic sanity check. All executables and directories must exist."""
101
102    checks = { 'none':    _check_none,
103               'triplet': _check_triplet,
104               'dir':     _check_dir,
105               'exe':     _check_exe }
106
107    sane = True
108
109    for d in opts.defaults.keys():
110        try:
111            (test, constraint, value) = opts.defaults.get(d)
112        except:
113            if opts.defaults.get(d) is None:
114                raise error.general('invalid default: %s: not found' % (d))
115            else:
116                raise error.general('invalid default: %s [%r]' % (d, opts.defaults.get(d)))
117        if test != 'none':
118            value = opts.defaults.expand(value)
119            if test not in checks:
120                raise error.general('invalid check test: %s [%r]' % (test, opts.defaults.get(d)))
121            ok = checks[test](opts, d, value, constraint)
122            if ok:
123                tag = ' '
124            else:
125                tag = '*'
126            log.trace('%c %15s: %r -> "%s"' % (tag, d, opts.defaults.get(d), value))
127            if sane and not ok:
128                sane = False
129
130    return sane
131
132
133def check_exe(label, exe):
134    return _check_exe(None, label, exe, None, True)
135
136
137def run():
138    import sys
139    try:
140        _opts = options.load(args = sys.argv)
141        log.notice('RTEMS Source Builder - Check, v%s' % (version.str()))
142        if host_setup(_opts):
143            print 'Environment is ok'
144        else:
145            print 'Environment is not correctly set up'
146    except error.general, gerr:
147        print gerr
148        sys.exit(1)
149    except error.internal, ierr:
150        print ierr
151        sys.exit(1)
152    except error.exit, eerr:
153        pass
154    except KeyboardInterrupt:
155        log.notice('abort: user terminated')
156        sys.exit(1)
157    sys.exit(0)
158
159
160if __name__ == '__main__':
161    run()
Note: See TracBrowser for help on using the repository browser.