flat-packager 0.4.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- flat_packager/__init__.py +25 -0
- flat_packager/archive.py +69 -0
- flat_packager/inspect.py +150 -0
- flat_packager/pack.py +495 -0
- flat_packager/unpack.py +568 -0
- flat_packager/verify.py +41 -0
- flat_packager-0.4.0.dist-info/METADATA +244 -0
- flat_packager-0.4.0.dist-info/RECORD +12 -0
- flat_packager-0.4.0.dist-info/WHEEL +5 -0
- flat_packager-0.4.0.dist-info/entry_points.txt +5 -0
- flat_packager-0.4.0.dist-info/licenses/LICENSE +21 -0
- flat_packager-0.4.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""Pack and restore repositories through a single flat text archive."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from flat_packager.inspect import inspect_data
|
|
6
|
+
from flat_packager.pack import build_archive, prepare_source
|
|
7
|
+
from flat_packager.unpack import (
|
|
8
|
+
ArchiveManifest,
|
|
9
|
+
load_and_validate_archive,
|
|
10
|
+
process_archive,
|
|
11
|
+
restore_archive,
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
__version__ = "0.4.0"
|
|
15
|
+
|
|
16
|
+
__all__ = [
|
|
17
|
+
"ArchiveManifest",
|
|
18
|
+
"__version__",
|
|
19
|
+
"build_archive",
|
|
20
|
+
"inspect_data",
|
|
21
|
+
"load_and_validate_archive",
|
|
22
|
+
"prepare_source",
|
|
23
|
+
"process_archive",
|
|
24
|
+
"restore_archive",
|
|
25
|
+
]
|
flat_packager/archive.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"""Shared archive constants and helpers."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import gzip
|
|
6
|
+
import json
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from typing import Any, Iterator, TextIO
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
ARCHIVE_KIND = "repo-flat-archive"
|
|
12
|
+
ARCHIVE_VERSION = 2
|
|
13
|
+
SUPPORTED_ARCHIVE_VERSIONS = {1, 2}
|
|
14
|
+
DEFAULT_CHUNK_SIZE = 1024 * 1024
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def detect_archive_compression(path: Path) -> str:
|
|
18
|
+
try:
|
|
19
|
+
with path.open("rb") as handle:
|
|
20
|
+
if handle.read(2) == b"\x1f\x8b":
|
|
21
|
+
return "gzip"
|
|
22
|
+
except FileNotFoundError:
|
|
23
|
+
pass
|
|
24
|
+
return "none"
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def should_compress(path: Path, compression: str) -> bool:
|
|
28
|
+
if compression == "gzip":
|
|
29
|
+
return True
|
|
30
|
+
if compression == "none":
|
|
31
|
+
return False
|
|
32
|
+
if compression == "auto":
|
|
33
|
+
return path.suffix == ".gz"
|
|
34
|
+
raise ValueError(f"unsupported compression mode: {compression}")
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def open_archive_for_write(
|
|
38
|
+
path: Path,
|
|
39
|
+
compression: str = "auto",
|
|
40
|
+
) -> TextIO:
|
|
41
|
+
if should_compress(path, compression):
|
|
42
|
+
return gzip.open(path, "wt", encoding="utf-8", newline="\n")
|
|
43
|
+
return path.open("w", encoding="utf-8", newline="\n")
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def open_archive_for_read(path: Path) -> TextIO:
|
|
47
|
+
if detect_archive_compression(path) == "gzip":
|
|
48
|
+
return gzip.open(path, "rt", encoding="utf-8")
|
|
49
|
+
return path.open("r", encoding="utf-8")
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def emit_json_line(handle: TextIO, payload: dict[str, object]) -> None:
|
|
53
|
+
handle.write(json.dumps(payload, sort_keys=True, separators=(",", ":")))
|
|
54
|
+
handle.write("\n")
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def read_json_lines(archive_path: Path) -> Iterator[tuple[int, dict[str, Any]]]:
|
|
58
|
+
with open_archive_for_read(archive_path) as handle:
|
|
59
|
+
for line_number, line in enumerate(handle, start=1):
|
|
60
|
+
stripped = line.strip()
|
|
61
|
+
if not stripped:
|
|
62
|
+
continue
|
|
63
|
+
try:
|
|
64
|
+
payload = json.loads(stripped)
|
|
65
|
+
except json.JSONDecodeError as exc:
|
|
66
|
+
raise ValueError(f"line {line_number}: invalid JSON: {exc}") from exc
|
|
67
|
+
if not isinstance(payload, dict):
|
|
68
|
+
raise ValueError(f"line {line_number}: archive record must be an object")
|
|
69
|
+
yield line_number, payload
|
flat_packager/inspect.py
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
"""Inspect flat-pack archive contents."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import json
|
|
7
|
+
import sys
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
from .archive import detect_archive_compression
|
|
12
|
+
from .unpack import ArchiveManifest, load_and_validate_archive
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def parse_args() -> argparse.Namespace:
|
|
16
|
+
parser = argparse.ArgumentParser(description="Inspect a flat-pack archive.")
|
|
17
|
+
parser.add_argument("archive", help="Archive to inspect.")
|
|
18
|
+
parser.add_argument(
|
|
19
|
+
"--json",
|
|
20
|
+
action="store_true",
|
|
21
|
+
help="Print machine-readable JSON instead of text.",
|
|
22
|
+
)
|
|
23
|
+
parser.add_argument(
|
|
24
|
+
"--limit",
|
|
25
|
+
type=int,
|
|
26
|
+
default=10,
|
|
27
|
+
help="Number of largest files and symlinks to display.",
|
|
28
|
+
)
|
|
29
|
+
return parser.parse_args()
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def human_size(size: int) -> str:
|
|
33
|
+
units = ("B", "KiB", "MiB", "GiB", "TiB")
|
|
34
|
+
value = float(size)
|
|
35
|
+
for unit in units:
|
|
36
|
+
if value < 1024 or unit == units[-1]:
|
|
37
|
+
if unit == "B":
|
|
38
|
+
return f"{int(value)} {unit}"
|
|
39
|
+
return f"{value:.1f} {unit}"
|
|
40
|
+
value /= 1024
|
|
41
|
+
return f"{size} B"
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def file_records(manifest: ArchiveManifest) -> list[dict[str, Any]]:
|
|
45
|
+
return [
|
|
46
|
+
record
|
|
47
|
+
for _line_number, record in manifest.records
|
|
48
|
+
if record.get("type") == "file"
|
|
49
|
+
]
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def symlink_records(manifest: ArchiveManifest) -> list[dict[str, Any]]:
|
|
53
|
+
return [
|
|
54
|
+
record
|
|
55
|
+
for _line_number, record in manifest.records
|
|
56
|
+
if record.get("type") == "symlink"
|
|
57
|
+
]
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def inspect_data(archive_path: Path, manifest: ArchiveManifest) -> dict[str, Any]:
|
|
61
|
+
files = file_records(manifest)
|
|
62
|
+
symlinks = symlink_records(manifest)
|
|
63
|
+
largest_files = sorted(files, key=lambda record: int(record.get("size", 0)), reverse=True)
|
|
64
|
+
return {
|
|
65
|
+
"archive": str(archive_path),
|
|
66
|
+
"compression": detect_archive_compression(archive_path),
|
|
67
|
+
"version": manifest.version,
|
|
68
|
+
"source": manifest.header.get("source"),
|
|
69
|
+
"created_at": manifest.header.get("created_at"),
|
|
70
|
+
"root_name": manifest.header.get("root_name"),
|
|
71
|
+
"entries": manifest.entries,
|
|
72
|
+
"files": manifest.files,
|
|
73
|
+
"dirs": manifest.dirs,
|
|
74
|
+
"symlinks": manifest.symlinks,
|
|
75
|
+
"chunks": manifest.chunks,
|
|
76
|
+
"bytes": manifest.bytes,
|
|
77
|
+
"largest_files": [
|
|
78
|
+
{
|
|
79
|
+
"path": record["path"],
|
|
80
|
+
"size": record.get("size", 0),
|
|
81
|
+
"sha256": record.get("sha256"),
|
|
82
|
+
"chunks": record.get("chunks", 0),
|
|
83
|
+
}
|
|
84
|
+
for record in largest_files
|
|
85
|
+
],
|
|
86
|
+
"symlink_records": [
|
|
87
|
+
{
|
|
88
|
+
"path": record["path"],
|
|
89
|
+
"target": record.get("target"),
|
|
90
|
+
}
|
|
91
|
+
for record in symlinks
|
|
92
|
+
],
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def print_text(data: dict[str, Any], limit: int) -> None:
|
|
97
|
+
print(f"Archive: {data['archive']}")
|
|
98
|
+
print(f"Version: {data['version']}")
|
|
99
|
+
print(f"Compression: {data['compression']}")
|
|
100
|
+
if data.get("source"):
|
|
101
|
+
print(f"Source: {data['source']}")
|
|
102
|
+
if data.get("created_at"):
|
|
103
|
+
print(f"Created: {data['created_at']}")
|
|
104
|
+
if data.get("root_name"):
|
|
105
|
+
print(f"Root: {data['root_name']}")
|
|
106
|
+
print(
|
|
107
|
+
"Entries: "
|
|
108
|
+
f"{data['entries']} "
|
|
109
|
+
f"({data['files']} files, {data['dirs']} dirs, {data['symlinks']} symlinks)"
|
|
110
|
+
)
|
|
111
|
+
print(f"File bytes: {human_size(int(data['bytes']))}")
|
|
112
|
+
print(f"Chunks: {data['chunks']}")
|
|
113
|
+
|
|
114
|
+
largest_files = data["largest_files"][: max(limit, 0)]
|
|
115
|
+
if largest_files:
|
|
116
|
+
print("Largest files:")
|
|
117
|
+
for record in largest_files:
|
|
118
|
+
chunk_text = f", {record['chunks']} chunks" if record.get("chunks") else ""
|
|
119
|
+
print(f" {human_size(int(record['size']))} {record['path']}{chunk_text}")
|
|
120
|
+
|
|
121
|
+
symlinks = data["symlink_records"][: max(limit, 0)]
|
|
122
|
+
if symlinks:
|
|
123
|
+
print("Symlinks:")
|
|
124
|
+
for record in symlinks:
|
|
125
|
+
print(f" {record['path']} -> {record['target']}")
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def main() -> int:
|
|
129
|
+
args = parse_args()
|
|
130
|
+
archive_path = Path(args.archive).expanduser().resolve()
|
|
131
|
+
|
|
132
|
+
try:
|
|
133
|
+
manifest = load_and_validate_archive(archive_path)
|
|
134
|
+
except (OSError, ValueError) as exc:
|
|
135
|
+
print(f"error: {exc}", file=sys.stderr)
|
|
136
|
+
return 1
|
|
137
|
+
|
|
138
|
+
data = inspect_data(archive_path, manifest)
|
|
139
|
+
limit = max(args.limit, 0)
|
|
140
|
+
data["largest_files"] = data["largest_files"][:limit]
|
|
141
|
+
data["symlink_records"] = data["symlink_records"][:limit]
|
|
142
|
+
if args.json:
|
|
143
|
+
print(json.dumps(data, indent=2, sort_keys=True))
|
|
144
|
+
else:
|
|
145
|
+
print_text(data, limit)
|
|
146
|
+
return 0
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
if __name__ == "__main__":
|
|
150
|
+
raise SystemExit(main())
|