Changeset View
Changeset View
Standalone View
Standalone View
swh/provenance/cli.py
Show First 20 Lines • Show All 159 Lines • ▼ Show 20 Lines | |||||
@click.argument("swhid") | @click.argument("swhid") | ||||
@click.pass_context | @click.pass_context | ||||
def find_first(ctx, swhid): | def find_first(ctx, swhid): | ||||
"""Find first occurrence of the requested blob.""" | """Find first occurrence of the requested blob.""" | ||||
from . import get_provenance | from . import get_provenance | ||||
provenance = get_provenance(**ctx.obj["config"]["provenance"]) | provenance = get_provenance(**ctx.obj["config"]["provenance"]) | ||||
# TODO: return a dictionary with proper keys for each field | # TODO: return a dictionary with proper keys for each field | ||||
row = provenance.content_find_first(hash_to_bytes(swhid)) | occur = provenance.content_find_first(hash_to_bytes(swhid)) | ||||
if row is not None: | if occur is not None: | ||||
print( | print( | ||||
"swh:1:cnt:{cnt}, swh:1:rev:{rev}, {date}, {path}".format( | f"swh:1:cnt:{hash_to_hex(occur.content)}, " | ||||
cnt=hash_to_hex(row[0]), | f"swh:1:rev:{hash_to_hex(occur.revision)}, " | ||||
rev=hash_to_hex(row[1]), | f"{occur.date}, " | ||||
date=row[2], | f"{occur.origin}, " | ||||
path=os.fsdecode(row[3]), | f"{os.fsdecode(occur.path)}" | ||||
) | |||||
) | ) | ||||
else: | else: | ||||
print(f"Cannot find a content with the id {swhid}") | print(f"Cannot find a content with the id {swhid}") | ||||
@cli.command(name="find-all") | @cli.command(name="find-all") | ||||
@click.argument("swhid") | @click.argument("swhid") | ||||
@click.option("-l", "--limit", type=int) | @click.option("-l", "--limit", type=int) | ||||
@click.pass_context | @click.pass_context | ||||
def find_all(ctx, swhid, limit): | def find_all(ctx, swhid, limit): | ||||
"""Find all occurrences of the requested blob.""" | """Find all occurrences of the requested blob.""" | ||||
from . import get_provenance | from . import get_provenance | ||||
provenance = get_provenance(**ctx.obj["config"]["provenance"]) | provenance = get_provenance(**ctx.obj["config"]["provenance"]) | ||||
# TODO: return a dictionary with proper keys for each field | # TODO: return a dictionary with proper keys for each field | ||||
for row in provenance.content_find_all(hash_to_bytes(swhid), limit=limit): | for occur in provenance.content_find_all(hash_to_bytes(swhid), limit=limit): | ||||
print( | print( | ||||
"swh:1:cnt:{cnt}, swh:1:rev:{rev}, {date}, {path}".format( | f"swh:1:cnt:{hash_to_hex(occur.content)}, " | ||||
cnt=hash_to_hex(row[0]), | f"swh:1:rev:{hash_to_hex(occur.revision)}, " | ||||
rev=hash_to_hex(row[1]), | f"{occur.date}, " | ||||
date=row[2], | f"{occur.origin}, " | ||||
path=os.fsdecode(row[3]), | f"{os.fsdecode(occur.path)}" | ||||
) | |||||
) | ) |