Skip to content

Commit

Permalink
Convert Python examples to proper packages (#5966)
Browse files Browse the repository at this point in the history
### What

This PR does the following:
- Convert all Python examples to proper python packages, replacing the
`requirements.txt` file with a `pyproject.toml` file.
- Applies same conversion to `tests/python/test_api`.
- Add WIP support for running example with pixi
- Add a `all_examples` meta-project.
- Fix `dev-tools build-examples` for the new example format.
- Add a `dev-tools build-examples install` command to `pip install` the
examples for the given channel (prerequisite to `build-examples rrd`).
- Fix CI for the new example format.
- Updates pixi to 0.19.1 in CI.

#### pixi support

This WIP pixi support allows running examples in the `examples`
environment:

```
pixi run -e examples clock
```

Note that by default pixi builds the environment with a shim/empty/fake
rerun package (from `examples/python/_empty_rerun_sdk`), which is a
work-around to allow manual installation of rerun in the environment. As
a result, at first (and whenever a rebuild is needed), this must be run:

```
pixi run -e examples py-build
```

#### `all_examples`

This is a meta-package that dynamically depends on all examples. The
original hope was to be able to depend on it from pixi, avoiding the
need to list example explicitly. However, this is current impossible for
a number a reason:

- doing so means that examples would not be installed as editable,
causing issue with `__file__` relative paths
(#6054)
- doing so we leak full path in the lock file
- pixi currently has an issue where indirect dependency with git
dependencies are broken

`all_examples` also has a very basic CLI tool that allows listing all
examples in a format suitable for pixi: `all_examples list`.

Also, it should be relatively easy to cleanly reimplement the
functionality of `run_all.py`, which is currently broken:
- #6057

As it stands, it allows for efficiently checking dependency conflicts,
especially with uv:
```shell
cd /tmp
uv venv
source .venv/bin/activate
uv pip install -e path/to/rerun/examples/python/all_examples   # ok??
```

#### TODO

- [x] convert a few example to python package
- [x] initial version of a `all_examples` meta-package
- [x] fix re_build_example
- [x] **RE-ENABLE LINK CHECKS!!!!**
- ~~properly working pixi setup~~
- ~~python version matrix testing setup~~

### Checklist
* [x] I have read and agree to [Contributor
Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and
the [Code of
Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md)
* [x] I've included a screenshot or gif (if applicable)
* [x] I have tested the web demo (if applicable):
* Using examples from latest `main` build:
[rerun.io/viewer](https://rerun.io/viewer/pr/5966?manifest_url=https://app.rerun.io/version/main/examples_manifest.json)
* Using full set of examples from `nightly` build:
[rerun.io/viewer](https://rerun.io/viewer/pr/5966?manifest_url=https://app.rerun.io/version/nightly/examples_manifest.json)
* [x] The PR title and labels are set such as to maximize their
usefulness for the next release's CHANGELOG
* [x] If applicable, add a new check to the [release
checklist](https://github.com/rerun-io/rerun/blob/main/tests/python/release_checklist)!

- [PR Build Summary](https://build.rerun.io/pr/5966)
- [Recent benchmark results](https://build.rerun.io/graphs/crates.html)
- [Wasm size tracking](https://build.rerun.io/graphs/sizes.html)

To run all checks from `main`, comment on the PR with `@rerun-bot
full-check`.
  • Loading branch information
abey79 authored Apr 22, 2024
1 parent f7b3788 commit 5cafbe3
Show file tree
Hide file tree
Showing 222 changed files with 6,902 additions and 1,169 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/auto_release_crates.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:

- uses: prefix-dev/[email protected]
with:
pixi-version: v0.19.0
pixi-version: v0.19.1

- name: Update crate versions
run: pixi run python scripts/ci/crates.py version --bump prerelease
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/contrib_checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:

- uses: prefix-dev/[email protected]
with:
pixi-version: v0.19.0
pixi-version: v0.19.1

- name: Python format check
run: pixi run py-fmt-check
Expand Down Expand Up @@ -92,7 +92,7 @@ jobs:

- uses: prefix-dev/[email protected]
with:
pixi-version: v0.19.0
pixi-version: v0.19.1

- name: Codegen check
run: pixi run codegen --force --check
Expand All @@ -114,7 +114,7 @@ jobs:

- uses: prefix-dev/[email protected]
with:
pixi-version: v0.19.0
pixi-version: v0.19.1

- name: Rerun lints
run: pixi run lint-rerun
Expand Down Expand Up @@ -159,7 +159,7 @@ jobs:

- uses: prefix-dev/[email protected]
with:
pixi-version: v0.19.0
pixi-version: v0.19.1
environments: cpp

# TODO(emilk): make this work somehow. Right now this just results in
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/contrib_rerun_py.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:

- uses: prefix-dev/[email protected]
with:
pixi-version: v0.19.0
pixi-version: v0.19.1
environments: wheel-test

- name: Build Wheel
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/on_pull_request_target_contrib.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:

- uses: prefix-dev/[email protected]
with:
pixi-version: v0.19.0
pixi-version: v0.19.1

- name: Update PR description
run: |
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ jobs:

- uses: prefix-dev/[email protected]
with:
pixi-version: v0.19.0
pixi-version: v0.19.1

- name: Update crate versions
id: versioning
Expand Down Expand Up @@ -379,7 +379,7 @@ jobs:

- uses: prefix-dev/[email protected]
with:
pixi-version: v0.19.0
pixi-version: v0.19.1

- name: Commit new version
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/reusable_build_and_upload_rerun_cli.yml
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ jobs:

- uses: prefix-dev/[email protected]
with:
pixi-version: v0.19.0
pixi-version: v0.19.1

- name: Build web-viewer (release)
run: pixi run rerun-build-web-release
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/reusable_build_and_upload_wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ jobs:

- uses: prefix-dev/[email protected]
with:
pixi-version: v0.19.0
pixi-version: v0.19.1

- name: Get sha
id: get-sha
Expand Down
7 changes: 3 additions & 4 deletions .github/workflows/reusable_build_examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ jobs:

- uses: prefix-dev/[email protected]
with:
pixi-version: v0.19.0
pixi-version: v0.19.1
environments: wheel-test

- name: Download Wheel
Expand All @@ -75,9 +75,8 @@ jobs:

- name: Install example dependencies
run: |
pixi run -e wheel-test pip install \
-r scripts/ci/requirements-examples-${{ inputs.CHANNEL }}.txt \
--no-input
pixi run -e wheel-test build-examples install \
--channel ${{ inputs.CHANNEL }}
- name: Install built wheel
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/reusable_build_js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ jobs:

- uses: prefix-dev/[email protected]
with:
pixi-version: v0.19.0
pixi-version: v0.19.1

- name: Install yarn dependencies
run: pixi run yarn --cwd rerun_js install
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/reusable_build_web.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ jobs:

- uses: prefix-dev/[email protected]
with:
pixi-version: v0.19.0
pixi-version: v0.19.1

- name: Build web-viewer (release)
run: |
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/reusable_checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ jobs:

- uses: prefix-dev/[email protected]
with:
pixi-version: v0.19.0
pixi-version: v0.19.1

- name: Codegen check
run: |
Expand All @@ -104,7 +104,7 @@ jobs:

- uses: prefix-dev/[email protected]
with:
pixi-version: v0.19.0
pixi-version: v0.19.1

- name: Set up Python
uses: actions/setup-python@v5
Expand Down Expand Up @@ -156,7 +156,7 @@ jobs:

- uses: prefix-dev/[email protected]
with:
pixi-version: v0.19.0
pixi-version: v0.19.1

- name: prettier --check
run: pixi run misc-fmt-check
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/reusable_checks_cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ jobs:

- uses: prefix-dev/[email protected]
with:
pixi-version: v0.19.0
pixi-version: v0.19.1
environments: cpp

- name: Set up Rust
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/reusable_checks_python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:

- uses: prefix-dev/[email protected]
with:
pixi-version: v0.19.0
pixi-version: v0.19.1

- name: Python format check
run: pixi run py-fmt-check
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/reusable_checks_rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ jobs:

- uses: prefix-dev/[email protected]
with:
pixi-version: v0.19.0
pixi-version: v0.19.1

# We need to build the web viewer for `rust_checks.py` to succeed.
# We build in release so that we can reuse the results for actual publishing, if necessary
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/reusable_deploy_docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ jobs:

- uses: prefix-dev/[email protected]
with:
pixi-version: v0.19.0
pixi-version: v0.19.1

- name: Doxygen C++ docs
run: pixi run cpp-docs
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/reusable_publish_js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ jobs:

- uses: prefix-dev/[email protected]
with:
pixi-version: v0.19.0
pixi-version: v0.19.1

- name: Publish packages
env:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/reusable_publish_web.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ jobs:

- uses: prefix-dev/[email protected]
with:
pixi-version: v0.19.0
pixi-version: v0.19.1
environments: wheel-test

# built by `reusable_build_and_publish_wheels`
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/reusable_publish_wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ jobs:

- uses: prefix-dev/[email protected]
with:
pixi-version: v0.19.0
pixi-version: v0.19.1

- id: "auth"
uses: google-github-actions/auth@v2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/reusable_release_crates.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:

- uses: prefix-dev/[email protected]
with:
pixi-version: v0.19.0
pixi-version: v0.19.1

- name: Build web-viewer (release)
run: pixi run rerun-build-web-release
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/reusable_run_notebook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:

- uses: prefix-dev/[email protected]
with:
pixi-version: v0.19.0
pixi-version: v0.19.1
environments: wheel-test

- name: Download Wheel
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/reusable_test_wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ jobs:

- uses: prefix-dev/[email protected]
with:
pixi-version: v0.19.0
pixi-version: v0.19.1
environments: wheel-test

- name: Download Wheel
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.DS_Store

# Codegen stuff:
crates/re_types/source_hash.txt
crates/re_types/source_hash.txt§
crates/re_types_builder/source_hash.txt

# C++ and CMake stuff:
Expand All @@ -27,6 +27,7 @@ _deps

# Python virtual environment:
**/venv*
**/.venv*
.python-version

# Python build artifacts:
Expand Down
2 changes: 1 addition & 1 deletion .mypy.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[mypy]
files = rerun_py/rerun_sdk/rerun, rerun_py/tests, examples/python
exclude = examples/python/objectron/proto|examples/python/ros_node
exclude = examples/python/objectron|examples/python/ros_node
namespace_packages = True
show_error_codes = True
strict = True
Expand Down
54 changes: 46 additions & 8 deletions crates/re_dev_tools/src/build_examples/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use anyhow::Context;
pub struct Example {
pub name: String,
pub title: String,
pub dir: PathBuf,
pub description: String,
pub tags: Vec<String>,
pub thumbnail_url: String,
Expand Down Expand Up @@ -60,21 +61,35 @@ impl Example {
else {
anyhow::bail!("example {name:?} has no frontmatter");
};
let script_path = dir.join(language.entrypoint_path(&dir, name)?);
Ok(Some(Example {
name: name.to_owned(),
title: readme.title,
dir,
description: readme.description,
tags: readme.tags,
thumbnail_url: readme.thumbnail,
thumbnail_dimensions: readme.thumbnail_dimensions,
script_path: dir.join(language.entrypoint_path()),
script_path,
script_args: readme.build_args,
readme_body: body,
language,
}))
}
}

#[derive(Debug)]
pub struct CouldNotFindEntryPoint(pub PathBuf);

impl std::error::Error for CouldNotFindEntryPoint {}

impl Display for CouldNotFindEntryPoint {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("could not find entrypoint for example: ")?;
self.0.display().fmt(f)
}
}

#[derive(Clone, Copy)]
pub enum Language {
Rust,
Expand Down Expand Up @@ -112,12 +127,32 @@ impl Language {
/// For example:
/// - `main.py` for Python
/// - `src/main.rs` for Rust
pub fn entrypoint_path(&self) -> &'static Path {
pub fn entrypoint_path(
&self,
example_dir: &Path,
example_name: &str,
) -> anyhow::Result<PathBuf> {
match self {
Language::Rust => Path::new("src/main.rs"),
Language::Python => Path::new("main.py"),
Language::C => Path::new("main.c"),
Language::Cpp => Path::new("main.cpp"),
Language::Rust => Ok(PathBuf::from("src/main.rs")),
Language::Python => {
// we must handle two cases:
// - single script named after the example: `example.py`
// - package named after the example: `example/`

let mut script_path = PathBuf::from(example_name);
if example_dir.join(&script_path).exists() {
return Ok(script_path);
}

script_path.set_extension("py");
if example_dir.join(&script_path).exists() {
return Ok(script_path);
}

anyhow::bail!(CouldNotFindEntryPoint(example_dir.into()))
}
Language::C => Ok(PathBuf::from("main.c")),
Language::Cpp => Ok(PathBuf::from("main.cpp")),
}
}
}
Expand Down Expand Up @@ -168,7 +203,7 @@ pub enum Channel {
/// Examples built for each release, plus all `Main` examples.
Release,

/// Examples built nightly, plus all `Main` and `Nightly`.
/// Examples built nightly, plus all `Main` and `Release`.
Nightly,
}

Expand Down Expand Up @@ -235,14 +270,17 @@ impl Channel {
}

eprintln!("{name:?}: added");
let dir = folder.path();
let script_path = dir.join(language.entrypoint_path(&dir, &name)?);
examples.push(Example {
name,
title: readme.title,
dir,
description: readme.description,
tags: readme.tags,
thumbnail_url: readme.thumbnail,
thumbnail_dimensions: readme.thumbnail_dimensions,
script_path: folder.path().join(language.entrypoint_path()),
script_path,
script_args: readme.build_args,
readme_body: body,
language: Language::Python,
Expand Down
Loading

0 comments on commit 5cafbe3

Please sign in to comment.