Skip to content

Commit

Permalink
chore: merge branch 'origin/main' into release-merge/4.2.3
Browse files Browse the repository at this point in the history
  • Loading branch information
lengau committed Sep 18, 2024
2 parents 8afd506 + e546fae commit 5c93384
Show file tree
Hide file tree
Showing 21 changed files with 1,563 additions and 55 deletions.
13 changes: 7 additions & 6 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ jobs:
3.10
3.12
cache: 'pip'
- name: Setup LXD
uses: canonical/setup-lxd@v0.1.1
with:
channel: latest/stable
- name: Configure environment
run: |
echo "::group::apt-get"
Expand Down Expand Up @@ -111,10 +107,12 @@ jobs:
cache: 'pip'
- name: Setup LXD
uses: canonical/setup-lxd@v0.1.1
with:
channel: latest/stable
- name: Configure environment
run: |
echo "::group::Begin snap install"
echo "Installing snaps in the background while running apt and pip..."
sudo snap install --no-wait --channel=beta fetch-service
echo "::endgroup::"
echo "::group::apt-get"
sudo apt update
sudo apt-get install -y libapt-pkg-dev
Expand All @@ -123,6 +121,9 @@ jobs:
python -m pip install tox
echo "::endgroup::"
mkdir -p results
echo "::group::Wait for snap to complete"
snap watch --last=install
echo "::endgroup::"
- name: Setup Tox environments
run: tox run -e integration-${{ matrix.python }} --notest
- name: Integration tests
Expand Down
23 changes: 21 additions & 2 deletions craft_application/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ def __init__(
else:
self._work_dir = pathlib.Path.cwd()

# Whether the command execution should use the fetch-service
self._use_fetch_service = False

@property
def app_config(self) -> dict[str, Any]:
"""Get the configuration passed to dispatcher.load_command().
Expand Down Expand Up @@ -368,8 +371,14 @@ def run_managed(self, platform: str | None, build_for: str | None) -> None:
instance_path = pathlib.PosixPath("/root/project")

with self.services.provider.instance(
build_info, work_dir=self._work_dir
build_info,
work_dir=self._work_dir,
clean_existing=self._use_fetch_service,
) as instance:
if self._use_fetch_service:
session_env = self.services.fetch.create_session(instance)
env.update(session_env)

cmd = [self.app.name, *sys.argv[1:]]
craft_cli.emit.debug(
f"Executing {cmd} in instance location {instance_path} with {extra_args}."
Expand All @@ -387,6 +396,12 @@ def run_managed(self, platform: str | None, build_for: str | None) -> None:
raise craft_providers.ProviderError(
f"Failed to execute {self.app.name} in instance."
) from exc
finally:
if self._use_fetch_service:
self.services.fetch.teardown_session()

if self._use_fetch_service:
self.services.fetch.shutdown(force=True)

def configure(self, global_args: dict[str, Any]) -> None:
"""Configure the application using any global arguments."""
Expand Down Expand Up @@ -482,12 +497,14 @@ def _pre_run(self, dispatcher: craft_cli.Dispatcher) -> None:
At the time this is run, the command is loaded in the dispatcher, but
the project has not yet been loaded.
"""
args = dispatcher.parsed_args()

# Some commands might have a project_dir parameter. Those commands and
# only those commands should get a project directory, but only when
# not managed.
if self.is_managed():
self.project_dir = pathlib.Path("/root/project")
elif project_dir := getattr(dispatcher.parsed_args(), "project_dir", None):
elif project_dir := getattr(args, "project_dir", None):
self.project_dir = pathlib.Path(project_dir).expanduser().resolve()
if self.project_dir.exists() and not self.project_dir.is_dir():
raise errors.ProjectFileMissingError(
Expand All @@ -496,6 +513,8 @@ def _pre_run(self, dispatcher: craft_cli.Dispatcher) -> None:
resolution="Ensure the path entered is correct.",
)

self._use_fetch_service = getattr(args, "use_fetch_service", False)

def get_arg_or_config(
self, parsed_args: argparse.Namespace, item: str
) -> Any: # noqa: ANN401
Expand Down
6 changes: 6 additions & 0 deletions craft_application/commands/lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,12 @@ def _fill_parser(self, parser: argparse.ArgumentParser) -> None:
help="Output directory for created packages.",
)

parser.add_argument(
"--use-fetch-service",
action="store_true",
help="Use the Fetch Service to inspect downloaded assets.",
)

@override
def _run(
self,
Expand Down
4 changes: 4 additions & 0 deletions craft_application/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,7 @@ def __init__( # (too many arguments)
reportable=reportable,
retcode=retcode,
)


class FetchServiceError(CraftError):
"""Errors related to the fetch-service."""
Loading

0 comments on commit 5c93384

Please sign in to comment.