Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 35 additions & 11 deletions platformio/package/commands/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import json
import os
from typing import List

import click

from platformio import fs
from platformio.package.graph import walk_package_dependencies
from platformio.package.inventory import collect_project_inventory
from platformio.package.manager.library import LibraryPackageManager
from platformio.package.manager.platform import PlatformPackageManager
from platformio.package.manager.tool import ToolPackageManager
from platformio.package.meta import PackageItem, PackageSpec
from platformio.package.meta import PackageSpec
from platformio.platform.exception import UnknownPlatform
from platformio.platform.factory import PlatformFactory
from platformio.project.config import ProjectConfig
Expand All @@ -48,14 +50,42 @@
@click.option("--only-platforms", is_flag=True, help="List only platform packages")
@click.option("--only-tools", is_flag=True, help="List only tool packages")
@click.option("--only-libraries", is_flag=True, help="List only library packages")
@click.option(
"--json-output", is_flag=True, help="Output the project package inventory"
)
@click.option("-v", "--verbose", is_flag=True)
def package_list_cmd(**options):
if options.get("global"):
if options.get("json_output"):
_print_project_inventory(options)
elif options.get("global"):
list_global_packages(options)
else:
list_project_packages(options)


def _print_project_inventory(options):
incompatible = (
"global",
"storage_dir",
"platforms",
"tools",
"libraries",
"only_platforms",
"only_tools",
"only_libraries",
"verbose",
)
for option in incompatible:
if options.get(option):
raise click.UsageError(
f"--json-output cannot be combined with --{option.replace('_', '-')}."
)
inventory = collect_project_inventory(
options["project_dir"], options["environments"]
)
click.echo(json.dumps(inventory, indent=2))


def humanize_package(pkg, spec=None, verbose=False):
if spec and not isinstance(spec, PackageSpec):
spec = PackageSpec(spec)
Expand Down Expand Up @@ -120,14 +150,8 @@ def print_dependency_tree(pm, specs=None, filter_specs=None, level=0, verbose=Fa
)


def _pkg_tree_contains(pm, root: PackageItem, children: List[PackageItem]):
if root in children:
return True
for dependency in pm.get_pkg_dependencies(root) or []:
pkg = pm.get_package(pm.dependency_to_spec(dependency))
if pkg and _pkg_tree_contains(pm, pkg, children):
return True
return False
def _pkg_tree_contains(pm, root, children):
return any(pkg in children for pkg in walk_package_dependencies(pm, root))


def list_global_packages(options):
Expand Down
37 changes: 37 additions & 0 deletions platformio/package/graph.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright (c) 2014-present PlatformIO <contact@platformio.org>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


def iter_package_dependencies(manager, package):
"""Yield each declared dependency spec and its installed package, if any."""
for dependency in manager.get_pkg_dependencies(package) or ():
spec = manager.dependency_to_spec(dependency)
yield spec, manager.get_package(spec)


def walk_package_dependencies(manager, root):
"""Visit the root and each reachable installed package once."""
visited = set()
pending = [root]
while pending:
package = pending.pop()
if package in visited:
continue
visited.add(package)
yield package
pending.extend(
child
for _, child in iter_package_dependencies(manager, package)
if child is not None
)
107 changes: 107 additions & 0 deletions platformio/package/inventory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Copyright (c) 2014-present PlatformIO <contact@platformio.org>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os

from platformio import fs
from platformio.package.graph import iter_package_dependencies
from platformio.package.manager.library import LibraryPackageManager
from platformio.package.manager.platform import PlatformPackageManager
from platformio.package.meta import PackageSpec
from platformio.platform.factory import PlatformFactory
from platformio.project.config import ProjectConfig


def _package_edges(manager, specs, parent=None):
pending = [(parent, spec, manager.get_package(spec)) for spec in specs]
visited = set()
while pending:
next_edges = []
for source, spec, target in pending:
yield source, manager.pkg_type, spec, target
if target is None or target in visited:
continue
visited.add(target)
next_edges.extend(
(target, child_spec, child)
for child_spec, child in iter_package_dependencies(manager, target)
)
pending = next_edges


def _environment_edges(config, env):
section = f"env:{env}"
platform_spec = config.get(section, "platform", None)
if platform_spec:
manager = PlatformPackageManager()
spec = PackageSpec(platform_spec)
package = manager.get_package(spec)
yield None, manager.pkg_type, spec, package
if package is not None:
platform = PlatformFactory.from_env(env)
required = [
platform.get_package_spec(name)
for name, options in platform.packages.items()
if not options.get("optional")
]
yield from _package_edges(platform.pm, required, parent=package)

lib_deps = config.get(section, "lib_deps", [])
if lib_deps:
manager = LibraryPackageManager(
os.path.join(config.get("platformio", "libdeps_dir"), env)
)
yield from _package_edges(manager, map(PackageSpec, lib_deps))


