Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(client): Fix "acq" command group #206

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
221 changes: 0 additions & 221 deletions alpenhorn/client/acq.py

This file was deleted.

20 changes: 20 additions & 0 deletions alpenhorn/client/acq/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""Alpenhorn client interface for operations on `ArchiveAcq`s."""

import re
import sys
import click
import peewee as pw

from .files import files
from .list import list_
from .show import show


@click.group(context_settings={"help_option_names": ["-h", "--help"]})
def cli():
"""Manage Acquisitions."""


cli.add_command(files, "files")
cli.add_command(list_, "list")
cli.add_command(show, "show")
111 changes: 111 additions & 0 deletions alpenhorn/client/acq/files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
"""alpenhorn acq files command."""

import click
import peewee as pw
from tabulate import tabulate

from ...common.util import pretty_bytes
from ...db import ArchiveAcq, ArchiveFile, ArchiveFileCopy, StorageGroup, StorageNode
from ..cli import echo
from ..options import client_option, not_both


@click.command()
@click.argument("acq", metavar="ACQ")
@client_option("group")
@client_option("node")
@click.option(
"--show-removed",
is_flag=True,
help="If used with --node or --group, also list files that have been removed from the node/group.",
)
def files(acq, group, node, show_removed):
"""List files in an Acqusition.

Lists files in the Acqusition named ACQ."""

not_both(node, "node", group, "group")

try:
acq = ArchiveAcq.get(name=acq)
except pw.DoesNotExist:
raise click.ClickException("No such Acquisition: " + acq)

data = []
if node:
headers = ["Name", "Size", "Node State", "Size on Node", "MD5"]

try:
node = StorageNode.get(name=node)
except pw.DoesNotExist:
raise click.ClickException("No such Storage Node: " + node)
query = (
ArchiveFileCopy.select()
.join(ArchiveFile)
.where(ArchiveFile.acq == acq, ArchiveFileCopy.node == node)
)

for copy in query:
state = copy.state
if show_removed or state != "Removed":
file_size = (
"-" if copy.file.size_b is None else pretty_bytes(copy.file.size_b)
)
node_size = pretty_bytes(copy.size_b) if copy.size_b else "-"
data.append(
(
copy.file.name,
file_size,
state,
node_size,
copy.file.md5sum,
)
)
elif group:
headers = ["Name", "Size", "Group State", "MD5"]

try:
group = StorageGroup.get(name=group)
except pw.DoesNotExist:
raise click.ClickException("No such Storage Group: " + group)
query = (
ArchiveFileCopy.select()
.join(ArchiveFile)
.switch(ArchiveFileCopy)
.join(StorageNode)
.where(ArchiveFile.acq == acq, StorageNode.group == group)
.group_by(ArchiveFile.id)
)

for copy in query:
state = group.filecopy_state(copy.file)

if show_removed or state != "N":
# Convert state to words
if state == "Y":
state = "Present"
elif state == "M":
state = "Needs Check"
elif state == "N":
state = "Removed"
else:
state = "Corrupt"

file_size = (
"-" if copy.file.size_b is None else pretty_bytes(copy.file.size_b)
)

data.append((copy.file.name, file_size, state, copy.file.md5sum))
else:
headers = ["Name", "Size", "MD5"]

query = ArchiveFile.select().where(ArchiveFile.acq == acq)

for file in query:
file_size = "-" if file.size_b is None else pretty_bytes(file.size_b)
data.append((file.name, file_size, file.md5sum))

if data:
echo(tabulate(data, headers=headers))
else:
echo("No files.")
Loading