source: rtems-tools/rtemstoolkit/check.py

Last change on this file was 75683061, checked in by Chris Johns <chrisj@…>, on 05/30/19 at 10:21:35

rtemstoolkit/check: Optionally check exe silently.

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