Added extra metadata options to publish and package commands

You can now run these commands like so:

    datasette now publish mydb.db \
        --title="My Title" \
        --source="Source" \
        --source_url="http://www.example.com/" \
        --license="CC0" \
        --license_url="https://creativecommons.org/publicdomain/zero/1.0/"

This will write those values into the metadata.json that is packaged with the
app. If you also pass --metadata= that file will be updated with the extra
values before being written into the Docker image.

Closes #92
pull/107/head
Simon Willison 2017-11-14 21:02:11 -08:00
rodzic 86755503d2
commit 7fe1e8b482
3 zmienionych plików z 36 dodań i 8 usunięć

Wyświetl plik

@ -139,6 +139,11 @@ This will create a docker image containing both the datasette application and th
-m, --metadata FILENAME Path to JSON file containing metadata to publish
--extra-options TEXT Extra options to pass to datasette serve
--force Pass --force option to now
--title TEXT Title for metadata
--license TEXT License label for metadata
--license_url TEXT License URL for metadata
--source TEXT Source label for metadata
--source_url TEXT Source URL for metadata
--help Show this message and exit.
## datasette package
@ -155,6 +160,11 @@ If you have docker installed you can use `datasette package` to create a new Doc
optionally use name:tag format
-m, --metadata FILENAME Path to JSON file containing metadata to publish
--extra-options TEXT Extra options to pass to datasette serve
--title TEXT Title for metadata
--license TEXT License label for metadata
--license_url TEXT License URL for metadata
--source TEXT Source label for metadata
--source_url TEXT Source URL for metadata
--help Show this message and exit.
Both publish and package accept an `extra_options` argument option, which will affect how the resulting application is executed. For example, say you want to increase the SQL time limit for a particular container:

Wyświetl plik

@ -38,7 +38,12 @@ def build(files, inspect_file):
)
@click.option('--extra-options', help='Extra options to pass to datasette serve')
@click.option('--force', is_flag=True, help='Pass --force option to now')
def publish(publisher, files, name, metadata, extra_options, force):
@click.option('--title', help='Title for metadata')
@click.option('--license', help='License label for metadata')
@click.option('--license_url', help='License URL for metadata')
@click.option('--source', help='Source label for metadata')
@click.option('--source_url', help='Source URL for metadata')
def publish(publisher, files, name, metadata, extra_options, force, **extra_metadata):
"""
Publish specified SQLite database files to the internet along with a datasette API.
@ -58,7 +63,7 @@ def publish(publisher, files, name, metadata, extra_options, force):
click.echo('Follow the instructions at https://zeit.co/now#whats-now', err=True)
sys.exit(1)
with temporary_docker_directory(files, name, metadata, extra_options):
with temporary_docker_directory(files, name, metadata, extra_options, extra_metadata):
if force:
call(['now', '--force'])
else:
@ -76,7 +81,12 @@ def publish(publisher, files, name, metadata, extra_options, force):
help='Path to JSON file containing metadata to publish'
)
@click.option('--extra-options', help='Extra options to pass to datasette serve')
def package(files, tag, metadata, extra_options):
@click.option('--title', help='Title for metadata')
@click.option('--license', help='License label for metadata')
@click.option('--license_url', help='License URL for metadata')
@click.option('--source', help='Source label for metadata')
@click.option('--source_url', help='Source URL for metadata')
def package(files, tag, metadata, extra_options, **extra_metadata):
"Package specified SQLite files into a new datasette Docker container"
if not shutil.which('docker'):
click.secho(
@ -87,7 +97,7 @@ def package(files, tag, metadata, extra_options):
err=True,
)
sys.exit(1)
with temporary_docker_directory(files, 'datasette', metadata, extra_options):
with temporary_docker_directory(files, 'datasette', metadata, extra_options, extra_metadata):
args = ['docker', 'build']
if tag:
args.append('-t')

Wyświetl plik

@ -166,7 +166,8 @@ CMD [{}]'''.format(
@contextmanager
def temporary_docker_directory(files, name, metadata, extra_options):
def temporary_docker_directory(files, name, metadata, extra_options, extra_metadata=None):
extra_metadata = extra_metadata or {}
tmp = tempfile.TemporaryDirectory()
# We create a datasette folder in there to get a nicer now deploy name
datasette_dir = os.path.join(tmp.name, name)
@ -177,12 +178,19 @@ def temporary_docker_directory(files, name, metadata, extra_options):
for name in files
]
file_names = [os.path.split(f)[-1] for f in files]
if metadata:
metadata_content = json.load(open(metadata))
else:
metadata_content = {}
for key, value in extra_metadata.items():
if value:
metadata_content[key] = value
try:
dockerfile = make_dockerfile(file_names, metadata and 'metadata.json', extra_options)
dockerfile = make_dockerfile(file_names, metadata_content and 'metadata.json', extra_options)
os.chdir(datasette_dir)
if metadata_content:
open('metadata.json', 'w').write(json.dumps(metadata_content, indent=2))
open('Dockerfile', 'w').write(dockerfile)
if metadata:
open('metadata.json', 'w').write(metadata.read())
for path, filename in zip(file_paths, file_names):
os.link(path, os.path.join(datasette_dir, filename))
yield