source: rtems-source-builder/source-builder/sb/check.py @ 069c7de

4.104.114.95
Last change on this file since 069c7de was 6e10a49, checked in by Chris Johns <chrisj@…>, on 11/10/12 at 05:10:30

Clean up the exception handling. Lets --help work.

  • Property mode set to 100644
File size: 4.5 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 defaults
27import error
28import execute
29import log
30import path
31
32#
33# Version of Sourcer Builder Check.
34#
35version = '0.1'
36
37def _notice(opts, text):
38    if not opts.quiet() and log.default and not log.default.has_stdout():
39        print text
40    log.output(text)
41    log.flush()
42
43
44def _check_none(_opts, macro, value, constraint):
45    return True
46
47
48def _check_triplet(_opts, macro, value, constraint):
49    return True
50
51
52def _check_dir(_opts, macro, value, constraint):
53    if constraint != 'none' and not path.isdir(value):
54        if constraint == 'required':
55            _notice(_opts, 'error: dir: not found: (%s) %s' % (macro, value))
56            return False
57        if _opts.warn_all():
58            _notice(_opts, 'warning: dir: not found: (%s) %s' % (macro, value))
59    return True
60
61
62def _check_exe(_opts, macro, value, constraint):
63
64    if len(value) == 0 or constraint == 'none':
65        return True
66
67    orig_value = value
68
69    if path.isabspath(value):
70        if path.isfile(value):
71            return True
72        if os.name == 'nt':
73            if path.isfile('%s.exe' % (value)):
74                return True
75        value = path.basename(value)
76        absexe = True
77    else:
78        absexe = False
79
80    paths = os.environ['PATH'].split(os.pathsep)
81
82    if _check_paths(value, paths):
83        if absexe:
84            _notice(_opts,
85                    'warning: exe: absolute exe found in path: (%s) %s' % (macro, orig_value))
86        return True
87
88    if constraint == 'optional':
89        if _opts.trace():
90            _notice(_opts, 'warning: exe: optional exe not found: (%s) %s' % (macro, orig_value))
91        return True
92
93    _notice(_opts, 'error: exe: not found: (%s) %s' % (macro, orig_value))
94    return False
95
96
97def _check_paths(name, paths):
98    for p in paths:
99        exe = path.join(p, name)
100        if path.isfile(exe):
101            return True
102        if os.name == 'nt':
103            if path.isfile('%s.exe' % (exe)):
104                return True
105    return False
106
107def host_setup(_opts, _defaults):
108    """ Basic sanity check. All executables and directories must exist."""
109
110    checks = { 'none':    _check_none,
111               'triplet': _check_triplet,
112               'dir':     _check_dir,
113               'exe':     _check_exe }
114
115    sane = True
116
117    for d in sorted(_defaults.iterkeys()):
118        try:
119            (test, constraint, value) = _defaults[d]
120        except:
121            raise error.general('invalid default: %s [%r]' % (d, _defaults[d]))
122        if test != 'none':
123            value = _opts.expand(value, _defaults)
124            if test not in checks:
125                raise error.general('invalid check test: %s [%r]' % (test, _defaults[d]))
126            ok = checks[test](_opts, d, value, constraint)
127            if _opts.trace():
128                if ok:
129                    tag = ' '
130                else:
131                    tag = '*'
132                _notice(_opts, '%c %15s: %r -> "%s"' % (tag, d, _defaults[d], value))
133            if sane and not ok:
134                sane = False
135
136    return sane
137
138
139def run():
140    import sys
141    try:
142        _opts, _defaults = defaults.load(args = sys.argv)
143        if host_setup(_opts, _defaults):
144            print 'Source Builder environent is ok'
145        else:
146            print 'Source Builder environent 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, '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.