Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/cli/client.py
# Copyright (C) 2017-2019 The Software Heritage developers | # Copyright (C) 2017-2020 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 json | ||||
import yaml | import yaml | ||||
import click | import click | ||||
import xmltodict | import xmltodict | ||||
from swh.deposit.client import PublicApiDepositClient | from swh.deposit.client import PublicApiDepositClient, MaintenanceError | ||||
from swh.deposit.cli import deposit | from swh.deposit.cli import deposit | ||||
logger = logging.getLogger(__name__) | logger = logging.getLogger(__name__) | ||||
class InputError(ValueError): | class InputError(ValueError): | ||||
"""Input script error | """Input script error | ||||
▲ Show 20 Lines • Show All 130 Lines • ▼ Show 20 Lines | """Parse the client subcommand options and make sure the combination | ||||
file or author(s) and name provided in params | file or author(s) and name provided in params | ||||
- A deposit update requires a deposit_id | - A deposit update requires a deposit_id | ||||
This will not prevent all failure cases though. The remaining | This will not prevent all failure cases though. The remaining | ||||
errors are already dealt with by the underlying api client. | errors are already dealt with by the underlying api client. | ||||
Raises: | Raises: | ||||
InputError explaining the issue | InputError explaining the user input related issue | ||||
MaintenanceError explaining the api status | |||||
Returns: | Returns: | ||||
dict with the following keys: | dict with the following keys: | ||||
'archive': the software archive to deposit | 'archive': the software archive to deposit | ||||
'username': username | 'username': username | ||||
'password': associated password | 'password': associated password | ||||
'metadata': the metadata file to deposit | 'metadata': the metadata file to deposit | ||||
▲ Show 20 Lines • Show All 250 Lines • ▼ Show 20 Lines | with tempfile.TemporaryDirectory() as temp_dir: | ||||
url, | url, | ||||
name, | name, | ||||
author, | author, | ||||
temp_dir, | temp_dir, | ||||
) | ) | ||||
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) | ||||
except MaintenanceError as e: | |||||
logger.error(e) | |||||
sys.exit(1) | |||||
if verbose: | if verbose: | ||||
logger.info("Parsed configuration: %s" % (config,)) | logger.info("Parsed configuration: %s" % (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) | ||||
Show All 30 Lines | def status(ctx, url, username, password, deposit_id, output_format): | ||||
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) | ||||
except MaintenanceError as e: | |||||
logger.error(e) | |||||
sys.exit(1) | |||||
print_result( | print_result( | ||||
client.deposit_status(collection=collection, deposit_id=deposit_id), | client.deposit_status(collection=collection, deposit_id=deposit_id), | ||||
output_format, | output_format, | ||||
) | ) | ||||
def print_result(data, output_format): | def print_result(data, output_format): | ||||
if output_format == "json": | if output_format == "json": | ||||
click.echo(json.dumps(data)) | click.echo(json.dumps(data)) | ||||
elif output_format == "yaml": | elif output_format == "yaml": | ||||
click.echo(yaml.dump(data)) | click.echo(yaml.dump(data)) | ||||
else: | else: | ||||
logger.info(data) | logger.info(data) |