Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/automated-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- run: apk add git
- run: sh bin/ci/check_versions.sh
- run: uv run isort --check homely test
- run: uv run pytest test
- run: uv run pytest -W error test
Mypy:
runs-on: ubuntu-latest
# only run on oldest supported python version
Expand Down
3 changes: 2 additions & 1 deletion homely/_test/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ def system(cmd, cwd=None, expecterror=False, tmpdir=None):
print(open(stdoutpath).read())
raise

return open(stdoutpath, 'r').read()
with open(stdoutpath, 'r') as f:
return f.read()

return system

Expand Down
33 changes: 28 additions & 5 deletions homely/_utils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import contextlib
import importlib.util
import json
import os
import re
import shutil
import subprocess
import sys
import tempfile
from datetime import timedelta
from functools import partial
from importlib.machinery import SourceFileLoader
from itertools import chain
from os.path import exists, join
from typing import Any, Optional, Union
Expand All @@ -17,8 +18,28 @@
from homely._vcs import Repo, fromdict


def _loadmodule(name, path):
return SourceFileLoader(name, path).load_module()
def _loadmodule(name: str, file_path: str):
spec = importlib.util.spec_from_file_location(name, file_path)
if spec is None:
raise ImportError(f"Cannot find module spec for {name} at {file_path}")
if spec.loader is None:
raise Exception(f"No loader for module {name} at {file_path}")

module = importlib.util.module_from_spec(spec)

# Crucial step: Register the module in sys.modules *before* execution
# This prevents issues with relative imports within the module
sys.modules[name] = module

# Execute the module's code in its own namespace
try:
spec.loader.exec_module(module)
except Exception:
# If execution fails, remove the module from sys.modules
del sys.modules[name]
raise

return module


# for python3, we open text files with universal newline support
Expand Down Expand Up @@ -495,8 +516,10 @@ def filereplacer(filepath):
stripped = firstline.rstrip('\r\n')
NL = firstline[len(stripped):]
assert NL in ("\r", "\n", "\r\n"), "Bad NL %r" % NL
origlines = chain([stripped],
(line.rstrip('\r\n') for line in orig))
origlines = chain(
[stripped],
(line.rstrip('\r\n') for line in orig),
)
yield tmp, origlines, NL
else:
yield tmp, None, "\n"
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ version = "0.20.3"
dependencies = [
"python-daemon>=2.3.0",
"requests>=2.25.1",
"click>=7.1.2",
"click>=8.3",
# tomli is required before python 3.11
"tomli>=2.3.0 ; python_full_version < '3.11'",
]
Expand Down
2 changes: 1 addition & 1 deletion test/tests.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ RUN uv venv .venv && uv pip install -e . --group=dev
COPY ./test ./test
COPY ./homely ./homely

RUN .venv/bin/pytest test -x
RUN .venv/bin/pytest -W error test -x
4 changes: 2 additions & 2 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.