Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@
# Sample maintainers. Each sample's folder holds its code, README, images, and
# tests; own the whole folder. The nightly Newton-main workflow assigns failure
# issues to these owners. See CONTRIBUTING.md.
# New sample template (replace placeholders and uncomment):
# /examples/<topic>/<example_folder>/ @your-handle
/examples/multiphysics/franka_pulley_mechanical_advantage/ @felixmey
90 changes: 90 additions & 0 deletions .github/scripts/check_examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

"""Validate sample layout and explicit per-sample CODEOWNERS entries."""

from __future__ import annotations

import subprocess
import sys
from pathlib import Path, PurePosixPath


def validate_examples(files: list[str], codeowners: str) -> list[str]:
errors = []
samples: set[str] = set()
entry_points: set[str] = set()
shared = {"examples/__init__.py", "examples/__main__.py"}
for name in sorted(files):
path = PurePosixPath(name)
parts = path.parts
if not parts or parts[0] != "examples" or name in shared:
continue
if len(parts) == 3 and path.name == "__init__.py":
continue
if len(parts) < 4:
errors.append(
f"{name}: misplaced file; put each sample and its supporting files in "
"examples/<topic>/<example_folder>/ (see CONTRIBUTING.md)."
)
continue
sample = "/".join(parts[:3])
samples.add(sample)
if path.match("example_*.py"):
if len(parts) != 4:
errors.append(
f"{name}: example entry points must be directly inside "
"examples/<topic>/<example_folder>/; remove extra directory levels."
)
else:
entry_points.add(sample)

# Exact keys intentionally exclude wildcard and topic-wide rules. Retain
# the last occurrence, so an ownerless duplicate cannot count as owned.
rules = {}
for line in codeowners.splitlines():
fields = line.split("#", 1)[0].split()
if fields:
rules[fields[0]] = fields[1:]

for sample in sorted(samples):
if sample not in entry_points:
errors.append(
f"{sample}/: missing a direct example_*.py entry point; expected "
"examples/<topic>/<example_folder>/example_<name>.py."
)
pattern = f"/{sample}/"
if not any(owner.startswith("@") and len(owner) > 1 for owner in rules.get(pattern, [])):
errors.append(
f"{sample}/: missing an explicit owned folder rule in .github/CODEOWNERS. "
f"Add '{pattern} @your-handle'; wildcard and topic-wide rules do not count."
)
return errors


def main() -> int:
root = Path(__file__).resolve().parents[2]
# Include untracked, non-ignored files for local use as well as CI's checkout.
result = subprocess.run(
["git", "ls-files", "--cached", "--others", "--exclude-standard", "-z", "--", "examples"],
cwd=root,
check=True,
capture_output=True,
text=True,
)
files = [name for name in result.stdout.split("\0") if name]
codeowners = root / ".github/CODEOWNERS"
if not codeowners.is_file():
print("ERROR: .github/CODEOWNERS is missing; add an explicit owner rule for every sample.", file=sys.stderr)
return 1
errors = validate_examples(files, codeowners.read_text(encoding="utf-8"))
for error in errors:
print(f"ERROR: {error}", file=sys.stderr)
if errors:
return 1
print("All example folders follow the required layout and have explicit CODEOWNERS entries.")
return 0


if __name__ == "__main__":
sys.exit(main())
74 changes: 74 additions & 0 deletions .github/scripts/tests/test_check_examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

import importlib.util
import unittest
from pathlib import Path

spec = importlib.util.spec_from_file_location(
"check_examples", Path(__file__).resolve().parents[1] / "check_examples.py"
)
checker = importlib.util.module_from_spec(spec)
spec.loader.exec_module(checker)

SAMPLE = "examples/rigid_body/pendulum"
ENTRY = f"{SAMPLE}/example_pendulum.py"
RULE = f"/{SAMPLE}/ @maintainer"


class ExampleValidationTests(unittest.TestCase):
def test_valid_layout_with_support_files_and_shared_modules(self):
files = [
"examples/__init__.py",
"examples/__main__.py",
"examples/rigid_body/__init__.py",
ENTRY,
f"{SAMPLE}/README.md",
f"{SAMPLE}/tests/test_pendulum.py",
f"{SAMPLE}/images/preview.jpg",
]
self.assertEqual(checker.validate_examples(files, RULE), [])

