Skip to content

Commit

Permalink
Fix compatibility with watchexec 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
raxod502 committed May 23, 2024
1 parent 146db42 commit 88e574a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
2 changes: 1 addition & 1 deletion watcher/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
author_email="[email protected]",
description="Internal utilities for straight.el.",
license="MIT",
install_requires=["psutil==5.6.6"],
install_requires=["psutil>=5.6.6,<6.0.0", "packaging>=24.0,<25.0"],
name="straight-watcher",
url="https://github.com/radian-software/straight.el",
version="1.0-dev",
Expand Down
39 changes: 37 additions & 2 deletions watcher/straight_watch.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#!/usr/bin/env python3

import os
import psutil
import shlex
import signal
import subprocess
import sys

from packaging.version import parse as parse_version
import psutil

SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
CALLBACK_SCRIPT = os.path.join(SCRIPT_DIR, "straight_watch_callback.py")

Expand Down Expand Up @@ -44,12 +46,45 @@ def write_process_data(pid_file):
print(create_time, file=f)


def get_watchexec_version():
return parse_version(
subprocess.run(["watchexec", "--version"], stdout=subprocess.PIPE)
.stdout.decode()
.splitlines()[0]
.removeprefix("watchexec ")
.split()[0]
)


def start_watch(repos_dir, modified_dir):
callback_cmd = [CALLBACK_SCRIPT, repos_dir, modified_dir]
callback_sh = " ".join(map(shlex.quote, callback_cmd))
# Use --debounce explicitly as some versions of watchexec do not support -d
# https://github.com/watchexec/watchexec/pull/513#issuecomment-1683304057
cmd = ["watchexec", "--no-vcs-ignore", "-p", "--debounce", "100", callback_sh]
#
# Use --emit-events-to=environment, which used to be the default
# but is no longer in watchexec 2.0. It is considered deprecated
# and we will replace it in the future with another approach, but
# for now I am leaving it as is.
#
# We have to make sure to only use --emit-events-to when the
# version of watchexec is 1.22.0 or later, I am keeping in support
# for the old version because it only came out in March 2023 and a
# lot of people have not upgraded yet.
watchexec_ver = get_watchexec_version()
cmd = [
"watchexec",
"--no-vcs-ignore",
"-p",
"--debounce",
"100ms",
*(
["--emit-events-to=environment"]
if watchexec_ver >= parse_version("1.22.0")
else []
),
callback_sh,
]
cmd_sh = " ".join(map(shlex.quote, cmd))
print("$ " + cmd_sh, file=sys.stderr)
subprocess.run(cmd, cwd=repos_dir, check=True)
Expand Down
2 changes: 1 addition & 1 deletion watcher/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1
2

0 comments on commit 88e574a

Please sign in to comment.