Skip to content

Commit

Permalink
Update move-to-archive to use a product and a version
Browse files Browse the repository at this point in the history
Signed-off-by: Aurélien Bompard <[email protected]>
  • Loading branch information
abompard committed Jul 30, 2024
1 parent 1809f00 commit f82a6d1
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 51 deletions.
91 changes: 49 additions & 42 deletions mirrormanager2/utility/move_to_archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"""

import os
import re

import click

Expand All @@ -14,59 +13,67 @@

from .common import config_option

archiveCategory = "Fedora Archive"
originalCategory = "Fedora Linux"
DEFAULT_ARCHIVE_CATEGORY = "Fedora Archive"
SKIP_DIR_PREFIX = "pub/"


def doit(session, original_cat, archive_cat, directory_re):
c = mirrormanager2.lib.get_category_by_name(session, original_cat)
if c is None:
raise click.ClickException(f"No category could be found by the name: {original_cat}")
a = mirrormanager2.lib.get_category_by_name(session, archive_cat)
if a is None:
raise click.ClickException(f"No category could be found by the name: {archive_cat}")
originaltopdir = c.topdir.name
archivetopdir = os.path.join(a.topdir.name, "fedora", "linux")
dirRe = re.compile(directory_re)
for d in c.directories:
if dirRe.search(d.name):
for r in d.repositories:
t = os.path.join(archivetopdir, d.name[len(originaltopdir) + 1 :])
print(f"trying to find {t}")
new_d = mirrormanager2.lib.get_directory_by_name(session, t)
if new_d is None:
raise click.ClickException(
f"Unable to find a directory in [{archive_cat}] for {d.name}"
)
r.directory = new_d
r.category = a
session.add(r)
session.commit()
print(f"{d.name} => {t}")
def doit(session, product, version, archive_cat):
archivetopdir = archive_cat.topdir.name
repos = mirrormanager2.lib.get_repositories(session, product_name=product, version_name=version)
for repo in repos:
if repo.directory is None:
click.echo(f"Repo {repo.name} (prefix {repo.prefix}) has no directory, skipping.")
continue
if repo.category == archive_cat:
click.echo(f"Repo {repo.name} (prefix {repo.prefix}) is already archived, skipping.")
continue

subdir = repo.directory.name
if subdir.startswith(SKIP_DIR_PREFIX):
subdir = subdir[len(SKIP_DIR_PREFIX) :]
target_dir = os.path.join(archivetopdir, subdir)
new_d = mirrormanager2.lib.get_directory_by_name(session, target_dir)
if new_d is None:
raise click.ClickException(
f"Unable to find a directory in [{archive_cat.name}] for {repo.directory.name}"
)
click.echo(f"{repo.directory.name} => {target_dir}")
repo.directory = new_d
repo.category = archive_cat
session.add(repo)
session.commit()


@click.command()
@config_option
@click.option(
"--originalCategory",
metavar="CATEGORY",
help=f"original Category (default={originalCategory})",
default=originalCategory,
"--product",
required=True,
help="Product name",
)
@click.option(
"--archiveCategory",
metavar="CATEGORY",
help=f"archive Category (default={archiveCategory})",
default=archiveCategory,
"--version",
required=True,
help="Version to archive",
)
@click.option(
"--directoryRe",
metavar="RE",
required=True,
help="subdirectory regular expression to move (e.g. '/7/') " "[required]",
"--archive-category",
metavar="CATEGORY",
help="Archive Category",
default=DEFAULT_ARCHIVE_CATEGORY,
show_default=1,
)
def main(config, originalcategory, archivecategory, directoryre):
def main(config, archive_category, product, version):
d = mirrormanager2.lib.read_config(config)
db_manager = get_db_manager(d)
session = db_manager.Session()
doit(session, originalcategory, archivecategory, directoryre)
if mirrormanager2.lib.get_product_by_name(session, product) is None:
raise click.BadOptionUsage("--product", f"No such product: {product}")
if mirrormanager2.lib.get_version_by_name_version(session, product, version) is None:
raise click.BadOptionUsage("--version", f"No such version: {version}")
archive_cat = mirrormanager2.lib.get_category_by_name(session, archive_category)
if archive_cat is None:
raise click.BadOptionUsage(
"--archive-category", f"No category could be found by the name: {archive_category}"
)
doit(session, product, version, archive_cat)
16 changes: 7 additions & 9 deletions tests/test_mta.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def configfile(tmp_path):

@pytest.fixture()
def command_args(configfile):
return ["-c", configfile, "--directoryRe=/26"]
return ["-c", configfile, "--product", "Fedora", "--version", "26"]


def run_command(args):
Expand All @@ -48,8 +48,8 @@ def test_mta_empty_db(command_args, db):

# Ignore for now
# assert stderr == ''
assert result.exit_code == 1
assert result.output == "Error: No category could be found by the name: Fedora Linux\n"
assert result.exit_code == 2
assert "Error: No such product: Fedora" in result.output


def test_mta(
Expand All @@ -58,8 +58,8 @@ def test_mta(
"""Test the mta script."""
result = run_command(command_args)

assert result.exit_code == 1
assert result.output == "Error: No category could be found by the name: Fedora Archive\n"
assert result.exit_code == 2, result.output
assert "Error: No category could be found by the name: Fedora Archive" in result.output
# Ignore for now
# assert stderr == ''

Expand Down Expand Up @@ -119,8 +119,7 @@ def test_mta(
assert results[9].name == "pub/archive"

result = run_command(command_args)
assert (
result.output == "trying to find pub/archive/fedora/linux/updates/testing/26/x86_64\n"
assert result.output == (
"Error: Unable to find a directory in [Fedora Archive] for pub/fedora/"
"linux/updates/testing/26/x86_64\n"
)
Expand All @@ -137,8 +136,7 @@ def test_mta(
db.commit()

result = run_command(command_args)
assert (
result.output == "trying to find pub/archive/fedora/linux/updates/testing/26/x86_64\n"
assert result.output == (
"pub/fedora/linux/updates/testing/26/x86_64 => "
"pub/archive/fedora/linux/updates/testing/26/x86_64\n"
)
Expand Down

0 comments on commit f82a6d1

Please sign in to comment.