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