File size: 2,347 Bytes
e58615a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Sube a Azure (report_artifacts root) los artifacts que necesitan los
reportes reales: parquets + bundles GNN. Reusa el patrón de
scripts/upload_preprocessed_labeled.py.

Uso:
    python scripts/upload_report_artifacts.py \
        --source /Users/pagrois/Racing/data [--overwrite]
"""

from __future__ import annotations

import argparse
import logging
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))

from azure.identity import ClientSecretCredential, DefaultAzureCredential
from azure.storage.filedatalake import DataLakeServiceClient

from racing_reports.config import DEFAULT_SETTINGS
from racing_reports.vendor_env import ARTIFACT_RELATIVE_PATHS


def _credential():
    import os

    t, c, s = (
        os.getenv("AZURE_TENANT_ID"),
        os.getenv("AZURE_CLIENT_ID"),
        os.getenv("AZURE_CLIENT_SECRET"),
    )
    if t and c and s:
        return ClientSecretCredential(tenant_id=t, client_id=c, client_secret=s)
    return DefaultAzureCredential()


def main() -> None:
    logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
    log = logging.getLogger(__name__)
    parser = argparse.ArgumentParser(description="Sube artifacts de reportes a Azure.")
    parser.add_argument("--source", required=True, help="Carpeta local con los artifacts.")
    parser.add_argument("--overwrite", action="store_true")
    args = parser.parse_args()

    source = Path(args.source)
    settings = DEFAULT_SETTINGS
    service = DataLakeServiceClient(
        account_url=settings.account_url, credential=_credential()
    )
    fs = service.get_file_system_client(settings.azure_filesystem)
    root = settings.azure_report_artifacts_root

    for rel in ARTIFACT_RELATIVE_PATHS:
        local = source / rel
        if not local.exists():
            log.warning("No existe (se saltea): %s", local)
            continue
        remote = f"{root}/{rel}".replace("//", "/")
        fc = fs.get_file_client(remote)
        if fc.exists() and not args.overwrite:
            log.info("Ya existe (usar --overwrite): %s", remote)
            continue
        log.info("Subiendo %s -> %s", local, remote)
        with local.open("rb") as f:
            fc.upload_data(f, overwrite=True)
    log.info("Listo.")


if __name__ == "__main__":
    main()