Skip to content

Commit

Permalink
yarn_v1: connecting metadispatcher to the code
Browse files Browse the repository at this point in the history
Now it is the sole entrypoint for both Yarn v1 and v2.
This change graduates Yarn Classic from dev to fully
supported package manager.

Signed-off-by: Alexey Ovchinnikov <[email protected]>
  • Loading branch information
a-ovchinnikov committed Dec 12, 2024
1 parent b1f4494 commit ffa602a
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 46 deletions.
2 changes: 2 additions & 0 deletions cachi2/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class Config(BaseModel, extra="forbid"):
requests_timeout: int = 300
concurrency_limit: int = 5

allow_yarnberry_processing: bool = True

@model_validator(mode="before")
@classmethod
def _print_deprecation_warning(cls, data: Any) -> Any:
Expand Down
12 changes: 8 additions & 4 deletions cachi2/core/package_managers/metayarn.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from cachi2.core.config import get_config
from cachi2.core.models.input import Request
from cachi2.core.models.output import RequestOutput
from cachi2.core.package_managers import yarn, yarn_classic
from cachi2.core.package_managers.yarn_classic.main import MissingLockfile, NotV1Lockfile
from cachi2.core.resolver import _merge_outputs
from cachi2.core.utils import merge_outputs


def fetch_yarn_source(request: Request) -> RequestOutput:
Expand All @@ -15,7 +16,10 @@ def fetch_yarn_source(request: Request) -> RequestOutput:
new_request = request.model_copy(update={"packages": [package]})
try:
fetched_packages.append(yarn_classic.fetch_yarn_source(new_request))
except (MissingLockfile, NotV1Lockfile):
except (MissingLockfile, NotV1Lockfile) as e:
# It is assumed that if a package is not v1 then it is probably v2.
fetched_packages.append(yarn.fetch_yarn_source(new_request))
return _merge_outputs(fetched_packages)
if get_config().allow_yarnberry_processing:
fetched_packages.append(yarn.fetch_yarn_source(new_request))
else:
raise e
return merge_outputs(fetched_packages)
2 changes: 1 addition & 1 deletion cachi2/core/package_managers/yarn_classic/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def fetch_yarn_source(request: Request) -> RequestOutput:
def _ensure_mirror_dir_exists(output_dir: RootedPath) -> None:
output_dir.join_within_root(MIRROR_DIR).path.mkdir(parents=True, exist_ok=True)

for package in request.yarn_classic_packages:
for package in request.yarn_packages:
package_path = request.source_dir.join_within_root(package.path)
_ensure_mirror_dir_exists(request.output_dir)
_resolve_yarn_project(Project.from_source_dir(package_path), request.output_dir)
Expand Down
29 changes: 4 additions & 25 deletions cachi2/core/resolver.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
from collections.abc import Iterable
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import Any, Callable

from cachi2.core.errors import UnsupportedFeature
from cachi2.core.models.input import PackageManagerType, Request
from cachi2.core.models.output import RequestOutput
from cachi2.core.package_managers import bundler, generic, gomod, npm, pip, rpm, yarn, yarn_classic
from cachi2.core.package_managers import bundler, generic, gomod, metayarn, npm, pip, rpm
from cachi2.core.rooted_path import RootedPath
from cachi2.core.utils import copy_directory
from cachi2.core.utils import copy_directory, merge_outputs

Handler = Callable[[Request], RequestOutput]

Expand All @@ -17,15 +16,14 @@
"gomod": gomod.fetch_gomod_source,
"npm": npm.fetch_npm_source,
"pip": pip.fetch_pip_source,
"yarn": yarn.fetch_yarn_source,
"yarn": metayarn.fetch_yarn_source,
"generic": generic.fetch_generic_source,
}

# This is where we put package managers currently under development in order to
# invoke them via CLI
_dev_package_managers: dict[PackageManagerType, Handler] = {
"rpm": rpm.fetch_rpm_source,
"yarn-classic": yarn_classic.fetch_yarn_source,
}

# This is *only* used to provide a list for `cachi2 --version`
Expand Down Expand Up @@ -68,26 +66,7 @@ def _resolve_packages(request: Request) -> RequestOutput:
solution="But the good news is that we're already working on it!",
)
pkg_managers = [_supported_package_managers[type_] for type_ in sorted(requested_types)]
return _merge_outputs(pkg_manager(request) for pkg_manager in pkg_managers)


def _merge_outputs(outputs: Iterable[RequestOutput]) -> RequestOutput:
"""Merge RequestOutput instances."""
components = []
env_vars = []
project_files = []

for output in outputs:
components.extend(output.components)
env_vars.extend(output.build_config.environment_variables)
project_files.extend(output.build_config.project_files)

return RequestOutput.from_obj_list(
components=components,
environment_variables=env_vars,
project_files=project_files,
options=output.build_config.options if output.build_config.options else None,
)
return merge_outputs(pkg_manager(request) for pkg_manager in pkg_managers)


def inject_files_post(from_output_dir: Path, for_output_dir: Path, **kwargs: Any) -> None:
Expand Down
22 changes: 21 additions & 1 deletion cachi2/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
import sys
from functools import cache
from pathlib import Path
from typing import Callable, Iterator, Optional, Sequence
from typing import Callable, Iterable, Iterator, Optional, Sequence

