From a0b83f4f881d7daa0a5bc4d6cd540faf16a3f89d Mon Sep 17 00:00:00 2001 From: oakkitten Date: Mon, 23 May 2022 17:03:44 +0100 Subject: [PATCH] Slightly refactor GitHub workflows & tools MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Remove unused code * Future-proof version regexes * Fixup wraps * Make tests run on tags—again --- .github/workflows/fast-forward.yml | 2 +- .github/workflows/tests.yml | 16 +++++++++++----- .github/workflows/update.py | 21 ++++----------------- .github/workflows/update.yml | 6 +++--- anki_wallpaper/tools.py | 9 +++------ 5 files changed, 22 insertions(+), 32 deletions(-) diff --git a/.github/workflows/fast-forward.yml b/.github/workflows/fast-forward.yml index 658f468..0b12391 100644 --- a/.github/workflows/fast-forward.yml +++ b/.github/workflows/fast-forward.yml @@ -6,7 +6,7 @@ on: jobs: fast_forward: - name: Fast-forward + name: \#${{ github.event.number }} runs-on: ubuntu-latest if: github.event.label.name == 'fast-forward' diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index cbe37d4..1831cad 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -4,6 +4,8 @@ on: push: branches-ignore: - update-test-environment/* + tags: + - '**' pull_request: workflow_dispatch: @@ -64,18 +66,22 @@ jobs: - name: Check out repository uses: actions/checkout@v2 + # https://github.com/actions/checkout/issues/290 + - name: Fetch tag annotations + run: git fetch --tags --force + - name: Assemble the add-on - run: | + run: | export VERSION="$(git describe --long)" cd anki_wallpaper sed -i -r "s/# Wallpaper/\0 $VERSION<\/version>/" config.md - zip -r ../anki_wallpaper.ankiaddon * - echo ASSET_NAME="anki_wallpaper-$VERSION.ankiaddon" >> "$GITHUB_ENV" + zip -r ../anki-wallpaper.ankiaddon * + echo ASSET_NAME="anki-wallpaper-$VERSION.ankiaddon" >> "$GITHUB_ENV" - name: Store artifact - uses: svenstaro/upload-release-action@1.0.1 + uses: svenstaro/upload-release-action@v2 with: tag: ${{ github.ref }} repo_token: ${{ secrets.GITHUB_TOKEN }} - file: anki_wallpaper.ankiaddon + file: anki-wallpaper.ankiaddon asset_name: ${{ env.ASSET_NAME }} diff --git a/.github/workflows/update.py b/.github/workflows/update.py index cd072b7..1c5b9c4 100644 --- a/.github/workflows/update.py +++ b/.github/workflows/update.py @@ -1,5 +1,3 @@ -import subprocess -import shlex import re import sys from contextlib import contextmanager @@ -12,14 +10,6 @@ def log(*args): print(*args, file=sys.stderr) -def run(command: str) -> str: - return ( - subprocess.run(shlex.split(command), capture_output=True, check=True) - .stdout - .decode() - ) - - def remove_two_edge_newlines(text: str) -> str: if text.startswith("\n"): text = text[1:] @@ -39,12 +29,9 @@ def insert_before_line(self, line_re: str, addition: str): lines = [] for line in self.contents.split("\n"): if re.search(line_re, line): - leading_spaces = len(line) - len(line.lstrip()) + leading_spaces = " " * (len(line) - len(line.lstrip())) for new_line in remove_two_edge_newlines(dedent(addition)).split("\n"): - if new_line.strip(): - lines.append(" " * leading_spaces + new_line) - else: - lines.append("") + lines.append(leading_spaces + new_line if new_line.strip() else "") lines.append(line) self.contents = "\n".join(lines) @@ -67,13 +54,13 @@ def update_prerelease(tox_ini: File, tests_yml: File, version): log(f":: updating pre-release test environment with Anki {version}") tox_ini.contents = re.sub( - r"ankipre: (anki|aqt\[qt6])==\d+\.\d+\.[a-z0-9]+", + r"ankipre: (anki|aqt\[qt6])==[\d.a-z]+", fr"ankipre: \1=={version}", tox_ini.contents ) tests_yml.contents = re.sub( - r"Pre-release \(\d+\.\d+\.[a-z0-9]+\)", + r"Pre-release \([\d.a-z]+\)", fr"Pre-release ({version})", tests_yml.contents ) diff --git a/.github/workflows/update.yml b/.github/workflows/update.yml index acfcb52..1007344 100644 --- a/.github/workflows/update.yml +++ b/.github/workflows/update.yml @@ -44,8 +44,8 @@ jobs: if: matrix.environment == 'pre-release' continue-on-error: true run: | - export PRERELEASE=$(pip index --pre versions aqt | perl -ne 'm/aqt \((2.1.[a-z0-9]+)\)/ && print "$1"') - export STABLE=$(pip index versions aqt | perl -ne 'm/aqt \((2.1.[a-z0-9]+)\)/ && print "$1"') + export PRERELEASE=$(pip index --pre versions aqt | perl -ne 'm/aqt \(([\d.a-z]+)\)/ && print "$1"') + export STABLE=$(pip index versions aqt | perl -ne 'm/aqt \(([\d.a-z]+)\)/ && print "$1"') if [ "$PRERELEASE" = "$STABLE" ]; then exit 3; fi python .github/workflows/update.py update-prerelease $PRERELEASE \ && git add tox.ini .github/workflows/tests.yml \ @@ -56,7 +56,7 @@ jobs: if: matrix.environment == 'stable' continue-on-error: true run: | - export STABLE=$(pip index versions aqt | perl -ne 'm/aqt \((2.1.[a-z0-9]+)\)/ && print "$1"') + export STABLE=$(pip index versions aqt | perl -ne 'm/aqt \(([\d.a-z]+)\)/ && print "$1"') python .github/workflows/update.py add-stable $STABLE \ && git add tox.ini .github/workflows/tests.yml \ && git commit -am "Tests: add new environment for Anki $STABLE" \ diff --git a/anki_wallpaper/tools.py b/anki_wallpaper/tools.py index 6657517..ef973a9 100644 --- a/anki_wallpaper/tools.py +++ b/anki_wallpaper/tools.py @@ -7,22 +7,19 @@ def patch_method(obj, method_name, action): original_method = getattr(obj, method_name) def decorator(function): - if not hasattr(obj, method_name): - raise Exception(f"Object {obj} has no method with the name {method_name}") - if action == "replace": - @wraps(function) + @wraps(original_method) def patched_method(*args, **kwargs): return function(*args, **kwargs) elif action == "prepend": - @wraps(function) + @wraps(original_method) def patched_method(*args, **kwargs): function(*args, **kwargs) return original_method(*args, **kwargs) elif action == "append": - @wraps(function) + @wraps(original_method) def patched_method(*args, **kwargs): result = original_method(*args, **kwargs) function(*args, **kwargs)