Skip to content

Commit

Permalink
Merge pull request #25 from lubomir/fix-repacking
Browse files Browse the repository at this point in the history
Fix repacking
  • Loading branch information
lubomir committed Sep 24, 2019
2 parents 5f9e033 + 0a531fa commit 90b48ce
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 11 deletions.
20 changes: 18 additions & 2 deletions rcm_nexus/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def create_partitioned_zips_from_dir(


def create_partitioned_zips_from_zip(
src, out_dir, max_count=MAX_COUNT, max_size=MAX_SIZE
src, out_dir, max_count=MAX_COUNT, max_size=MAX_SIZE, debug=False
):
"""
Given a zip archive, split it into smaller chunks and possibly filter out
Expand All @@ -52,22 +52,33 @@ def create_partitioned_zips_from_zip(
directory and only repackage its contents. If there is no such
subdirectory, all content will be taken without any changes.
The top-level directory name does not matter at all.
Alternatively, the content to be published can be directly under the
top-level directory. In such case the top-level directory is dropped and
everything else is published.
If there is more than one top-level entry in the archive, an error is
reported.
"""
zips = Zipper(out_dir, max_count, max_size)
zf = zipfile.ZipFile(src)
repodir = None

zip_objects = zf.infolist()
toplevel_objects = set()

# Find if there is a maven-repository subdir under top-level directory.
for info in zip_objects:
parts = info.filename.split("/")
toplevel_objects.add(parts[0])
if len(parts) < 3:
# Not a subdirectory of top-level dir or a file in there.
continue
if parts[1] == "maven-repository":
repodir = os.path.join(*parts[:2]) + "/"
break

if len(toplevel_objects) > 1:
raise RuntimeError("Invalid zip file: there are multiple top-level entries.")

# Iterate over all objects in the directory.
for info in zip_objects:
Expand All @@ -85,7 +96,12 @@ def create_partitioned_zips_from_zip(
else:
# Not correct location, ignore it.
continue
else:
# Otherwise we only strip the leading component.
filename = filename.split("/", 1)[-1]

if debug:
print("Mapping %s -> %s" % (info.filename, filename))
zips.append(filename, info.file_size, lambda: zf.read(info.filename))

return zips.list()
Expand Down
12 changes: 10 additions & 2 deletions rcm_nexus/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ def push(repo, environment, product, version, ga=False, debug=False):
else:
print("Processing repository zip archive: %s" % repo)

# Open the zip, walk the entries and normalize the structure to clean zip (if necessary)
zip_paths = archive.create_partitioned_zips_from_zip(repo, zips_dir)
# Open the zip, walk the entries and normalize the structure to
# clean zip
zip_paths = archive.create_partitioned_zips_from_zip(
repo, zips_dir, debug=debug
)

# Open new staging repository with description
staging_repo_id = staging.start_staging_repo(session, nexus_config, product, version, ga)
Expand Down Expand Up @@ -104,6 +107,11 @@ def push(repo, environment, product, version, ga=False, debug=False):

if staging.verify_action(session, promote_entity, "promote"):
sys.exit(1)
except RuntimeError as exc:
if debug:
raise
print("Error: %s" % exc, file=sys.stderr)
sys.exit(1)
except requests.exceptions.HTTPError as exc:
if debug:
raise
Expand Down
28 changes: 23 additions & 5 deletions tests/test_archive_zip.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import print_function

import os

from rcm_nexus import archive
from .base import NexupBaseTest
import tempfile
Expand All @@ -22,9 +24,25 @@ def test_small(self):

self.assertEqual(
sorted(info.filename for info in zipfile.ZipFile(zips[0]).infolist()),
sorted(paths),
sorted(os.path.join(*path.split("/")[1:]) for path in paths),
)

def test_multiple_top_level_entries(self):
self.load_words()

paths = ["path/one.txt", "another/two.txt"]

(_f, src_zip) = tempfile.mkstemp(suffix=".zip")

self.write_zip(src_zip, paths)

outdir = tempfile.mkdtemp()
try:
archive.create_partitioned_zips_from_zip(src_zip, outdir)
self.fail("RuntimeError not raised")
except RuntimeError as exc:
self.assertIn("multiple top-level entries", str(exc))

def test_trim_maven_dir(self):
self.load_words()

Expand Down Expand Up @@ -59,11 +77,11 @@ def test_count_rollover(self):

self.assertEqual(
sorted(info.filename for info in zipfile.ZipFile(zips[0]).infolist()),
paths[:2],
sorted(os.path.join(*path.split("/")[1:]) for path in paths[:2]),
)
self.assertEqual(
sorted(info.filename for info in zipfile.ZipFile(zips[1]).infolist()),
paths[2:],
sorted(os.path.join(*path.split("/")[1:]) for path in paths[2:]),
)

def test_size_rollover(self):
Expand All @@ -84,9 +102,9 @@ def test_size_rollover(self):

self.assertEqual(
sorted(info.filename for info in zipfile.ZipFile(zips[0]).infolist()),
paths[:2],
sorted(os.path.join(*path.split("/")[1:]) for path in paths[:2]),
)
self.assertEqual(
sorted(info.filename for info in zipfile.ZipFile(zips[1]).infolist()),
paths[2:],
sorted(os.path.join(*path.split("/")[1:]) for path in paths[2:]),
)
2 changes: 0 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@ max-line-length = 88

[testenv]
commands = nosetests --with-coverage --cover-package=rcm_nexus --cover-html --cover-html-dir=coverage
setenv =
XDG_CACHE_HOME={envtmpdir}/
deps = .[test,ci]

0 comments on commit 90b48ce

Please sign in to comment.