File size: 4,680 Bytes
08c964e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env bash
# ============================================================
# OpenClaw HF Space 强制推送脚本
#
# 用法:
#   ./push-to-space.sh <SPACE_REPO_ID> [HF_TOKEN]
#
# 示例:
#   ./push-to-space.sh GGSheng/page hf_xxxxx
#
#   方式1:直接提供参数
#   ./scripts/push-to-space.sh GGSheng/page hf_xxxxx
#
#   方式2:设置环境变量
#   export SPACE_REPO_ID="GGSheng/page"
#   export HF_TOKEN="hf_xxxxx"
#   ./scripts/push-to-space.sh
#
#   方式3:使用缓存的 token(默认从 ~/.cache/huggingface/token 读取)
#   ./scripts/push-to-space.sh GGSheng/page
#
# 环境变量 (可选):
#   SPACE_REPO_ID  - Space 仓库 ID (如 GGSheng/page)
#   HF_TOKEN       - Hugging Face API Token
#   HF_TOKEN_FILE  - Token 文件路径 (默认 ~/.cache/huggingface/token)
#
# 说明:
#   此脚本用于将本地代码强制推送到已存在的 Hugging Face Space
#   - 会覆盖 Space 中的所有文件
#   - 排除 .git, logs, scripts, docs, tests 等非必要文件
#   - 推送完成后会自动请求 Space 重启
#
# 注意事项:
#   1. 确保 HF_TOKEN 有 write 权限
#   2. Space 必须使用 Docker SDK
#   3. 推送后 Space 会自动重启使更改生效
# ============================================================

update_readme_title() {
  local new_title="$1"
  local readme_file="$REPO_ROOT/README.md"
  local temp_file

  if [[ ! -f "$readme_file" ]]; then
    return 0
  fi

  temp_file="$(mktemp)"
  if sed "s/^title:.*/title: $new_title/" "$readme_file" > "$temp_file"; then
    mv "$temp_file" "$readme_file"
  else
    rm -f "$temp_file"
  fi
}

restore_readme_title() {
  local original_title="$1"
  local readme_file="$REPO_ROOT/README.md"
  local temp_file

  if [[ ! -f "$readme_file" ]]; then
    return 0
  fi

  temp_file="$(mktemp)"
  if sed "s/^title:.*/title: $original_title/" "$readme_file" > "$temp_file"; then
    mv "$temp_file" "$readme_file"
  else
    rm -f "$temp_file"
  fi
}

SPACE_REPO_ID="${1:-${SPACE_REPO_ID:-}}"
HF_TOKEN="${2:-${HF_TOKEN:-}}"

if [[ -z "$SPACE_REPO_ID" ]]; then
  echo "Usage: $0 <SPACE_REPO_ID> [HF_TOKEN]"
  echo ""
  echo "Examples:"
  echo "  $0 GGSheng/page hf_xxxxx"
  echo "  SPACE_REPO_ID=GGSheng/page HF_TOKEN=hf_xxxxx $0"
  echo "  $0 GGSheng/page"
  echo ""
  echo "Environment variables:"
  echo "  SPACE_REPO_ID   - Space 仓库 ID (如 GGSheng/page)"
  echo "  HF_TOKEN        - Hugging Face API Token"
  echo "  HF_TOKEN_FILE   - Token 文件路径 (默认 ~/.cache/huggingface/token)"
  exit 1
fi

if [[ -z "$HF_TOKEN" ]]; then
  HF_TOKEN_FILE="${HF_TOKEN_FILE:-$HOME/.cache/huggingface/token}"
  if [[ -f "$HF_TOKEN_FILE" ]]; then
    HF_TOKEN="$(cat "$HF_TOKEN_FILE")"
  fi
fi

if [[ -z "$HF_TOKEN" ]]; then
  echo "Error: HF_TOKEN is required. Provide as 2nd arg, set HF_TOKEN env var, or ensure ~/.cache/huggingface/token exists."
  exit 1
fi

SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd -- "$SCRIPT_DIR/.." && pwd)"

SPACE_NAME="${SPACE_REPO_ID##*/}"

echo "============================================"
echo "OpenClaw HF Space Push Script"
echo "============================================"
echo "Space: $SPACE_REPO_ID"
echo "Repo:  $REPO_ROOT"
echo ""

cd "$REPO_ROOT"

echo "[1/5] Logging into Hugging Face..."
echo "$HF_TOKEN" | hf auth login --token "$HF_TOKEN"

echo ""
echo "[2/5] Saving original README title..."
original_readme_title="$(sed -n 's/^title: *//p' "$REPO_ROOT/README.md" | head -n 1)"
update_readme_title "$SPACE_NAME"

restore_readme_on_exit() {
  restore_readme_title "${original_readme_title:-HF Space}"
}
trap restore_readme_on_exit EXIT

echo ""
echo "[3/5] Ensuring Space exists..."
hf repos create "$SPACE_REPO_ID" --repo-type space --space-sdk docker --exist-ok

echo ""
echo "[4/5] Uploading files to Space..."
hf upload "$SPACE_REPO_ID" . --repo-type space \
  --exclude '.git/**' \
  --exclude '.git' \
  --exclude '.github/**' \
  --exclude 'logs/**' \
  --exclude '*.log' \
  --exclude 'scripts/*.sh' \
  --exclude 'docs/**' \
  --exclude 'tests/**' \
  --exclude 'LICENSE' \
  --exclude '.dockerignore' \
  --exclude '.python-version' \
  --commit-message "fix: 强制推送更新 backup.py 修复逻辑"

echo ""
echo "[5/5] Requesting Space restart..."
SPACE_RESTART_REPO_ID="$SPACE_REPO_ID" python3 -c "
import os
from huggingface_hub import HfApi
api = HfApi(token=os.environ.get('HF_TOKEN'))
api.restart_space(repo_id=os.environ.get('SPACE_RESTART_REPO_ID'))
print('Space restart requested.')
"

echo ""
echo "============================================"
echo "Done! Space updated: https://huggingface.co/spaces/$SPACE_REPO_ID"
echo "============================================"