From 73e97894619a8bee629cc768f85079964dc03067 Mon Sep 17 00:00:00 2001 From: Andrei Durlea Date: Tue, 5 May 2026 01:45:28 +0300 Subject: [PATCH] Change stamp.sh into stamp.py --- README.md | 4 +- cookiecutter/check_cookiecutter.sh | 2 +- .../{{cookiecutter.project_name}}/README.md | 4 +- stamp.py | 38 ++++++++++++++++ stamp.sh | 44 ------------------- 5 files changed, 43 insertions(+), 49 deletions(-) create mode 100644 stamp.py delete mode 100755 stamp.sh diff --git a/README.md b/README.md index 10db53f4..ab9e9dc6 100644 --- a/README.md +++ b/README.md @@ -10,13 +10,13 @@ top-right and select "Create a new repository": This will create a new repository that's an exact copy of exemplar. The next step is to customize it for your use case. -To do so, execute the bash script `stamp.sh`. This script will prompt for parameters like +To do so, execute the python script `stamp.py`. This script will prompt for parameters like the new library's name, paper number, and description. Then it will replace your exemplar copy with a stamped-out template containing these parameters and create a corresponding git commit and branch: ``` -$ ./stamp.sh +$ python stamp.py [1/6] project_name (my_project_name): example_library [2/6] maintainer (your_github_username): your_username [3/6] minimum_cpp_build_version (20): diff --git a/cookiecutter/check_cookiecutter.sh b/cookiecutter/check_cookiecutter.sh index 394cca7e..ea4b4433 100755 --- a/cookiecutter/check_cookiecutter.sh +++ b/cookiecutter/check_cookiecutter.sh @@ -35,7 +35,7 @@ function check_consistency() { cp "$script_dir"/../.github/workflows/todo_exemplar_test.yml "$out_dir_path"/exemplar/.github/workflows mkdir "$out_dir_path"/exemplar/images cp "$script_dir"/../images/use-this-template.png "$out_dir_path"/exemplar/images/use-this-template.png - cp "$script_dir"/../stamp.sh "$out_dir_path"/exemplar/stamp.sh + cp "$script_dir"/../stamp.py "$out_dir_path"/exemplar/stamp.py local diff_path diff_path=$(mktemp) diff -r "$script_dir/.." "$out_dir_path/exemplar" \ diff --git a/cookiecutter/{{cookiecutter.project_name}}/README.md b/cookiecutter/{{cookiecutter.project_name}}/README.md index 527c9086..114300b9 100644 --- a/cookiecutter/{{cookiecutter.project_name}}/README.md +++ b/cookiecutter/{{cookiecutter.project_name}}/README.md @@ -11,13 +11,13 @@ top-right and select "Create a new repository": This will create a new repository that's an exact copy of exemplar. The next step is to customize it for your use case. -To do so, execute the bash script `stamp.sh`. This script will prompt for parameters like +To do so, execute the python script `stamp.py`. This script will prompt for parameters like the new library's name, paper number, and description. Then it will replace your exemplar copy with a stamped-out template containing these parameters and create a corresponding git commit and branch: ``` -$ ./stamp.sh +$ python stamp.py [1/6] project_name (my_project_name): example_library [2/6] maintainer (your_github_username): your_username [3/6] minimum_cpp_build_version (20): diff --git a/stamp.py b/stamp.py new file mode 100644 index 00000000..524cd4d5 --- /dev/null +++ b/stamp.py @@ -0,0 +1,38 @@ +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +#!/usr/bin/env python3 +import os, shutil, subprocess, sys, tempfile +from pathlib import Path + +if "-h" in sys.argv or "--help" in sys.argv: + print("stamp.py -- beman exemplar template library creation tool\n\n" + "This script is intended to be run on a fork of exemplar.\n\n" + "It sets up cookiecutter, runs it on the cookiecutter template, replaces the\n" + "repository's current contents with the result, runs pre-commit,\n" + "switches to a new branch 'stamp', and creates a git commit.\n\n" + "All parameters are passed through to the cookiecutter invocation.") + sys.exit(0) + +repo_dir = Path(__file__).parent.resolve() +os.chdir(repo_dir) + +with tempfile.TemporaryDirectory() as v_dir, tempfile.TemporaryDirectory() as o_dir: + v_dir, o_dir = Path(v_dir), Path(o_dir) + subprocess.run([sys.executable, "-m", "venv", str(v_dir)], check=True) + bin_dir = v_dir / ("Scripts" if os.name == "nt" else "bin") + python = bin_dir / ("python.exe" if os.name == "nt" else "python") + + subprocess.run([str(python), "-m", "pip", "install", "cookiecutter", "pre-commit"], capture_output=True, check=True) + subprocess.run([str(python), "-m", "cookiecutter", str(repo_dir / "cookiecutter"), "-o", str(o_dir)] + sys.argv[1:], check=True) + subprocess.run(["git", "rm", "-rf", "."], capture_output=True, check=True) + gen_dir = next(o_dir.iterdir()) + for item in gen_dir.iterdir(): + if item.is_dir(): shutil.copytree(item, repo_dir / item.name, dirs_exist_ok=True) + else: shutil.copy2(item, repo_dir / item.name) + subprocess.run(["git", "add", "."], capture_output=True, check=True) + pc = bin_dir / ("pre-commit.exe" if os.name == "nt" else "pre-commit") + subprocess.run([str(pc), "run", "--all-files"], capture_output=True) + subprocess.run(["git", "add", "."], capture_output=True, check=True) + subprocess.run(["git", "checkout", "-b", "stamp"], check=True) + subprocess.run(["git", "commit", "-q", "-m", "Stamp out exemplar template"], check=True) + print("Successfully stamped out exemplar template to the new branch 'stamp'.") + print("Try 'git push origin stamp' to push the branch upstream,\nthen create a pull request.") diff --git a/stamp.sh b/stamp.sh deleted file mode 100755 index 7dc9ee84..00000000 --- a/stamp.sh +++ /dev/null @@ -1,44 +0,0 @@ -# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -#!/usr/bin/env bash - -{ - if [[ "$1" == "-h" || "$1" == "--help" ]] ; then - cat <<-'EOF' - stamp.sh -- beman exemplar template library creation tool - - This script is intended to be run on a fork of exemplar. - - It sets up cookiecutter, runs it on the cookiecutter template, replaces the - repository's current contents with the result, runs pre-commit, - switches to a new branch 'stamp', and creates a git commit. - - All parameters are passed through to the cookiecutter invocation. -EOF - fi - set -eu - if ! type -P python3 >/dev/null ; then - echo "Couldn't find python3 in PATH" >&2 - exit 1 - fi - declare repo_dir=$(realpath $(dirname "$BASH_SOURCE")) - cd "$repo_dir" - declare cookiecutter_venv_path - cookiecutter_venv_path=$(mktemp --directory --dry-run) - python3 -m venv "$cookiecutter_venv_path" - source "$cookiecutter_venv_path/bin/activate" - python3 -m pip install cookiecutter pre-commit >& /dev/null - declare cookiecutter_out_path - cookiecutter_out_path=$(mktemp --directory) - python3 -m cookiecutter "$repo_dir/cookiecutter" -o "$cookiecutter_out_path" "$@" - git rm -rf . &>/dev/null - cp -r "$cookiecutter_out_path"/*/. . - git add . &>/dev/null - pre-commit run --all-files &>/dev/null || true - git add . &>/dev/null - git checkout -b stamp - git commit -q -m "Stamp out exemplar template" - echo "Successfully stamped out exemplar template to the new branch 'stamp'." - echo "Try 'git push origin stamp' to push the branch upstream," - echo "then create a pull request." - rm -r "$cookiecutter_venv_path" "$cookiecutter_out_path" -}; exit