Repository navigation
Expand file tree
/
Copy pathmaterialize_localization_artifact.py
More file actions
85 lines (67 loc) · 2.94 KB
/
Copy pathmaterialize_localization_artifact.py
File metadata and controls
85 lines (67 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python3
"""Safely copy generated localization outputs into a trusted checkout."""
from __future__ import annotations
import argparse
import os
import shutil
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
REGISTRY_PATH = Path("docs/locale-registry.js")
def validate_artifact(artifact_root: Path) -> None:
localization_root = artifact_root / "localizations"
registry_path = artifact_root / REGISTRY_PATH
if not localization_root.is_dir() or localization_root.is_symlink():
raise RuntimeError("Artifact must contain a regular localizations directory.")
if not registry_path.is_file() or registry_path.is_symlink():
raise RuntimeError(f"Artifact must contain a regular {REGISTRY_PATH} file.")
for directory, directory_names, file_names in os.walk(
artifact_root,
followlinks=False,
):
directory_path = Path(directory)
for name in directory_names:
path = directory_path / name
if path.is_symlink():
raise RuntimeError(f"Artifact contains a symbolic link: {path}")
relative_path = path.relative_to(artifact_root)
if (
relative_path != Path("docs")
and relative_path.parts[0] != "localizations"
):
raise RuntimeError(f"Artifact contains an unexpected path: {relative_path}")
for name in file_names:
path = directory_path / name
if path.is_symlink() or not path.is_file():
raise RuntimeError(f"Artifact contains a non-regular file: {path}")
relative_path = path.relative_to(artifact_root)
if (
relative_path != REGISTRY_PATH
and relative_path.parts[0] != "localizations"
):
raise RuntimeError(f"Artifact contains an unexpected path: {relative_path}")
def materialize(artifact_root: Path, repository_root: Path = ROOT) -> None:
artifact_root = artifact_root.resolve()
repository_root = repository_root.resolve()
validate_artifact(artifact_root)
target_localizations = repository_root / "localizations"
if target_localizations.exists():
shutil.rmtree(target_localizations)
shutil.copytree(artifact_root / "localizations", target_localizations)
target_registry = repository_root / REGISTRY_PATH
target_registry.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(artifact_root / REGISTRY_PATH, target_registry)
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument(
"artifact_root",
type=Path,
help="Directory containing localizations/ and docs/locale-registry.js.",
)
return parser.parse_args()
def main() -> int:
args = parse_args()
materialize(args.artifact_root)
print("Materialized trusted localization output paths.")
return 0
if __name__ == "__main__":
raise SystemExit(main())