File size: 4,565 Bytes
92cf271
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Copy civic intelligence data to SafeVixAI-Dataset-Hub for HuggingFace upload.

Usage:
    python scripts/data/sync_to_dataset_hub.py
"""

from __future__ import annotations

import json
import shutil
from datetime import datetime, timezone
from pathlib import Path

PROJECT_ROOT = Path(__file__).resolve().parents[2]
CIVIC_DATA = PROJECT_ROOT / 'data' / 'civic_intel'
FRONTEND_OFFLINE = PROJECT_ROOT.parent / 'frontend' / 'public' / 'offline-data'

HUB_ROOT = PROJECT_ROOT.parent.parent / 'SafeVixAI-Dataset-Hub'
HUB_CIVIC = HUB_ROOT / 'data' / 'backend' / 'data' / 'civic_intel'
HUB_SCRIPTS = HUB_ROOT / 'scripts' / 'backend' / 'data'


def _human_size(size_bytes: int) -> str:
    if size_bytes < 1024:
        return f'{size_bytes} B'
    elif size_bytes < 1024 * 1024:
        return f'{size_bytes / 1024:.1f} KB'
    else:
        return f'{size_bytes / (1024 * 1024):.1f} MB'


def copy_tree(src: Path, dst: Path, label: str) -> tuple[int, int]:
    """Copy directory tree, return (files_copied, bytes_copied)."""
    dst.mkdir(parents=True, exist_ok=True)
    files = 0
    total_bytes = 0

    for item in sorted(src.rglob('*')):
        if item.is_dir():
            continue
        if item.name.startswith('.') or '__pycache__' in str(item):
            continue

        rel = item.relative_to(src)
        dest = dst / rel
        dest.parent.mkdir(parents=True, exist_ok=True)
        shutil.copy2(item, dest)
        files += 1
        total_bytes += item.stat().st_size

    print(f'  {label}: {files} files, {_human_size(total_bytes)}')
    return files, total_bytes


def main():
    print('\n' + '=' * 60)
    print('  SafeVixAI -> Dataset Hub Sync')
    print('=' * 60)

    if not HUB_ROOT.exists():
        print(f'  ERROR: Hub not found at {HUB_ROOT}')
        return

    if not CIVIC_DATA.exists():
        print(f'  ERROR: Civic data not found at {CIVIC_DATA}')
        return

    total_files = 0
    total_bytes = 0

    # 1. Copy civic_intel data
    print('\n  [1] Copying civic_intel data...')
    f, b = copy_tree(CIVIC_DATA, HUB_CIVIC, 'civic_intel')
    total_files += f
    total_bytes += b

    # 2. Copy civic data scripts
    print('\n  [2] Copying civic data scripts...')
    scripts_src = PROJECT_ROOT / 'scripts' / 'data'
    civic_scripts = [
        'fetch_lgd_hierarchy.py',
        'fetch_osm_civic_features.py',
        'fetch_datameet_boundaries.py',
        'validate_civic_data.py',
        'prepare_huggingface_upload.py',
        'generate_frontend_civic_summary.py',
        'fix_boundaries.py',
    ]
    HUB_SCRIPTS.mkdir(parents=True, exist_ok=True)
    for script_name in civic_scripts:
        src = scripts_src / script_name
        if src.exists():
            dst = HUB_SCRIPTS / script_name
            shutil.copy2(src, dst)
            total_files += 1
            total_bytes += src.stat().st_size
            print(f'    {script_name}')
        else:
            print(f'    SKIP {script_name} (not found)')

    # 3. Copy frontend offline bundles
    print('\n  [3] Copying frontend offline bundles...')
    frontend_hub = HUB_ROOT / 'data' / 'frontend' / 'offline-data'
    civic_frontend_files = [
        'municipalities_bundle.json',
        'civic_features_summary.json',
    ]
    frontend_hub.mkdir(parents=True, exist_ok=True)
    for fname in civic_frontend_files:
        src = FRONTEND_OFFLINE / fname
        if src.exists():
            shutil.copy2(src, frontend_hub / fname)
            total_files += 1
            total_bytes += src.stat().st_size
            print(f'    {fname}: {_human_size(src.stat().st_size)}')
        else:
            print(f'    SKIP {fname} (not found)')

    # 4. Generate sync manifest
    manifest = {
        'synced_at': datetime.now(timezone.utc).isoformat(),
        'source': str(PROJECT_ROOT),
        'destination': str(HUB_ROOT),
        'total_files': total_files,
        'total_bytes': total_bytes,
        'total_size_human': _human_size(total_bytes),
    }
    manifest_path = HUB_CIVIC / '_sync_manifest.json'
    manifest_path.write_text(json.dumps(manifest, indent=2), encoding='utf-8')

    print(f'\n{"=" * 60}')
    print(f'  SYNC COMPLETE')
    print(f'{"=" * 60}')
    print(f'  Files: {total_files}')
    print(f'  Size: {_human_size(total_bytes)}')
    print(f'  Destination: {HUB_ROOT}')
    print(f'\n  Next steps:')
    print(f'    cd {HUB_ROOT}')
    print(f'    git add .')
    print(f'    git commit -m "feat: add civic intelligence data"')
    print(f'    git push')
    print()


if __name__ == '__main__':
    main()