File size: 3,263 Bytes
9a066b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env bash

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
LOCAL_TOKENS_FILE="${LOCAL_TOKENS_FILE:-$SCRIPT_DIR/local_tokens.sh}"

if [[ -f "$LOCAL_TOKENS_FILE" ]]; then
  # shellcheck disable=SC1090
  source "$LOCAL_TOKENS_FILE"
fi

REMOTE="${REMOTE:-hf}"
BRANCH="${BRANCH:-main}"
SKIP_VALIDATE=0


usage() {
  cat <<'EOF'
Uso:
  ./scripts/publish_hf.sh [opcoes]

O script:
1. troca para a branch main
2. valida backend e frontend
3. faz push para o Space do Hugging Face

Opcoes:
  --branch NAME            Branch a publicar (padrao: main)
  --remote NAME            Remote do Hugging Face (padrao: hf)
  --skip-validate          Pula compileall e build do frontend
  --help                   Mostra esta ajuda

Autenticacao:
  - Se existir scripts/local_tokens.sh, ele sera carregado automaticamente.
  - Se HF_TOKEN estiver definido, o push usa URL autenticada temporaria.
  - Caso contrario, usa o remote local ja configurado.
EOF
}


log() {
  printf '[publish-hf] %s\n' "$*"
}


fail() {
  printf '[publish-hf] erro: %s\n' "$*" >&2
  exit 1
}


require_cmd() {
  command -v "$1" >/dev/null 2>&1 || fail "comando obrigatorio ausente: $1"
}


ensure_clean_tracked_tree() {
  git diff --quiet || fail "ha alteracoes rastreadas nao commitadas; limpe a arvore antes do publish"
  git diff --cached --quiet || fail "ha alteracoes staged; finalize o commit antes do publish"
}


authenticated_hf_url() {
  local remote_url="$1"
  local token="${HF_TOKEN:-}"
  [[ -n "$token" ]] || {
    printf '%s\n' "$remote_url"
    return 0
  }

  case "$remote_url" in
    https://huggingface.co/*)
      ;;
    *)
      printf '%s\n' "$remote_url"
      return 0
      ;;
  esac

  local path="${remote_url#https://huggingface.co/}"
  local owner
  owner="$(python3 - "$path" <<'PY'
import sys

parts = [chunk for chunk in sys.argv[1].split("/") if chunk]
if len(parts) >= 2 and parts[0] in {"spaces", "datasets", "models"}:
    print(parts[1])
elif parts:
    print(parts[0])
PY
)"

  [[ -n "$owner" ]] || fail "nao consegui identificar o owner do remote do HF"
  printf 'https://%s:%s@huggingface.co/%s\n' "$owner" "$token" "$path"
}


while [[ $# -gt 0 ]]; do
  case "$1" in
    --branch)
      [[ $# -ge 2 ]] || fail "faltou valor para --branch"
      BRANCH="$2"
      shift 2
      ;;
    --remote)
      [[ $# -ge 2 ]] || fail "faltou valor para --remote"
      REMOTE="$2"
      shift 2
      ;;
    --skip-validate)
      SKIP_VALIDATE=1
      shift
      ;;
    --help|-h)
      usage
      exit 0
      ;;
    *)
      fail "opcao desconhecida: $1"
      ;;
  esac
done


require_cmd git
require_cmd python3

git show-ref --verify --quiet "refs/heads/$BRANCH" || fail "branch local inexistente: $BRANCH"
git remote get-url "$REMOTE" >/dev/null 2>&1 || fail "remote inexistente: $REMOTE"
ensure_clean_tracked_tree

git checkout "$BRANCH" >/dev/null

if (( SKIP_VALIDATE == 0 )); then
  log "validando backend"
  python3 -m compileall backend/app >/dev/null
  log "buildando frontend"
  (cd frontend && npm run build >/dev/null)
fi

remote_url="$(git remote get-url "$REMOTE")"
push_url="$(authenticated_hf_url "$remote_url")"

log "publicando $BRANCH no remote $REMOTE"
git push "$push_url" "$BRANCH:$BRANCH"
log "concluido"