def test_misplaced_files(self):
for path in ("examples/example_pendulum.py", "examples/rigid_body/example_pendulum.py"):
with self.subTest(path=path):
errors = checker.validate_examples([path], RULE)
self.assertTrue(any(path in error and "misplaced" in error for error in errors))

def test_nested_example_is_rejected_even_with_valid_entry_point(self):
errors = checker.validate_examples([ENTRY, f"{SAMPLE}/extra/example_other.py"], RULE)
self.assertTrue(any("extra directory levels" in error for error in errors))

def test_folder_without_entry_point(self):
errors = checker.validate_examples([f"{SAMPLE}/README.md"], RULE)
self.assertTrue(any("missing a direct example_*.py" in error for error in errors))

def test_requires_exact_owned_folder_rule(self):
for rule in (
"",
f"# {RULE}",
"/examples/ @maintainer",
"/examples/rigid_body/*/ @maintainer",
f"{SAMPLE}/ @maintainer",
f"/{SAMPLE} @maintainer",
f"/{SAMPLE}/",
f"{RULE}\n/{SAMPLE}/ # clear owners",
f"/{SAMPLE}/example_pendulum.py @maintainer",
):
with self.subTest(rule=rule):
errors = checker.validate_examples([ENTRY], rule)
self.assertTrue(any("missing an explicit owned folder rule" in error for error in errors))

def test_every_sample_is_checked(self):
other = "examples/soft_body/cloth"
errors = checker.validate_examples([ENTRY, f"{other}/example_cloth.py"], RULE)
self.assertEqual(len(errors), 1)
self.assertIn(other, errors[0])

def test_teams_multiple_owners_and_comments(self):
rule = f"/{SAMPLE}/ @org/team @maintainer # sample owners"
self.assertEqual(checker.validate_examples([ENTRY], rule), [])


if __name__ == "__main__":
unittest.main()
13 changes: 13 additions & 0 deletions .github/workflows/quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ concurrency:
cancel-in-progress: true

jobs:
example-structure:
name: Example layout and CODEOWNERS
runs-on: ubuntu-24.04
timeout-minutes: 5
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Test example validation
run: python3 -m unittest discover -s .github/scripts/tests -v
- name: Validate all example folders and owners
run: python3 .github/scripts/check_examples.py

cpu:
name: CPU / Python ${{ matrix.python-version }}
runs-on: ubuntu-24.04
Expand Down
23 changes: 21 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Contributions are currently limited to Nvidians only.
Each sample lives in its own folder, which holds everything that belongs to it:

```
examples/<category>/<sample>/
examples/<topic>/<example_folder>/
__init__.py
example_<sample>.py # run with: uv run -m examples <sample>
README.md # usage, configuration, and validation
Expand All @@ -29,7 +29,26 @@ Add a card for the sample to the table in the top-level [README](README.md#examp

## Registering as a Contributor

To register as a contributor, please add your sample's folder to [`.github/CODEOWNERS`](.github/CODEOWNERS) with your GitHub handle, for example `/examples/<category>/<sample>/ @your-handle`. This is how you identify yourself as a maintainer for your example. When the nightly run against Newton `main` fails, the issue is assigned to the owners of the failing sample.
To register as a contributor, please add your sample's folder to [`.github/CODEOWNERS`](.github/CODEOWNERS) with your GitHub handle, for example `/examples/<topic>/<example_folder>/ @your-handle`. This is how you identify yourself as a maintainer for your example. When the nightly run against Newton `main` fails, the issue is assigned to the owners of the failing sample.

For example, the existing sample and a newly contributed sample would look like:

```text
# Existing sample
/examples/multiphysics/franka_pulley_mechanical_advantage/ @felixmey
# New sample: replace the topic, example folder, and handle with your own
/examples/rigid_body/my_new_example/ @your-handle
```

PR CI checks every sample, including existing ones. Each sample must live at
`examples/<topic>/<example_folder>/` with its `example_*.py` entry point directly
inside that folder. Tests, images, and other supporting files belong inside the
same folder. Only `examples/__init__.py`, `examples/__main__.py`, and topic-level
`__init__.py` files are allowed outside sample folders.

Each sample must have an exact, root-anchored folder entry with a trailing `/`
and at least one GitHub owner in CODEOWNERS, as shown above. A wildcard or
topic-wide rule does not satisfy this check.


## Remediation Process
Expand Down