Skip to content

Commit

Permalink
test new paths
Browse files Browse the repository at this point in the history
  • Loading branch information
syu-w committed Oct 14, 2023
1 parent b271694 commit 0689cfc
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 8 deletions.
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ colorama==0.4.6
coverage==7.3.1
craft-archives==1.1.3
craft-cli==2.1.0
craft-parts==1.25.1
craft-parts@git+https://github.com/canonical/craft-parts.git@snap-bin-path
craft-providers==1.18.0
cryptography==41.0.4
Deprecated==1.2.14
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ certifi==2023.7.22
charset-normalizer==3.2.0
craft-archives==1.1.3
craft-cli==2.1.0
craft-parts==1.25.1
craft-parts@git+https://github.com/canonical/craft-parts.git@snap-bin-path
craft-providers==1.18.0
Deprecated==1.2.14
distro==1.8.0
Expand Down
5 changes: 5 additions & 0 deletions rockcraft/oci.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

from rockcraft import errors
from rockcraft.pebble import Pebble
from rockcraft.utils import get_snap_command_path

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -794,6 +795,10 @@ def _inject_architecture_variant(image_path: Path, variant: str) -> None:

def _process_run(command: List[str], **kwargs: Any) -> subprocess.CompletedProcess:
"""Run a command and handle its output."""
if not Path(command[0]).is_absolute():
command[0] = get_snap_command_path(command[0])
emit.trace(f"Found command absolute path: {command!r}")

emit.trace(f"Execute process: {command!r}, kwargs={kwargs!r}")
try:
return subprocess.run(
Expand Down
68 changes: 68 additions & 0 deletions rockcraft/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
import logging
import os
import pathlib
import shutil
import sys
from collections import namedtuple
from distutils.util import strtobool # pylint: disable=deprecated-module
from typing import Optional

import rockcraft.errors

logger = logging.getLogger(__name__)

OSPlatform = namedtuple("OSPlatform", "system release machine")
Expand Down Expand Up @@ -80,3 +83,68 @@ def confirm_with_user(prompt: str, default: bool = False) -> bool:

reply = input(prompt + choices).lower().strip()
return reply[0] == "y" if reply else default


def _find_command_path_in_root(root: str, command_name: str) -> Optional[str]:
"""Find the path of a command in a given root path."""
for bin_directory in (
"usr/local/sbin",
"usr/local/bin",
"usr/sbin",
"usr/bin",
"sbin",
"bin",
):
path = os.path.join(root, bin_directory, command_name)
if os.path.exists(path):
return path

return None


def get_host_command(command_name: str) -> str:
"""Return the full path of the given host tool.
:param command_name: the name of the command to resolve a path for.
:return: Path to command
:raises SnapcraftError: if command_name was not found.
"""
tool = shutil.which(command_name)
if not tool:
raise rockcraft.errors.RockcraftError(
f"A tool rockcraft depends on could not be found: {command_name!r}",
resolution="Ensure the tool is installed and available, and try again.",
)
return tool


def get_snap_command_path(command_name: str) -> str:
"""Return the path of a command found in the snap.
If rockcraft is not running as a snap, shutil.which() is used
to resolve the command using PATH.
:param command_name: the name of the command to resolve a path for.
:return: Path to command
:raises RockcraftError: if command_name was not found.
"""
if os.environ.get("SNAP_NAME") != "rockcraft":
return get_host_command(command_name)

snap_path = os.getenv("SNAP")
if snap_path is None:
raise RuntimeError(
"The SNAP environment variable is not defined, but SNAP_NAME is?"
)

command_path = _find_command_path_in_root(snap_path, command_name)

if command_path is None:
raise rockcraft.errors.RockcraftError(
f"Cannot find snap tool {command_name!r}",
resolution="Please report this error to the Rockcraft maintainers.",
)

return command_path
2 changes: 2 additions & 0 deletions snap/snapcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,7 @@ parts:
plugin: go
source: https://github.com/canonical/chisel.git
source-commit: bd27f8700cd7d2a6b4e0df6b10c3761c83a70485
build-attributes:
- enable-patchelf
build-snaps:
- go/1.18/stable
12 changes: 6 additions & 6 deletions tests/unit/test_oci.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def test_from_docker_registry(self, mock_run, new_dir):
assert mock_run.mock_calls == [
call(
[
"skopeo",
"/usr/bin/skopeo",
"--insecure-policy",
"--override-arch",
"amd64",
Expand All @@ -153,7 +153,7 @@ def test_from_docker_registry(self, mock_run, new_dir):
assert mock_run.mock_calls == [
call(
[
"skopeo",
"/usr/bin/skopeo",
"--insecure-policy",
"--override-arch",
"arm64",
Expand Down Expand Up @@ -206,7 +206,7 @@ def test_copy_to(self, mock_run):
assert mock_run.mock_calls == [
call(
[
"skopeo",
"/usr/bin/skopeo",
"--insecure-policy",
"copy",
"oci:/c/a:b",
Expand Down Expand Up @@ -780,7 +780,7 @@ def test_to_docker_daemon(self, mock_run):
assert mock_run.mock_calls == [
call(
[
"skopeo",
"/usr/bin/skopeo",
"--insecure-policy",
"copy",
"oci:/c/a:tag",
Expand All @@ -795,7 +795,7 @@ def test_to_oci_archive(self, mock_run):
assert mock_run.mock_calls == [
call(
[
"skopeo",
"/usr/bin/skopeo",
"--insecure-policy",
"copy",
"oci:/c/a:tag",
Expand All @@ -816,7 +816,7 @@ def test_digest(self, mocker):
assert mock_output.mock_calls == [
call(
[
"skopeo",
"/usr/bin/skopeo",
"inspect",
"--format",
"{{.Digest}}",
Expand Down

0 comments on commit 0689cfc

Please sign in to comment.