Browse Source

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.
pull/107/merge
Vikram Shirgur 12 years ago committed by Simon Conseil
parent
commit
9e92415a50
  1. 1
      AUTHORS
  2. 21
      docs/getting_started.rst
  3. 47
      sigal/__init__.py
  4. 14
      tests/test_cli.py

1
AUTHORS

@ -3,4 +3,5 @@ alphabetical order):
- Christophe-Marie Duquesne - Christophe-Marie Duquesne
- Matthias Vogelgesang - Matthias Vogelgesang
- Vikram Shirgur
- Yuce Tekol - Yuce Tekol

21
docs/getting_started.rst

@ -34,7 +34,7 @@ Serve
``index.html`` to the urls to allow browsing without a server. ``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`` ``-n NCPU, --ncpu NCPU``
Number of cpu to use (default: all) 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)

47
sigal/__init__.py

@ -169,24 +169,35 @@ def init_plugins(settings):
@main.command() @main.command()
@argument('path', default='_build') @argument('destination', default='_build')
@option('-p', '--port', help="Port to use", default=8000) @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.""" """Run a simple web server."""
if os.path.exists(destination):
if os.path.exists(path): pass
os.chdir(path) elif os.path.exists(config):
Handler = server.SimpleHTTPRequestHandler settings = read_settings(config)
httpd = socketserver.TCPServer(("", port), Handler, False) destination = settings.get('destination')
print(" * Running on http://127.0.0.1:{}/".format(port)) if not os.path.exists(destination):
sys.stderr.write("The '{}' directory doesn't exist, maybe try building first?\n".format(destination))
try: sys.exit(1)
httpd.allow_reuse_address = True
httpd.server_bind()
httpd.server_activate()
httpd.serve_forever()
except KeyboardInterrupt:
print('\nAll done!')
else: else:
sys.stderr.write("The '%s' directory doesn't exist.\n" % path) 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(1) 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!')

14
tests/test_cli.py

@ -4,6 +4,7 @@ import os
from click.testing import CliRunner from click.testing import CliRunner
from sigal import init from sigal import init
from sigal import serve
def test_init(tmpdir): def test_init(tmpdir):
@ -18,3 +19,16 @@ def test_init(tmpdir):
assert result.exit_code == 1 assert result.exit_code == 1
assert result.output == ("Found an existing config file, will abort to " assert result.output == ("Found an existing config file, will abort to "
"keep it safe.\n") "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

Loading…
Cancel
Save