]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/tools/build/test/toolset-mock/src/MockProgram.py
1 # Copyright 2017 Steven Watanabe
3 # Distributed under the Boost Software License, Version 1.0.
4 # (See accompanying file LICENSE_1_0.txt or copy at
5 # http://www.boost.org/LICENSE_1_0.txt)
7 from __future__
import print_function
14 # Represents a sequence of arguments that must appear
17 def __init__(self
, *args
):
19 def match(self
, command_line
, pos
, outputs
):
21 res
= try_match(command_line
, pos
, p
, outputs
)
27 # Represents a sequence of arguments that can appear
30 def __init__(self
, *args
):
31 self
.args
= list(args
)
32 def match(self
, command_line
, pos
, outputs
):
33 unmatched
= self
.args
[:]
34 while len(unmatched
) > 0:
35 res
= try_match_one(command_line
, pos
, unmatched
, outputs
)
41 # Represents a single input file.
42 # If id is set, then the file must have been created
43 # by a prior use of output_file.
44 # If source is set, then the file must be that source file.
46 def __init__(self
, id=None, source
=None):
47 assert((id is None) ^
(source
is None))
50 def check(self
, path
):
51 if path
.startswith("-"):
53 if self
.id is not None:
55 with
open(path
, "r") as f
:
57 if data
== make_file_contents(self
.id):
63 elif self
.source
is not None:
64 if self
.source
== path
:
69 def match(self
, command_line
, pos
, outputs
):
70 if self
.check(command_line
[pos
]):
73 # Matches an output file.
74 # If the full pattern is matched, The
75 # file will be created.
77 def __init__(self
, id):
79 def match(self
, command_line
, pos
, outputs
):
80 if command_line
[pos
].startswith("-"):
82 outputs
.append((command_line
[pos
], self
.id))
86 def __init__(self
, id):
88 def match(self
, command_line
, pos
, outputs
):
89 if command_line
[pos
].startswith("-"):
91 if fnmatch
.fnmatch(command_line
[pos
], self
.id):
96 # Matches the directory containing an input_file
97 class target_path(object):
98 def __init__(self
, id):
99 self
.tester
= input_file(id=id)
100 def match(self
, command_line
, pos
, outputs
):
101 arg
= command_line
[pos
]
102 if arg
.startswith("-"):
105 for path
in os
.listdir(arg
):
106 if self
.tester
.check(os
.path
.join(arg
, path
)):
111 # Matches a single argument, which is composed of a prefix and a path
112 # for example arguments of the form -ofilename.
114 def __init__(self
, prefix
, a
):
115 # The prefix should be a string, a should be target_path or input_file.
118 def match(self
, command_line
, pos
, outputs
):
119 s
= command_line
[pos
]
120 if s
.startswith(self
.prefix
) and try_match([s
[len(self
.prefix
):]], 0, self
.a
, outputs
) == 1:
123 # Given a file id, returns a string that will be
124 # written to the file to allow it to be recognized.
125 def make_file_contents(id):
128 # Matches a single pattern from a list.
129 # If it succeeds, the matching pattern
130 # is removed from the list.
131 # Returns the index after the end of the match
132 def try_match_one(command_line
, pos
, patterns
, outputs
):
135 res
= try_match(command_line
, pos
, p
, tmp
)
141 # returns the end of the match if any
142 def try_match(command_line
, pos
, pattern
, outputs
):
143 if pos
== len(command_line
):
145 elif type(pattern
) is str:
146 if pattern
== command_line
[pos
]:
149 return pattern
.match(command_line
, pos
, outputs
)
154 # Registers a command
155 # The arguments should be a sequence of:
156 # str, ordered, unordered, arg, input_file, output_file, target_path
157 # kwarg: stdout is text that will be printed on success.
158 def command(*args
, **kwargs
):
159 global known_patterns
161 stdout
= kwargs
.get("stdout", None)
162 pattern
= ordered(*args
)
163 known_patterns
+= [(pattern
, stdout
)]
164 if program_name
is None:
165 program_name
= args
[0]
167 assert(program_name
== args
[0])
169 # Use this to filter the recognized commands, based on the properties
171 def allow_properties(*args
):
173 return all(a
in os
.environ
["B2_PROPERTIES"].split(" ") for a
in args
)
177 # Use this in the stdout argument of command to print the command
178 # for running another script.
180 return os
.path
.join(os
.path
.dirname(__file__
), "bin", re
.sub('\.py$', '', name
))
182 def match(command_line
):
183 for (p
, stdout
) in known_patterns
:
185 if try_match(command_line
, 0, p
, outputs
) == len(command_line
):
186 return (stdout
, outputs
)
188 # Every mock program should call this after setting up all the commands.
190 command_line
= [program_name
] + sys
.argv
[1:]
191 result
= match(command_line
)
192 if result
is not None:
193 (stdout
, outputs
) = result
194 if stdout
is not None:
196 for (file,id) in outputs
:
197 with
open(file, "w") as f
:
198 f
.write(make_file_contents(id))
201 print("ERROR on command: %s"%(" ".join(command_line
)))
204 # file should be the name of a file in the same directory
205 # as this. Must be called after verify_setup
206 def verify_file(filename
):
208 if filename
not in known_files
:
209 known_files
.add(filename
)
210 srcdir
= os
.path
.dirname(__file__
)
211 execfile(os
.path
.join(srcdir
, filename
), {})
214 """Override the behavior of most module components
215 in order to detect whether they are being used correctly."""
217 global allow_properties
227 def allow_properties(*args
):
235 verify_error("duplicate output_file: %s" % id)
237 def input_file(id=None, source
=None):
242 def script(filename
):
243 verify_file(filename
)
244 def command(*args
, **kwargs
):
251 def verify_error(message
):
253 verify_errors
+= [message
]
255 def verify_finalize():
257 if not id in output_ids
:
258 verify_error("Input file does not exist: %s" % id)
259 for error
in verify_errors
:
260 print("error: %s" % error
)
261 if len(verify_errors
) != 0:
267 srcdir
= os
.path
.dirname(__file__
)
271 for f
in os
.listdir(srcdir
):
272 if re
.match(r
"(gcc|clang|darwin|intel)-.*\.py", f
):
274 exit(verify_finalize())