80 lines
2.8 KiB
Python
Executable File
80 lines
2.8 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# SPDX-License-Identifier: MIT
|
|
"""Find the OTA engine: the tth.meshcore Ansible collection's module_utils.
|
|
|
|
tools/ota_collection.py # where it is found (or how to get it)
|
|
|
|
The transfer of an OTA package (src/helpers/FwUpdate) is implemented ONCE, in
|
|
module_utils/meshcore_ota_engine.py of https://git.rfa.cz/tth-pub/meshcore-ansible
|
|
-- the Ansible module and these tools wrap the same code. Its module_utils do not
|
|
import Ansible, so the tools need only the files:
|
|
|
|
1. $MESHCORE_ANSIBLE: a checkout of meshcore-ansible
|
|
2. an installed collection (ansible-galaxy collection install
|
|
git+https://git.rfa.cz/tth-pub/meshcore-ansible.git), found on sys.path,
|
|
in ~/.ansible/collections or /usr/share/ansible/collections
|
|
3. a checkout next to this repo: ../meshcore-ansible
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
import types
|
|
from pathlib import Path
|
|
|
|
if __name__ == "__main__" and any(a in ("-h", "--help") for a in sys.argv[1:]):
|
|
print(__doc__)
|
|
sys.exit(0)
|
|
|
|
PKG = "ansible_collections.tth.meshcore.plugins.module_utils"
|
|
REPO = Path(__file__).resolve().parent.parent
|
|
|
|
|
|
def _mount(checkout: Path) -> None:
|
|
"""Make a plain checkout importable under its collection name."""
|
|
parts = PKG.split(".")
|
|
for i in range(1, len(parts) + 1):
|
|
name = ".".join(parts[:i])
|
|
mod = sys.modules.get(name)
|
|
if mod is None:
|
|
mod = types.ModuleType(name)
|
|
mod.__path__ = []
|
|
sys.modules[name] = mod
|
|
sys.modules[PKG].__path__ = [str(checkout / "plugins" / "module_utils")]
|
|
|
|
|
|
def _has(checkout: Path) -> bool:
|
|
return (checkout / "plugins" / "module_utils" / "meshcore_ota_engine.py").is_file()
|
|
|
|
|
|
def locate() -> str:
|
|
"""Make the engine importable; returns where it came from, exits with a hint if nowhere."""
|
|
env = os.environ.get("MESHCORE_ANSIBLE")
|
|
if env:
|
|
if not _has(Path(env)):
|
|
sys.exit(f"MESHCORE_ANSIBLE={env}: no plugins/module_utils/meshcore_ota_engine.py there")
|
|
_mount(Path(env))
|
|
return env
|
|
roots = [Path(p) for p in sys.path if p] + [
|
|
Path.home() / ".ansible" / "collections",
|
|
Path("/usr/share/ansible/collections"),
|
|
]
|
|
for root in roots:
|
|
c = root / "ansible_collections" / "tth" / "meshcore"
|
|
if _has(c):
|
|
if str(root) not in sys.path:
|
|
sys.path.append(str(root))
|
|
return str(c)
|
|
sibling = REPO.parent / "meshcore-ansible"
|
|
if _has(sibling):
|
|
_mount(sibling)
|
|
return str(sibling)
|
|
sys.exit(
|
|
"the OTA engine (tth.meshcore collection) is not here: "
|
|
"ansible-galaxy collection install git+https://git.rfa.cz/tth-pub/meshcore-ansible.git, "
|
|
"or MESHCORE_ANSIBLE=<checkout> (tools/ota_collection.py --help)"
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print(locate())
|