From 7077b2115938ed5eba05c4894eb87ad9ea143e16 Mon Sep 17 00:00:00 2001 From: Leo Vasanko Date: Mon, 13 Nov 2023 16:19:33 -0800 Subject: [PATCH] Add frontend build to Python packaging. Remove dead code, cleanup. --- cista/protocol.py | 12 ------------ cista/watching.py | 26 +++----------------------- pyproject.toml | 8 +++----- scripts/build-frontend.py | 12 ++++++++++++ 4 files changed, 18 insertions(+), 40 deletions(-) create mode 100644 scripts/build-frontend.py diff --git a/cista/protocol.py b/cista/protocol.py index ff76abe..6714f39 100644 --- a/cista/protocol.py +++ b/cista/protocol.py @@ -149,15 +149,3 @@ class Space(msgspec.Struct): free: int usage: int storage: int - - -def make_dir_data(root): - if len(root) == 3: - return FileEntry(*root) - id_, size, mtime, listing = root - converted = {} - for name, data in listing.items(): - converted[name] = make_dir_data(data) - sz = sum(x.size for x in converted.values()) - mt = max(x.mtime for x in converted.values()) - return DirEntry(id_, sz, max(mt, mtime), converted) diff --git a/cista/watching.py b/cista/watching.py index aa50005..a42bb0b 100644 --- a/cista/watching.py +++ b/cista/watching.py @@ -110,26 +110,6 @@ class State: with self.lock: del self._listing[self._slice(relpath)] - def _index(self, rel: PurePosixPath): - idx = 0 - ret = [] - - def _dir(self, idx: int): - level = self._listing[idx].level + 1 - end = len(self._listing) - idx += 1 - ret = [] - while idx < end and (r := self._listing[idx]).level >= level: - if r.level == level: - ret.append(idx) - return ret, idx - - def update(self, rel: PurePosixPath, value: FileEntry): - begin = 0 - parents = [] - while self._listing[begin].level < len(rel.parts): - parents.append(begin) - state = State() rootpath: Path = None # type: ignore @@ -160,8 +140,8 @@ def watcher_thread(loop): state.root = new broadcast(format_update(old, new), loop) - # The watching is not entirely reliable, so do a full refresh every minute - refreshdl = time.monotonic() + 60.0 + # The watching is not entirely reliable, so do a full refresh every 30 seconds + refreshdl = time.monotonic() + 30.0 for event in i.event_gen(): if quit: @@ -337,7 +317,7 @@ async def abroadcast(msg): async def start(app, loop): config.load_config() - use_inotify = False and sys.platform == "linux" + use_inotify = sys.platform == "linux" app.ctx.watcher = threading.Thread( target=watcher_thread if use_inotify else watcher_thread_poll, args=[loop], diff --git a/pyproject.toml b/pyproject.toml index bc4fdb8..d61480d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -40,20 +40,18 @@ dev = [ "ruff", ] -[tool.hatchling] -# Build frontend -pre_build = "npm run build --prefix cista-front" - [tool.hatch.version] source = "vcs" [tool.hatch.build] +artifacts = ["cista/wwwroot"] +hooks.custom.path = "scripts/build-frontend.py" hooks.vcs.version-file = "cista/_version.py" hooks.vcs.template = """ # This file is automatically generated by hatch build. __version__ = {version!r} """ - +only-packages = true targets.sdist.include = [ "/cista", ] diff --git a/scripts/build-frontend.py b/scripts/build-frontend.py new file mode 100644 index 0000000..10c1756 --- /dev/null +++ b/scripts/build-frontend.py @@ -0,0 +1,12 @@ +# noqa: INP001 +import subprocess + +from hatchling.builders.hooks.plugin.interface import BuildHookInterface + + +class CustomBuildHook(BuildHookInterface): + def initialize(self, version, build_data): + super().initialize(version, build_data) + print("Building Cista frontend...") + subprocess.run("npm install --prefix frontend".split(" "), check=True) # noqa: S603 + subprocess.run("npm run build --prefix frontend".split(" "), check=True) # noqa: S603