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