Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ignore changes in installed packages #40

Merged
merged 4 commits into from
Oct 26, 2018
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
10 changes: 10 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
unreleased
==========

- Ignore changes to any system / installed files. This includes mostly
changes to any files in the stdlib and ``site-packages``. Anything that is
installed in editable mode or not installed at all will still be monitored.
This drastically reduces the number of files that ``hupper`` needs to
monitor.
See https://github.com/Pylons/hupper/pull/40

1.3.1 (2018-10-05)
==================

Expand Down
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ show-source = True
max-line-length = 80

ignore =
E722
# W504: line break after binary operator
W504,

[check-manifest]
ignore =
Expand Down
21 changes: 21 additions & 0 deletions src/hupper/compat.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# flake8: noqa
import imp
import importlib
import site
import sys

PY2 = sys.version_info[0] == 2
Expand Down Expand Up @@ -34,6 +35,26 @@
import pickle


def get_site_packages(): # pragma: no cover
try:
paths = site.getsitepackages()
if site.ENABLE_USER_SITE:
paths.append(site.getusersitepackages())
return paths

# virtualenv does not ship with a getsitepackages impl so we fallback
# to using distutils if we can
# https://github.com/pypa/virtualenv/issues/355
except Exception:
try:
from distutils.sysconfig import get_python_lib
return [get_python_lib()]

# just incase, don't fail here, it's not worth it
except Exception:
return []


################################################
# cross-compatible metaclass implementation
# Copyright (c) 2010-2012 Benjamin Peterson
Expand Down
30 changes: 28 additions & 2 deletions src/hupper/worker.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
import os
import signal
import sys
import sysconfig
import threading
import time
import traceback

from . import ipc
from .compat import get_py_path
from .compat import interrupt_main
from .compat import get_site_packages
from .interfaces import IReloaderProxy
from .utils import resolve_spec


class WatchSysModules(threading.Thread):
""" Poll ``sys.modules`` for imported modules."""
poll_interval = 1
ignore_system_paths = True

def __init__(self, callback):
super(WatchSysModules, self).__init__()
self.paths = set()
self.callback = callback
self.lock = threading.Lock()
self.stopped = False
self.system_paths = get_system_paths()

def run(self):
while not self.stopped:
Expand All @@ -40,7 +44,7 @@ def update_paths(self):
self.paths.add(path)
new_paths.append(path)
if new_paths:
self.callback(new_paths)
self.watch_paths(new_paths)

def search_traceback(self, tb):
""" Inspect a traceback for new paths to add to our path set."""
Expand All @@ -52,7 +56,29 @@ def search_traceback(self, tb):
self.paths.add(path)
new_paths.append(path)
if new_paths:
self.callback(new_paths)
self.watch_paths(new_paths)

def watch_paths(self, paths):
if self.ignore_system_paths:
paths = [
path for path in paths
if not self.in_system_paths(path)
]
if paths:
self.callback(paths)

def in_system_paths(self, path):
for prefix in self.system_paths:
if path.startswith(prefix):
return True
return False


def get_system_paths():
paths = get_site_packages()
for path in sysconfig.get_paths().values():
paths.append(path)
return paths


def expand_source_paths(paths):
Expand Down