buildcache: recognize . and .. as paths instead of names (#47105)

This commit is contained in:
Harmen Stoppels 2024-10-22 13:05:06 +02:00 committed by Harmen Stoppels
parent e733eb0fd9
commit ba89754ee1
3 changed files with 19 additions and 11 deletions

View File

@ -661,34 +661,32 @@ def mirror_name_or_url(m):
# accidentally to a dir in the current working directory.
# If there's a \ or / in the name, it's interpreted as a path or url.
if "/" in m or "\\" in m:
if "/" in m or "\\" in m or m in (".", ".."):
return spack.mirror.Mirror(m)
# Otherwise, the named mirror is required to exist.
try:
return spack.mirror.require_mirror_name(m)
except ValueError as e:
raise argparse.ArgumentTypeError(
str(e) + ". Did you mean {}?".format(os.path.join(".", m))
)
raise argparse.ArgumentTypeError(f"{e}. Did you mean {os.path.join('.', m)}?") from e
def mirror_url(url):
try:
return spack.mirror.Mirror.from_url(url)
except ValueError as e:
raise argparse.ArgumentTypeError(str(e))
raise argparse.ArgumentTypeError(str(e)) from e
def mirror_directory(path):
try:
return spack.mirror.Mirror.from_local_path(path)
except ValueError as e:
raise argparse.ArgumentTypeError(str(e))
raise argparse.ArgumentTypeError(str(e)) from e
def mirror_name(name):
try:
return spack.mirror.require_mirror_name(name)
except ValueError as e:
raise argparse.ArgumentTypeError(str(e))
raise argparse.ArgumentTypeError(str(e)) from e

View File

@ -87,9 +87,8 @@ def from_url(url: str):
"""Create an anonymous mirror by URL. This method validates the URL."""
if not urllib.parse.urlparse(url).scheme in supported_url_schemes:
raise ValueError(
'"{}" is not a valid mirror URL. Scheme must be once of {}.'.format(
url, ", ".join(supported_url_schemes)
)
f'"{url}" is not a valid mirror URL. '
f"Scheme must be one of {supported_url_schemes}."
)
return Mirror(url)
@ -734,7 +733,7 @@ def require_mirror_name(mirror_name):
"""Find a mirror by name and raise if it does not exist"""
mirror = spack.mirror.MirrorCollection().get(mirror_name)
if not mirror:
raise ValueError('no mirror named "{0}"'.format(mirror_name))
raise ValueError(f'no mirror named "{mirror_name}"')
return mirror

View File

@ -8,6 +8,7 @@
import pytest
from llnl.util.filesystem import working_dir
from llnl.util.symlink import resolve_link_target_relative_to_the_link
import spack.mirror
@ -15,6 +16,7 @@
import spack.util.executable
import spack.util.spack_json as sjson
import spack.util.url as url_util
from spack.cmd.common.arguments import mirror_name_or_url
from spack.spec import Spec
from spack.stage import Stage
from spack.util.executable import which
@ -366,3 +368,12 @@ def test_update_connection_params(direction):
assert m.get_access_token(direction) == "token"
assert m.get_profile(direction) == "profile"
assert m.get_endpoint_url(direction) == "https://example.com"
def test_mirror_name_or_url_dir_parsing(tmp_path):
curdir = tmp_path / "mirror"
curdir.mkdir()
with working_dir(curdir):
assert mirror_name_or_url(".").fetch_url == curdir.as_uri()
assert mirror_name_or_url("..").fetch_url == tmp_path.as_uri()