flat-packager 0.4.0__tar.gz → 0.4.1__tar.gz
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-0.4.0/src/flat_packager.egg-info → flat_packager-0.4.1}/PKG-INFO +25 -3
- {flat_packager-0.4.0 → flat_packager-0.4.1}/README.md +24 -2
- {flat_packager-0.4.0 → flat_packager-0.4.1}/pyproject.toml +1 -1
- {flat_packager-0.4.0 → flat_packager-0.4.1}/src/flat_packager/__init__.py +1 -1
- flat_packager-0.4.1/src/flat_packager/content.py +52 -0
- {flat_packager-0.4.0 → flat_packager-0.4.1}/src/flat_packager/pack.py +94 -21
- flat_packager-0.4.1/src/flat_packager/text_encoding.py +99 -0
- {flat_packager-0.4.0 → flat_packager-0.4.1}/src/flat_packager/unpack.py +19 -17
- {flat_packager-0.4.0 → flat_packager-0.4.1/src/flat_packager.egg-info}/PKG-INFO +25 -3
- {flat_packager-0.4.0 → flat_packager-0.4.1}/src/flat_packager.egg-info/SOURCES.txt +2 -0
- {flat_packager-0.4.0 → flat_packager-0.4.1}/tests/test_roundtrip.py +37 -0
- {flat_packager-0.4.0 → flat_packager-0.4.1}/LICENSE +0 -0
- {flat_packager-0.4.0 → flat_packager-0.4.1}/setup.cfg +0 -0
- {flat_packager-0.4.0 → flat_packager-0.4.1}/src/flat_packager/archive.py +0 -0
- {flat_packager-0.4.0 → flat_packager-0.4.1}/src/flat_packager/inspect.py +0 -0
- {flat_packager-0.4.0 → flat_packager-0.4.1}/src/flat_packager/verify.py +0 -0
- {flat_packager-0.4.0 → flat_packager-0.4.1}/src/flat_packager.egg-info/dependency_links.txt +0 -0
- {flat_packager-0.4.0 → flat_packager-0.4.1}/src/flat_packager.egg-info/entry_points.txt +0 -0
- {flat_packager-0.4.0 → flat_packager-0.4.1}/src/flat_packager.egg-info/requires.txt +0 -0
- {flat_packager-0.4.0 → flat_packager-0.4.1}/src/flat_packager.egg-info/top_level.txt +0 -0
- {flat_packager-0.4.0 → flat_packager-0.4.1}/tests/test_version.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: flat-packager
|
|
3
|
-
Version: 0.4.
|
|
3
|
+
Version: 0.4.1
|
|
4
4
|
Summary: Pack a repository into one flat text archive and restore it later.
|
|
5
5
|
Author: Thomas
|
|
6
6
|
License-Expression: MIT
|
|
@@ -114,8 +114,29 @@ Useful options:
|
|
|
114
114
|
flat-pack /path/to/repo repo.flat.txt --exclude "node_modules/*"
|
|
115
115
|
flat-pack /path/to/repo repo.flat.txt --max-file-bytes 10485760
|
|
116
116
|
flat-pack /path/to/repo repo.flat.txt --include-git
|
|
117
|
+
flat-pack /path/to/repo repo.flat.txt --text-encoding auto
|
|
117
118
|
```
|
|
118
119
|
|
|
120
|
+
## Text Encoding
|
|
121
|
+
|
|
122
|
+
By default every file is stored as base64 (binary-safe). For human-readable
|
|
123
|
+
archives, use `--text-encoding auto` to store known text files such as `.py`,
|
|
124
|
+
`.md`, `.yaml`, and `.json` as plain UTF-8 JSON strings:
|
|
125
|
+
|
|
126
|
+
```bash
|
|
127
|
+
flat-pack /path/to/repo repo.flat.txt --text-encoding auto --compress none
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
In the archive, text files look like:
|
|
131
|
+
|
|
132
|
+
```json
|
|
133
|
+
{"encoding":"utf8","path":"src/app.py","content":"def main():\n ..."}
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
Binary files still use base64 chunks. Use `--text-encoding utf8` to store any
|
|
137
|
+
valid UTF-8 file as readable text. The default `--text-encoding base64` keeps
|
|
138
|
+
every file base64-encoded for maximum compatibility.
|
|
139
|
+
|
|
119
140
|
## Format and Compression
|
|
120
141
|
|
|
121
142
|
New archives use format version 2 by default. Version 2 stores each regular
|
|
@@ -209,8 +230,9 @@ written as text files instead.
|
|
|
209
230
|
|
|
210
231
|
## Limitations
|
|
211
232
|
|
|
212
|
-
-
|
|
213
|
-
|
|
233
|
+
- With the default `--text-encoding base64`, file contents are base64 encoded,
|
|
234
|
+
so archives are larger than raw file bytes. Use `--text-encoding auto` for
|
|
235
|
+
readable source text, or gzip compression when archive size matters.
|
|
214
236
|
- Legacy format version 1 stores each file as one JSON record and is
|
|
215
237
|
memory-heavy for large files. Version 2 is chunked and is the default.
|
|
216
238
|
- Remote GitHub packing depends on local `git` and whatever authentication your
|
|
@@ -83,8 +83,29 @@ Useful options:
|
|
|
83
83
|
flat-pack /path/to/repo repo.flat.txt --exclude "node_modules/*"
|
|
84
84
|
flat-pack /path/to/repo repo.flat.txt --max-file-bytes 10485760
|
|
85
85
|
flat-pack /path/to/repo repo.flat.txt --include-git
|
|
86
|
+
flat-pack /path/to/repo repo.flat.txt --text-encoding auto
|
|
86
87
|
```
|
|
87
88
|
|
|
89
|
+
## Text Encoding
|
|
90
|
+
|
|
91
|
+
By default every file is stored as base64 (binary-safe). For human-readable
|
|
92
|
+
archives, use `--text-encoding auto` to store known text files such as `.py`,
|
|
93
|
+
`.md`, `.yaml`, and `.json` as plain UTF-8 JSON strings:
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
flat-pack /path/to/repo repo.flat.txt --text-encoding auto --compress none
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
In the archive, text files look like:
|
|
100
|
+
|
|
101
|
+
```json
|
|
102
|
+
{"encoding":"utf8","path":"src/app.py","content":"def main():\n ..."}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Binary files still use base64 chunks. Use `--text-encoding utf8` to store any
|
|
106
|
+
valid UTF-8 file as readable text. The default `--text-encoding base64` keeps
|
|
107
|
+
every file base64-encoded for maximum compatibility.
|
|
108
|
+
|
|
88
109
|
## Format and Compression
|
|
89
110
|
|
|
90
111
|
New archives use format version 2 by default. Version 2 stores each regular
|
|
@@ -178,8 +199,9 @@ written as text files instead.
|
|
|
178
199
|
|
|
179
200
|
## Limitations
|
|
180
201
|
|
|
181
|
-
-
|
|
182
|
-
|
|
202
|
+
- With the default `--text-encoding base64`, file contents are base64 encoded,
|
|
203
|
+
so archives are larger than raw file bytes. Use `--text-encoding auto` for
|
|
204
|
+
readable source text, or gzip compression when archive size matters.
|
|
183
205
|
- Legacy format version 1 stores each file as one JSON record and is
|
|
184
206
|
memory-heavy for large files. Version 2 is chunked and is the default.
|
|
185
207
|
- Remote GitHub packing depends on local `git` and whatever authentication your
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"""Encode and decode inline archive file payloads."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import base64
|
|
6
|
+
import binascii
|
|
7
|
+
import hashlib
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
INLINE_ENCODINGS = frozenset({"base64", "utf8"})
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def file_content_bytes(path: str, record: dict[str, Any]) -> bytes:
|
|
15
|
+
encoding = record.get("encoding")
|
|
16
|
+
if encoding not in INLINE_ENCODINGS:
|
|
17
|
+
raise ValueError(f"{path}: unsupported inline encoding {encoding!r}")
|
|
18
|
+
|
|
19
|
+
if encoding == "base64":
|
|
20
|
+
content = record.get("content")
|
|
21
|
+
if not isinstance(content, str):
|
|
22
|
+
raise ValueError(f"{path}: content must be a base64 string")
|
|
23
|
+
try:
|
|
24
|
+
return base64.b64decode(content.encode("ascii"), validate=True)
|
|
25
|
+
except (UnicodeEncodeError, binascii.Error) as exc:
|
|
26
|
+
raise ValueError(f"{path}: invalid base64 content") from exc
|
|
27
|
+
|
|
28
|
+
content = record.get("content")
|
|
29
|
+
if not isinstance(content, str):
|
|
30
|
+
raise ValueError(f"{path}: content must be a UTF-8 string")
|
|
31
|
+
return content.encode("utf-8")
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def validate_inline_file_record(record: dict[str, Any]) -> int:
|
|
35
|
+
path = record.get("path")
|
|
36
|
+
if not isinstance(path, str):
|
|
37
|
+
raise ValueError("record path must be a string")
|
|
38
|
+
encoding = record.get("encoding")
|
|
39
|
+
if encoding not in INLINE_ENCODINGS:
|
|
40
|
+
raise ValueError(f"{path}: unsupported encoding {encoding!r}")
|
|
41
|
+
|
|
42
|
+
content = file_content_bytes(path, record)
|
|
43
|
+
expected_size = record.get("size")
|
|
44
|
+
if not isinstance(expected_size, int) or expected_size < 0:
|
|
45
|
+
raise ValueError(f"{path}: invalid size {expected_size!r}")
|
|
46
|
+
if len(content) != expected_size:
|
|
47
|
+
raise ValueError(f"{path}: size mismatch")
|
|
48
|
+
|
|
49
|
+
digest = hashlib.sha256(content).hexdigest()
|
|
50
|
+
if digest != record.get("sha256"):
|
|
51
|
+
raise ValueError(f"{path}: sha256 mismatch")
|
|
52
|
+
return expected_size
|
|
@@ -23,6 +23,7 @@ from .archive import (
|
|
|
23
23
|
emit_json_line,
|
|
24
24
|
open_archive_for_write,
|
|
25
25
|
)
|
|
26
|
+
from .text_encoding import TEXT_ENCODING_CHOICES, should_store_as_utf8
|
|
26
27
|
|
|
27
28
|
|
|
28
29
|
def parse_args() -> argparse.Namespace:
|
|
@@ -97,6 +98,16 @@ def parse_args() -> argparse.Namespace:
|
|
|
97
98
|
default="auto",
|
|
98
99
|
help="Compress archive output. 'auto' uses gzip for .gz output paths.",
|
|
99
100
|
)
|
|
101
|
+
parser.add_argument(
|
|
102
|
+
"--text-encoding",
|
|
103
|
+
choices=sorted(TEXT_ENCODING_CHOICES),
|
|
104
|
+
default="base64",
|
|
105
|
+
help=(
|
|
106
|
+
"How to store file contents. 'base64' always encodes binary-safe base64. "
|
|
107
|
+
"'auto' stores known text files as readable UTF-8 JSON strings. "
|
|
108
|
+
"'utf8' stores any valid UTF-8 file as readable text."
|
|
109
|
+
),
|
|
110
|
+
)
|
|
100
111
|
return parser.parse_args()
|
|
101
112
|
|
|
102
113
|
|
|
@@ -273,21 +284,39 @@ def validate_regular_file(path: Path, rel: str, max_file_bytes: int | None) -> o
|
|
|
273
284
|
return metadata
|
|
274
285
|
|
|
275
286
|
|
|
276
|
-
def
|
|
287
|
+
def inline_file_record(
|
|
288
|
+
root: Path,
|
|
289
|
+
path: Path,
|
|
290
|
+
max_file_bytes: int | None,
|
|
291
|
+
text_encoding: str,
|
|
292
|
+
) -> dict[str, object]:
|
|
277
293
|
rel = safe_relative_path(root, path)
|
|
278
294
|
metadata = validate_regular_file(path, rel, max_file_bytes)
|
|
279
295
|
mode = stat.S_IMODE(metadata.st_mode)
|
|
280
|
-
size = metadata.st_size
|
|
281
296
|
content = path.read_bytes()
|
|
282
|
-
|
|
297
|
+
record: dict[str, object] = {
|
|
283
298
|
"type": "file",
|
|
284
299
|
"path": rel,
|
|
285
300
|
"mode": mode,
|
|
286
|
-
"size":
|
|
301
|
+
"size": len(content),
|
|
287
302
|
"sha256": hashlib.sha256(content).hexdigest(),
|
|
288
|
-
"encoding": "base64",
|
|
289
|
-
"content": base64.b64encode(content).decode("ascii"),
|
|
290
303
|
}
|
|
304
|
+
if should_store_as_utf8(rel, content, text_encoding):
|
|
305
|
+
record["encoding"] = "utf8"
|
|
306
|
+
record["content"] = content.decode("utf-8")
|
|
307
|
+
else:
|
|
308
|
+
record["encoding"] = "base64"
|
|
309
|
+
record["content"] = base64.b64encode(content).decode("ascii")
|
|
310
|
+
return record
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
def file_record_v1(
|
|
314
|
+
root: Path,
|
|
315
|
+
path: Path,
|
|
316
|
+
max_file_bytes: int | None,
|
|
317
|
+
text_encoding: str = "base64",
|
|
318
|
+
) -> dict[str, object]:
|
|
319
|
+
return inline_file_record(root, path, max_file_bytes, text_encoding)
|
|
291
320
|
|
|
292
321
|
|
|
293
322
|
def hash_file(path: Path, chunk_size: int) -> str:
|
|
@@ -298,6 +327,47 @@ def hash_file(path: Path, chunk_size: int) -> str:
|
|
|
298
327
|
return digest.hexdigest()
|
|
299
328
|
|
|
300
329
|
|
|
330
|
+
def emit_file_records(
|
|
331
|
+
handle,
|
|
332
|
+
root: Path,
|
|
333
|
+
path: Path,
|
|
334
|
+
max_file_bytes: int | None,
|
|
335
|
+
format_version: int,
|
|
336
|
+
chunk_size: int,
|
|
337
|
+
text_encoding: str,
|
|
338
|
+
) -> int:
|
|
339
|
+
rel = safe_relative_path(root, path)
|
|
340
|
+
metadata = validate_regular_file(path, rel, max_file_bytes)
|
|
341
|
+
content = path.read_bytes()
|
|
342
|
+
|
|
343
|
+
if should_store_as_utf8(rel, content, text_encoding):
|
|
344
|
+
emit_json_line(handle, inline_file_record(root, path, max_file_bytes, text_encoding))
|
|
345
|
+
return 0
|
|
346
|
+
|
|
347
|
+
if format_version == 1:
|
|
348
|
+
emit_json_line(
|
|
349
|
+
handle,
|
|
350
|
+
{
|
|
351
|
+
"type": "file",
|
|
352
|
+
"path": rel,
|
|
353
|
+
"mode": stat.S_IMODE(metadata.st_mode),
|
|
354
|
+
"size": len(content),
|
|
355
|
+
"sha256": hashlib.sha256(content).hexdigest(),
|
|
356
|
+
"encoding": "base64",
|
|
357
|
+
"content": base64.b64encode(content).decode("ascii"),
|
|
358
|
+
},
|
|
359
|
+
)
|
|
360
|
+
return 0
|
|
361
|
+
|
|
362
|
+
return emit_file_records_v2(
|
|
363
|
+
handle,
|
|
364
|
+
root,
|
|
365
|
+
path,
|
|
366
|
+
max_file_bytes,
|
|
367
|
+
chunk_size,
|
|
368
|
+
)
|
|
369
|
+
|
|
370
|
+
|
|
301
371
|
def emit_file_records_v2(
|
|
302
372
|
handle,
|
|
303
373
|
root: Path,
|
|
@@ -367,11 +437,14 @@ def build_archive(
|
|
|
367
437
|
format_version: int = ARCHIVE_VERSION,
|
|
368
438
|
chunk_size: int = DEFAULT_CHUNK_SIZE,
|
|
369
439
|
compression: str = "auto",
|
|
440
|
+
text_encoding: str = "base64",
|
|
370
441
|
) -> tuple[int, int]:
|
|
371
442
|
if format_version not in (1, 2):
|
|
372
443
|
raise ValueError(f"unsupported archive format version: {format_version}")
|
|
373
444
|
if chunk_size <= 0:
|
|
374
445
|
raise ValueError("--chunk-size must be greater than zero")
|
|
446
|
+
if text_encoding not in TEXT_ENCODING_CHOICES:
|
|
447
|
+
raise ValueError(f"unsupported text encoding mode: {text_encoding}")
|
|
375
448
|
|
|
376
449
|
output_path.parent.mkdir(parents=True, exist_ok=True)
|
|
377
450
|
|
|
@@ -386,6 +459,8 @@ def build_archive(
|
|
|
386
459
|
}
|
|
387
460
|
if format_version == 2:
|
|
388
461
|
header["chunk_size"] = chunk_size
|
|
462
|
+
if text_encoding != "base64":
|
|
463
|
+
header["text_encoding"] = text_encoding
|
|
389
464
|
|
|
390
465
|
entry_count = 0
|
|
391
466
|
file_count = 0
|
|
@@ -417,21 +492,18 @@ def build_archive(
|
|
|
417
492
|
symlink_count += 1
|
|
418
493
|
emit_json_line(handle, record)
|
|
419
494
|
else:
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
max_file_bytes,
|
|
433
|
-
chunk_size,
|
|
434
|
-
)
|
|
495
|
+
rel = safe_relative_path(root, path)
|
|
496
|
+
metadata = validate_regular_file(path, rel, max_file_bytes)
|
|
497
|
+
byte_count += metadata.st_size
|
|
498
|
+
chunk_count += emit_file_records(
|
|
499
|
+
handle,
|
|
500
|
+
root,
|
|
501
|
+
path,
|
|
502
|
+
max_file_bytes,
|
|
503
|
+
format_version,
|
|
504
|
+
chunk_size,
|
|
505
|
+
text_encoding,
|
|
506
|
+
)
|
|
435
507
|
file_count += 1
|
|
436
508
|
entry_count += 1
|
|
437
509
|
|
|
@@ -476,6 +548,7 @@ def main() -> int:
|
|
|
476
548
|
format_version=int(args.format_version),
|
|
477
549
|
chunk_size=args.chunk_size,
|
|
478
550
|
compression=args.compress,
|
|
551
|
+
text_encoding=args.text_encoding,
|
|
479
552
|
)
|
|
480
553
|
except (OSError, ValueError) as exc:
|
|
481
554
|
print(f"error: {exc}", file=sys.stderr)
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"""Detect text files suitable for UTF-8 archive storage."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from pathlib import PurePosixPath
|
|
6
|
+
|
|
7
|
+
TEXT_EXTENSIONS = frozenset(
|
|
8
|
+
{
|
|
9
|
+
".c",
|
|
10
|
+
".cfg",
|
|
11
|
+
".conf",
|
|
12
|
+
".cpp",
|
|
13
|
+
".css",
|
|
14
|
+
".csv",
|
|
15
|
+
".env",
|
|
16
|
+
".gitattributes",
|
|
17
|
+
".gitignore",
|
|
18
|
+
".go",
|
|
19
|
+
".h",
|
|
20
|
+
".hpp",
|
|
21
|
+
".html",
|
|
22
|
+
".ini",
|
|
23
|
+
".java",
|
|
24
|
+
".js",
|
|
25
|
+
".json",
|
|
26
|
+
".jsx",
|
|
27
|
+
".kt",
|
|
28
|
+
".lock",
|
|
29
|
+
".lua",
|
|
30
|
+
".md",
|
|
31
|
+
".mjs",
|
|
32
|
+
".php",
|
|
33
|
+
".pl",
|
|
34
|
+
".properties",
|
|
35
|
+
".py",
|
|
36
|
+
".pyi",
|
|
37
|
+
".rb",
|
|
38
|
+
".rs",
|
|
39
|
+
".rst",
|
|
40
|
+
".scala",
|
|
41
|
+
".sh",
|
|
42
|
+
".sql",
|
|
43
|
+
".svg",
|
|
44
|
+
".swift",
|
|
45
|
+
".toml",
|
|
46
|
+
".ts",
|
|
47
|
+
".tsx",
|
|
48
|
+
".txt",
|
|
49
|
+
".xml",
|
|
50
|
+
".yaml",
|
|
51
|
+
".yml",
|
|
52
|
+
".zsh",
|
|
53
|
+
}
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
TEXT_FILENAMES = frozenset(
|
|
57
|
+
{
|
|
58
|
+
"CODEOWNERS",
|
|
59
|
+
"Dockerfile",
|
|
60
|
+
"LICENSE",
|
|
61
|
+
"Makefile",
|
|
62
|
+
"README",
|
|
63
|
+
"requirements.txt",
|
|
64
|
+
}
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
TEXT_ENCODING_CHOICES = frozenset({"base64", "auto", "utf8"})
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def bytes_look_like_utf8_text(data: bytes) -> bool:
|
|
71
|
+
if b"\x00" in data:
|
|
72
|
+
return False
|
|
73
|
+
try:
|
|
74
|
+
data.decode("utf-8")
|
|
75
|
+
except UnicodeDecodeError:
|
|
76
|
+
return False
|
|
77
|
+
return True
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def path_looks_like_text(rel_path: str) -> bool:
|
|
81
|
+
name = PurePosixPath(rel_path).name
|
|
82
|
+
if name in TEXT_FILENAMES:
|
|
83
|
+
return True
|
|
84
|
+
if name.startswith("README."):
|
|
85
|
+
return True
|
|
86
|
+
suffix = PurePosixPath(rel_path).suffix.lower()
|
|
87
|
+
return suffix in TEXT_EXTENSIONS
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def should_store_as_utf8(rel_path: str, data: bytes, text_encoding: str) -> bool:
|
|
91
|
+
if text_encoding not in TEXT_ENCODING_CHOICES:
|
|
92
|
+
raise ValueError(f"unsupported text encoding mode: {text_encoding}")
|
|
93
|
+
if text_encoding == "base64":
|
|
94
|
+
return False
|
|
95
|
+
if not bytes_look_like_utf8_text(data):
|
|
96
|
+
return False
|
|
97
|
+
if text_encoding == "utf8":
|
|
98
|
+
return True
|
|
99
|
+
return path_looks_like_text(rel_path)
|
|
@@ -15,6 +15,7 @@ from pathlib import Path
|
|
|
15
15
|
from typing import Any
|
|
16
16
|
|
|
17
17
|
from .archive import ARCHIVE_KIND, SUPPORTED_ARCHIVE_VERSIONS, read_json_lines
|
|
18
|
+
from .content import file_content_bytes, validate_inline_file_record
|
|
18
19
|
|
|
19
20
|
|
|
20
21
|
ArchiveRecord = tuple[int, dict[str, Any]]
|
|
@@ -140,25 +141,14 @@ def decode_base64(path: str, content: Any) -> bytes:
|
|
|
140
141
|
raise ValueError(f"{path}: invalid base64 content") from exc
|
|
141
142
|
|
|
142
143
|
|
|
143
|
-
def
|
|
144
|
+
def process_inline_file(
|
|
144
145
|
record: dict[str, Any],
|
|
145
146
|
write_root: Path | None,
|
|
146
147
|
) -> int:
|
|
147
148
|
path = record_path(record)
|
|
148
149
|
validate_mode(record)
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
content = decode_base64(path, record.get("content"))
|
|
153
|
-
expected_size = record.get("size")
|
|
154
|
-
if not isinstance(expected_size, int) or expected_size < 0:
|
|
155
|
-
raise ValueError(f"{path}: invalid size {expected_size!r}")
|
|
156
|
-
if len(content) != expected_size:
|
|
157
|
-
raise ValueError(f"{path}: size mismatch")
|
|
158
|
-
|
|
159
|
-
digest = hashlib.sha256(content).hexdigest()
|
|
160
|
-
if digest != record.get("sha256"):
|
|
161
|
-
raise ValueError(f"{path}: sha256 mismatch")
|
|
150
|
+
expected_size = validate_inline_file_record(record)
|
|
151
|
+
content = file_content_bytes(path, record)
|
|
162
152
|
|
|
163
153
|
if write_root is not None:
|
|
164
154
|
target = safe_output_path(write_root, path)
|
|
@@ -171,6 +161,13 @@ def process_file_v1(
|
|
|
171
161
|
return expected_size
|
|
172
162
|
|
|
173
163
|
|
|
164
|
+
def process_file_v1(
|
|
165
|
+
record: dict[str, Any],
|
|
166
|
+
write_root: Path | None,
|
|
167
|
+
) -> int:
|
|
168
|
+
return process_inline_file(record, write_root)
|
|
169
|
+
|
|
170
|
+
|
|
174
171
|
def start_active_file(
|
|
175
172
|
record: dict[str, Any],
|
|
176
173
|
write_root: Path | None,
|
|
@@ -405,11 +402,16 @@ def process_archive(
|
|
|
405
402
|
process_dir_record(record, write_root, dir_modes)
|
|
406
403
|
dirs += 1
|
|
407
404
|
elif record_type == "file":
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
405
|
+
encoding = record.get("encoding")
|
|
406
|
+
if version == 1 or encoding in {"base64", "utf8"}:
|
|
407
|
+
bytes_count += process_inline_file(record, write_root)
|
|
408
|
+
elif encoding == "base64-chunks":
|
|
411
409
|
active_file = start_active_file(record, write_root)
|
|
412
410
|
bytes_count += int(record["size"])
|
|
411
|
+
else:
|
|
412
|
+
raise ValueError(
|
|
413
|
+
f"{path}: unsupported file encoding {encoding!r}"
|
|
414
|
+
)
|
|
413
415
|
files += 1
|
|
414
416
|
else:
|
|
415
417
|
process_symlink_record(record, write_root, no_symlinks)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: flat-packager
|
|
3
|
-
Version: 0.4.
|
|
3
|
+
Version: 0.4.1
|
|
4
4
|
Summary: Pack a repository into one flat text archive and restore it later.
|
|
5
5
|
Author: Thomas
|
|
6
6
|
License-Expression: MIT
|
|
@@ -114,8 +114,29 @@ Useful options:
|
|
|
114
114
|
flat-pack /path/to/repo repo.flat.txt --exclude "node_modules/*"
|
|
115
115
|
flat-pack /path/to/repo repo.flat.txt --max-file-bytes 10485760
|
|
116
116
|
flat-pack /path/to/repo repo.flat.txt --include-git
|
|
117
|
+
flat-pack /path/to/repo repo.flat.txt --text-encoding auto
|
|
117
118
|
```
|
|
118
119
|
|
|
120
|
+
## Text Encoding
|
|
121
|
+
|
|
122
|
+
By default every file is stored as base64 (binary-safe). For human-readable
|
|
123
|
+
archives, use `--text-encoding auto` to store known text files such as `.py`,
|
|
124
|
+
`.md`, `.yaml`, and `.json` as plain UTF-8 JSON strings:
|
|
125
|
+
|
|
126
|
+
```bash
|
|
127
|
+
flat-pack /path/to/repo repo.flat.txt --text-encoding auto --compress none
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
In the archive, text files look like:
|
|
131
|
+
|
|
132
|
+
```json
|
|
133
|
+
{"encoding":"utf8","path":"src/app.py","content":"def main():\n ..."}
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
Binary files still use base64 chunks. Use `--text-encoding utf8` to store any
|
|
137
|
+
valid UTF-8 file as readable text. The default `--text-encoding base64` keeps
|
|
138
|
+
every file base64-encoded for maximum compatibility.
|
|
139
|
+
|
|
119
140
|
## Format and Compression
|
|
120
141
|
|
|
121
142
|
New archives use format version 2 by default. Version 2 stores each regular
|
|
@@ -209,8 +230,9 @@ written as text files instead.
|
|
|
209
230
|
|
|
210
231
|
## Limitations
|
|
211
232
|
|
|
212
|
-
-
|
|
213
|
-
|
|
233
|
+
- With the default `--text-encoding base64`, file contents are base64 encoded,
|
|
234
|
+
so archives are larger than raw file bytes. Use `--text-encoding auto` for
|
|
235
|
+
readable source text, or gzip compression when archive size matters.
|
|
214
236
|
- Legacy format version 1 stores each file as one JSON record and is
|
|
215
237
|
memory-heavy for large files. Version 2 is chunked and is the default.
|
|
216
238
|
- Remote GitHub packing depends on local `git` and whatever authentication your
|
|
@@ -3,8 +3,10 @@ README.md
|
|
|
3
3
|
pyproject.toml
|
|
4
4
|
src/flat_packager/__init__.py
|
|
5
5
|
src/flat_packager/archive.py
|
|
6
|
+
src/flat_packager/content.py
|
|
6
7
|
src/flat_packager/inspect.py
|
|
7
8
|
src/flat_packager/pack.py
|
|
9
|
+
src/flat_packager/text_encoding.py
|
|
8
10
|
src/flat_packager/unpack.py
|
|
9
11
|
src/flat_packager/verify.py
|
|
10
12
|
src/flat_packager.egg-info/PKG-INFO
|
|
@@ -427,6 +427,43 @@ class RoundTripTests(unittest.TestCase):
|
|
|
427
427
|
|
|
428
428
|
self.assertFalse(output.exists())
|
|
429
429
|
|
|
430
|
+
def test_auto_text_encoding_stores_source_as_readable_utf8(self) -> None:
|
|
431
|
+
source = self.work_dir / "source"
|
|
432
|
+
archive = self.work_dir / "repo.flat.txt"
|
|
433
|
+
restored = self.work_dir / "restored"
|
|
434
|
+
source.mkdir()
|
|
435
|
+
(source / "hello.py").write_text("def greet():\n return 'hi'\n", encoding="utf-8")
|
|
436
|
+
(source / "data.bin").write_bytes(bytes(range(256)))
|
|
437
|
+
|
|
438
|
+
build_archive(
|
|
439
|
+
root=source,
|
|
440
|
+
output_path=archive,
|
|
441
|
+
source_label=str(source),
|
|
442
|
+
tracked_only=False,
|
|
443
|
+
include_git=False,
|
|
444
|
+
exclude_patterns=[],
|
|
445
|
+
max_file_bytes=None,
|
|
446
|
+
text_encoding="auto",
|
|
447
|
+
)
|
|
448
|
+
|
|
449
|
+
archive_text = archive.read_text(encoding="utf-8")
|
|
450
|
+
self.assertIn('"encoding":"utf8"', archive_text)
|
|
451
|
+
self.assertIn("def greet():", archive_text)
|
|
452
|
+
self.assertIn('"encoding":"base64-chunks"', archive_text)
|
|
453
|
+
|
|
454
|
+
restore_archive(
|
|
455
|
+
archive_path=archive,
|
|
456
|
+
output_dir=restored,
|
|
457
|
+
overwrite=False,
|
|
458
|
+
verify_only=False,
|
|
459
|
+
no_symlinks=False,
|
|
460
|
+
)
|
|
461
|
+
self.assertEqual(
|
|
462
|
+
(restored / "hello.py").read_text(encoding="utf-8"),
|
|
463
|
+
"def greet():\n return 'hi'\n",
|
|
464
|
+
)
|
|
465
|
+
self.assertEqual((restored / "data.bin").read_bytes(), bytes(range(256)))
|
|
466
|
+
|
|
430
467
|
def test_remote_clone_options_are_passed_to_git(self) -> None:
|
|
431
468
|
with patch("flat_packager.pack.subprocess.run") as run:
|
|
432
469
|
run.return_value = subprocess.CompletedProcess(args=[], returncode=0)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|