1# Copyright 2020 The Pigweed Authors
2#
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
5# the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
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
13# the License.
14"""Wrapper for the CLI commands for Python .whl building."""
15
16import argparse
17import logging
18import os
19import subprocess
20import sys
21
22_LOG = logging.getLogger(__name__)
23
24
25def _parse_args():
26    parser = argparse.ArgumentParser(description=__doc__)
27    parser.add_argument(
28        'setup_files',
29        nargs='+',
30        help='Path to a setup.py file to invoke to build wheels.')
31    parser.add_argument('--out_dir',
32                        help='Path where the build artifacts should be put.')
33
34    return parser.parse_args()
35
36
37def build_wheels(setup_files, out_dir):
38    """Build Python wheels by calling 'python setup.py bdist_wheel'."""
39    dist_dir = os.path.abspath(out_dir)
40
41    for filename in setup_files:
42        if not (filename.endswith('setup.py') and os.path.isfile(filename)):
43            raise RuntimeError(f'Unable to find setup.py file at {filename}.')
44
45        working_dir = os.path.dirname(filename)
46
47        cmd = [
48            sys.executable,
49            'setup.py',
50            'bdist_wheel',
51            '--dist-dir',
52            dist_dir,
53        ]
54        _LOG.debug('Running command:\n  %s', ' '.join(cmd))
55        subprocess.check_call(cmd, cwd=working_dir)
56
57
58def main():
59    build_wheels(**vars(_parse_args()))
60
61
62if __name__ == '__main__':
63    logging.basicConfig()
64    main()
65    sys.exit(0)
66