Use monkey patching for linkml patches #2

Open
opened 2026-11-24 19:31:06 +00:00 by jsheunis · 1 comment
Owner

raise the point of permissions. Our approach assumes that a user has the permissions to change the code. A regular (system) installation would not allow for that. This has worked for us, because we require dedicated/duplicate environments.
An alternative would be what we did with datalad (monkey patching).

Monkey patching is also done in dump-things-service, see https://hub.psychoinformatics.de/orinoco/dump-things-service/src/branch/master/dump_things_service/patches/init.py.

If we follow this route, we also have to attend to the environment in which the monkey patches will be applied and available. This in turn depends on how we run linkml commands, which depends on #1. If we switch do using linkml python api, monkey patches can be applied in the same way as for dump-things-service. If we stick to subprocess, we will need to apply monkey patches for the base environment of each subprocess call.

> raise the point of permissions. Our approach assumes that a user has the permissions to change the code. A regular (system) installation would not allow for that. This has worked for us, because we require dedicated/duplicate environments. > An alternative would be what we did with datalad (monkey patching). Monkey patching is also done in `dump-things-service`, see https://hub.psychoinformatics.de/orinoco/dump-things-service/src/branch/master/dump_things_service/patches/__init__.py. If we follow this route, we also have to attend to the environment in which the monkey patches will be applied and available. This in turn depends on how we run linkml commands, which depends on https://hub.psychoinformatics.de/orinoco/define-things/issues/1. If we switch do using linkml python api, monkey patches can be applied in the same way as for `dump-things-service`. If we stick to `subprocess`, we will need to apply monkey patches for the base environment of each subprocess call.
Author
Owner

An approach if sticking to subprocess is to use sitecustomize to define how patching is done (i.e. importing the monkey patches) and then pass that to the env argument of subprocess to let the process apply the patches.

IIUC it would be e.g. ./site_bootstrap/sitecustomize.py containing:

from define_things.patches import enabled # same as what dump-things does

and then the subprocess environment config would be set up, and passed to subprocess.run:

env = os.environ.copy()

env["PYTHONPATH"] = (
    "./site_bootstrap"
    + os.pathsep
    + env.get("PYTHONPATH", "")
)

subprocess.run(
    [
        'gen-shacl',
            '--include-annotations',
            '--include-stripped-prefixes',
            '--exclude-order',
            'src/registrations/unreleased.yaml'
    ],
    env=env,
    check=True,
)
An approach if sticking to subprocess is to use [`sitecustomize`](https://docs.python.org/3/library/site.html#module-sitecustomize) to define how patching is done (i.e. importing the monkey patches) and then pass that to the `env` argument of `subprocess` to let the process apply the patches. IIUC it would be e.g. `./site_bootstrap/sitecustomize.py` containing: ```python from define_things.patches import enabled # same as what dump-things does ``` and then the subprocess environment config would be set up, and passed to `subprocess.run`: ``` env = os.environ.copy() env["PYTHONPATH"] = ( "./site_bootstrap" + os.pathsep + env.get("PYTHONPATH", "") ) subprocess.run( [ 'gen-shacl', '--include-annotations', '--include-stripped-prefixes', '--exclude-order', 'src/registrations/unreleased.yaml' ], env=env, check=True, ) ```
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
orinoco/define-things#2
No description provided.