Repository navigation
Expand file tree
/
Copy pathpackage_install_tarball.py
More file actions
executable file
·193 lines (167 loc) · 5.75 KB
/
Copy pathpackage_install_tarball.py
File metadata and controls
executable file
·193 lines (167 loc) · 5.75 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env python3
"""package_install_tarball.py — build and pack an install-tree tarball.
Used for Ansible artifact deployment.
Usage:
python3 tools/python/package_install_tarball.py
python3 tools/python/package_install_tarball.py --output /tmp/autonomy.tar.gz
BUNDLE_CONFIG=OFF python3 tools/python/package_install_tarball.py
"""
from __future__ import annotations
import argparse
import json
import os
import shutil
import subprocess
import sys
import tempfile
from datetime import datetime, timezone
from pathlib import Path
def repository_root() -> Path:
return Path(__file__).resolve().parent.parent
def git_describe(root: Path) -> str:
try:
out = subprocess.run(
["git", "-C", str(root), "describe", "--tags", "--always"],
check=True,
capture_output=True,
text=True,
)
return out.stdout.strip() or "dev"
except (subprocess.CalledProcessError, FileNotFoundError):
return "dev"
def git_sha(root: Path) -> str:
try:
out = subprocess.run(
["git", "-C", str(root), "rev-parse", "HEAD"],
check=True,
capture_output=True,
text=True,
)
return out.stdout.strip() or "unknown"
except (subprocess.CalledProcessError, FileNotFoundError):
return "unknown"
def default_jobs() -> int:
try:
return len(os.sched_getaffinity(0))
except (AttributeError, OSError):
return os.cpu_count() or 4
def run(cmd: list[str], *, env: dict[str, str] | None = None) -> None:
print("+", " ".join(cmd), flush=True)
subprocess.run(cmd, check=True, env=env)
def main(argv: list[str] | None = None) -> int:
root = repository_root()
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--build-dir",
default=os.environ.get("BUILD_DIR", str(root / "build")),
)
parser.add_argument(
"--output",
default=os.environ.get(
"OUTPUT",
str(root / "dist" / f"autonomy-{git_describe(root)}.tar.gz"),
),
)
parser.add_argument(
"--prefix",
default=os.environ.get("INSTALL_PREFIX", "/opt/autonomy"),
)
parser.add_argument(
"--no-config",
action="store_true",
help="Do not bundle config/ into the tarball",
)
parser.add_argument(
"-j",
"--jobs",
type=int,
default=int(os.environ.get("JOBS", default_jobs())),
)
args = parser.parse_args(argv)
build_dir = Path(args.build_dir)
output = Path(args.output)
install_prefix = Path(args.prefix)
cmake_build_type = os.environ.get("CMAKE_BUILD_TYPE", "Release")
build_grpc = os.environ.get("BUILD_GRPC", "ON")
build_test = os.environ.get("BUILD_TEST", "OFF")
bundle_config = (
False
if args.no_config
else os.environ.get("BUNDLE_CONFIG", "ON").upper() in ("1", "ON", "TRUE", "YES")
)
output.parent.mkdir(parents=True, exist_ok=True)
build_dir.mkdir(parents=True, exist_ok=True)
stage = Path(tempfile.mkdtemp(prefix="autonomy-pkg-"))
try:
print(f"[package] configure: {build_dir}")
run(
[
"cmake",
"-G",
"Ninja",
"-S",
str(root),
"-B",
str(build_dir),
f"-DCMAKE_BUILD_TYPE={cmake_build_type}",
f"-DCMAKE_INSTALL_PREFIX={install_prefix}",
f"-DBUILD_GRPC={build_grpc}",
f"-DBUILD_TEST={build_test}",
]
)
print(f"[package] build + stage install under {stage}")
env = os.environ.copy()
env["DESTDIR"] = str(stage)
run(["ninja", "-C", str(build_dir), f"-j{args.jobs}", "install"], env=env)
staged_prefix = stage / install_prefix.relative_to("/")
if bundle_config:
config_dest = staged_prefix / "share" / "autonomy" / "config"
print("[package] bundle config/ -> share/autonomy/config")
config_dest.mkdir(parents=True, exist_ok=True)
# Prefer rsync when available; fall back to copytree.
src_config = root / "config"
if shutil.which("rsync"):
run(
[
"rsync",
"-a",
"--delete",
f"{src_config}/",
f"{config_dest}/",
]
)
else:
if config_dest.exists():
shutil.rmtree(config_dest)
shutil.copytree(src_config, config_dest)
manifest_path = staged_prefix / "share" / "autonomy" / "MANIFEST.json"
manifest_path.parent.mkdir(parents=True, exist_ok=True)
manifest = {
"git_sha": git_sha(root),
"git_describe": git_describe(root),
"install_prefix": str(install_prefix),
"cmake_build_type": cmake_build_type,
"build_grpc": build_grpc,
"build_test": build_test,
"config_bundled": bundle_config,
"packaged_at": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
}
manifest_path.write_text(json.dumps(manifest, indent=2) + "\n", encoding="utf-8")
tar_name = install_prefix.name
tar_parent = stage / install_prefix.parent.relative_to("/")
run(
[
"tar",
"-C",
str(tar_parent),
"-czf",
str(output),
tar_name,
]
)
print(f"[package] wrote {output}")
finally:
shutil.rmtree(stage, ignore_errors=True)
return 0
if __name__ == "__main__":
raise SystemExit(main())