summaryrefslogtreecommitdiff
path: root/system/setup.py
blob: cdb76f8a3a53db03525c6b0b91d0ea73fd772434 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python3
#
#   Copyright 2020 - The Android Open Source Project
#
#   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
#
#       http://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.

from distutils import log
import os
from setuptools import find_packages
from setuptools import setup
from setuptools.command.install import install
import stat
import subprocess
import sys

reuse_libraries = False
force_install = False

install_requires = [
    'grpcio',
    'psutil',
    'protobuf>=3.14.0',
    'mobly',
]

host_executables = [
    'root-canal',
    'bluetooth_stack_with_facade',  # c++
    'bluetooth_with_facades',  # rust
    'bt_topshim_facade',  # topshim
]


def set_permissions_for_host_executables(outputs):
    for file in outputs:
        if os.path.basename(file) in host_executables:
            current_mode = os.stat(file).st_mode
            new_mode = current_mode | stat.S_IEXEC
            os.chmod(file, new_mode)
            log.log(log.INFO, "Changed file mode of %s from %s to %s" % (file, oct(current_mode), oct(new_mode)))


class InstallLocalPackagesForInstallation(install):

    def run(self):
        global reuse_libraries, force_install
        install_args = [sys.executable, '-m', 'pip', 'install']
        subprocess.check_call(install_args + ['--upgrade', 'pip'])

        for package in install_requires:
            self.announce('Installing %s...' % package, log.INFO)
            cmd = install_args + ['-v', '--no-cache-dir', package]
            if force_install and not reuse_libraries:
                cmd.append("--force-reinstall")
            subprocess.check_call(cmd)
        self.announce('Dependencies installed.')

        install.run(self)
        set_permissions_for_host_executables(self.get_outputs())


def main():
    global reuse_libraries, force_install
    if sys.argv[-1] == "--reuse-libraries":
        reuse_libraries = True
        sys.argv = sys.argv[:-1]
    if "--force" in sys.argv:
        force_install = True
    # Relative path from calling directory to this file
    our_dir = os.path.dirname(__file__)
    # Must cd into this dir for package resolution to work
    # This won't affect the calling shell
    os.chdir(our_dir)
    setup(
        name='bluetooth_cert_tests',
        version='1.0',
        author='Android Open Source Project',
        license='Apache2.0',
        description="""Bluetooth Cert Tests Package""",
        # Include root package so that bluetooth_packets_python3.so can be
        # included as well
        packages=[''] + find_packages(exclude=['llvm_binutils', 'llvm_binutils.*']),
        install_requires=install_requires,
        package_data={
            '': host_executables + ['*.so', 'lib64/*.so', 'target/*', 'llvm_binutils/bin/*', 'llvm_binutils/lib64/*'],
        },
        cmdclass={
            'install': InstallLocalPackagesForInstallation,
        })


if __name__ == '__main__':
    main()