Skip to content

Commit 1ed640a

Browse files
committed
DOCS-3311: Streamline Python module test, package, and upload steps
1 parent 0e7bee7 commit 1ed640a

File tree

2 files changed

+77
-17
lines changed

2 files changed

+77
-17
lines changed

docs/operate/get-started/other-hardware/_index.md

+71-17
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ It's a good idea to test your module locally before uploading it to the [Viam Re
531531

532532
{{% expand "Prerequisite: A running machine connected to the Viam app." %}}
533533

534-
You can write a module without a machine, but to test your module you'll need a machine.
534+
You can write a module without a machine, but to test your module you'll need a [machine](/operate/get-started/setup/).
535535
Make sure to physically connect your sensor to your machine's computer to prepare your machine for testing.
536536

537537
{{% snippet "setup.md" %}}
@@ -544,25 +544,56 @@ Make sure to physically connect your sensor to your machine's computer to prepar
544544

545545
{{< tabs >}}
546546
{{% tab name="Python: pyinstaller (recommended)" %}}
547-
{{% alert title="Note" color="note" %}}
548-
To follow these PyInstaller packaging steps, you must have enabled cloud build when moving through the module generator prompts.
549-
If you did not, you will need to manually create a <file>build.sh</file> entrypoint script.
550-
{{% /alert %}}
551547

552-
From within the <file>hello-world</file> directory, create a virtual Python environment with the necessary packages and then build an executable by running the setup and build scripts:
548+
If you enabled cloud build, use these steps.
553549

554-
```sh {id="terminal-prompt" class="command-line" data-prompt="$"}
555-
sh setup.sh
556-
sh build.sh
557-
```
550+
1. Create a <file>reload.sh</file> script in your module directory.
551+
You'll use this for local testing and can delete it before you upload your module.
552+
Paste the following contents into it and save the file:
558553

559-
This environment is where the local module will run.
560-
`viam-server` does not need to run inside this environment.
554+
```sh {id="terminal-prompt" class="command-line" data-prompt="$"}
555+
#!/usr/bin/env bash
556+
557+
# bash safe mode. look at `set --help` to see what these are doing
558+
set -euxo pipefail
559+
560+
cd $(dirname $0)
561+
MODULE_DIR=$(dirname $0)
562+
VIRTUAL_ENV=$MODULE_DIR/venv
563+
PYTHON=$VIRTUAL_ENV/bin/python
564+
./setup.sh
565+
566+
# Be sure to use `exec` so that termination signals reach the python process,
567+
# or handle forwarding termination signals manually
568+
exec $PYTHON src/main.py $@
569+
```
570+
571+
1. Make your reload script executable by running the following command in your module directory:
572+
573+
```sh {id="terminal-prompt" class="command-line" data-prompt="$"}
574+
sudo chmod +x reload.sh
575+
```
576+
577+
1. Edit your <file>meta.json</file>, replacing the `"entrypoint"`, `"build"`, and `"path"` fields as follows:
578+
579+
```json {class="line-numbers linkable-line-numbers" data-start="13" data-line="1, 4, 6" }
580+
"entrypoint": "reload.sh",
581+
"first_run": "",
582+
"build": {
583+
"build": "rm -f module.tar.gz && tar czf module.tar.gz requirements.txt src/*.py meta.json setup.sh reload.sh",
584+
"setup": "./setup.sh",
585+
"path": "module.tar.gz",
586+
"arch": [
587+
"linux/amd64",
588+
"linux/arm64"
589+
]
590+
}
591+
```
561592

562593
{{% /tab %}}
563594
{{% tab name="Python: venv" %}}
564595

565-
Create a virtual Python environment with the necessary packages by running the setup file from within the <file>hello-world</file> directory:
596+
Create a virtual Python environment with the necessary packages by running the setup file from within the module directory:
566597

567598
```sh {id="terminal-prompt" class="command-line" data-prompt="$"}
568599
sh setup.sh
@@ -574,7 +605,7 @@ This environment is where the local module will run.
574605
{{% /tab %}}
575606
{{% tab name="Go" %}}
576607

577-
From within the <file>hello-world</file> directory, compile your module into a single executable:
608+
From within the module directory, compile your module into a single executable:
578609

579610
```sh {class="command-line" data-prompt="$" data-output="5-10"}
580611
make setup
@@ -596,9 +627,9 @@ Type in the _absolute_ path on your machine's filesystem to your module's execut
596627
{{< tabs >}}
597628
{{% tab name="Python: pyinstaller (recommended)" %}}
598629

599-
Enter the absolute path to the <file>dist/main</file> executable, for example:
630+
Enter the absolute path to the <file>reload.sh</file> script, for example:
600631

601-
<file>/Users/jessamy/my-python-sensor-module/dist/main</file>
632+
<file>/Users/jessamy/my-python-sensor-module/reload.sh</file>
602633

603634
{{% /tab %}}
604635
{{% tab name="Python: venv" %}}
@@ -829,9 +860,32 @@ When packaged in this fashion, you can run the resulting executable on your desi
829860

830861
{{% alert title="Note" color="note" %}}
831862
To follow these PyInstaller packaging steps, you must have enabled cloud build when moving through the module generator prompts.
863+
If you did not, you will need to manually create a <file>build.sh</file> entrypoint script.
832864
{{% /alert %}}
833865

834-
The <file>build.sh</file> script packaged a tarball for you when you ran it before [testing](#test-your-module-locally).
866+
Edit your <file>meta.json</file> file back to its original state, reverting the edits you made for local testing purposes.
867+
It should resemble the following:
868+
869+
```json {class="line-numbers linkable-line-numbers" data-start="13" data-line="1, 4, 6" }
870+
"entrypoint": "dist/main",
871+
"first_run": "",
872+
"build": {
873+
"build": "./build.sh",
874+
"setup": "./setup.sh",
875+
"path": "dist/archive.tar.gz",
876+
"arch": [
877+
"linux/amd64",
878+
"linux/arm64"
879+
]
880+
}
881+
```
882+
883+
From within the module directory, create a virtual Python environment with the necessary packages and then build an executable by running the setup and build scripts:
884+
885+
```sh {id="terminal-prompt" class="command-line" data-prompt="$"}
886+
sh setup.sh
887+
sh build.sh
888+
```
835889

836890
Run the `viam module upload` CLI command to upload the module to the registry, replacing `any` with one or more of `linux/any` or `darwin/any` if your module requires Linux OS-level support or macOS OS-level support, respectively.
837891
If your module does not require OS-level support (such as platform-specific dependencies), you can run the following command exactly:

docs/operate/get-started/other-hardware/hello-world-module.md

+6
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,12 @@ If you prefer the simpler path, skip the sensor sections in the steps below.
179179

180180
The easiest way to generate the files for your module is to use the [Viam CLI](/dev/tools/cli/).
181181

182+
{{% alert title="Note" color="note" %}}
183+
The steps below suggest that you disable cloud build when generating your stub files, for simplicity of local testing.
184+
If you plan to publish your module to the Viam registry, we recommend enabling cloud build, and then following the testing, packaging and uploading steps in [Integrate other hardware](/operate/get-started/other-hardware/) once you are done writing your API implementation in this guide.
185+
Enabling cloud build will set up your module for a more automated deployment process if you plan to use your module for more than just learning.
186+
{{% /alert %}}
187+
182188
### Generate the camera files
183189

184190
The CLI module generator generates the files for one modular resource at a time.

0 commit comments

Comments
 (0)