Skip to content

Commit

Permalink
pw_watch: Make httpwatcher optional
Browse files Browse the repository at this point in the history
httpwatcher 0.5.2 requires tornado <5, which is not compatible with
Python 3.10. httpwatcher does not appear to be actively maintained, and
has not been updated since 2018.

To ensure pw_watch works with Python 3.10 without breaking existing
users of the --serve-docs option, make the httpwatcher import optional.

Change-Id: I4ad3571b87358ab227e902d6299e8a5f97f97cb8
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/100662
Reviewed-by: Anthony DiGirolamo <tonymd@google.com>
Pigweed-Auto-Submit: Wyatt Hepler <hepler@google.com>
Commit-Queue: Auto-Submit <auto-submit@pigweed.google.com.iam.gserviceaccount.com>
  • Loading branch information
255 authored and CQ Bot Account committed Jul 1, 2022
1 parent de64d37 commit 74144e6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ googleapis-common-protos==1.56.0
graphlib-backport==1.0.3;python_version<'3.9'
grpcio==1.43.0
grpcio-tools==1.43.0
httpwatcher==0.5.2
humanfriendly==10.0
idna==3.3
imagesize==1.3.0
Expand Down
53 changes: 32 additions & 21 deletions pw_watch/py/pw_watch/watch.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@
Tuple,
)

import httpwatcher # type: ignore
try:
import httpwatcher # type: ignore[import]
except ImportError:
httpwatcher = None

from watchdog.events import FileSystemEventHandler # type: ignore[import]
from watchdog.observers import Observer # type: ignore[import]
Expand Down Expand Up @@ -607,23 +610,23 @@ def add_parser_arguments(parser: argparse.ArgumentParser) -> None:
dest='serve_docs',
action='store_true',
default=False,
help='Start a webserver for docs on localhost. The port for this '
' webserver can be set with the --serve-docs-port option. '
' Defaults to http://127.0.0.1:8000')
help=('Start a webserver for docs on localhost. The port for this '
'webserver can be set with the --serve-docs-port option. '
'Defaults to http://127.0.0.1:8000. This option requires '
'the httpwatcher package to be installed.'))
parser.add_argument(
'--serve-docs-port',
dest='serve_docs_port',
type=int,
default=8000,
help='Set the port for the docs webserver. Default to 8000.')

parser.add_argument(
'--serve-docs-path',
dest='serve_docs_path',
type=Path,
default="docs/gen/docs",
help='Set the path for the docs to serve. Default to docs/gen/docs'
' in the build directory.')
help=('Set the path for the docs to serve. Default to docs/gen/docs'
' in the build directory.'))
parser.add_argument(
'-j',
'--jobs',
Expand Down Expand Up @@ -788,6 +791,26 @@ def get_common_excludes() -> List[Path]:
return exclude_list


def _serve_docs(build_dir: Path, serve_docs_port: int,
serve_docs_path: Path) -> None:
if httpwatcher is None:
_LOG.warning(
'--serve-docs was specified, but httpwatcher is not available')
_LOG.info('Install httpwatcher to use --serve-docs')
return

def httpwatcher_thread():
# Disable logs from httpwatcher and deps
logging.getLogger('httpwatcher').setLevel(logging.CRITICAL)
logging.getLogger('tornado').setLevel(logging.CRITICAL)

docs_path = build_dir.joinpath(serve_docs_path.joinpath('html'))
httpwatcher.watch(docs_path, host='127.0.0.1', port=serve_docs_port)

# Spin up an httpwatcher in a new thread since it blocks
threading.Thread(None, httpwatcher_thread, 'httpwatcher').start()


def watch_setup(
default_build_targets: List[str],
build_directories: List[str],
Expand Down Expand Up @@ -846,20 +869,8 @@ def watch_setup(
_LOG.debug('Patterns: %s', patterns)

if serve_docs:

def _serve_docs():
# Disable logs from httpwatcher and deps
logging.getLogger('httpwatcher').setLevel(logging.CRITICAL)
logging.getLogger('tornado').setLevel(logging.CRITICAL)

docs_path = build_commands[0].build_dir.joinpath(
serve_docs_path.joinpath('html'))
httpwatcher.watch(docs_path,
host="127.0.0.1",
port=serve_docs_port)

# Spin up an httpwatcher in a new thread since it blocks
threading.Thread(None, _serve_docs, "httpwatcher").start()
_serve_docs(build_commands[0].build_dir, serve_docs_port,
serve_docs_path)

# Try to make a short display path for the watched directory that has
# "$HOME" instead of the full home directory. This is nice for users
Expand Down
1 change: 0 additions & 1 deletion pw_watch/py/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ description = Pigweed automatic builder
packages = find:
zip_safe = False
install_requires =
httpwatcher
watchdog>=2.1.0

[options.package_data]
Expand Down

0 comments on commit 74144e6

Please sign in to comment.