-
Notifications
You must be signed in to change notification settings - Fork 8
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
Handle additional path-based version specifiers #25
Merged
taylormadore
merged 3 commits into
containerbuildsystem:master
from
taylormadore:add-alternate-file-dep-style
Oct 30, 2024
Merged
Changes from all commits
Commits
Show all changes
3 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
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -18,7 +18,8 @@ | |
import json | ||
import logging | ||
import re | ||
from typing import Pattern | ||
from pathlib import Path | ||
from typing import Any, Dict, Optional, Pattern | ||
|
||
from ply import lex, yacc | ||
|
||
|
@@ -34,8 +35,16 @@ | |
|
||
class Package: | ||
def __init__( | ||
self, name, version, url=None, checksum=None, relpath=None, dependencies=None, alias=None | ||
): | ||
self, | ||
name: str, | ||
version: str, | ||
url: Optional[str] = None, | ||
checksum: Optional[str] = None, | ||
path: Optional[str] = None, | ||
relpath: Optional[str] = None, | ||
dependencies: Optional[Dict[str, str]] = None, | ||
alias: Optional[str] = None, | ||
) -> None: | ||
if not name: | ||
raise ValueError("Package name was not provided") | ||
|
||
|
@@ -46,12 +55,32 @@ def __init__( | |
self.version = version | ||
self.url = url | ||
self.checksum = checksum | ||
self.relpath = relpath | ||
self.path = path if path is not None else relpath | ||
self.dependencies = dependencies or {} | ||
self.alias = alias | ||
|
||
@property | ||
def relpath(self) -> Optional[str]: | ||
""" | ||
Return the path to the package. | ||
|
||
This is strictly kept for backwards compatibility and path should be used directly | ||
instead. The path is not always relative and may be absolute. | ||
""" | ||
return self.path | ||
|
||
@relpath.setter | ||
def relpath(self, path: Optional[str]) -> None: | ||
""" | ||
Set the path to the package. | ||
|
||
This is strictly kept for backwards compatibility and path should be used directly | ||
instead. The path is not always relative and may be absolute. | ||
""" | ||
self.path = path | ||
|
||
@classmethod | ||
def from_dict(cls, raw_name, data): | ||
def from_dict(cls, raw_name: str, data: Dict[str, Any]) -> "Package": | ||
name_at_version = re.compile(r"(?P<name>@?[^@]+)(?:@(?P<version>[^,]*))?") | ||
|
||
# _version is the version as declared in package.json, not the resolved version | ||
|
@@ -63,19 +92,40 @@ def from_dict(cls, raw_name, data): | |
alias = name | ||
name, _version = _must_match(name_at_version, _remove_prefix(_version, "npm:")).groups() | ||
|
||
if _version and _version.startswith("file:"): | ||
path = _remove_prefix(_version, "file:") | ||
if _version: | ||
path = cls.get_path_from_version_specifier(_version) | ||
|
||
# Ensure the resolved version key exists to appease mypy | ||
version = data.get("version") | ||
if not version: | ||
raise ValueError("Package version was not provided") | ||
|
||
return cls( | ||
name=name, | ||
version=data.get("version"), | ||
version=version, | ||
url=data.get("resolved"), | ||
checksum=data.get("integrity"), | ||
relpath=path, | ||
path=path, | ||
dependencies=data.get("dependencies", {}), | ||
alias=alias, | ||
) | ||
|
||
@staticmethod | ||
def get_path_from_version_specifier(version: str) -> Optional[str]: | ||
"""Return the path from a package.json file dependency version specifier.""" | ||
version_path = Path(version) | ||
|
||
if version.startswith("file:"): | ||
return _remove_prefix(version, "file:") | ||
elif version.startswith("link:"): | ||
return _remove_prefix(version, "link:") | ||
elif version_path.is_absolute() or version.startswith(("./", "../")): | ||
return str(version_path) | ||
else: | ||
# Some non-path version specifier, (e.g. "1.0.0" or a web link) | ||
# See https://docs.npmjs.com/cli/v10/configuring-npm/package-json#dependencies | ||
return None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A minor, non-blocking suggestion: could you please add a short comment or a link to a doc about when this branch could be reached? |
||
|
||
|
||
def _remove_prefix(s: str, prefix: str) -> str: | ||
return s[len(prefix) :] | ||
|
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a note stating that this is deprecated and is left for backward compatibility only.