1#!/usr/bin/env python3
2#
3#   Copyright 2020 - The Android Open Source Project
4#
5#   Licensed under the Apache License, Version 2.0 (the "License");
6#   you may not use this file except in compliance with the License.
7#   You may obtain a copy of the License at
8#
9#       http://www.apache.org/licenses/LICENSE-2.0
10#
11#   Unless required by applicable law or agreed to in writing, software
12#   distributed under the License is distributed on an "AS IS" BASIS,
13#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#   See the License for the specific language governing permissions and
15#   limitations under the License.
16
17from distutils import log
18import os
19from setuptools import find_packages
20from setuptools import setup
21from setuptools.command.install import install
22import stat
23import subprocess
24import sys
25
26reuse_libraries = False
27force_install = False
28
29install_requires = [
30    'grpcio',
31    'psutil',
32    'protobuf>=3.14.0, <4.0',
33    'mobly',
34]
35
36host_executables = [
37    'root-canal',
38    'bluetooth_stack_with_facade',  # c++
39    'bluetooth_with_facades',  # rust
40    'bt_topshim_facade',  # topshim
41]
42
43
44def set_permissions_for_host_executables(outputs):
45    for file in outputs:
46        if os.path.basename(file) in host_executables:
47            current_mode = os.stat(file).st_mode
48            new_mode = current_mode | stat.S_IEXEC
49            os.chmod(file, new_mode)
50            log.log(log.INFO, "Changed file mode of %s from %s to %s" % (file, oct(current_mode), oct(new_mode)))
51
52
53class InstallLocalPackagesForInstallation(install):
54
55    def run(self):
56        global reuse_libraries, force_install
57        install_args = [sys.executable, '-m', 'pip', 'install']
58        subprocess.check_call(install_args + ['--upgrade', 'pip'])
59
60        for package in install_requires:
61            self.announce('Installing %s...' % package, log.INFO)
62            cmd = install_args + ['-v', '--no-cache-dir', package]
63            if force_install and not reuse_libraries:
64                cmd.append("--force-reinstall")
65            subprocess.check_call(cmd)
66        self.announce('Dependencies installed.')
67
68        install.run(self)
69        set_permissions_for_host_executables(self.get_outputs())
70
71
72def main():
73    global reuse_libraries, force_install
74    if sys.argv[-1] == "--reuse-libraries":
75        reuse_libraries = True
76        sys.argv = sys.argv[:-1]
77    if "--force" in sys.argv:
78        force_install = True
79    # Relative path from calling directory to this file
80    our_dir = os.path.dirname(__file__)
81    # Must cd into this dir for package resolution to work
82    # This won't affect the calling shell
83    os.chdir(our_dir)
84    setup(
85        name='bluetooth_cert_tests',
86        version='1.0',
87        author='Android Open Source Project',
88        license='Apache2.0',
89        description="""Bluetooth Cert Tests Package""",
90        packages=[''] + find_packages(exclude=['llvm_binutils', 'llvm_binutils.*']),
91        install_requires=install_requires,
92        package_data={
93            '': host_executables + ['*.so', 'lib64/*.so', 'target/*', 'llvm_binutils/bin/*', 'llvm_binutils/lib64/*'],
94        },
95        cmdclass={
96            'install': InstallLocalPackagesForInstallation,
97        })
98
99
100if __name__ == '__main__':
101    main()
102