File size: 2,981 Bytes
25a3971
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
🦞 龙虾自动同步脚本
将本地代码和记忆同步到 Hugging Face Space

使用方式:
    HF_TOKEN=your_token python3 auto-sync.py
"""

import os
import sys
import json
from datetime import datetime
from pathlib import Path

# 从环境变量读取配置
HF_TOKEN = os.environ.get('HF_TOKEN')
HF_REPO_ID = os.environ.get('HF_REPO_ID', 'qf46578/omnics')
LOCAL_PATH = '/root/.openclaw/workspace/omnics'
MEMORY_PATH = '/root/.openclaw/workspace/omnics/memory'

# 忽略模式
IGNORE_PATTERNS = [
    'node_modules/',
    '.git/',
    'dist/',
    'build/',
    'coverage/',
    '.env',
    '*.log',
    '__pycache__/',
    '.vscode/',
    '.idea/',
    '.DS_Store',
    '*.swp',
    '*.swo',
    '*~',
    '.env.local',
    '.env.production',
    'memory/compressed/',
    'memory/session-state.json',
]


def get_file_count():
    """获取待上传文件数量"""
    count = 0
    for root, dirs, files in os.walk(LOCAL_PATH):
        dirs[:] = [d for d in dirs if d not in ['node_modules', '.git', 'dist', 'build', '__pycache__']]
        count += len(files)
    return count


def sync_to_hf():
    """同步到 Hugging Face"""
    if not HF_TOKEN:
        print('❌ 错误: 请设置 HF_TOKEN 环境变量')
        print('   使用方式: HF_TOKEN=your_token python3 auto-sync.py')
        return False
    
    print(f'🦞 龙虾开始同步...')
    print(f'📁 本地路径: {LOCAL_PATH}')
    print(f'🎯 目标仓库: {HF_REPO_ID}')
    
    try:
        from huggingface_hub import HfApi
        
        api = HfApi(token=HF_TOKEN)
        sync_time = datetime.utcnow().isoformat()
        
        result = api.upload_folder(
            folder_path=LOCAL_PATH,
            repo_id=HF_REPO_ID,
            repo_type='space',
            commit_message=f'🦞 龙虾自动同步 - {sync_time}',
            ignore_patterns=IGNORE_PATTERNS,
        )
        
        print(f'✅ 同步成功!')
        print(f'🔗 查看项目: https://huggingface.co/spaces/{HF_REPO_ID}')
        
        # 更新同步状态
        sync_state = {
            'last_sync': sync_time,
            'status': 'success',
            'files_synced': get_file_count(),
        }
        
        os.makedirs(MEMORY_PATH, exist_ok=True)
        with open(os.path.join(MEMORY_PATH, 'hf-sync-state.json'), 'w') as f:
            json.dump(sync_state, f, indent=2)
        
        return True
        
    except Exception as e:
        print(f'❌ 同步失败: {e}')
        return False


def get_sync_status():
    """获取同步状态"""
    state_file = os.path.join(MEMORY_PATH, 'hf-sync-state.json')
    if os.path.exists(state_file):
        with open(state_file, 'r') as f:
            return json.load(f)
    return {'status': 'never_synced'}


if __name__ == '__main__':
    if len(sys.argv) > 1 and sys.argv[1] == 'status':
        status = get_sync_status()
        print(json.dumps(status, indent=2))
    else:
        sync_to_hf()