File size: 7,610 Bytes
0790cf1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/env python3
"""
Atomic Dataset Persistence for HermesFace
Save state to Hugging Face Dataset with atomic commit, checksum, and backup.

Usage:
    python3 save_to_dataset_atomic.py <repo_id> <source_path1> [source_path2...]

Env vars:
    HF_TOKEN              HF access token (read from env by huggingface_hub)
"""
import hashlib
import json
import logging
import os
import shutil
import sys
import tempfile
import time
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, List, Optional

from huggingface_hub import CommitOperationAdd, HfApi, hf_hub_download
from huggingface_hub.utils import RepositoryNotFoundError

logging.basicConfig(
    level=logging.INFO,
    format='{"timestamp": "%(asctime)s", "level": "%(levelname)s", "module": "atomic-save", "message": "%(message)s"}',
)
logger = logging.getLogger(__name__)


class AtomicDatasetSaver:
    def __init__(self, repo_id: str, dataset_path: str = "state"):
        self.repo_id = repo_id
        self.dataset_path = Path(dataset_path)
        self.api = HfApi()
        self.max_retries = 3
        self.base_delay = 1.0
        self.max_backups = 3
        logger.info(f"init repo_id={repo_id} dataset_path={dataset_path}")

    def calculate_checksum(self, file_path: Path) -> str:
        h = hashlib.sha256()
        with open(file_path, "rb") as f:
            for chunk in iter(lambda: f.read(4096), b""):
                h.update(chunk)
        return h.hexdigest()

    def create_backup(self, current_commit: Optional[str]) -> Optional[str]:
        if not current_commit:
            return None
        try:
            files = self.api.list_repo_files(
                repo_id=self.repo_id, repo_type="dataset", revision=current_commit
            )
            state_files = [f for f in files if f.startswith(str(self.dataset_path))]
            if not state_files:
                return None

            timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
            backup_path = f"backups/state_{timestamp}"
            logger.info(f"creating_backup path={backup_path} files={len(state_files)}")

            with tempfile.TemporaryDirectory() as tmpdir:
                tmpdir_path = Path(tmpdir)
                ops = []
                for file_path in state_files:
                    local = hf_hub_download(
                        repo_id=self.repo_id,
                        repo_type="dataset",
                        filename=file_path,
                        revision=current_commit,
                    )
                    if local:
                        dst = tmpdir_path / Path(file_path).name
                        shutil.copy2(local, dst)
                        ops.append(CommitOperationAdd(
                            path_in_repo=f"{backup_path}/{Path(file_path).name}",
                            path_or_fileobj=str(dst),
                        ))

                if ops:
                    info = self.api.create_commit(
                        repo_id=self.repo_id,
                        repo_type="dataset",
                        operations=ops,
                        commit_message=f"Backup state before update - {timestamp}",
                        parent_commit=current_commit,
                    )
                    logger.info(f"backup_created commit={info.oid}")
                    return info.oid
        except Exception as e:
            logger.error(f"backup_failed error={e}")
        return None

    def save_state_atomic(
        self, state_data: Dict[str, Any], source_paths: List[str]
    ) -> Dict[str, Any]:
        operation_id = f"save_{int(time.time())}"
        logger.info(f"starting_atomic_save op={operation_id} sources={source_paths}")

        try:
            try:
                repo_info = self.api.repo_info(repo_id=self.repo_id, repo_type="dataset")
                current_commit = repo_info.sha
            except RepositoryNotFoundError:
                current_commit = None

            backup_commit = self.create_backup(current_commit)

            with tempfile.TemporaryDirectory() as tmpdir:
                state_dir = Path(tmpdir) / self.dataset_path
                state_dir.mkdir(parents=True, exist_ok=True)

                metadata = {
                    "timestamp": datetime.now().isoformat(),
                    "operation_id": operation_id,
                    "checksum": None,
                    "backup_commit": backup_commit,
                    "state_data": state_data,
                }
                metadata_path = state_dir / "metadata.json"
                with open(metadata_path, "w") as f:
                    json.dump(metadata, f, indent=2)

                ops = [CommitOperationAdd(
                    path_in_repo="state/metadata.json",
                    path_or_fileobj=str(metadata_path),
                )]

                for source_path in source_paths:
                    src = Path(source_path)
                    if src.exists():
                        dst = state_dir / src.name
                        shutil.copy2(src, dst)
                        checksum = self.calculate_checksum(dst)
                        ops.append(CommitOperationAdd(
                            path_in_repo=f"state/{src.name}",
                            path_or_fileobj=str(dst),
                        ))
                        logger.info(f"file_added source={source_path} sha256={checksum[:12]}")

                metadata["checksum"] = hashlib.sha256(
                    json.dumps(state_data, sort_keys=True).encode()
                ).hexdigest()
                with open(metadata_path, "w") as f:
                    json.dump(metadata, f, indent=2)

                info = self.api.create_commit(
                    repo_id=self.repo_id,
                    repo_type="dataset",
                    operations=ops,
                    commit_message=f"Atomic state update - {operation_id}",
                    parent_commit=current_commit,
                )

                result = {
                    "success": True,
                    "operation_id": operation_id,
                    "commit_id": info.oid,
                    "backup_commit": backup_commit,
                    "timestamp": datetime.now().isoformat(),
                    "files_count": len(source_paths),
                }
                logger.info(f"atomic_save_completed {result}")
                return result

        except Exception as e:
            logger.error(f"atomic_save_failed error={e}")
            raise


def main() -> None:
    if len(sys.argv) < 3:
        print(json.dumps({
            "error": "Usage: python save_to_dataset_atomic.py <repo_id> <source_path1> [source_path2...]",
            "status": "error",
        }, indent=2))
        sys.exit(1)

    repo_id = sys.argv[1]
    source_paths = sys.argv[2:]
    for p in source_paths:
        if not os.path.exists(p):
            print(json.dumps({"error": f"Source path does not exist: {p}", "status": "error"}, indent=2))
            sys.exit(1)

    state_data = {
        "environment": "production",
        "version": "1.0.0",
        "platform": "huggingface-spaces",
        "app": "hermesface",
        "timestamp": datetime.now().isoformat(),
    }

    try:
        saver = AtomicDatasetSaver(repo_id)
        result = saver.save_state_atomic(state_data, source_paths)
        print(json.dumps(result, indent=2))
    except Exception as e:
        print(json.dumps({"error": str(e), "status": "error"}, indent=2))
        sys.exit(1)


if __name__ == "__main__":
    main()