2017-10-27 07:08:24 +00:00
|
|
|
import click
|
2017-11-04 23:53:50 +00:00
|
|
|
from click_default_group import DefaultGroup
|
2017-11-11 20:10:51 +00:00
|
|
|
import json
|
2017-11-11 07:25:22 +00:00
|
|
|
import os
|
2017-11-11 16:00:00 +00:00
|
|
|
import shutil
|
2017-11-11 07:25:22 +00:00
|
|
|
from subprocess import call
|
2017-11-11 16:00:00 +00:00
|
|
|
import sys
|
2017-11-11 07:25:22 +00:00
|
|
|
import tempfile
|
2017-11-11 20:10:51 +00:00
|
|
|
from .app import Datasette
|
2017-11-11 07:25:22 +00:00
|
|
|
from .utils import make_dockerfile
|
2017-10-27 07:08:24 +00:00
|
|
|
|
2017-11-04 23:53:50 +00:00
|
|
|
|
|
|
|
@click.group(cls=DefaultGroup, default='serve', default_if_no_args=True)
|
2017-10-27 07:08:24 +00:00
|
|
|
def cli():
|
|
|
|
"""
|
2017-11-10 18:38:35 +00:00
|
|
|
Datasette!
|
2017-10-27 07:08:24 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
@cli.command()
|
2017-11-06 02:24:43 +00:00
|
|
|
@click.argument('files', type=click.Path(exists=True), nargs=-1)
|
2017-11-13 15:20:02 +00:00
|
|
|
@click.option('--inspect-file', default='inspect-data.json')
|
|
|
|
def build(files, inspect_file):
|
2017-11-11 20:10:51 +00:00
|
|
|
app = Datasette(files)
|
2017-11-13 15:20:02 +00:00
|
|
|
open(inspect_file, 'w').write(json.dumps(app.inspect(), indent=2))
|
2017-10-27 07:08:24 +00:00
|
|
|
|
|
|
|
|
2017-11-11 07:25:22 +00:00
|
|
|
@cli.command()
|
|
|
|
@click.argument('files', type=click.Path(exists=True), nargs=-1)
|
2017-11-13 15:20:02 +00:00
|
|
|
@click.option(
|
|
|
|
'-n', '--name', default='datasette',
|
|
|
|
help='Application name to use when deploying to Now'
|
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
'-m', '--metadata', type=click.File(mode='r'),
|
|
|
|
help='Path to JSON file containing metadata to publish'
|
|
|
|
)
|
|
|
|
def publish(files, name, metadata):
|
2017-11-11 16:00:00 +00:00
|
|
|
if not shutil.which('now'):
|
|
|
|
click.secho(
|
|
|
|
' The publish command requires "now" to be installed and configured ',
|
|
|
|
bg='red',
|
|
|
|
fg='white',
|
|
|
|
bold=True,
|
|
|
|
err=True,
|
|
|
|
)
|
|
|
|
click.echo('Follow the instructions at https://zeit.co/now#whats-now', err=True)
|
|
|
|
sys.exit(1)
|
2017-11-11 07:25:22 +00:00
|
|
|
tmp = tempfile.TemporaryDirectory()
|
|
|
|
# We create a datasette folder in there to get a nicer now deploy name
|
2017-11-13 02:12:21 +00:00
|
|
|
datasette_dir = os.path.join(tmp.name, name)
|
2017-11-11 07:25:22 +00:00
|
|
|
os.mkdir(datasette_dir)
|
|
|
|
saved_cwd = os.getcwd()
|
|
|
|
file_paths = [
|
|
|
|
os.path.join(saved_cwd, name)
|
|
|
|
for name in files
|
|
|
|
]
|
2017-11-12 23:16:24 +00:00
|
|
|
file_names = [os.path.split(f)[-1] for f in files]
|
2017-11-11 07:25:22 +00:00
|
|
|
try:
|
2017-11-13 15:20:02 +00:00
|
|
|
dockerfile = make_dockerfile(file_names, metadata and 'metadata.json')
|
2017-11-11 07:25:22 +00:00
|
|
|
os.chdir(datasette_dir)
|
|
|
|
open('Dockerfile', 'w').write(dockerfile)
|
2017-11-13 15:20:02 +00:00
|
|
|
if metadata:
|
|
|
|
open('metadata.json', 'w').write(metadata.read())
|
2017-11-12 23:16:24 +00:00
|
|
|
for path, filename in zip(file_paths, file_names):
|
2017-11-11 07:25:22 +00:00
|
|
|
os.link(path, os.path.join(datasette_dir, filename))
|
|
|
|
call('now')
|
|
|
|
finally:
|
|
|
|
tmp.cleanup()
|
|
|
|
os.chdir(saved_cwd)
|
|
|
|
|
|
|
|
|
2017-10-27 07:08:24 +00:00
|
|
|
@cli.command()
|
|
|
|
@click.argument('files', type=click.Path(exists=True), nargs=-1)
|
|
|
|
@click.option('-h', '--host', default='0.0.0.0')
|
|
|
|
@click.option('-p', '--port', default=8001)
|
|
|
|
@click.option('--debug', is_flag=True)
|
2017-11-09 13:46:16 +00:00
|
|
|
@click.option('--reload', is_flag=True)
|
2017-11-13 15:20:02 +00:00
|
|
|
@click.option('--inspect-file')
|
|
|
|
@click.option('-m', '--metadata', type=click.File(mode='r'))
|
|
|
|
def serve(files, host, port, debug, reload, inspect_file, metadata):
|
2017-11-04 23:53:50 +00:00
|
|
|
"""Serve up specified database files with a web UI"""
|
2017-11-09 13:46:16 +00:00
|
|
|
if reload:
|
|
|
|
import hupper
|
2017-11-10 18:38:35 +00:00
|
|
|
hupper.start_reloader('datasette.cli.serve')
|
2017-11-09 13:46:16 +00:00
|
|
|
|
2017-11-13 15:20:02 +00:00
|
|
|
inspect_data = None
|
|
|
|
if inspect_file:
|
|
|
|
inspect_data = json.load(open(inspect_file))
|
|
|
|
|
|
|
|
metadata_data = None
|
2017-11-11 20:10:51 +00:00
|
|
|
if metadata:
|
2017-11-13 15:20:02 +00:00
|
|
|
metadata_data = json.loads(metadata.read())
|
2017-11-11 20:10:51 +00:00
|
|
|
|
2017-10-27 07:08:24 +00:00
|
|
|
click.echo('Serve! files={} on port {}'.format(files, port))
|
2017-11-11 20:10:51 +00:00
|
|
|
app = Datasette(
|
|
|
|
files,
|
|
|
|
cache_headers=not debug and not reload,
|
2017-11-13 15:20:02 +00:00
|
|
|
inspect_data=inspect_data,
|
|
|
|
metadata=metadata_data,
|
2017-11-11 20:10:51 +00:00
|
|
|
).app()
|
2017-10-27 07:08:24 +00:00
|
|
|
app.run(host=host, port=port, debug=debug)
|