Keep source, config, and deploy files. Leave runtime data, uploads, and logs out of git. Co-authored-by: Cursor <cursoragent@cursor.com>
54 lines
1.7 KiB
Python
54 lines
1.7 KiB
Python
"""Rewrite Debian apt sources when mirror build args are set.
|
|
|
|
Empty DEBIAN_MIRROR and DEBIAN_SECURITY_MIRROR leave the image sources unchanged.
|
|
Security URLs are rewritten first so a main-archive mirror does not swallow them.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import pathlib
|
|
|
|
_HTTP_SECURITY = "http://deb.debian.org/debian-security"
|
|
_HTTPS_SECURITY = "https://deb.debian.org/debian-security"
|
|
_HTTP_MAIN = "http://deb.debian.org/debian"
|
|
_HTTPS_MAIN = "https://deb.debian.org/debian"
|
|
_HOLD_HTTP = "__KAMIDSO_HTTP_SEC__"
|
|
_HOLD_HTTPS = "__KAMIDSO_HTTPS_SEC__"
|
|
|
|
|
|
def _sources() -> list[pathlib.Path]:
|
|
paths: list[pathlib.Path] = []
|
|
root = pathlib.Path("/etc/apt/sources.list")
|
|
if root.is_file():
|
|
paths.append(root)
|
|
extra = pathlib.Path("/etc/apt/sources.list.d")
|
|
if extra.is_dir():
|
|
paths.extend(path for path in extra.iterdir() if path.is_file())
|
|
return paths
|
|
|
|
|
|
def _rewrite(text: str, mirror: str, security: str) -> str:
|
|
if security:
|
|
text = text.replace(_HTTP_SECURITY, security).replace(_HTTPS_SECURITY, security)
|
|
else:
|
|
text = text.replace(_HTTP_SECURITY, _HOLD_HTTP).replace(_HTTPS_SECURITY, _HOLD_HTTPS)
|
|
if mirror:
|
|
text = text.replace(_HTTP_MAIN, mirror).replace(_HTTPS_MAIN, mirror)
|
|
if not security:
|
|
text = text.replace(_HOLD_HTTP, _HTTP_SECURITY).replace(_HOLD_HTTPS, _HTTPS_SECURITY)
|
|
return text
|
|
|
|
|
|
def main() -> None:
|
|
mirror = os.environ.get("DEBIAN_MIRROR", "").strip()
|
|
security = os.environ.get("DEBIAN_SECURITY_MIRROR", "").strip()
|
|
if not mirror and not security:
|
|
return
|
|
for path in _sources():
|
|
path.write_text(_rewrite(path.read_text(), mirror, security))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|