Skip to content

Commit

Permalink
Update support generate-links to symlink ibek and pvi yaml
Browse files Browse the repository at this point in the history
Make generate_links take a specific support module to link rather
than doing glob of all directories.
  • Loading branch information
GDYendell authored and root committed Nov 7, 2023
1 parent 5a7125b commit 93ced51
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 25 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ dev = [
"pydata-sphinx-theme>=0.12",
"pytest",
"pytest-cov",
"pytest-mock",
"ruff",
"Sphinx==6.2.1",
"sphinx-autobuild",
Expand Down
3 changes: 2 additions & 1 deletion src/ibek/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@

# Folder containing templates for IOC src etc.
TEMPLATES = Path(__file__).parent / "templates"
IBEK_FILES = EPICS_ROOT / "ibek"
IBEK_DEFS = EPICS_ROOT / "ibek-defs"
PVI_DEFS = EPICS_ROOT / "pvi-defs"

IOC_DBDS = SUPPORT / "configure/dbd_list"
IOC_LIBS = SUPPORT / "configure/lib_list"
Expand Down
4 changes: 2 additions & 2 deletions src/ibek/ioc_cmds/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import typer

from ibek.globals import EPICS_ROOT, IBEK_FILES, IOC_FOLDER
from ibek.globals import EPICS_ROOT, IBEK_DEFS, IOC_FOLDER


def get_ioc_source() -> Path:
Expand Down Expand Up @@ -69,7 +69,7 @@ def extract_assets(destination: Path, source: Path, extras: List[Path], defaults
default_assets = [
get_ioc_source() / "ibek-support",
source / "support" / "configure",
IBEK_FILES,
IBEK_DEFS,
IOC_FOLDER,
Path("/venv"),
]
Expand Down
39 changes: 17 additions & 22 deletions src/ibek/support_cmds/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
from typing_extensions import Annotated

from ibek.globals import (
IBEK_FILES,
IBEK_DEFS,
IOC_DBDS,
IOC_LIBS,
PVI_DEFS,
RELEASE,
RUNTIME_DEBS,
SUPPORT,
Expand All @@ -36,6 +37,7 @@
add_list_to_file,
add_text_once,
get_config_site_file,
symlink_files,
)


Expand Down Expand Up @@ -231,6 +233,12 @@ def compile(

@support_cli.command()
def generate_links(
support_module: Annotated[
Path,
typer.Argument(
help="Support module to generate links for (directory in ibek-support)"
),
],
ibek_support: Annotated[
Optional[Path],
typer.Option(
Expand All @@ -239,30 +247,17 @@ def generate_links(
),
] = None,
):
"""
generate symlinks to the bob, pvi and support YAML for a compiled IOC
"""
"""Generate symlinks to the ibek and pvi YAML files for a compiled IOC.
# symlink the bob YAML
# TODO TODO
# symlink the pvi YAML
Args:
support_module: Support module to generate links for
ibek_support: Root of ibek support to find support module directory in
# TODO ALSO symlink pvi YAML and read the bobs out of e.g. links/ibek/ADCore.ibek.support.yaml
# and link them too
# see line 33 gen_scripts.py

# symlink the support YAML
from_path = ibek_support or get_ioc_source() / "ibek-support"

support_yaml = from_path.glob("*/*.ibek.support.yaml")
"""
support_defs = (ibek_support or get_ioc_source() / "ibek-support") / support_module

to_path = IBEK_FILES
to_path.mkdir(parents=True, exist_ok=True)
for yaml in support_yaml:
link_from = to_path / yaml.name
link_from.unlink(missing_ok=True)
typer.echo(f"symlinking {yaml} to {to_path}")
link_from.symlink_to(yaml)
symlink_files(support_defs, "*ibek.support.yaml", IBEK_DEFS)
symlink_files(support_defs, "*pvi.device.yaml", PVI_DEFS)


@support_cli.command()
Expand Down
20 changes: 20 additions & 0 deletions src/ibek/support_cmds/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from pathlib import Path
from typing import List

import typer

from ibek.globals import SUPPORT


Expand Down Expand Up @@ -82,3 +84,21 @@ def add_text_once(file: Path, text: str):
current = file.read_text()
if text not in current:
file.write_text(current + text + "\n")


def symlink_files(source_directory: Path, file_pattern: str, target_directory: Path):
"""Symlink files patching the given pattern in source directory to target directory.
Args:
source_directory: Directory containing source files
file_patterm: Pattern of files in source directory to be symlinked
target_directory: Directory to create symlinks in
"""
typer.echo(f"Symlinking {file_pattern} files:")
target_directory.mkdir(parents=True, exist_ok=True)
for yaml in source_directory.glob(file_pattern):
typer.echo(f" {target_directory / yaml.name} -> {yaml}")
target = target_directory / yaml.name
target.unlink(missing_ok=True)
target.symlink_to(yaml)
20 changes: 20 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
import sys
from pathlib import Path

from pytest_mock import MockerFixture

from ibek import __version__
from ibek.ioc import clear_entity_model_ids
from ibek.support_cmds.commands import generate_links
from tests.conftest import run_cli


Expand Down Expand Up @@ -161,3 +164,20 @@ def test_build_utils_features(tmp_path: Path, samples: Path):
example_db = (samples / "outputs" / "utils.ioc.subst").read_text()
actual_db = out_db.read_text()
assert example_db == actual_db


def test_generate_links_ibek(samples: Path, mocker: MockerFixture):
symlink_mock = mocker.patch("ibek.support_cmds.commands.symlink_files")

generate_links(Path("yaml"), samples)

symlink_mock.assert_any_call(
Path("/workspace/ibek/tests/samples/yaml"),
"*pvi.device.yaml",
Path("/epics/pvi-defs"),
)
symlink_mock.assert_any_call(
Path("/workspace/ibek/tests/samples/yaml"),
"*ibek.support.yaml",
Path("/epics/ibek-defs"),
)
21 changes: 21 additions & 0 deletions tests/test_support.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""Unit tests for the support subcommand"""
from pathlib import Path

from ibek.support_cmds.files import symlink_files


def test_symlink_ibek(tmp_path: Path, samples: Path):
symlink_files(samples / "yaml", "*.ibek.support.yaml", tmp_path)

assert [f.name for f in tmp_path.iterdir()] == [
"bad_db.ibek.support.yaml",
"objects.ibek.support.yaml",
"utils.ibek.support.yaml",
"all.ibek.support.yaml",
]


def test_symlink_pvi(tmp_path: Path, samples: Path):
symlink_files(samples / "yaml", "*.pvi.device.yaml", tmp_path)

assert [f.name for f in tmp_path.iterdir()] == ["simple.pvi.device.yaml"]

0 comments on commit 93ced51

Please sign in to comment.