Jack He | 1817a2a | 2020-03-18 02:52:54 -0700 | [diff] [blame] | 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 | |
| 17 | from distutils import log |
Jack He | 838422b | 2020-04-08 22:58:07 -0700 | [diff] [blame] | 18 | from distutils.errors import DistutilsModuleError |
Jack He | 1817a2a | 2020-03-18 02:52:54 -0700 | [diff] [blame] | 19 | import os |
Jack He | 838422b | 2020-04-08 22:58:07 -0700 | [diff] [blame] | 20 | from setuptools import find_packages |
| 21 | from setuptools import setup |
Jack He | 1817a2a | 2020-03-18 02:52:54 -0700 | [diff] [blame] | 22 | from setuptools.command.install import install |
Jack He | 3a27655 | 2020-03-23 17:23:45 -0700 | [diff] [blame] | 23 | import stat |
Jack He | 1817a2a | 2020-03-18 02:52:54 -0700 | [diff] [blame] | 24 | import subprocess |
| 25 | import sys |
| 26 | |
| 27 | install_requires = [ |
| 28 | 'grpcio', |
Jack He | ebd72a9 | 2020-04-12 19:02:52 -0700 | [diff] [blame^] | 29 | 'psutil', |
Jack He | 1817a2a | 2020-03-18 02:52:54 -0700 | [diff] [blame] | 30 | ] |
| 31 | |
Jack He | 3a27655 | 2020-03-23 17:23:45 -0700 | [diff] [blame] | 32 | host_executables = [ |
| 33 | 'root-canal', |
| 34 | 'bluetooth_stack_with_facade', |
| 35 | ] |
| 36 | |
Jack He | 1817a2a | 2020-03-18 02:52:54 -0700 | [diff] [blame] | 37 | |
Jack He | 838422b | 2020-04-08 22:58:07 -0700 | [diff] [blame] | 38 | # Need to verify acts is importable in a new Python context |
| 39 | def is_acts_importable(): |
| 40 | cmd = [sys.executable, '-c', 'import acts'] |
| 41 | completed_process = subprocess.run(cmd, cwd=os.getcwd()) |
| 42 | return completed_process.returncode == 0 |
| 43 | |
| 44 | |
Jack He | 1817a2a | 2020-03-18 02:52:54 -0700 | [diff] [blame] | 45 | def setup_acts_for_cmd_or_die(cmd_str): |
| 46 | acts_framework_dir = os.path.abspath('acts_framework') |
| 47 | acts_setup_bin = os.path.join(acts_framework_dir, 'setup.py') |
| 48 | cmd = [sys.executable, acts_setup_bin, cmd_str] |
Jack He | 838422b | 2020-04-08 22:58:07 -0700 | [diff] [blame] | 49 | subprocess.run(cmd, cwd=acts_framework_dir, check=True) |
Jack He | 1817a2a | 2020-03-18 02:52:54 -0700 | [diff] [blame] | 50 | |
| 51 | |
Jack He | 838422b | 2020-04-08 22:58:07 -0700 | [diff] [blame] | 52 | def set_permissions_for_host_executables(outputs): |
Jack He | 3a27655 | 2020-03-23 17:23:45 -0700 | [diff] [blame] | 53 | for file in outputs: |
| 54 | if os.path.basename(file) in host_executables: |
| 55 | current_mode = os.stat(file).st_mode |
| 56 | new_mode = current_mode | stat.S_IEXEC |
| 57 | os.chmod(file, new_mode) |
| 58 | log.log( |
| 59 | log.INFO, "Changed file mode of %s from %s to %s" % |
| 60 | (file, oct(current_mode), oct(new_mode))) |
| 61 | |
| 62 | |
Jack He | 1817a2a | 2020-03-18 02:52:54 -0700 | [diff] [blame] | 63 | class InstallLocalPackagesForInstallation(install): |
| 64 | |
Jack He | 838422b | 2020-04-08 22:58:07 -0700 | [diff] [blame] | 65 | user_options = install.user_options + [ |
| 66 | ('reuse-acts', None, "Skip ACTS installation if already installed"), |
| 67 | ] |
| 68 | boolean_options = install.boolean_options + ['reuse-acts'] |
| 69 | |
| 70 | def initialize_options(self): |
| 71 | install.initialize_options(self) |
| 72 | self.reuse_acts = False |
| 73 | |
Jack He | 1817a2a | 2020-03-18 02:52:54 -0700 | [diff] [blame] | 74 | def run(self): |
Jack He | 838422b | 2020-04-08 22:58:07 -0700 | [diff] [blame] | 75 | if self.reuse_acts and is_acts_importable(): |
| 76 | self.announce('Reusing existing ACTS installation', log.WARN) |
| 77 | else: |
| 78 | self.announce('Installing ACTS for installation', log.WARN) |
| 79 | setup_acts_for_cmd_or_die("install") |
| 80 | self.announce('ACTS installed for installation.', log.WARN) |
| 81 | if not is_acts_importable(): |
| 82 | raise DistutilsModuleError("Cannot import acts after installation") |
Jack He | 1817a2a | 2020-03-18 02:52:54 -0700 | [diff] [blame] | 83 | install.run(self) |
Jack He | 838422b | 2020-04-08 22:58:07 -0700 | [diff] [blame] | 84 | set_permissions_for_host_executables(self.get_outputs()) |
Jack He | 1817a2a | 2020-03-18 02:52:54 -0700 | [diff] [blame] | 85 | |
| 86 | |
| 87 | def main(): |
| 88 | # Relative path from calling directory to this file |
| 89 | our_dir = os.path.dirname(__file__) |
| 90 | # Must cd into this dir for package resolution to work |
| 91 | # This won't affect the calling shell |
| 92 | os.chdir(our_dir) |
| 93 | setup( |
| 94 | name='bluetooth_cert_tests', |
| 95 | version='1.0', |
| 96 | author='Android Open Source Project', |
| 97 | license='Apache2.0', |
| 98 | description="""Bluetooth Cert Tests Package""", |
| 99 | # Include root package so that bluetooth_packets_python3.so can be |
| 100 | # included as well |
Jack He | 838422b | 2020-04-08 22:58:07 -0700 | [diff] [blame] | 101 | packages=[''] + |
| 102 | find_packages(exclude=['acts_framework', 'acts_framework.*']), |
Jack He | 1817a2a | 2020-03-18 02:52:54 -0700 | [diff] [blame] | 103 | install_requires=install_requires, |
| 104 | package_data={ |
Jack He | 3a27655 | 2020-03-23 17:23:45 -0700 | [diff] [blame] | 105 | '': host_executables + ['*.so', 'lib64/*.so', 'target/*'], |
Jack He | 1817a2a | 2020-03-18 02:52:54 -0700 | [diff] [blame] | 106 | 'cert': ['all_test_cases'], |
| 107 | }, |
| 108 | cmdclass={ |
| 109 | 'install': InstallLocalPackagesForInstallation, |
Jack He | 1817a2a | 2020-03-18 02:52:54 -0700 | [diff] [blame] | 110 | }) |
| 111 | |
| 112 | |
| 113 | if __name__ == '__main__': |
| 114 | main() |