fix: prevent double-nested directory on archive extraction
Archives like 64bit.7z that already contain a 64bit/ top-level dir were extracted into STAGING_IN/64bit/, creating 64bit/64bit/ nesting. Now extract into STAGING_IN root, then: - If the archive produced a single dir matching the stem, use it - Otherwise, collect scattered files into a stem-named subdirectory Removes the need for nested path: 64bit/Debian.vmdk instead of 64bit/64bit/Debian.vmdk
This commit is contained in:
parent
0e7f8c81f3
commit
7808b16946
1 changed files with 24 additions and 3 deletions
|
|
@ -75,13 +75,34 @@ def extract_if_needed(filename: str) -> Path:
|
||||||
if stem.endswith(e):
|
if stem.endswith(e):
|
||||||
stem = stem[:-len(e)]
|
stem = stem[:-len(e)]
|
||||||
break
|
break
|
||||||
out_dir = STAGING_IN / stem
|
|
||||||
out_dir.mkdir(parents=True, exist_ok=True)
|
|
||||||
|
|
||||||
result = subprocess.run(cmd, cwd=str(out_dir), capture_output=True, text=True, timeout=600)
|
# Snapshot STAGING_IN before extraction
|
||||||
|
before = set(STAGING_IN.iterdir()) if STAGING_IN.exists() else set()
|
||||||
|
|
||||||
|
# Extract into STAGING_IN root (not a subdir) to avoid double nesting
|
||||||
|
# when the archive already has a top-level directory
|
||||||
|
result = subprocess.run(cmd, cwd=str(STAGING_IN), capture_output=True, text=True, timeout=600)
|
||||||
if result.returncode != 0:
|
if result.returncode != 0:
|
||||||
raise RuntimeError(f"{tool} failed: {result.stderr.strip()[:500]}")
|
raise RuntimeError(f"{tool} failed: {result.stderr.strip()[:500]}")
|
||||||
|
|
||||||
|
# Determine what was created
|
||||||
|
after = set(STAGING_IN.iterdir())
|
||||||
|
new_items = after - before
|
||||||
|
|
||||||
|
# If the archive had a single top-level directory matching the stem, use it
|
||||||
|
expected_dir = STAGING_IN / stem
|
||||||
|
if expected_dir in new_items and expected_dir.is_dir():
|
||||||
|
return expected_dir
|
||||||
|
|
||||||
|
# Otherwise, collect scattered files into a subdirectory
|
||||||
|
out_dir = STAGING_IN / stem
|
||||||
|
out_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
for item in new_items:
|
||||||
|
dest = out_dir / item.name
|
||||||
|
if item.is_dir():
|
||||||
|
shutil.move(str(item), str(dest))
|
||||||
|
else:
|
||||||
|
shutil.move(str(item), str(dest))
|
||||||
return out_dir
|
return out_dir
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue