Skip to content

Commit

Permalink
remove check and uprev
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin committed Feb 10, 2018
1 parent c95f78d commit 777eb46
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 66 deletions.
3 changes: 0 additions & 3 deletions aiohttp_devtools/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ def serve(path, livereload, port, verbose):
host_help = ('host used when referencing livereload and static files, if blank host is taken from the request header '
'with default of localhost. env variable AIO_HOST')
debugtoolbar_help = 'Whether to enable debug toolbar. env variable: AIO_DEBUG_TOOLBAR'
precheck_help = ("Whether to start and stop the app before creating it in a subprocess to check it's working. "
"env variable AIO_PRECHECK")
app_factory_help = ('name of the app factory to create an aiohttp.web.Application with, if missing default app-factory '
'names are tried. This can be either a function with signature '
'"def create_app(loop): -> Application" or "def create_app(): -> Application" '
Expand All @@ -68,7 +66,6 @@ def serve(path, livereload, port, verbose):
@click.option('--livereload/--no-livereload', envvar='AIO_LIVERELOAD', default=None, help=livereload_help)
@click.option('--host', default=INFER_HOST, help=host_help)
@click.option('--debug-toolbar/--no-debug-toolbar', envvar='AIO_DEBUG_TOOLBAR', default=None, help=debugtoolbar_help)
@click.option('--pre-check/--no-pre-check', envvar='AIO_PRECHECK', default=None, help=precheck_help)
@click.option('--app-factory', 'app_factory_name', envvar='AIO_APP_FACTORY', help=app_factory_help)
@click.option('-p', '--port', 'main_port', envvar='AIO_PORT', type=click.INT, help=port_help)
@click.option('--aux-port', envvar='AIO_AUX_PORT', type=click.INT, help=aux_port_help)
Expand Down
37 changes: 1 addition & 36 deletions aiohttp_devtools/runserver/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def __init__(self, *,
static_url: str='/static/',
livereload: bool=True,
debug_toolbar: bool=False, # TODO set True once debug toolbar is fixed
pre_check: bool=True,
app_factory_name: str=None,
host: str=INFER_HOST,
main_port: int=8000,
Expand All @@ -61,7 +60,6 @@ def __init__(self, *,
self.static_url = static_url
self.livereload = livereload
self.debug_toolbar = debug_toolbar
self.pre_check = pre_check
self.app_factory_name = app_factory_name
self.infer_host = host == INFER_HOST
self.host = 'localhost' if self.infer_host else host
Expand All @@ -75,10 +73,6 @@ def __init__(self, *,
def static_path_str(self):
return self.static_path and str(self.static_path)

@property
def code_directory_str(self):
return self.code_directory and str(self.code_directory)

def _find_app_path(self, app_path: str) -> Path:
path = (self.root_path / app_path).resolve()
if path.is_file():
Expand Down Expand Up @@ -166,23 +160,6 @@ def import_app_factory(self):
self.code_directory = Path(self._imported_module.__file__).parent
return attr

def check(self):
"""
run the app factory as a very basic check it's working and returns the right thing,
this should catch config errors and database connection errors.
"""
if not self.pre_check:
logger.debug('pre-check disabled, not checking app factory')
return
logger.info('pre-check enabled, checking app factory')
app_factory = self.import_app_factory()
if not callable(app_factory):
raise AdevConfigError('app_factory "{.app_factory_name}" is not callable or an '
'instance of aiohttp.web.Application'.format(self))

loop = asyncio.get_event_loop()
loop.run_until_complete(self._startup_and_clean())

def load_app(self):
app_factory = self.import_app_factory()
if isinstance(app_factory, web.Application):
Expand All @@ -203,19 +180,7 @@ def load_app(self):

return app

async def _startup_and_clean(self):
app = self.load_app()
logger.debug('app "%s" successfully created', app)
logger.debug('running app startup...')
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, host='localhost', port=8080, shutdown_timeout=0.1)
await site.start()
await app.startup()
logger.debug('running app cleanup...')
await runner.cleanup()

def __str__(self):
fields = ('py_file', 'static_path', 'static_url', 'livereload', 'debug_toolbar', 'pre_check',
fields = ('py_file', 'static_path', 'static_url', 'livereload', 'debug_toolbar',
'app_factory_name', 'host', 'main_port', 'aux_port')
return 'Config:\n' + '\n'.join(' {0}: {1!r}'.format(f, getattr(self, f)) for f in fields)
4 changes: 2 additions & 2 deletions aiohttp_devtools/runserver/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def runserver(**config_kwargs):
set_start_method('spawn')

config = Config(**config_kwargs)
config.check()
config.import_app_factory()
loop = asyncio.get_event_loop()

loop.run_until_complete(check_port_open(config.main_port, loop))
Expand All @@ -59,7 +59,7 @@ def runserver(**config_kwargs):
aux_app.on_shutdown.append(main_manager.close)

if config.static_path:
static_manager = LiveReloadTask(config.static_path_str, loop)
static_manager = LiveReloadTask(config.static_path, loop)
logger.debug('starting livereload to watch %s', config.static_path_str)
aux_app.on_startup.append(static_manager.start)
aux_app.on_shutdown.append(static_manager.close)
Expand Down
15 changes: 8 additions & 7 deletions aiohttp_devtools/runserver/watch.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def __init__(self, path: str, loop: asyncio.AbstractEventLoop):
self._loop = loop
self._app = None
self._task = None
assert path
self._awatch = awatch(path)

async def start(self, app):
Expand All @@ -37,17 +38,17 @@ def __init__(self, config: Config, loop: asyncio.AbstractEventLoop):
self._reloads = 0
self._session = ClientSession(loop=loop)
self._runner = None
super().__init__(self._config.code_directory_str, loop)
super().__init__(self._config.code_directory, loop)

async def _run(self):
await self._start_process()
await self._start_dev_server()

async for changes in self._awatch:
self._reloads += 1
if any(f.endswith('.py') for _, f in changes):
logger.debug('%d changes, restarting server', len(changes))
await self._stop_process()
await self._start_process()
await self._stop_dev_server()
await self._start_dev_server()
await self._src_reload_when_live()
elif len(changes) > 1 or any(f.endswith(self.template_files) for _, f in changes):
await src_reload(self._app)
Expand All @@ -70,17 +71,17 @@ async def _src_reload_when_live(self, checks=20):
await src_reload(self._app)
return

async def _start_process(self):
async def _start_dev_server(self):
act = 'Start' if self._reloads == 0 else 'Restart'
logger.info('%sing dev server at http://%s:%s ●', act, self._config.host, self._config.main_port)
self._runner = await start_main_app(self._config)

async def _stop_process(self):
async def _stop_dev_server(self):
logger.debug('stopping server process...')
self._runner and await self._runner.cleanup()

async def close(self, *args):
await self._stop_process()
await self._stop_dev_server()
await super().close()
await self._session.close()

Expand Down
6 changes: 3 additions & 3 deletions aiohttp_devtools/start/template/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# {# This file is special: lines are made unique, stripped and sorted before the new requirements.txt file is created #}
# you will need to install these requirements with `pip install -r requirements.txt`

aiohttp==2.3.9
pytest==3.2.3
aiohttp==3.0.0
pytest==3.4.0
pytest-aiohttp==0.1.3
pytest-cov==2.5.1

# {% if template_engine.is_jinja %}
aiohttp-jinja2==0.14.0
aiohttp-jinja2==0.15.0
# {% endif %}

# {% if session.is_secure %}
Expand Down
2 changes: 1 addition & 1 deletion tests/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pytest-xdist==1.20.1
Sphinx==1.6.5

# required to run "start" apps
aiohttp-jinja2==0.14.0
aiohttp-jinja2==0.15.0
aiohttp-session[secure]==1.0.1
aiohttp-session[aioredis]==1.0.1
aiopg==0.13.1
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_runserver(mocker):
mock_runserver = mocker.patch('aiohttp_devtools.cli._runserver')
runner = CliRunner()
result = runner.invoke(cli, ['runserver', '.'])
assert result.exit_code == 0
assert result.exit_code == 0, result.output
assert '' == result.output
assert mock_run_app.call_count == 1
assert mock_runserver.call_count == 1
Expand Down
12 changes: 0 additions & 12 deletions tests/test_runserver_config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import asyncio

import pytest
from pytest_toolbox import mktree

Expand Down Expand Up @@ -54,13 +52,3 @@ def app_factory(loop):
'app factory "app_factory" returned "int" not an aiohttp.web.Application'
)
]


@if_boxed
@pytest.mark.parametrize('files,exc', invalid_apps, ids=['%s...' % v[1][:40] for v in invalid_apps])
def test_invalid_options(tmpworkdir, files, exc, loop):
asyncio.set_event_loop(loop)
mktree(tmpworkdir, files)
with pytest.raises(AiohttpDevConfigError) as excinfo:
Config(app_path='.').check()
assert exc.format(tmpworkdir=tmpworkdir) == excinfo.value.args[0]
1 change: 0 additions & 1 deletion tests/test_runserver_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ async def check_callback(session):
finally:
loop.run_until_complete(stop_app(aux_app))
assert (
'adev.server.dft INFO: pre-check enabled, checking app factory\n'
'adev.server.dft INFO: Starting aux server at http://localhost:8001 ◆\n'
'adev.server.dft INFO: serving static files from ./static_dir/ at http://localhost:8001/static/\n'
'adev.server.dft INFO: Starting dev server at http://localhost:8000 ●\n'
Expand Down

0 comments on commit 777eb46

Please sign in to comment.