Skip to content

Commit

Permalink
Add script to update or init a repository (#2)
Browse files Browse the repository at this point in the history
I'm gonna try it out a bit now on existing repositories…

Testing in
* rerun-io/cpp-example-vrs#10
* rerun-io/example-rs-github-stars#2
  • Loading branch information
emilk authored Apr 16, 2024
1 parent ede62b4 commit 340e5b4
Show file tree
Hide file tree
Showing 17 changed files with 216 additions and 27 deletions.
1 change: 1 addition & 0 deletions .github/workflows/cpp.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copied from https://github.com/rerun-io/rerun_template
on: [push, pull_request]

name: C++
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/labels.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Copied from https://github.com/rerun-io/rerun_template

# https://github.com/marketplace/actions/require-labels
# Check for existence of labels
# See all our labels at https://github.com/rerun-io/rerun/issues/labels
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/links.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copied from https://github.com/rerun-io/rerun_template
on: [push, pull_request]

name: Link checker
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/python.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copied from https://github.com/rerun-io/rerun_template
on: [push, pull_request]

name: C++
Expand Down
9 changes: 8 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copied from https://github.com/rerun-io/rerun_template
on: [push, pull_request]

name: Rust
Expand Down Expand Up @@ -78,7 +79,13 @@ jobs:
command: doc
args: --document-private-items --no-deps --all-features

- name: Test
- name: Build tests
uses: actions-rs/cargo@v1
with:
command: test
args: --all-features --no-run

- name: Run test
uses: actions-rs/cargo@v1
with:
command: test
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/typos.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Copied from https://github.com/rerun-io/rerun_template

# https://github.com/crate-ci/typos
# Add exceptions to `.typos.toml`
# install and run locally: cargo install typos-cli && typos
Expand Down
9 changes: 5 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
# C++ build directory
build

# Rust compile target directory:
**/target
**/target_ra
**/target_wasm
# Rust compile target directories:
target
target_ra
target_wasm

# https://github.com/lycheeverse/lychee
.lycheecache

# Pixi environment
.pixi
.ruff_cache
2 changes: 2 additions & 0 deletions Cranky.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Copied from https://github.com/rerun-io/rerun_template
#
# https://github.com/ericseppanen/cargo-cranky
# cargo install cargo-cranky && cargo cranky
# See also clippy.toml
Expand Down
2 changes: 1 addition & 1 deletion LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2022 Rerun Technologies AB <[email protected]>
Copyright (c) 2024 Rerun Technologies AB <[email protected]>

Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,12 @@ This template includes
## How to use
Start by clicking "Use this template" at https://github.com/rerun-io/rerun_template/ or follow [these instructions](https://docs.github.com/en/free-pro-team@latest/github/creating-cloning-and-archiving-repositories/creating-a-repository-from-a-template).

Then search for `TODO` and fill in all those places (including changing the whole contents of this `README.md`, obviously).
Then follow these steps:
* Search and replace `rerun_template` with the name of the repository
* Run `scripts/template_update.py init --languages cpp,rust,python` to delete files you don't need (give the languages you need support for)
* Search for `TODO` and fill in all those places
* Replace this `README.md` with something better
* Commit!

In the future you can always update this repository with the latest changes from the template by running:
* `scripts/template_update.py update --languages cpp,rust,python`
24 changes: 8 additions & 16 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Copied from https://github.com/rerun-io/rerun_template
#
# There is also a scripts/clippy_wasm/clippy.toml which forbids some methods that are not available in wasm.

# -----------------------------------------------------------------------------
# Section identical to the main scripts/clippy_wasm/clippy.toml:
# Section identical to scripts/clippy_wasm/clippy.toml:

msrv = "1.76"

Expand All @@ -11,32 +13,22 @@ allow-unwrap-in-tests = true
# We want suggestions, even if it changes public API.
avoid-breaking-exported-api = false

excessive-nesting-threshold = 16 # TODO(emilk): lower this
excessive-nesting-threshold = 8

max-fn-params-bools = 2 # TODO(emilk): decrease this to 1
max-fn-params-bools = 1

# https://rust-lang.github.io/rust-clippy/master/index.html#/large_include_file
max-include-file-size = 1000000

# https://rust-lang.github.io/rust-clippy/master/index.html#/large_stack_frames
stack-size-threshold = 512000

too-many-lines-threshold = 600 # TODO(emilk): decrease this
too-many-lines-threshold = 200

# -----------------------------------------------------------------------------

# https://rust-lang.github.io/rust-clippy/master/index.html#disallowed_macros
disallowed-macros = [
'dbg',

# TODO(emilk): consider forbidding these to encourage the use of proper log stream, and then explicitly allow legitimate uses
# 'std::eprint',
# 'std::eprintln',
# 'std::print',
# 'std::println',

# 'std::unimplemented', # generated by ArrowDeserialize derive-macro :(
]
disallowed-macros = ['dbg']

# https://rust-lang.github.io/rust-clippy/master/index.html#disallowed_methods
disallowed-methods = [
Expand All @@ -50,7 +42,7 @@ disallowed-methods = [

# There are many things that aren't allowed on wasm,
# but we cannot disable them all here (because of e.g. https://github.com/rust-lang/rust-clippy/issues/10406)
# so we do that in `clipppy_wasm.toml` instead.
# so we do that in `scripts/clippy_wasm/clippy.toml` instead.
]

# https://rust-lang.github.io/rust-clippy/master/index.html#disallowed_names
Expand Down
2 changes: 2 additions & 0 deletions deny.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Copied from https://github.com/rerun-io/rerun_template
#
# https://github.com/EmbarkStudios/cargo-deny
#
# cargo-deny checks our dependency tree for copy-left licenses,
Expand Down
2 changes: 2 additions & 0 deletions lychee.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Copied from https://github.com/rerun-io/rerun_template

################################################################################
# Config for the link checker lychee.
#
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Copied from https://github.com/rerun-io/rerun_template

[tool.ruff]
lint.ignore = [
# Missing docstring in public function - TODO(emilk): enable for SDK but not for examples
Expand Down
10 changes: 6 additions & 4 deletions scripts/clippy_wasm/clippy.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# This is used by `scripts/clippy_wasm.sh` so we can forbid some methods that are not available in wasm.
# Copied from https://github.com/rerun-io/rerun_template

# This is used by the CI so we can forbid some methods that are not available in wasm.
#
# We cannot forbid all these methods in the main `clippy.toml` because of
# https://github.com/rust-lang/rust-clippy/issues/10406
Expand All @@ -14,14 +16,14 @@ allow-unwrap-in-tests = true
# We want suggestions, even if it changes public API.
avoid-breaking-exported-api = false

excessive-nesting-threshold = 16 # TODO(emilk): lower this
excessive-nesting-threshold = 8

max-fn-params-bools = 2 # TODO(emilk): decrease this to 1
max-fn-params-bools = 1

# https://rust-lang.github.io/rust-clippy/master/index.html#/large_include_file
max-include-file-size = 1000000

too-many-lines-threshold = 600 # TODO(emilk): decrease this
too-many-lines-threshold = 200

# -----------------------------------------------------------------------------

Expand Down
1 change: 1 addition & 0 deletions scripts/generate_changelog.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python3
# Copied from https://github.com/rerun-io/rerun_template

"""
Summarizes recent PRs based on their GitHub labels.
Expand Down
163 changes: 163 additions & 0 deletions scripts/template_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
#!/usr/bin/env python3
# Copied from https://github.com/rerun-io/rerun_template

"""
The script has two purposes.
After using `rerun_template` as a template, run this to clean out things you don't need.
Use `scripts/template_update.py init --languages cpp,rust,python` for this.
Update an existing repository with the latest changes from the template.
Use `scripts/template_update.py update --languages cpp,rust,python` for this.
In either case, make sure the list of languages matches the languages you want to support.
You can also use `--dry-run` to see what would happen without actually changing anything.
"""

import argparse
import os
import shutil
import tempfile
from typing import Set

from git import Repo

OWNER = "rerun-io"

# Files requires by C++, but not by both Python or Rust.
CPP_FILES = {
".clang-format",
".github/workflows/cpp.yml",
"CMakeLists.txt",
"pixi.lock", # Not needed by Rust
"pixi.toml", # Not needed by Rust
"src/main.cpp",
"src/",
}

# Files requires by Python, but not by both C++ or Rust
PYTHON_FILES = {
".github/workflows/python.yml",
".mypy.ini",
"pixi.lock", # Not needed by Rust
"pixi.toml", # Not needed by Rust
"pyproject.toml",
}

# Files requires by Rust, but not by both C++ or Python
RUST_FILES = {
".github/workflows/rust.yml",
"bacon.toml",
"Cargo.lock",
"Cargo.toml",
"clippy.toml",
"Cranky.toml",
"deny.toml",
"rust-toolchain",
"scripts/clippy_wasm/",
"scripts/clippy_wasm/clippy.toml",
"src/lib.rs",
"src/main.rs",
"src/",
}


def parse_languages(lang_str: str) -> Set[str]:
languages = lang_str.split(",") if lang_str else []
for lang in languages:
assert lang in ["cpp", "python", "rust"], f"Unsupported language: {lang}"
return set(languages)


def calc_deny_set(languages: Set[str]) -> Set[str]:
"""The set of files to delete/ignore."""
files_to_delete = CPP_FILES | PYTHON_FILES | RUST_FILES
if "cpp" in languages:
files_to_delete -= CPP_FILES
if "python" in languages:
files_to_delete -= PYTHON_FILES
if "rust" in languages:
files_to_delete -= RUST_FILES
return files_to_delete


def init(languages: Set[str], dry_run: bool) -> None:
print("Removing all language-specific files not needed for languages {languages}.")
files_to_delete = calc_deny_set(languages)
delete_files_and_folder(files_to_delete, dry_run)


def delete_files_and_folder(paths: Set[str], dry_run: bool) -> None:
repo_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
for path in paths:
full_path = os.path.join(repo_path, path)
if os.path.exists(full_path):
if os.path.isfile(full_path):
print(f"Removing file {full_path}…")
if not dry_run:
os.remove(full_path)
elif os.path.isdir(full_path):
print(f"Removing folder {full_path}…")
if not dry_run:
shutil.rmtree(full_path)


def update(languages: Set[str], dry_run: bool) -> None:
files_to_ignore = calc_deny_set(languages)
repo_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))

with tempfile.TemporaryDirectory() as temp_dir:
Repo.clone_from("https://github.com/rerun-io/rerun_template.git", temp_dir)
for root, dirs, files in os.walk(temp_dir):
for file in files:
src_path = os.path.join(root, file)
rel_path = os.path.relpath(src_path, temp_dir)

if rel_path.startswith(".git/"):
continue
if rel_path.startswith("src/"):
continue

if rel_path in {"README.md", "pixi.lock", "Cargo.lock"}:
continue

if rel_path not in files_to_ignore:
dest_path = os.path.join(repo_path, rel_path)

print(f"Updating {rel_path}…")
if not dry_run:
os.makedirs(os.path.dirname(dest_path), exist_ok=True)
shutil.copy2(src_path, dest_path)


def main() -> None:
parser = argparse.ArgumentParser(description="Handle the Rerun template.")
subparsers = parser.add_subparsers(dest="command")

init_parser = subparsers.add_parser("init", help="Initialize a new checkout of the template.")
init_parser.add_argument(
"--languages", default="", nargs="?", const="", help="The languages to support (e.g. `cpp,python,rust`)."
)
init_parser.add_argument("--dry-run", action="store_true", help="Don't actually delete any files.")

update_parser = subparsers.add_parser(
"update", help="Update all existing Rerun repositories with the latest changes from the template"
)
update_parser.add_argument(
"--languages", default="", nargs="?", const="", help="The languages to support (e.g. `cpp,python,rust`)."
)
update_parser.add_argument("--dry-run", action="store_true", help="Don't actually delete any files.")

args = parser.parse_args()

if args.command == "init":
init(parse_languages(args.languages), args.dry_run)
elif args.command == "update":
update(parse_languages(args.languages), args.dry_run)
else:
parser.print_help()
exit(1)


if __name__ == "__main__":
main()

0 comments on commit 340e5b4

Please sign in to comment.