Skip to content

Commit

Permalink
base: Add expose flag for tool deps
Browse files Browse the repository at this point in the history
  • Loading branch information
avdgrinten committed Nov 3, 2024
1 parent f5e5412 commit 9c03119
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 11 deletions.
87 changes: 76 additions & 11 deletions xbstrap/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ def __init__(self, missing=False, updatable=False, timestamp=None):
ArtifactFile = collections.namedtuple("ArtifactFile", ["name", "filepath", "architecture"])


def name_from_subject_id(subject_id):
assert len(subject_id) == 1
return subject_id[0]


class Config:
def __init__(
self, path, changed_source_root=None, *, debug_cfg_files=False, ignore_cfg_cache=False
Expand Down Expand Up @@ -734,6 +739,37 @@ def cargo_home(self):
return self._step_yml["cargo_home"]


# Traverse a graph until all nodes have been seen.
# Arguments:
# roots: Iterator over root nodes.
# visit: Function that is called at each node.
# Returns the nodes to traverse next (= neighbors).
# key: Optional function to map nodes to node IDs.
def traverse_graph(*, roots, visit, key=None):
seen = set()
stack = []

if key is None:
key = lambda n: n

Check failure on line 753 in xbstrap/base.py

View workflow job for this annotation

GitHub Actions / lint

xbstrap/base.py#L753

[E731] do not assign a lambda expression

for n in roots:
k = key(n)
if k in seen:
continue
seen.add(k)
stack.append(n)

while stack:
c = stack.pop()

for n in visit(c):
k = key(n)
if k in seen:
continue
seen.add(k)
stack.append(n)


class RequirementsMixin:
@property
def source_dependencies(self):
Expand Down Expand Up @@ -773,8 +809,7 @@ def visit_yml(yml):

@property
def tool_dependencies(self):
deps = set(tool_name for tool_name, stage_name in self.tool_stage_dependencies)
yield from deps
return {name_from_subject_id(subject_id) for subject_id in self.resolve_tool_deps()}

@property
def tool_stage_dependencies(self):
Expand Down Expand Up @@ -849,6 +884,36 @@ def tasks_ordered_before(self):
if "order_only" in yml and yml["order_only"]:
yield yml["task"]

def resolve_tool_deps(self, *, exposed_only=False):
deps = set()

def visit(subject):
assert isinstance(subject, RequirementsMixin)

for yml in subject._this_yml.get("tools_required", []):
if isinstance(yml, str):
yml = {"tool": yml}
assert isinstance(yml, dict)

# This interface does not support virtual tools.
if yml.get("virtual", False):
continue

# Filter out deps that should not be returned or descended into.
if exposed_only and not yml.get("expose", True):
continue

tool = self._cfg.get_tool_pkg(yml["tool"])
deps.add(tool.subject_id)

# Do not descend into deps unless the recursive flag is set.
if not yml.get("recursive", False):
continue
yield tool

traverse_graph(roots=[self], visit=visit)
return deps

def discover_recursive_pkg_dependencies(self):
s = set()
stack = [self]
Expand Down Expand Up @@ -2438,7 +2503,7 @@ def patch_src(cfg, src):
def regenerate_src(cfg, src):
for step in src.regenerate_steps:
tool_pkgs = []
for dep_name in src.tool_dependencies:
for dep_name in map(name_from_subject_id, src.resolve_tool_deps(exposed_only=True)):
tool_pkgs.append(cfg.get_tool_pkg(dep_name))

run_step(cfg, "source", src, step, tool_pkgs, src.virtual_tools)
Expand All @@ -2457,7 +2522,7 @@ def configure_tool(cfg, pkg):

for step in pkg.configure_steps:
tool_pkgs = []
for dep_name in pkg.tool_dependencies:
for dep_name in map(name_from_subject_id, pkg.resolve_tool_deps(exposed_only=True)):
tool_pkgs.append(cfg.get_tool_pkg(dep_name))

run_step(cfg, "tool", pkg, step, tool_pkgs, pkg.virtual_tools)
Expand All @@ -2470,7 +2535,7 @@ def compile_tool_stage(cfg, stage):

for step in stage.compile_steps:
tool_pkgs = []
for dep_name in pkg.tool_dependencies:
for dep_name in map(name_from_subject_id, pkg.resolve_tool_deps(exposed_only=True)):
tool_pkgs.append(cfg.get_tool_pkg(dep_name))

run_step(cfg, "tool-stage", stage, step, tool_pkgs, pkg.virtual_tools)
Expand Down Expand Up @@ -2508,7 +2573,7 @@ def install_tool_stage(cfg, stage):

for step in stage.install_steps:
tool_pkgs = []
for dep_name in tool.tool_dependencies:
for dep_name in map(name_from_subject_id, tool.resolve_tool_deps(exposed_only=True)):
tool_pkgs.append(cfg.get_tool_pkg(dep_name))

run_step(cfg, "tool-stage", stage, step, tool_pkgs, tool.virtual_tools)
Expand All @@ -2533,7 +2598,7 @@ def configure_pkg(cfg, pkg, *, sysroot):

for step in pkg.configure_steps:
tool_pkgs = []
for dep_name in pkg.tool_dependencies:
for dep_name in map(name_from_subject_id, pkg.resolve_tool_deps(exposed_only=True)):
tool_pkgs.append(cfg.get_tool_pkg(dep_name))

run_step(
Expand All @@ -2550,7 +2615,7 @@ def build_pkg(cfg, pkg, *, sysroot, reproduce=False):

for step in pkg.build_steps:
tool_pkgs = []
for dep_name in pkg.tool_dependencies:
for dep_name in map(name_from_subject_id, pkg.resolve_tool_deps(exposed_only=True)):
tool_pkgs.append(cfg.get_tool_pkg(dep_name))

run_step(
Expand Down Expand Up @@ -2860,7 +2925,7 @@ def pull_archive(cfg, subject):

def run_task(cfg, task):
tools_required = []
for dep_name in task.tool_dependencies:
for dep_name in map(name_from_subject_id, task.resolve_tool_deps(exposed_only=True)):
tools_required.append(cfg.get_tool_pkg(dep_name))

run_step(
Expand All @@ -2870,7 +2935,7 @@ def run_task(cfg, task):

def run_pkg_task(cfg, task):
tools_required = []
for dep_name in task.pkg.tool_dependencies:
for dep_name in map(name_from_subject_id, task.pkg.resolve_tool_deps(exposed_only=True)):
tools_required.append(cfg.get_tool_pkg(dep_name))

run_step(
Expand All @@ -2886,7 +2951,7 @@ def run_pkg_task(cfg, task):

def run_tool_task(cfg, task):
tools_required = []
for dep_name in task.pkg.tool_dependencies:
for dep_name in map(name_from_subject_id, task.pkg.resolve_tool_deps(exposed_only=True)):
tools_required.append(cfg.get_tool_pkg(dep_name))

run_step(
Expand Down
2 changes: 2 additions & 0 deletions xbstrap/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ definitions:
type: array
items:
type: string
'expose':
type: boolean
- type: object
additionalProperties: false
properties:
Expand Down

0 comments on commit 9c03119

Please sign in to comment.