From 03572ae35573c2ea802a540624ce116f540ba1ac Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Fri, 17 Nov 2017 06:13:35 -0800 Subject: [PATCH] Allow --load-extension to be set via environment variable I tesed this by first building and running a container using the new Dockerfile from #114: docker build . docker run -it -p 8001:8001 6c9ca7e29181 /bin/sh Then I ran this inside the container itself: apt update && apt-get install wget -y \ && wget http://www.gaia-gis.it/spatialite-2.3.1/test-2.3.sqlite.gz \ && gunzip test-2.3.sqlite.gz \ && mv test-2.3.sqlite test23.sqlite \ && datasette -h 0.0.0.0 test23.sqlite I visited this URL to confirm I got an error due to spatialite not being loaded: http://localhost:8001/test23-c88bc35?sql=select+ST_AsText%28Geometry%29+from+HighWays+limit+1 Then I checked that loading it with `--load-extension` worked correctly: datasette -h 0.0.0.0 test23.sqlite \ --load-extension=/usr/lib/x86_64-linux-gnu/mod_spatialite.so Then, finally, I tested it with the new environment variable option: SQLITE_EXTENSIONS=/usr/lib/x86_64-linux-gnu/mod_spatialite.so \ datasette -h 0.0.0.0 test23.sqlite Running it with an invalid environment variable option shows an error: $ SQLITE_EXTENSIONS=/usr/lib/x86_64-linux-gnu/blah.so datasette \ -h 0.0.0.0 test23.sqlite Usage: datasette -h [OPTIONS] [FILES]... Error: Invalid value for "--load-extension": Path "/usr/lib/x86_64-linux-gnu/blah.so" does not exist. Closes #112 --- datasette/cli.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/datasette/cli.py b/datasette/cli.py index d5d4957d..3c07d7df 100644 --- a/datasette/cli.py +++ b/datasette/cli.py @@ -116,10 +116,13 @@ def package(files, tag, metadata, extra_options, **extra_metadata): @click.option('--page_size', default=100, help='Page size - default is 100') @click.option('--max_returned_rows', default=1000, help='Max allowed rows to return at once - default is 1000. Set to 0 to disable check entirely.') @click.option('--sql_time_limit_ms', default=1000, help='Max time allowed for SQL queries in ms') -@click.option('--load-extension', multiple=True, help="Path to a SQLite extension to load") +@click.option( + 'sqlite_extensions', '--load-extension', envvar='SQLITE_EXTENSIONS', multiple=True, + type=click.Path(exists=True, resolve_path=True), help='Path to a SQLite extension to load' +) @click.option('--inspect-file', help='Path to JSON file created using "datasette build"') @click.option('-m', '--metadata', type=click.File(mode='r'), help='Path to JSON file containing license/source metadata') -def serve(files, host, port, debug, reload, cors, page_size, max_returned_rows, sql_time_limit_ms, load_extension, inspect_file, metadata): +def serve(files, host, port, debug, reload, cors, page_size, max_returned_rows, sql_time_limit_ms, sqlite_extensions, inspect_file, metadata): """Serve up specified SQLite database files with a web UI""" if reload: import hupper @@ -143,7 +146,7 @@ def serve(files, host, port, debug, reload, cors, page_size, max_returned_rows, sql_time_limit_ms=sql_time_limit_ms, inspect_data=inspect_data, metadata=metadata_data, - sqlite_extensions=load_extension, + sqlite_extensions=sqlite_extensions, ) # Force initial hashing/table counting ds.inspect()