from cachi2.core.config import get_config
from cachi2.core.errors import Cachi2Error
from cachi2.core.models.output import RequestOutput

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -195,3 +196,22 @@ def get_cache_dir() -> Path:
except KeyError:
cache_dir = Path.home().joinpath(".cache")
return cache_dir.joinpath("cachi2")


def merge_outputs(outputs: Iterable[RequestOutput]) -> RequestOutput:
"""Merge RequestOutput instances."""
components = []
env_vars = []
project_files = []

for output in outputs:
components.extend(output.components)
env_vars.extend(output.build_config.environment_variables)
project_files.extend(output.build_config.project_files)

return RequestOutput.from_obj_list(
components=components,
environment_variables=env_vars,
project_files=project_files,
options=output.build_config.options if output.build_config.options else None,
)
20 changes: 9 additions & 11 deletions tests/integration/test_yarn_classic.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
utils.TestParameters(
repo="https://github.com/cachito-testing/cachi2-yarn.git",
ref="corepack_packagemanager_ignored",
packages=({"path": ".", "type": "yarn-classic"},),
packages=({"path": ".", "type": "yarn"},),
flags=["--dev-package-managers"],
check_output=False,
check_deps_checksums=False,
Expand All @@ -29,7 +29,7 @@
utils.TestParameters(
repo="https://github.com/cachito-testing/cachi2-yarn.git",
ref="yarnpath_ignored",
packages=({"path": ".", "type": "yarn-classic"},),
packages=({"path": ".", "type": "yarn"},),
flags=["--dev-package-managers"],
check_output=False,
check_deps_checksums=False,
Expand All @@ -43,7 +43,7 @@
utils.TestParameters(
repo="https://github.com/cachito-testing/cachi2-yarn.git",
ref="invalid_checksum",
packages=({"path": ".", "type": "yarn-classic"},),
packages=({"path": ".", "type": "yarn"},),
flags=["--dev-package-managers"],
check_output=False,
check_deps_checksums=False,
Expand All @@ -57,7 +57,7 @@
utils.TestParameters(
repo="https://github.com/cachito-testing/cachi2-yarn.git",
ref="invalid_frozen_lockfile_add_dependency",
packages=({"path": ".", "type": "yarn-classic"},),
packages=({"path": ".", "type": "yarn"},),
flags=["--dev-package-managers"],
check_output=False,
check_deps_checksums=False,
Expand All @@ -71,7 +71,7 @@
utils.TestParameters(
repo="https://github.com/cachito-testing/cachi2-yarn.git",
ref="lifecycle_scripts",
packages=({"path": ".", "type": "yarn-classic"},),
packages=({"path": ".", "type": "yarn"},),
flags=["--dev-package-managers"],
check_output=False,
check_deps_checksums=False,
Expand All @@ -85,7 +85,7 @@
utils.TestParameters(
repo="https://github.com/cachito-testing/cachi2-yarn.git",
ref="offline-mirror-collision",
packages=({"path": ".", "type": "yarn-classic"},),
packages=({"path": ".", "type": "yarn"},),
flags=["--dev-package-managers"],
check_output=False,
check_deps_checksums=False,
Expand Down Expand Up @@ -128,8 +128,7 @@ def test_yarn_classic_packages(
utils.TestParameters(
repo="https://github.com/cachito-testing/cachi2-yarn.git",
ref="valid_yarn_all_dependency_types",
packages=({"path": ".", "type": "yarn-classic"},),
flags=["--dev-package-managers"],
packages=({"path": ".", "type": "yarn"},),
check_vendor_checksums=False,
expected_exit_code=0,
expected_output="All dependencies fetched successfully",
Expand All @@ -143,10 +142,9 @@ def test_yarn_classic_packages(
repo="https://github.com/cachito-testing/cachi2-yarn.git",
ref="valid_multiple_packages",
packages=(
{"path": "first-pkg", "type": "yarn-classic"},
{"path": "second-pkg", "type": "yarn-classic"},
{"path": "first-pkg", "type": "yarn"},
{"path": "second-pkg", "type": "yarn"},
),
flags=["--dev-package-managers"],
check_vendor_checksums=False,
expected_exit_code=0,
expected_output="All dependencies fetched successfully",
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/package_managers/yarn_classic/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ def test_generate_build_environment_variables(
"input_request, components",
[
pytest.param(
[{"type": "yarn-classic", "path": "."}],
[{"type": "yarn", "path": "."}],
[],
id="single_input_package",
),
pytest.param(
[{"type": "yarn-classic", "path": "."}, {"type": "yarn-classic", "path": "./path"}],
[{"type": "yarn", "path": "."}, {"type": "yarn", "path": "./path"}],
[],
id="multiple_input_packages",
),
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/package_managers/yarn_classic/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,8 @@ def test_pnp_installs_false(
# A yarn v1 project needs a valid yarn lock to be accepted.
_prepare_config_file(
rooted_tmp_path,
# I will deal with
# Ignoring type checking because of
# incompatible type "type[YarnLock]"; expected "Union[PackageJson, YarnLock]"
# later, will ignore mypy for now:
YarnLock, # type: ignore
"yarn.lock",
VALID_YARN_LOCK_FILE,
Expand Down

0 comments on commit ffa602a

Please sign in to comment.