Skip to content

Commit

Permalink
Script python library builds, update documentation and dependencies (#…
Browse files Browse the repository at this point in the history
…4640)

* Simplify python environment configuration:

- Add a build script that includes all of:
  - compile host python library
  - creates a virtual environment to use
  - installs WHL chip file

- Updated instructions for building - sudo should not be required for
installation (only running) and we should recommend virtual environment
usage due to the requirement to clean the package

- Added dependency to pygobject and six, removed dependency on pgi

* Restyle fixes

* Minor update to kick the restyle checker in github

* Use `realpath` to output nicer paths and not use .. thoughout

* Restyle fixes

* Add instruction to ask for Ubuntu 20.10 instead of 20.04 for Raspberry pi

* Restyle fixes
  • Loading branch information
andy31415 authored Feb 4, 2021
1 parent ac5ec98 commit e837757
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 17 deletions.
5 changes: 3 additions & 2 deletions docs/BUILDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ dependency.

#### How to install prerequisites on Raspberry Pi 4

Using `rpi-imager`, install the Ubuntu 20.04.1 LTS (Raspberry Pi 3/4) 64-bit
server OS for arm64 architectures on a micro SD card.
Using `rpi-imager`, install the Ubuntu _20.10_ LTS 64-bit _server_ OS for arm64
architectures on a micro SD card. This release will have bluez 5.55 which is
required for BLE functionality.

Boot the SD card, login with the default user account "ubuntu" and password
"ubuntu", then proceed with "How to install prerequisites on Linux".
Expand Down
61 changes: 61 additions & 0 deletions scripts/build_python.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env bash

#
# Copyright (c) 2021 Project CHIP 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
#
# 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.
#

set -e

echo_green() {
echo -e "\033[0;32m$*\033[0m"
}

echo_blue() {
echo -e "\033[1;34m$*\033[0m"
}

echo_bold_white() {
echo -e "\033[1;37m$*\033[0m"
}

CHIP_ROOT=$(realpath "$(dirname "$0")/..")
OUTPUT_ROOT="$CHIP_ROOT/out/python_lib"
ENVIRONMENT_ROOT="$CHIP_ROOT/out/python_env"

# Ensure we have a compilation environment
source "$CHIP_ROOT/scripts/activate.sh"

# Generates ninja files
gn --root="$CHIP_ROOT" gen "$OUTPUT_ROOT"

# Compiles python files
ninja -C "$OUTPUT_ROOT" python

# Create a virtual environment that has access to the built python tools
virtualenv --clear "$ENVIRONMENT_ROOT"

# Activate the new enviroment to register the python WHL
source "$ENVIRONMENT_ROOT"/bin/activate
"$ENVIRONMENT_ROOT"/bin/python -m pip install --upgrade pip
"$ENVIRONMENT_ROOT"/bin/pip install "$OUTPUT_ROOT"/controller/python/chip-*.whl

RELATIVE_ENVIRONMENT_ROOT=$(realpath --relative-to "$PWD" "$ENVIRONMENT_ROOT")

echo ""
echo_green "Compilation completed and WHL package installed in: "
echo_blue " $RELATIVE_ENVIRONMENT_ROOT"
echo ""
echo_green "To use please run:"
echo_bold_white " source $RELATIVE_ENVIRONMENT_ROOT/bin/activate"
40 changes: 26 additions & 14 deletions src/controller/python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,40 @@ focus on its python binding interface on Linux and mac os.
git clone https://github.com/project-chip/connectedhomeip.git
```

2. Build CHIP
2. Build the CHIP python package

Follow [BUILDING.md](/docs/BUILDING.md) to build CHIP on your platform.
Follow [BUILDING.md](/docs/BUILDING.md) to setup CHIP on your platform.

Genrally, once build dependencies are satisfied you can build the `python`
target.

Use `scripts/build_python.sh` or run something equivalent to:

```sh
gn gen out/python_lib
ninja -C out/python_lib python
```

3. Install Chip Device Controller

> Note: Python device controller is not versioned, so you need to uninstall the
> old device controller before install the new one.
**For Linux (Raspberry Pi 4)**
> It is recommended to setup a separate clean virtual environment The
> `scripts/build_python.sh` script sets up a python environment and installs the
> WHL file.
```
cd out/debug/controller/python
sudo pip3 install chip-0.0-cp38-cp38-linux_aarch64.whl
```sh
virtualenv out/python_env --clean
source out/python_env/bin/activate
pip install out/python_lib/controller/python/chip*.whl
```

> Note: for linux, sudo is necessary since it need to interact with bluetoothd
> via dbus with sudo permission
The WHL file installation will:

**For macOS**

```
cd out/debug/mac_x64_clang/controller/python
pip3 install chip-0.0-cp38-cp38-mac_aarch64.whl
```
- Install the 'chip' module
- create a `ENV/bin/chip-device-ctrl` script that provides an interactive
shell for the chip library

## BLE Virtualization on Linux (Optional)

Expand Down Expand Up @@ -78,6 +87,9 @@ sudo third_party/bluez/repo/emulator/btvirt -L -l2

1. Run CHIP Device Controller

> Running as root via `sudo` to ensure permissions to interface with the
> bluetooth adapter.
```
sudo chip-device-ctrl
```
Expand Down
3 changes: 2 additions & 1 deletion src/controller/python/build-chip-wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ def finalize_options(self):
if platform.system() == 'Linux':
requiredPackages = [
'dbus-python',
'pgi'
'six',
'pygobject',
]
else:
requiredPackages = []
Expand Down

0 comments on commit e837757

Please sign in to comment.