source: rtems-source-builder/source-builder/sb/check.py @ 04aadb6

4.104.114.95
Last change on this file since 04aadb6 was 04aadb6, checked in by Chris Johns <chrisj@…>, on 01/30/14 at 03:04:42

Check the user's PATH environment variable.

  • Property mode set to 100644
File size: 5.5 KB
RevLine 
[8f84a6b]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
[cb12e48]29import options
[8f84a6b]30import path
[efb6688]31import version
[8f84a6b]32
33def _check_none(_opts, macro, value, constraint):
34    return True
35
[3493ada]36
[8f84a6b]37def _check_triplet(_opts, macro, value, constraint):
38    return True
39
[3493ada]40
[1b40c77]41def _check_dir(_opts, macro, value, constraint, silent = False):
[8f84a6b]42    if constraint != 'none' and not path.isdir(value):
43        if constraint == 'required':
[1b40c77]44            if not silent:
45                log.notice('error: dir: not found: (%s) %s' % (macro, value))
[8f84a6b]46            return False
[1b40c77]47        if not silent and _opts.warn_all():
[5142bec]48            log.notice('warning: dir: not found: (%s) %s' % (macro, value))
[8f84a6b]49    return True
50
[3493ada]51
[1b40c77]52def _check_exe(_opts, macro, value, constraint, silent = False):
[8f84a6b]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:
[1b40c77]74            if not silent:
75                log.notice('warning: exe: absolute exe found in path: (%s) %s' % (macro, orig_value))
[8f84a6b]76        return True
77
[3493ada]78    if constraint == 'optional':
[1b40c77]79        if not silent:
80            log.trace('warning: exe: optional exe not found: (%s) %s' % (macro, orig_value))
[3493ada]81        return True
82
[1b40c77]83    if not silent:
84        log.notice('error: exe: not found: (%s) %s' % (macro, orig_value))
[8f84a6b]85    return False
86
[3493ada]87
[8f84a6b]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
[3493ada]97
[db65c6a]98
[04aadb6]99def path_check(opts, silent = False):
100    if 'PATH' in os.environ:
101        paths = os.environ['PATH'].split(os.pathsep)
102        for p in paths:
103            if len(p.strip()) == 0:
104                if not silent:
105                    log.notice('error: environment PATH contains an empty path')
106                return False
107            elif p.strip() == '.' or p.strip() == '..':
108                if not silent:
109                    log.notice('error: environment PATH invalid path: %s' % (p))
110                return False
111            elif not path.exists(p):
112                if not silent and opts.warn_all():
113                    log.notice('warning: environment PATH not found: %s' % (p))
114            elif not path.isdir(p):
115                if not silent and opts.warn_all():
116                    log.notice('warning: environment PATH not a directory: %s' % (p))
117    return True
118
119
[cb12e48]120def host_setup(opts):
[3493ada]121    """ Basic sanity check. All executables and directories must exist."""
122
[04aadb6]123    if not path_check(opts):
124        return False
125
[3493ada]126    checks = { 'none':    _check_none,
127               'triplet': _check_triplet,
128               'dir':     _check_dir,
129               'exe':     _check_exe }
130
131    sane = True
132
[cb12e48]133    for d in opts.defaults.keys():
[3493ada]134        try:
[cb12e48]135            (test, constraint, value) = opts.defaults.get(d)
[3493ada]136        except:
[51b4061]137            if opts.defaults.get(d) is None:
138                raise error.general('invalid default: %s: not found' % (d))
139            else:
140                raise error.general('invalid default: %s [%r]' % (d, opts.defaults.get(d)))
[3493ada]141        if test != 'none':
[cb12e48]142            value = opts.defaults.expand(value)
[3493ada]143            if test not in checks:
[cb12e48]144                raise error.general('invalid check test: %s [%r]' % (test, opts.defaults.get(d)))
145            ok = checks[test](opts, d, value, constraint)
[5142bec]146            if ok:
147                tag = ' '
148            else:
149                tag = '*'
150            log.trace('%c %15s: %r -> "%s"' % (tag, d, opts.defaults.get(d), value))
[3493ada]151            if sane and not ok:
152                sane = False
153
154    return sane
155
156
[db65c6a]157def check_exe(label, exe):
[1b40c77]158    return _check_exe(None, label, exe, None, True)
[db65c6a]159
160
[3493ada]161def run():
162    import sys
163    try:
[cb12e48]164        _opts = options.load(args = sys.argv)
[5142bec]165        log.notice('RTEMS Source Builder - Check, v%s' % (version.str()))
[cb12e48]166        if host_setup(_opts):
[efb6688]167            print 'Environment is ok'
[3493ada]168        else:
[efb6688]169            print 'Environment is not correctly set up'
[3493ada]170    except error.general, gerr:
171        print gerr
172        sys.exit(1)
173    except error.internal, ierr:
174        print ierr
175        sys.exit(1)
[6e10a49]176    except error.exit, eerr:
177        pass
178    except KeyboardInterrupt:
[5142bec]179        log.notice('abort: user terminated')
[6e10a49]180        sys.exit(1)
[3493ada]181    sys.exit(0)
182
183
[8f84a6b]184if __name__ == '__main__':
185    run()
Note: See TracBrowser for help on using the repository browser.