Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/cli/client.py
# Copyright (C) 2017-2020 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 | ||||
# WARNING: do not import unnecessary things here to keep cli startup time under | |||||
# control | |||||
import os | import os | ||||
import logging | import logging | ||||
import sys | import sys | ||||
import tempfile | |||||
import uuid | |||||
import json | |||||
import yaml | |||||
import click | import click | ||||
import xmltodict | |||||
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 | ||||
""" | """ | ||||
pass | pass | ||||
def generate_slug(): | def generate_slug(): | ||||
"""Generate a slug (sample purposes). | """Generate a slug (sample purposes). | ||||
""" | """ | ||||
import uuid | |||||
return str(uuid.uuid4()) | return str(uuid.uuid4()) | ||||
def _url(url): | def _url(url): | ||||
"""Force the /1 api version at the end of the url (avoiding confusing | """Force the /1 api version at the end of the url (avoiding confusing | ||||
issues without it). | issues without it). | ||||
Args: | Args: | ||||
Show All 21 Lines | Args: | ||||
name (str): Software's name | name (str): Software's name | ||||
external_id (str): External identifier (slug) or generated one | external_id (str): External identifier (slug) or generated one | ||||
authors (List[str]): List of author names | authors (List[str]): List of author names | ||||
Returns: | Returns: | ||||
Filepath to the metadata generated file | Filepath to the metadata generated file | ||||
""" | """ | ||||
import xmltodict | |||||
path = os.path.join(temp_dir, "metadata.xml") | path = os.path.join(temp_dir, "metadata.xml") | ||||
# generate a metadata file with the minimum required metadata | # generate a metadata file with the minimum required metadata | ||||
codemetadata = { | codemetadata = { | ||||
"entry": { | "entry": { | ||||
"@xmlns": "http://www.w3.org/2005/Atom", | "@xmlns": "http://www.w3.org/2005/Atom", | ||||
"@xmlns:codemeta": "https://doi.org/10.5063/SCHEMA/CODEMETA-2.0", | "@xmlns:codemeta": "https://doi.org/10.5063/SCHEMA/CODEMETA-2.0", | ||||
"codemeta:name": name, | "codemeta:name": name, | ||||
"codemeta:identifier": external_id, | "codemeta:identifier": external_id, | ||||
Show All 16 Lines | def _client(url, username, password): | ||||
"""Instantiate a client to access the deposit api server | """Instantiate a client to access the deposit api server | ||||
Args: | Args: | ||||
url (str): Deposit api server | url (str): Deposit api server | ||||
username (str): User | username (str): User | ||||
password (str): User's password | password (str): User's password | ||||
""" | """ | ||||
from swh.deposit.client import PublicApiDepositClient | |||||
client = PublicApiDepositClient( | client = PublicApiDepositClient( | ||||
{"url": url, "auth": {"username": username, "password": password},} | {"url": url, "auth": {"username": username, "password": password},} | ||||
) | ) | ||||
return client | return client | ||||
def _collection(client): | def _collection(client): | ||||
"""Retrieve the client's collection | """Retrieve the client's collection | ||||
▲ Show 20 Lines • Show All 285 Lines • ▼ Show 20 Lines | ): | ||||
"""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. | ||||
""" | """ | ||||
import tempfile | |||||
from swh.deposit.client import MaintenanceError | |||||
url = _url(url) | url = _url(url) | ||||
config = {} | config = {} | ||||
with tempfile.TemporaryDirectory() as temp_dir: | with tempfile.TemporaryDirectory() as temp_dir: | ||||
try: | try: | ||||
logger.debug("Parsing cli options") | logger.debug("Parsing cli options") | ||||
config = client_command_parse_input( | config = client_command_parse_input( | ||||
username, | username, | ||||
▲ Show 20 Lines • Show All 51 Lines • ▼ Show 20 Lines | @click.option( | ||||
type=click.Choice(["logging", "yaml", "json"]), | type=click.Choice(["logging", "yaml", "json"]), | ||||
help="Output format results.", | help="Output format results.", | ||||
) | ) | ||||
@click.pass_context | @click.pass_context | ||||
def status(ctx, url, username, password, deposit_id, output_format): | def status(ctx, url, username, password, deposit_id, output_format): | ||||
"""Deposit's status | """Deposit's status | ||||
""" | """ | ||||
from swh.deposit.client import MaintenanceError | |||||
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: | except MaintenanceError as e: | ||||
logger.error(e) | logger.error(e) | ||||
sys.exit(1) | 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): | ||||
import json | |||||
import yaml | |||||
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) |