Allow destination to be read from option sepcified in config file.
Write error to log when destination doesn't exist and config is not available. Add self to AUTHORS. Remove logging from serve command due to issues with running test_cli. Add test-case for 'serve' command. Add docs for the serve command.
This commit is contained in:
committed by
Simon Conseil
parent
2d1be134e7
commit
9e92415a50
1
AUTHORS
1
AUTHORS
@@ -3,4 +3,5 @@ alphabetical order):
|
||||
|
||||
- Christophe-Marie Duquesne
|
||||
- Matthias Vogelgesang
|
||||
- Vikram Shirgur
|
||||
- Yuce Tekol
|
||||
|
||||
@@ -34,7 +34,7 @@ Serve
|
||||
``index.html`` to the urls to allow browsing without a server.
|
||||
|
||||
|
||||
Help of the ``sigal build`` command
|
||||
Help on the ``sigal build`` command
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
::
|
||||
@@ -73,3 +73,22 @@ Optional arguments:
|
||||
|
||||
``-n NCPU, --ncpu NCPU``
|
||||
Number of cpu to use (default: all)
|
||||
|
||||
Help on the ``sigal serve`` command
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
::
|
||||
|
||||
$ sigal serve [-c CONFIG] [-p PORT] [destination]
|
||||
|
||||
Optional arguments:
|
||||
|
||||
``-c CONFIG, --config CONFIG``
|
||||
Configuration file (default: ``sigal.conf.py`` in the current working
|
||||
directory)
|
||||
|
||||
``-p PORT, --port PORT``
|
||||
Port number to start the server on (default: 8000)
|
||||
|
||||
``destination``
|
||||
Destination directory where the output of build is located (default: _build)
|
||||
|
||||
@@ -169,24 +169,35 @@ def init_plugins(settings):
|
||||
|
||||
|
||||
@main.command()
|
||||
@argument('path', default='_build')
|
||||
@argument('destination', default='_build')
|
||||
@option('-p', '--port', help="Port to use", default=8000)
|
||||
def serve(path, port):
|
||||
@option('-c', '--config', default=_DEFAULT_CONFIG_FILE, show_default=True,
|
||||
help='Configuration file')
|
||||
def serve(destination, port, config):
|
||||
"""Run a simple web server."""
|
||||
|
||||
if os.path.exists(path):
|
||||
os.chdir(path)
|
||||
Handler = server.SimpleHTTPRequestHandler
|
||||
httpd = socketserver.TCPServer(("", port), Handler, False)
|
||||
print(" * Running on http://127.0.0.1:{}/".format(port))
|
||||
|
||||
try:
|
||||
httpd.allow_reuse_address = True
|
||||
httpd.server_bind()
|
||||
httpd.server_activate()
|
||||
httpd.serve_forever()
|
||||
except KeyboardInterrupt:
|
||||
print('\nAll done!')
|
||||
if os.path.exists(destination):
|
||||
pass
|
||||
elif os.path.exists(config):
|
||||
settings = read_settings(config)
|
||||
destination = settings.get('destination')
|
||||
if not os.path.exists(destination):
|
||||
sys.stderr.write("The '{}' directory doesn't exist, maybe try building first?\n".format(destination))
|
||||
sys.exit(1)
|
||||
else:
|
||||
sys.stderr.write("The '%s' directory doesn't exist.\n" % path)
|
||||
sys.exit(1)
|
||||
sys.stderr.write("The {destination} directory doesn't exist and the config file ({config}) could not be read.".format(destination=destination, config=config))
|
||||
sys.exit(2)
|
||||
|
||||
print('DESTINATION : {}'.format(destination))
|
||||
os.chdir(destination)
|
||||
Handler = server.SimpleHTTPRequestHandler
|
||||
httpd = socketserver.TCPServer(("", port), Handler, False)
|
||||
print(" * Running on http://127.0.0.1:{}/".format(port))
|
||||
|
||||
try:
|
||||
httpd.allow_reuse_address = True
|
||||
httpd.server_bind()
|
||||
httpd.server_activate()
|
||||
httpd.serve_forever()
|
||||
except KeyboardInterrupt:
|
||||
print('\nAll done!')
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import os
|
||||
from click.testing import CliRunner
|
||||
|
||||
from sigal import init
|
||||
from sigal import serve
|
||||
|
||||
|
||||
def test_init(tmpdir):
|
||||
@@ -18,3 +19,16 @@ def test_init(tmpdir):
|
||||
assert result.exit_code == 1
|
||||
assert result.output == ("Found an existing config file, will abort to "
|
||||
"keep it safe.\n")
|
||||
|
||||
def test_serve(tmpdir):
|
||||
config_file = str(tmpdir.join('sigal.conf.py'))
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(init, [config_file])
|
||||
assert result.exit_code == 0
|
||||
|
||||
result = runner.invoke(serve)
|
||||
assert result.exit_code == 2
|
||||
|
||||
result = runner.invoke(serve, ['-c', config_file])
|
||||
assert result.exit_code == 1
|
||||
|
||||
|
||||
Reference in New Issue
Block a user