-
Notifications
You must be signed in to change notification settings - Fork 13
feat: update wrappers to their specific latest version instead of the global latest version of the wrapper repo #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
johanneskoester
merged 6 commits into
main
from
feat/update-wrappers-per-wrapper-version
Oct 7, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,44 +1,150 @@ | ||
| from pathlib import Path | ||
| import re | ||
| from typing import List | ||
| import tempfile | ||
| from typing import Iterable, List | ||
| from urllib.parse import urlparse | ||
| import subprocess as sp | ||
| from snakedeploy.logger import logger | ||
|
|
||
| from github import Github | ||
|
|
||
|
|
||
| def update_snakemake_wrappers(snakefiles: List[str], git_ref: str): | ||
| """Set all snakemake wrappers to the given git ref (e.g. tag or branch).""" | ||
|
|
||
| if git_ref is None: | ||
| logger.info("Obtaining latest release of snakemake-wrappers...") | ||
| github = Github() | ||
| repo = github.get_repo("snakemake/snakemake-wrappers") | ||
| releases = repo.get_releases() | ||
| git_ref = releases[0].tag_name | ||
|
|
||
| for snakefile in snakefiles: | ||
| with open(snakefile, "r") as infile: | ||
| snakefile_content = infile.read() | ||
|
|
||
| def update_spec(matchobj): | ||
| spec = matchobj.group("spec") | ||
| url = urlparse(spec) | ||
| if not url.scheme: | ||
| old_git_ref, rest = spec.split("/", 1) | ||
| return ( | ||
| matchobj.group("def") | ||
| + matchobj.group("quote") | ||
| + f"{git_ref}/{rest}" | ||
| + matchobj.group("quote") | ||
| ) | ||
| else: | ||
| return matchobj.group() | ||
|
|
||
| logger.info(f"Updating snakemake-wrappers in {snakefile} to {git_ref}...") | ||
| snakefile_content = re.sub( | ||
| "(?P<def>wrapper:\\n?\\s*)(?P<quote>['\"])(?P<spec>.+)(?P=quote)", | ||
| update_spec, | ||
| snakefile_content, | ||
|
|
||
| def get_latest_git_tag(path: Path, repo: Path) -> str | None: | ||
| """Get the latest git tag of any file in the given directory or below. | ||
| Thereby ignore later git tags outside of the given directory. | ||
| """ | ||
|
|
||
| # get the latest git commit that changed the given dir: | ||
| commit = ( | ||
| sp.run( | ||
| ["git", "rev-list", "-1", "HEAD", "--", str(path)], | ||
| stdout=sp.PIPE, | ||
| cwd=repo, | ||
| check=True, | ||
| ) | ||
| .stdout.decode() | ||
| .strip() | ||
| ) | ||
| # get the first git tag that includes this commit: | ||
| # Note: We want the EARLIEST tag containing the commit, which represents | ||
| # the first version where this wrapper reached its current state | ||
| tags = ( | ||
| sp.run( | ||
| ["git", "tag", "--sort", "creatordate", "--contains", commit], | ||
| check=True, | ||
| cwd=repo, | ||
| stdout=sp.PIPE, | ||
| ) | ||
| .stdout.decode() | ||
| .strip() | ||
| .splitlines() | ||
| ) | ||
| if not tags: | ||
| return None | ||
| else: | ||
| return tags[0] | ||
|
|
||
|
|
||
| def get_sparse_checkout_patterns() -> Iterable[str]: | ||
| for wrapper_pattern in ("*", "*/*"): | ||
| for filetype in ("wrapper.*", "environment.yaml"): | ||
| yield f"/*/{wrapper_pattern}/{filetype}" | ||
| yield "/meta/*/*/test/Snakefile" | ||
|
|
||
|
|
||
| class WrapperRepo: | ||
| def __init__(self): | ||
| self.tmpdir = tempfile.TemporaryDirectory() | ||
| logger.info("Cloning snakemake-wrappers repository...") | ||
| sp.run( | ||
| [ | ||
| "git", | ||
| "clone", | ||
| "--filter=blob:none", | ||
| "--no-checkout", | ||
| "https://github.com/snakemake/snakemake-wrappers.git", | ||
| ".", | ||
| ], | ||
| cwd=self.tmpdir.name, | ||
| check=True, | ||
| ) | ||
| sp.run( | ||
| ["git", "config", "core.sparseCheckoutCone", "false"], | ||
| cwd=self.tmpdir.name, | ||
| check=True, | ||
| ) | ||
| with open(snakefile, "w") as outfile: | ||
| outfile.write(snakefile_content) | ||
| sp.run(["git", "sparse-checkout", "disable"], cwd=self.tmpdir.name, check=True) | ||
| sp.run( | ||
| ["git", "sparse-checkout", "set", "--no-cone"] | ||
| + list(get_sparse_checkout_patterns()), | ||
| cwd=self.tmpdir.name, | ||
| check=True, | ||
| ) | ||
| sp.run(["git", "read-tree", "-mu", "HEAD"], cwd=self.tmpdir.name, check=True) | ||
| self.repo_dir = Path(self.tmpdir.name) | ||
|
|
||
| def get_wrapper_version(self, spec: str) -> str | None: | ||
| if not (self.repo_dir / spec).exists(): | ||
| return None | ||
| return get_latest_git_tag(Path(spec), self.repo_dir) | ||
|
|
||
| def __enter__(self): | ||
| return self | ||
|
|
||
| def __exit__(self, exc_type, exc_value, traceback): | ||
| self.tmpdir.cleanup() | ||
|
|
||
|
|
||
| def update_snakemake_wrappers(snakefiles: List[str]): | ||
| """Update all snakemake wrappers to their specific latest versions.""" | ||
|
|
||
| with WrapperRepo() as wrapper_repo: | ||
| for snakefile in snakefiles: | ||
| with open(snakefile, "r") as infile: | ||
| snakefile_content = infile.read() | ||
|
|
||
| def update_spec(matchobj): | ||
| spec = matchobj.group("spec") | ||
| url = urlparse(spec) | ||
| if not url.scheme: | ||
| parts = spec.split("/", 1) | ||
| if len(parts) != 2: | ||
| logger.warning( | ||
| f"Could not parse wrapper specification '{spec}' " | ||
| "(expected version/cat/name or version/cat/name/subcommand). " | ||
| "Leaving unchanged." | ||
| ) | ||
| return matchobj.group() | ||
| old_git_ref, rest = parts | ||
| git_ref = wrapper_repo.get_wrapper_version(rest) | ||
| if git_ref is None: | ||
| logger.warning( | ||
| f"Could not determine latest version of wrapper '{rest}'. " | ||
| "Leaving unchanged." | ||
| ) | ||
| return matchobj.group() | ||
| elif git_ref != old_git_ref: | ||
| logger.info( | ||
| f"Updated wrapper '{rest}' from {old_git_ref} to {git_ref}." | ||
| ) | ||
| else: | ||
| logger.info( | ||
| f"Wrapper '{rest}' is already at latest version {git_ref}." | ||
| ) | ||
| return ( | ||
| matchobj.group("def") | ||
| + matchobj.group("quote") | ||
| + f"{git_ref}/{rest}" | ||
| + matchobj.group("quote") | ||
| ) | ||
| else: | ||
| return matchobj.group() | ||
|
|
||
| logger.info( | ||
| f"Updating snakemake-wrappers and meta-wrappers in {snakefile}..." | ||
| ) | ||
| snakefile_content = re.sub( | ||
| "(?P<def>(meta_)?wrapper:\\n?\\s*)(?P<quote>['\"])(?P<spec>.+)(?P=quote)", | ||
| update_spec, | ||
| snakefile_content, | ||
| ) | ||
| with open(snakefile, "w") as outfile: | ||
| outfile.write(snakefile_content) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.