Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/cli/client.py
# Copyright (C) 2017-2019 The Software Heritage developers | # Copyright (C) 2017-2019 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import os | import os | ||||
import logging | import logging | ||||
import sys | import sys | ||||
import tempfile | import tempfile | ||||
import uuid | import uuid | ||||
import json | |||||
import yaml | |||||
import click | import click | ||||
import xmltodict | import xmltodict | ||||
from swh.deposit.client import PublicApiDepositClient | from swh.deposit.client import PublicApiDepositClient | ||||
from swh.deposit.cli import deposit | from swh.deposit.cli import deposit | ||||
▲ Show 20 Lines • Show All 284 Lines • ▼ Show 20 Lines | @click.option('--url', default='https://deposit.softwareheritage.org', | ||||
help="(Optional) Deposit server api endpoint. By default, https://deposit.softwareheritage.org/1") # noqa | help="(Optional) Deposit server api endpoint. By default, https://deposit.softwareheritage.org/1") # noqa | ||||
@click.option('--verbose/--no-verbose', default=False, | @click.option('--verbose/--no-verbose', default=False, | ||||
help='Verbose mode') | help='Verbose mode') | ||||
@click.option('--name', | @click.option('--name', | ||||
help='Software name') | help='Software name') | ||||
@click.option('--author', multiple=True, | @click.option('--author', multiple=True, | ||||
help='Software author(s), this can be repeated as many times' | help='Software author(s), this can be repeated as many times' | ||||
' as there are authors') | ' as there are authors') | ||||
@click.option('-f', '--format', 'output_format', default='logging', | |||||
type=click.Choice(['logging', 'yaml', 'json']), | |||||
help='Output format results.') | |||||
@click.pass_context | @click.pass_context | ||||
def upload(ctx, | def upload(ctx, | ||||
username, password, archive=None, metadata=None, | username, password, archive=None, metadata=None, | ||||
archive_deposit=False, metadata_deposit=False, | archive_deposit=False, metadata_deposit=False, | ||||
collection=None, slug=None, partial=False, deposit_id=None, | collection=None, slug=None, partial=False, deposit_id=None, | ||||
replace=False, url='https://deposit.softwareheritage.org', | replace=False, url='https://deposit.softwareheritage.org', | ||||
verbose=False, name=None, author=None): | verbose=False, name=None, author=None, output_format=None): | ||||
"""Software Heritage Public Deposit Client | """Software Heritage Public Deposit Client | ||||
Create/Update deposit through the command line. | Create/Update deposit through the command line. | ||||
More documentation can be found at | More documentation can be found at | ||||
https://docs.softwareheritage.org/devel/swh-deposit/getting-started.html. | https://docs.softwareheritage.org/devel/swh-deposit/getting-started.html. | ||||
""" | """ | ||||
Show All 16 Lines | with tempfile.TemporaryDirectory() as temp_dir: | ||||
config, )) | config, )) | ||||
deposit_id = config['deposit_id'] | deposit_id = config['deposit_id'] | ||||
if deposit_id: | if deposit_id: | ||||
r = deposit_update(config, logger) | r = deposit_update(config, logger) | ||||
else: | else: | ||||
r = deposit_create(config, logger) | r = deposit_create(config, logger) | ||||
print_result(r, output_format) | |||||
logger.info(r) | |||||
@deposit.command() | @deposit.command() | ||||
@click.option('--url', default='https://deposit.softwareheritage.org', | @click.option('--url', default='https://deposit.softwareheritage.org', | ||||
help="(Optional) Deposit server api endpoint. By default, " | help="(Optional) Deposit server api endpoint. By default, " | ||||
"https://deposit.softwareheritage.org/1") | "https://deposit.softwareheritage.org/1") | ||||
@click.option('--username', required=True, | @click.option('--username', required=True, | ||||
help="(Mandatory) User's name") | help="(Mandatory) User's name") | ||||
@click.option('--password', required=True, | @click.option('--password', required=True, | ||||
help="(Mandatory) User's associated password") | help="(Mandatory) User's associated password") | ||||
@click.option('--deposit-id', default=None, | @click.option('--deposit-id', default=None, | ||||
required=True, | required=True, | ||||
help="Deposit identifier.") | help="Deposit identifier.") | ||||
@click.option('-f', '--format', 'output_format', default='logging', | |||||
type=click.Choice(['logging', 'yaml', 'json']), | |||||
help='Output format results.') | |||||
@click.pass_context | @click.pass_context | ||||
def status(ctx, url, username, password, deposit_id): | def status(ctx, url, username, password, deposit_id, output_format): | ||||
"""Deposit's status | """Deposit's status | ||||
""" | """ | ||||
url = _url(url) | url = _url(url) | ||||
logger.debug('Status deposit') | logger.debug('Status deposit') | ||||
try: | try: | ||||
client = _client(url, username, password) | client = _client(url, username, password) | ||||
collection = _collection(client) | collection = _collection(client) | ||||
except InputError as e: | except InputError as e: | ||||
logger.error('Problem during parsing options: %s', e) | logger.error('Problem during parsing options: %s', e) | ||||
sys.exit(1) | sys.exit(1) | ||||
r = client.deposit_status( | print_result(client.deposit_status( | ||||
collection=collection, deposit_id=deposit_id) | collection=collection, deposit_id=deposit_id), | ||||
logger.info(r) | output_format) | ||||
def print_result(data, output_format): | |||||
if output_format == 'json': | |||||
click.echo(json.dumps(data)) | |||||
elif output_format == 'yaml': | |||||
click.echo(yaml.dump(data)) | |||||
else: | |||||
logger.info(data) |