1 # Copyright 2019 The Pigweed Authors
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 # use this file except in compliance with the License. You may obtain a copy of
7 # https://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations under
14 """Copies built host tools into Pigweed's host_tools directory."""
18 from pathlib import Path
21 from typing import Optional
25 _LOG = logging.getLogger(__name__)
29 parser: Optional[argparse.ArgumentParser] = None
30 ) -> argparse.ArgumentParser:
31 """Registers the script's arguments on an argument parser."""
34 parser = argparse.ArgumentParser(description=__doc__)
36 parser.add_argument('--dst',
39 help='Path to host tools directory')
40 parser.add_argument('--name', help='Name for the installed tool')
41 parser.add_argument('--out-root',
44 help='Root of Ninja out directory')
45 parser.add_argument('--src',
48 help='Path to host tool executable')
54 """Copies a host tool into a destination directory."""
55 args = argument_parser().parse_args()
57 if not args.src.is_file():
58 _LOG.error('%s is not a file', args.src)
61 args.dst.mkdir(parents=True, exist_ok=True)
63 if args.name is not None:
65 _LOG.error('Host tool name cannot contain "/"')
72 shutil.copy2(args.src, args.dst.joinpath(name))
73 except OSError as err:
76 # Errno 26 (text file busy) indicates that a host tool binary is
78 # TODO(frolv): Check if this works on Windows.
81 _LOG.error(' %s has been rebuilt but cannot be', name)
82 _LOG.error(' copied into the host tools directory:')
85 args.dst.relative_to(args.out_root).joinpath(name))
87 _LOG.error(' This can occur if the program is already running.')
89 ' If it is running, exit it and try re-running the build.')
97 if __name__ == '__main__':