Skip to content

Commit

Permalink
Add unit specifier logic for chunk-size option
Browse files Browse the repository at this point in the history
fixes: pulp#260
  • Loading branch information
gerrod3 committed Aug 18, 2021
1 parent a0c1e84 commit 94a7c9c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGES/260.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Chunked artifact and content uploads now allow unit specifier in ``--chunk-size`` option
19 changes: 19 additions & 0 deletions pulpcore/cli/common/generic.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import gettext
import json
import re
from typing import (
Any,
Callable,
Expand Down Expand Up @@ -233,6 +234,17 @@ def _callback(
return _callback


# based on https://stackoverflow.com/a/42865957/2002471
units = {"B": 1, "KB": 10**3, "MB": 10**6, "GB": 10**9, "TB": 10**12}

def parse_size_callback(size):
size = size.upper()
if not re.match(r' ', size):
size = re.sub(r'([KMGT]?B)', r' \1', size)
number, unit = [string.strip() for string in size.split()]
return int(float(number)*units[unit])


##############################################################################
# Decorator common options

Expand Down Expand Up @@ -393,6 +405,13 @@ def _multi_option_callback(
callback=load_json_callback,
)

chunk_size_option = pulp_option(
"--chunk-size",
help=_("Chunk size to break up {entity} into. Defaults to 1MB"),
default=1000000,
callback=parse_size_callback,
)

pulp_created_gte_option = pulp_option(
"--created-after",
"pulp_created__gte",
Expand Down
6 changes: 2 additions & 4 deletions pulpcore/cli/core/artifact.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import click

from pulpcore.cli.common.context import PulpContext, pass_entity_context, pass_pulp_context
from pulpcore.cli.common.generic import href_option, list_command, show_command
from pulpcore.cli.common.generic import chunk_size_option, href_option, list_command, show_command
from pulpcore.cli.core.context import PulpArtifactContext

_ = gettext.gettext
Expand All @@ -26,9 +26,7 @@ def artifact(ctx: click.Context, pulp_ctx: PulpContext) -> None:

@artifact.command()
@click.option("--file", type=click.File("rb"), required=True)
@click.option(
"--chunk-size", default=1000000, type=int, help=_("Chunk size in bytes (default is 1 MB)")
)
@chunk_size_option
@pass_entity_context
@pass_pulp_context
def upload(
Expand Down
12 changes: 8 additions & 4 deletions pulpcore/cli/file/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@
pass_entity_context,
pass_pulp_context,
)
from pulpcore.cli.common.generic import create_command, href_option, list_command, show_command
from pulpcore.cli.common.generic import (
chunk_size_option,
create_command,
href_option,
list_command,
show_command,
)
from pulpcore.cli.core.context import PulpArtifactContext
from pulpcore.cli.file.context import PulpFileContentContext

Expand Down Expand Up @@ -91,9 +97,7 @@ def content(ctx: click.Context, pulp_ctx: PulpContext, content_type: str) -> Non
@content.command()
@click.option("--relative-path", required=True)
@click.option("--file", type=click.File("rb"), required=True)
@click.option(
"--chunk-size", default=1000000, type=int, help=_("Chunk size in bytes (default is 1 MB)")
)
@chunk_size_option
@pass_entity_context
@pass_pulp_context
def upload(
Expand Down
12 changes: 8 additions & 4 deletions pulpcore/cli/python/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@
pass_entity_context,
pass_pulp_context,
)
from pulpcore.cli.common.generic import create_command, href_option, list_command, show_command
from pulpcore.cli.common.generic import (
chunk_size_option,
create_command,
href_option,
list_command,
show_command,
)
from pulpcore.cli.core.context import PulpArtifactContext
from pulpcore.cli.python.context import PulpPythonContentContext

Expand Down Expand Up @@ -68,9 +74,7 @@ def content(ctx: click.Context, pulp_ctx: PulpContext, content_type: str) -> Non
@content.command()
@click.option("--relative-path", required=True, help=_("Exact name of file"))
@click.option("--file", type=click.File("rb"), required=True, help=_("Path to file"))
@click.option(
"--chunk-size", default=1000000, type=int, help=_("Chunk size in bytes (default is 1 MB)")
)
@chunk_size_option
@pass_entity_context
@pass_pulp_context
def upload(
Expand Down

0 comments on commit 94a7c9c

Please sign in to comment.