define-things/define_things/cli/patch_linkml.py
Stephan Heunis 7e04121112
All checks were successful
Install define-things / Install define-things with uv (push) Successful in 22s
Update function of patching cli
Any new concepts project generated by and managed with this tool will not necessarily
use hatch as an environment management tool. Therefore, the current dependence on a
post-install hook to run the patches is not bulletproof. The new approach is to have
multiple python methods responsible for managing patches, and should then be called
from all other cli methodss that require the patches to be applied. The new methods
allow checking whether a patch has been applied already or needs to be applied, or
whether the to-be-patched file is in an ambiguous state. It the allows applying the
provided patch. Specified patches are currently part of the package code, and could
in future perhaps be provided externally, in addition. Another method is a TODO:
extracting a method that can be imported into other modules in order to check and
then apply a patch or all of the patches.
2026-11-19 14:23:41 +02:00

159 lines
No EOL
4.1 KiB
Python

from enum import Enum
from importlib import import_module
from importlib.resources import files
from pathlib import Path
import click
import linkml
import subprocess
PATCHES = [
(
"linkml.generators.shaclgen",
"shaclgen_annotations.diff",
),
(
"linkml_runtime.loaders",
"rdflib_loader_typedesignator.diff",
),
(
"linkml.generators.graphqlgen",
"graphqlgen_interface_list.diff",
),
(
"linkml.generators.common.ifabsent_processor",
"linkml_generators_common_ifabsent.diff",
),
(
"linkml_runtime.loaders",
"rdflib_loader_custom_types.diff",
),
]
def get_linkml_path():
"""Return the root directory of the installed LinkML package."""
return Path(linkml.__file__).parent
class PatchStatus(Enum):
APPLIED = "already applied"
NEEDS_APPLY = "needs to be applied"
INCOMPATIBLE = "cannot be applied"
AMBIGUOUS = "ambiguous patch state"
def check_patch(patch_file, target_dir):
"""Determine whether a patch is applied, needs applying, or is incompatible, or ambiguous.
This is done by comparing the outcomes of applying the patch in both forward and reverse
directions, using the --dry-run option to keep the file intact.
Forward Fails + Reverse Succeeds -> already applied
Forward Succeeds + Reverse Fails -> needs to be applied
Both fail -> cannot be applied for some reason
Both succeed -> unsure (likely the result of having applied patches twice)
"""
def dry_run(reverse=False):
command = [
"patch",
"--batch",
"--dry-run",
"--forward",
"-d",
str(target_dir),
"-i",
str(patch_file),
]
if reverse:
command.append("--reverse")
result = subprocess.run(
command,
capture_output=True,
text=True,
)
return result.returncode == 0
forward = dry_run()
reverse = dry_run(reverse=True)
if forward and not reverse:
return PatchStatus.NEEDS_APPLY
if reverse and not forward:
return PatchStatus.APPLIED
if forward and reverse:
return PatchStatus.AMBIGUOUS
return PatchStatus.INCOMPATIBLE
def apply_patch(patch_file, target_dir, force=False, reverse=False):
status = check_patch(patch_file, target_dir)
if not force:
if status == PatchStatus.APPLIED:
print(f"Already applied: {patch_file.name}")
return
if status == PatchStatus.INCOMPATIBLE:
raise RuntimeError(
f"Cannot apply patch {patch_file.name} to {target_dir}. "
)
if status == PatchStatus.AMBIGUOUS:
raise RuntimeError(
f"Ambiguous state for patch {patch_file.name} in {target_dir}."
)
print(f"Applying {patch_file.name} to {target_dir}")
subprocess.run(
[
"patch",
"--batch",
"-d",
str(target_dir),
"-i",
str(patch_file),
],
check=True,
)
def reverse_patch(patch_file, target_dir):
apply_patch(patch_file, target_dir, force=True, reverse=True)
@click.command('patch-linkml')
@click.option(
"--check",
is_flag=True,
help="Check LinkML patch status without applying patches.",
)
@click.option(
"--reverse",
is_flag=True,
help="Apply patches in reverse.",
)
def cli(check=False, reverse=False):
"""Apply all patches to the installed LinkML dependencies"""
patch_dir = files("define_things").joinpath("patches")
for module_name, patch_name in PATCHES:
module = import_module(module_name)
module_dir = Path(module.__file__).parent
patch_file = patch_dir.joinpath(patch_name)
if reverse:
reverse_patch(patch_file, module_dir)
continue
status = check_patch(patch_file, module_dir)
click.echo(f"{patch_name}: {status.value}")
if status == PatchStatus.NEEDS_APPLY and not check:
apply_patch(patch_file, module_dir)