From 37c775e039df18324fb2b3e1b49206649d3bcc5a Mon Sep 17 00:00:00 2001 From: Matthew Watkins Date: Fri, 19 Jan 2024 16:14:32 +0000 Subject: [PATCH 1/2] Chore: Make template substitutions [skip ci] Signed-off-by: Matthew Watkins --- CONTRIBUTING.rst | 26 +++--- README.rst | 12 +-- docs/conf.py | 10 +-- docs/index.rst | 4 +- pyproject.toml | 2 +- scripts/rename-tests.sh | 24 ++++++ scripts/template-to-repo.sh | 127 +++++++++++++++++++++++++++++ src/osc_data_extractor/__init__.py | 2 +- src/osc_data_extractor/skeleton.py | 10 +-- tests/conftest.py | 2 +- tests/osc_data_extractor_test.py | 2 +- 11 files changed, 186 insertions(+), 35 deletions(-) create mode 100755 scripts/rename-tests.sh create mode 100755 scripts/template-to-repo.sh diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst index 1086ea2..1598194 100644 --- a/CONTRIBUTING.rst +++ b/CONTRIBUTING.rst @@ -27,7 +27,7 @@ Contributing ============ -Welcome to ``osc-data-extractor`` contributor's guide. +Welcome to ``osc-transformer-based-extractor`` contributor's guide. This document focuses on getting any potential contributor familiarized with the development processes, but `other kinds of contributions`_ are also @@ -46,7 +46,7 @@ guidelines. Issue Reports ============= -If you experience bugs or general issues with ``osc-data-extractor``, please have a look +If you experience bugs or general issues with ``osc-transformer-based-extractor``, please have a look on the `issue tracker`_. If you don't see anything useful there, please feel free to fire an issue report. @@ -65,10 +65,10 @@ you help us to identify the root cause of the issue. Documentation Improvements ========================== -You can help improve ``osc-data-extractor`` docs by making them more readable and coherent, or +You can help improve ``osc-transformer-based-extractor`` docs by making them more readable and coherent, or by adding missing information and correcting mistakes. -``osc-data-extractor`` documentation uses Sphinx_ as its main documentation compiler. +``osc-transformer-based-extractor`` documentation uses Sphinx_ as its main documentation compiler. This means that the docs are kept in the same repository as the project code, and that any documentation update is done in the same way was a code contribution. @@ -80,7 +80,7 @@ that any documentation update is done in the same way was a code contribution. .. tip:: Please notice that the `GitHub web interface`_ provides a quick way of - propose changes in ``osc-data-extractor``'s files. While this mechanism can + propose changes in ``osc-transformer-based-extractor``'s files. While this mechanism can be tricky for normal code contributions, it works perfectly fine for contributing to the docs, and can be quite handy. @@ -131,8 +131,8 @@ This can easily be done via either |virtualenv|_:: or Miniconda_:: - conda create -n osc-data-extractor python=3 six virtualenv pytest pytest-cov - conda activate osc-data-extractor + conda create -n osc-transformer-based-extractor python=3 six virtualenv pytest pytest-cov + conda activate osc-transformer-based-extractor Clone the repository -------------------- @@ -142,8 +142,8 @@ Clone the repository page. This creates a copy of the code under your account on |the repository service|. #. Clone this copy to your local disk:: - git clone git@github.com:YourLogin/osc-data-extractor.git - cd osc-data-extractor + git clone git@github.com:YourLogin/osc-transformer-based-extractor.git + cd osc-transformer-based-extractor #. You should run:: @@ -158,7 +158,7 @@ Clone the repository pip install pre-commit pre-commit install - ``osc-data-extractor`` comes with a lot of hooks configured to automatically help the + ``osc-transformer-based-extractor`` comes with a lot of hooks configured to automatically help the developer to check the code being written. Implement your changes @@ -285,7 +285,7 @@ Releases If you are part of the group of maintainers and have correct user permissions on PyPI_, the following steps can be used to release a new version for -``osc-data-extractor``: +``osc-transformer-based-extractor``: #. Make sure all unit tests are successful. #. Tag the current commit on the main branch with a release tag, e.g., ``v1.2.3``. @@ -314,8 +314,8 @@ on PyPI_, the following steps can be used to release a new version for .. |the repository service| replace:: GitHub .. |contribute button| replace:: "Create pull request" -.. _repository: https://github.com//osc-data-extractor -.. _issue tracker: https://github.com//osc-data-extractor/issues +.. _repository: https://github.com//osc-transformer-based-extractor +.. _issue tracker: https://github.com//osc-transformer-based-extractor/issues .. <-- end --> diff --git a/README.rst b/README.rst index 6b09619..f17b1b0 100644 --- a/README.rst +++ b/README.rst @@ -9,15 +9,15 @@ .. image:: https://img.shields.io/badge/GitHub-100000?logo=github&logoColor=white :alt: Source code on GitHub - :target: https://github.com/ModeSevenIndustrialSolutions/osc-data-extractor + :target: https://github.com/ModeSevenIndustrialSolutions/osc-transformer-based-extractor -.. image:: https://img.shields.io/pypi/v/osc-data-extractor.svg +.. image:: https://img.shields.io/pypi/v/osc-transformer-based-extractor.svg :alt: PyPI package - :target: https://pypi.org/project/osc-data-extractor/ + :target: https://pypi.org/project/osc-transformer-based-extractor/ -.. image:: https://api.cirrus-ci.com/github/os-climate/osc-data-extractor.svg?branch=main +.. image:: https://api.cirrus-ci.com/github/os-climate/osc-transformer-based-extractor.svg?branch=main :alt: Built Status - :target: https://cirrus-ci.com/github/os-climate/osc-data-extractor + :target: https://cirrus-ci.com/github/os-climate/osc-transformer-based-extractor .. image:: https://img.shields.io/badge/PDM-Project-purple :alt: Built using PDM @@ -30,7 +30,7 @@ ================== -osc-data-extractor +osc-transformer-based-extractor ================== OS-Climate Data Extraction Tool diff --git a/docs/conf.py b/docs/conf.py index ef395b4..d7b2062 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -34,7 +34,7 @@ from sphinx import apidoc output_dir = os.path.join(__location__, "api") -module_dir = os.path.join(__location__, "../src/osc_data_extractor") +module_dir = os.path.join(__location__, "../src/osc_transformer_based_extractor") try: shutil.rmtree(output_dir) except FileNotFoundError: @@ -87,7 +87,7 @@ master_doc = "index" # General information about the project. -project = "osc-data-extractor" +project = "osc-transformer-based-extractor" copyright = "2023, Matthew Watkins" # The version info for the project you're documenting, acts as replacement for @@ -99,7 +99,7 @@ # If you don’t need the separation provided between version and release, # just set them both to the same value. try: - from osc_data_extractor import __version__ as version + from osc_transformer_based_extractor import __version__ as version except ImportError: version = "" @@ -229,7 +229,7 @@ # html_file_suffix = None # Output file base name for HTML help builder. -htmlhelp_basename = "osc-data-extractor-doc" +htmlhelp_basename = "osc-transformer-based-extractor-doc" # -- Options for LaTeX output ------------------------------------------------ @@ -246,7 +246,7 @@ # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, author, documentclass [howto/manual]). latex_documents = [ - ("index", "user_guide.tex", "osc-data-extractor Documentation", "Matthew Watkins", "manual") + ("index", "user_guide.tex", "osc-transformer-based-extractor Documentation", "Matthew Watkins", "manual") ] # The name of an image file (relative to this directory) to place at the top of diff --git a/docs/index.rst b/docs/index.rst index 0d1c364..3416d47 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -1,8 +1,8 @@ ================== -osc-data-extractor +osc-transformer-based-extractor ================== -This is the documentation of **osc-data-extractor**. +This is the documentation of **osc-transformer-based-extractor**. .. note:: diff --git a/pyproject.toml b/pyproject.toml index 6e6c22b..3dd685c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -42,7 +42,7 @@ build-backend = "pdm.backend" license-files = ["LICENSES.txt"] [project.scripts] -osc-transformer-based-extractor = "osc_data_extractor.skeleton:run" +osc-transformer-based-extractor = "osc_transformer_based_extractor.skeleton:run" [project.optional-dependencies] dev = [ diff --git a/scripts/rename-tests.sh b/scripts/rename-tests.sh new file mode 100755 index 0000000..2f03b0b --- /dev/null +++ b/scripts/rename-tests.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +#set -x + +REPO_NAME=$(basename "$(git rev-parse --show-toplevel)") +echo "Repository name: $REPO_NAME" + +if [ $# -ne 1 ]; then + echo "Usage: $0 [test folder]"; exit 1 +elif [ ! -d "$1" ]; then + echo "Error: specified target was not a folder"; exit 1 +else + # Target specified was a folder + TARGET="$1" +fi + +for TEST in $(find "$TARGET" -type f -name '*_test.py' | xargs -0); do + echo "Processing: $TEST" + FILE_PATH=$(dirname "$TEST") + FILE_NAME=$(basename "$TEST") + STRIPPED="${FILE_NAME//_test.py/.py}" + echo " git mv \"${TEST}\" $FILE_PATH/test_\"${STRIPPED%%}\"" + git mv "${TEST}" "$FILE_PATH"/test_"${STRIPPED%%}" +done diff --git a/scripts/template-to-repo.sh b/scripts/template-to-repo.sh new file mode 100755 index 0000000..f29f65b --- /dev/null +++ b/scripts/template-to-repo.sh @@ -0,0 +1,127 @@ +#!/bin/bash + +# set -x + +### Shared functions + +# Renames files/folders containing template name +rename_object() { + if [ $# -ne 1 ]; then + echo "Function requires an argumeent: rename_object [filesystem object]"; exit 1 + else + FS_OBJECT="$1" + fi + # Function take a filesystem object as a single argument + FS_OBJECT="$1" + OBJECT_PATH=$(dirname "$FS_OBJECT") + OBJECT_NAME=$(basename "$FS_OBJECT") + + # Check if filesystem object contains template name + if [[ ! "$OBJECT_NAME" == *"$TEMPLATE_NAME"* ]]; then + # Nothing to do; abort early + return + else + NEW_NAME="${OBJECT_NAME//$TEMPLATE_NAME/$REPO_NAME}" + fi + if [[ ! "$OBJECT_NAME" == *"$ALT_TEMPLATE_NAME"* ]]; then + # Nothing to do; abort early + return + else + NEW_NAME="${OBJECT_NAME//$ALT_TEMPLATE_NAME/$ALT_REPO_NAME}" + fi + + # Perform the renaming operation + if [ -d "$FS_OBJECT" ]; then + echo "Renaming folder: $FS_OBJECT" + elif [ -f "$FS_OBJECT" ]; then + echo "Renaming file: $FS_OBJECT" + elif [ -L "$FS_OBJECT" ]; then + echo "Renaming symlink: $FS_OBJECT" + fi + git mv "$OBJECT_PATH/$OBJECT_NAME" "$OBJECT_PATH/$NEW_NAME" +} + +# Checks file content for template name and replaces matching strings +file_content_substitution() { + if [ $# -ne 1 ]; then + echo "Function requires an argument: file_content_substitution [filename]"; exit 1 + else + FILENAME="$1" + fi + COUNT=0 + if (grep "$TEMPLATE_NAME" "$FILENAME" > /dev/null 2>&1); then + MATCHES=$(grep -c "$TEMPLATE_NAME" "$FILENAME") + if [ "$MATCHES" -eq 1 ]; then + echo "1 content substitution required: $FILENAME (dashes)" + COUNT=$((COUNT++)) + else + echo "$MATCHES content substitutions required: $FILENAME (dashes)" + COUNT=$((COUNT+MATCHES)) + fi + sed -i "s/$TEMPLATE_NAME/$REPO_NAME/g" "$FILENAME" + fi + if (grep "$ALT_TEMPLATE_NAME" "$FILENAME" > /dev/null 2>&1); then + MATCHES=$(grep -c "$ALT_TEMPLATE_NAME" "$FILENAME") + if [ "$MATCHES" -eq 1 ]; then + echo "1 content substitution required: $FILENAME (underscores)" + COUNT=$((COUNT++)) + else + echo "$MATCHES content substitutions required: $FILENAME (underscores)" + COUNT=$((COUNT+MATCHES)) + fi + sed -i "s/$ALT_TEMPLATE_NAME/$ALT_REPO_NAME/g" "$FILENAME" + fi + if [[ "$COUNT" != "0" ]] && [[ "$COUNT" = "1" ]]; then + echo "$COUNT substitution made in file: $FILENAME" + elif [[ "$COUNT" != "0" ]] && [[ "$COUNT" -gt "1" ]]; then + echo "$COUNT substitutions made in file: $FILENAME" + fi +} + +### Main script entry point + +TEMPLATE_NAME=osc-transformer-based-extractor +ALT_TEMPLATE_NAME="${TEMPLATE_NAME//-/_}" + +if ! (git rev-parse --show-toplevel > /dev/null); then + echo "Error: this folder is not part of a GIT repository"; exit 1 +fi + +REPO_DIR=$(git rev-parse --show-toplevel) +REPO_NAME=$(basename "$REPO_DIR") +ALT_REPO_NAME="${REPO_NAME//-/_}" + +if [ "$TEMPLATE_NAME" == "$REPO_NAME" ]; then + echo "WARNING: template name matches repository name" +else + echo "Template name: $TEMPLATE_NAME" + echo "Alternate name: $ALT_TEMPLATE_NAME" + echo "Repository name: $REPO_NAME" + echo "Alternate name: $ALT_REPO_NAME" +fi + +# Change to top-level of GIT repository +CURRENT_DIR=$(pwd) +if [ "$REPO_DIR" != "$CURRENT_DIR" ]; then + echo "Changing directory to: $REPO_DIR" + if ! (cd "$REPO_DIR"); then + echo "Could not change directory!"; exit 1 + fi +fi + +echo "Processing repository contents..." + +# Rename directories first, as they affect file paths afterwards +for FS_OBJECT in $(find -- * -type d | xargs -0); do + rename_object "$FS_OBJECT" + if [ -f "$FS_OBJECT" ]; then + file_content_substitution "$FS_OBJECT" + fi +done + +for FS_OBJECT in $(find -- * -type f | xargs -0); do + rename_object "$FS_OBJECT" + if [ -f "$FS_OBJECT" ]; then + file_content_substitution "$FS_OBJECT" + fi +done diff --git a/src/osc_data_extractor/__init__.py b/src/osc_data_extractor/__init__.py index f4255d2..93bb088 100644 --- a/src/osc_data_extractor/__init__.py +++ b/src/osc_data_extractor/__init__.py @@ -8,7 +8,7 @@ try: # Change here if project is renamed and does not equal the package name - dist_name = "osc-data-extractor" + dist_name = "osc-transformer-based-extractor" __version__ = version(dist_name) except PackageNotFoundError: # pragma: no cover __version__ = "unknown" diff --git a/src/osc_data_extractor/skeleton.py b/src/osc_data_extractor/skeleton.py index 03b4206..99735d5 100644 --- a/src/osc_data_extractor/skeleton.py +++ b/src/osc_data_extractor/skeleton.py @@ -4,7 +4,7 @@ ``[options.entry_points]`` section in ``setup.cfg``:: console_scripts = - fibonacci = osc_data_extractor.skeleton:run + fibonacci = osc_transformer_based_extractor.skeleton:run Then run ``pip install .`` (or ``pip install -e .`` for editable mode) which will install the command ``fibonacci`` inside your current environment. @@ -24,7 +24,7 @@ import logging import sys -from osc_data_extractor import __version__ +from osc_transformer_based_extractor import __version__ __author__ = "Matthew Watkins" __copyright__ = "Matthew Watkins" @@ -36,7 +36,7 @@ # ---- Python API ---- # The functions defined in this section can be imported by users in their # Python scripts/interactive interpreter, e.g. via -# `from osc_data_extractor.skeleton import fib`, +# `from osc_transformer_based_extractor.skeleton import fib`, # when using this Python module as a library. @@ -76,7 +76,7 @@ def parse_args(args): parser.add_argument( "--version", action="version", - version=f"osc-data-extractor {__version__}", + version=f"osc-transformer-based-extractor {__version__}", ) parser.add_argument(dest="n", help="n-th Fibonacci number", type=int, metavar="INT") parser.add_argument( @@ -142,6 +142,6 @@ def run(): # After installing your project with pip, users can also run your Python # modules as scripts via the ``-m`` flag, as defined in PEP 338:: # - # python -m osc_data_extractor.skeleton 42 + # python -m osc_transformer_based_extractor.skeleton 42 # run() diff --git a/tests/conftest.py b/tests/conftest.py index 7a41d0d..fa181dd 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,5 +1,5 @@ """ - Dummy conftest.py for osc_data_extractor. + Dummy conftest.py for osc_transformer_based_extractor. If you don't know what this is for, just leave it empty. Read more about conftest.py under: diff --git a/tests/osc_data_extractor_test.py b/tests/osc_data_extractor_test.py index c39011f..7d2ee26 100644 --- a/tests/osc_data_extractor_test.py +++ b/tests/osc_data_extractor_test.py @@ -1,6 +1,6 @@ import pytest -from osc_data_extractor.skeleton import fib, main +from osc_transformer_based_extractor.skeleton import fib, main __author__ = "Matthew Watkins" __copyright__ = "Matthew Watkins" From 03956ccf6638e0ab9c4266dbd160eb18a6221170 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 19 Jan 2024 16:15:04 +0000 Subject: [PATCH 2/2] Chore: pre-commit autoupdate --- tests/osc_data_extractor_test.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/osc_data_extractor_test.py b/tests/osc_data_extractor_test.py index 7d2ee26..c13407f 100644 --- a/tests/osc_data_extractor_test.py +++ b/tests/osc_data_extractor_test.py @@ -1,5 +1,4 @@ import pytest - from osc_transformer_based_extractor.skeleton import fib, main __author__ = "Matthew Watkins"