Skip to content

Commit

Permalink
fix: Support versioned URLs in Asset class and fix missing versions i…
Browse files Browse the repository at this point in the history
…n Snakemake report (#3203)

Support versioned URLs in Asset class and fix missing versions in
Snakemake report


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
- Introduced versioning for assets, allowing dynamic URL generation
based on specified versions.
	- Added a method to retrieve the version of specified assets.

- **Bug Fixes**
- Enhanced logic for determining package versions with fallback options
for version retrieval.

- **Documentation**
- Updated documentation to reflect new attributes and methods related to
asset versioning.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
fxwiegand authored Nov 21, 2024
1 parent 43885d7 commit f086f6c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
56 changes: 39 additions & 17 deletions snakemake/assets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 18,20 @@ class AssetDownloadError(Exception):
@dataclass
class Asset:
url: str
version: Optional[str] = None
sha256: Optional[str] = None

def get_content(self) -> bytes:
"""Get and validate asset content."""

req = urllib.request.Request(self.url, headers={"User-Agent": "snakemake"})
url = self.url.format(version=self.version) if self.version else self.url
req = urllib.request.Request(url, headers={"User-Agent": "snakemake"})
err = None
for _ in range(6):
try:
resp = urllib.request.urlopen(req)
content = resp.read()
except urllib.error.URLError as e:
err = AssetDownloadError(f"Failed to download asset {self.url}: {e}")
err = AssetDownloadError(f"Failed to download asset {url}: {e}")
continue
if self.sha256 is not None:
content_sha = hashlib.sha256(content).hexdigest()
Expand All @@ -57,61 58,75 @@ class Assets:
sha256="a9d66f1d526df02e29dce73436d34e56e8632f46c275bbdffc70569e882f9f17",
),
"tailwindcss/LICENSE": Asset(
url="https://raw.githubusercontent.com/tailwindlabs/tailwindcss/refs/tags/v3.0.23/LICENSE",
url="https://raw.githubusercontent.com/tailwindlabs/tailwindcss/refs/tags/v{version}/LICENSE",
sha256="60e0b68c0f35c078eef3a5d29419d0b03ff84ec1df9c3f9d6e39a519a5ae7985",
version="3.0.23",
),
"tailwindcss/tailwind.css": Asset(
url="https://cdn.tailwindcss.com/3.0.23[email protected],[email protected]",
url="https://cdn.tailwindcss.com/{version}[email protected],[email protected]",
# The tailwindcss cdn checksum is not stable. Since this is only included
# as CSS styles, the risk is low.
version="3.0.23",
),
"react/LICENSE": Asset(
url="https://raw.githubusercontent.com/facebook/react/refs/tags/v18.2.0/LICENSE",
url="https://raw.githubusercontent.com/facebook/react/refs/tags/v{version}/LICENSE",
sha256="52412d7bc7ce4157ea628bbaacb8829e0a9cb3c58f57f99176126bc8cf2bfc85",
version="18.2.0",
),
"react/react.production.min.js": Asset(
url="https://cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.production.min.js",
url="https://cdnjs.cloudflare.com/ajax/libs/react/{version}/umd/react.production.min.js",
sha256="4b4969fa4ef3863324da2c6d78ce8766fbbc2fd121fff395aedf997db0a99a06",
version="18.2.0",
),
"react/react-dom.production.min.js": Asset(
url="https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.2.0/umd/react-dom.production.min.js",
url="https://cdnjs.cloudflare.com/ajax/libs/react-dom/{version}/umd/react-dom.production.min.js",
sha256="21758ed084cd0e37e735722ee4f3957ea960628a29dfa6c3ce1a1d47a2d6e4f7",
version="18.2.0",
),
"vega/vega.js": Asset(
url="https://cdnjs.cloudflare.com/ajax/libs/vega/5.21.0/vega.js",
url="https://cdnjs.cloudflare.com/ajax/libs/vega/{version}/vega.js",
sha256="b34c43055ef5d39a093e937522955dc359fbaec6c5b0259ae2de4c9da698e9fe",
version="5.21.0",
),
"vega/LICENSE": Asset(
url="https://raw.githubusercontent.com/vega/vega/refs/tags/v5.21.0/LICENSE",
url="https://raw.githubusercontent.com/vega/vega/refs/tags/v{version}/LICENSE",
sha256="b75f7ed0af20dedadf92c52bc236161bcf0d294ff2e6e34ca76403203349f71d",
version="5.21.0",
),
"vega-lite/vega-lite.js": Asset(
url="https://cdnjs.cloudflare.com/ajax/libs/vega-lite/5.2.0/vega-lite.js",
url="https://cdnjs.cloudflare.com/ajax/libs/vega-lite/{version}/vega-lite.js",
sha256="6eb7f93121cd9f44cf8640244f87c5e143f87c7a0b6cd113da4a9e41e3adf0aa",
version="5.2.0",
),
"vega-lite/LICENSE": Asset(
url="https://raw.githubusercontent.com/vega/vega-lite/refs/tags/v5.2.0/LICENSE",
url="https://raw.githubusercontent.com/vega/vega-lite/refs/tags/v{version}/LICENSE",
sha256="f618900fd0d64046963b29f40590cdd1e341a2f41449f99110d82fd81fea808c",
version="5.2.0",
),
"vega-embed/vega-embed.js": Asset(
url="https://cdnjs.cloudflare.com/ajax/libs/vega-embed/6.20.8/vega-embed.js",
url="https://cdnjs.cloudflare.com/ajax/libs/vega-embed/{version}/vega-embed.js",
sha256="4e546c1f86eb200333606440e92f76e2940b905757018d9672cd1708e4e6ff0a",
version="6.20.8",
),
"vega-embed/LICENSE": Asset(
url="https://raw.githubusercontent.com/vega/vega-embed/refs/tags/v6.20.8/LICENSE",
url="https://raw.githubusercontent.com/vega/vega-embed/refs/tags/v{version}/LICENSE",
sha256="32df67148f0fc3db0eb9e263a7b75d07f1eb14c61955005a4a39c6918d10d137",
version="6.20.8",
),
"heroicons/LICENSE": Asset(
url="https://raw.githubusercontent.com/tailwindlabs/heroicons/refs/tags/v1.0.3/LICENSE",
url="https://raw.githubusercontent.com/tailwindlabs/heroicons/refs/tags/v{version}/LICENSE",
sha256="75523ddd65d9620bea09f84e89d0c373b4205a3708b8a1e9f9598a5438a3e641",
version="1.0.3",
),
"prop-types/prop-types.min.js": Asset(
url="https://cdnjs.cloudflare.com/ajax/libs/prop-types/15.7.2/prop-types.min.js",
url="https://cdnjs.cloudflare.com/ajax/libs/prop-types/{version}/prop-types.min.js",
sha256="4c88350517ee82aa4f3368e67ef1a453ca6636dcfa6449b4e3d6faa5c877066e",
version="15.7.2",
),
"prop-types/LICENSE": Asset(
url="https://raw.githubusercontent.com/facebook/prop-types/refs/tags/v15.7.2/LICENSE",
url="https://raw.githubusercontent.com/facebook/prop-types/refs/tags/v{version}/LICENSE",
sha256="f657f99d3fb9647db92628e96007aabb46e5f04f33e49999075aab8e250ca7ce",
version="15.7.2",
),
}

Expand Down Expand Up @@ -147,6 162,13 @@ def get_content(cls, asset_path: str) -> str:
)
return cls.spec[asset_path].get_content().decode("utf-8")

@classmethod
def get_version(cls, asset_path: str) -> Optional[str]:
if asset_path in cls.spec:
return cls.spec[asset_path].version
else:
return None

@classmethod
def base_path(cls) -> Path:
# this is called from within snakemake, so we can use importlib.resources
Expand Down
6 changes: 5 additions & 1 deletion snakemake/report/html_reporter/data/packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 74,11 @@ class Package:
def __init__(
self, version=None, license_path=None, source_path=None, **source_paths
):
self.version = version
self.version = (
version
or Assets.get_version(source_path)
or Assets.get_version(license_path)
)

try:
self.license = Assets.get_content(license_path)
Expand Down

0 comments on commit f086f6c

Please sign in to comment.