def _environment_inventory(config, env):
packages = {}
dependencies = []
for parent, kind, spec, package in _environment_edges(config, env):
if package is not None and package not in packages:
metadata = package.metadata
packages[package] = {
"id": len(packages) + 1,
"type": kind,
"name": metadata.name,
"owner": metadata.spec.owner,
"version": str(metadata.version),
}
requested = spec.as_dict()
# External sources can contain credentials or machine-specific paths.
requested.pop("uri")
if spec.external and not spec.has_custom_name():
requested["name"] = None
requested["external"] = spec.external
dependencies.append(
{
"from": packages[parent]["id"] if parent is not None else None,
"to": packages[package]["id"] if package is not None else None,
"type": kind,
"requested": requested,
}
)
return {"packages": list(packages.values()), "dependencies": dependencies}


def collect_project_inventory(project_dir, environments):
"""Snapshot declared project packages using local installation state."""
with fs.cd(project_dir):
config = ProjectConfig.get_instance()
config.validate(environments, silent=True)
return {
env: _environment_inventory(config, env)
for env in config.envs()
if not environments or env in environments
}
135 changes: 135 additions & 0 deletions tests/package/test_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# Copyright (c) 2014-present PlatformIO <contact@platformio.org>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import json

from platformio.package.commands.list import package_list_cmd


def _write_manifest(storage, filename, name, version, **fields):
package = storage / name
package.mkdir(parents=True)
(package / filename).write_text(
json.dumps({"name": name, "version": version, **fields}),
encoding="utf8",
)


def test_package_list_filter_handles_circular_dependencies(tmp_path, clirunner):
libraries = {
"A": {"B": "*", "Missing": "*"},
"B": {"A": "*"},
"Target": {},
"WithTarget": {"A": "*", "Target": "*"},
}
for name, dependencies in libraries.items():
_write_manifest(
tmp_path, "library.json", name, "1.0.0", dependencies=dependencies
)

result = clirunner.invoke(
package_list_cmd,
["--global", "--storage-dir", str(tmp_path), "--library", "Target"],
)

assert result.exit_code == 0, result.exception
assert "WithTarget @" in result.output
assert "Target @" in result.output
assert "A @" not in result.output
assert "B @" not in result.output


def test_project_package_list_json_reports_installed_and_missing_dependencies(
tmp_path, monkeypatch, clirunner
):
core_dir = tmp_path / "core"
monkeypatch.setenv("PLATFORMIO_CORE_DIR", str(core_dir))
(tmp_path / "platformio.ini").write_text(
"[env:one]\nplatform = example\nlib_deps = First\n"
"[env:two]\nplatform = missing-platform\nlib_deps =\n"
" First\n"
" External = https://user:example-token@example.invalid/library.git\n",
encoding="utf8",
)
_write_manifest(
core_dir / "platforms",
"platform.json",
"example",
"1.0.0",
packages={
"compiler": {"version": "*"},
"optional": {"version": "*", "optional": True},
},
)
_write_manifest(core_dir / "packages", "package.json", "compiler", "3.0.0")
libdeps = tmp_path / ".pio" / "libdeps"
_write_manifest(
libdeps / "one",
"library.json",
"First",
"1.0.0",
dependencies={"Second": "*", "Missing": "*"},
)
_write_manifest(
libdeps / "one", "library.json", "Second", "1.0.0", dependencies={"First": "*"}
)
_write_manifest(libdeps / "two", "library.json", "First", "2.0.0")

args = ["--project-dir", str(tmp_path), "--json-output"]
result = clirunner.invoke(package_list_cmd, args)
assert result.exit_code == 0, result.output
inventory = json.loads(result.stdout)
assert list(inventory) == ["one", "two"]
one, two = inventory["one"], inventory["two"]
assert {p["name"] for p in one["packages"]} == {
"example",
"compiler",
"First",
"Second",
}
assert [(p["name"], p["version"]) for p in two["packages"]] == [("First", "2.0.0")]

names = {p["id"]: p["name"] for p in one["packages"]}
edges = {
(names.get(edge["from"]), names.get(edge["to"]), edge["requested"]["name"])
for edge in one["dependencies"]
}
assert edges == {
(None, "example", "example"),
("example", "compiler", "compiler"),
(None, "First", "First"),
("First", "Second", "Second"),
("First", None, "Missing"),
("Second", "First", "First"),
}
assert any(
edge["to"] is None and edge["requested"]["name"] == "missing-platform"
for edge in two["dependencies"]
)
assert any(
edge["to"] is None
and edge["requested"]["external"]
and edge["requested"]["name"] == "External"
for edge in two["dependencies"]
)
assert "example-token" not in result.stdout
selected = clirunner.invoke(package_list_cmd, args + ["-e", "two"])
assert selected.exit_code == 0, selected.output
assert json.loads(selected.stdout) == {"two": two}


def test_project_package_list_json_rejects_global_options(clirunner):
result = clirunner.invoke(package_list_cmd, ["--json-output", "--global"])
assert result.exit_code == 2
assert "--json-output cannot be combined with --global" in result.output