From 9e92415a5046ecd3624ad155ba6b69d076eec080 Mon Sep 17 00:00:00 2001 From: Vikram Shirgur Date: Mon, 1 Sep 2014 00:37:33 -0700 Subject: [PATCH] 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. --- AUTHORS | 1 + docs/getting_started.rst | 21 +++++++++++++++++- sigal/__init__.py | 47 +++++++++++++++++++++++++--------------- tests/test_cli.py | 14 ++++++++++++ 4 files changed, 64 insertions(+), 19 deletions(-) diff --git a/AUTHORS b/AUTHORS index 14a185f..60b9c42 100644 --- a/AUTHORS +++ b/AUTHORS @@ -3,4 +3,5 @@ alphabetical order): - Christophe-Marie Duquesne - Matthias Vogelgesang +- Vikram Shirgur - Yuce Tekol diff --git a/docs/getting_started.rst b/docs/getting_started.rst index 0c65556..83d51c4 100644 --- a/docs/getting_started.rst +++ b/docs/getting_started.rst @@ -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) diff --git a/sigal/__init__.py b/sigal/__init__.py index b9e8e5f..03763d0 100644 --- a/sigal/__init__.py +++ b/sigal/__init__.py @@ -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!') + diff --git a/tests/test_cli.py b/tests/test_cli.py index a686280..86f0912 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -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 +