aboutsummaryrefslogtreecommitdiff
path: root/pw_build/py/pw_build/python_wheels.py
blob: 7f55b3eb341da957e29df0a9fc9fc288744f2de4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Copyright 2020 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
"""Wrapper for the CLI commands for Python .whl building."""

import argparse
import logging
import os
import subprocess
import sys

_LOG = logging.getLogger(__name__)


def _parse_args():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument(
        'setup_files',
        nargs='+',
        help='Path to a setup.py file to invoke to build wheels.',
    )
    parser.add_argument(
        '--out_dir', help='Path where the build artifacts should be put.'
    )

    return parser.parse_args()


def build_wheels(setup_files, out_dir):
    """Build Python wheels by calling 'python setup.py bdist_wheel'."""
    dist_dir = os.path.abspath(out_dir)

    for filename in setup_files:
        if not (filename.endswith('setup.py') and os.path.isfile(filename)):
            raise RuntimeError(f'Unable to find setup.py file at {filename}.')

        working_dir = os.path.dirname(filename)

        cmd = [
            sys.executable,
            'setup.py',
            'bdist_wheel',
            '--dist-dir',
            dist_dir,
        ]
        _LOG.debug('Running command:\n  %s', ' '.join(cmd))
        subprocess.check_call(cmd, cwd=working_dir)


def main():
    build_wheels(**vars(_parse_args()))


if __name__ == '__main__':
    logging.basicConfig()
    main()
    sys.exit(0)