#!/usr/bin/env python3 import os import subprocess import sys def run(cmd, check=True, capture=False): if isinstance(cmd, list): pass if capture: p = subprocess.run(cmd, capture_output=True, text=True) return p.stdout.strip() else: subprocess.run(cmd, check=check) def main(): token = os.environ.get("HF_TOKEN") if not token: print("HF_TOKEN not set. Set HF_TOKEN environment variable and retry.") sys.exit(1) repo_id = "JoaquinZ/DesgrabadorAI" repo_git = f"https://huggingface.co/spaces/{repo_id}.git" auth_url = f"https://{token}@huggingface.co/spaces/{repo_id}.git" # Get current origin URL try: orig = run(["git","remote","get-url","origin"], capture=True) except Exception: print("Failed to read current origin URL. Is this a git repo?") sys.exit(1) print("Original origin:", orig) try: print("Setting temporary auth remote URL (token in memory only)") run(["git","remote","set-url","origin", auth_url]) status = run(["git","status","--porcelain"], capture=True) if not status: print("No local changes to commit. Pushing existing branches...") else: print("Staging and committing local changes...") run(["git","add","-A"]) # commit may fail if no user.name/email set; allow failing gracefully try: run(["git","commit","-m","Update README frontmatter and setup files for HF Spaces"]) except Exception as e: print("Warning: git commit failed (maybe no changes or git config). Continuing.") print("Pushing to remote...") run(["git","push","origin","HEAD"], check=True) print("Push succeeded.") except subprocess.CalledProcessError as e: print("Git command failed:", e) sys.exit(1) finally: # restore original origin URL try: run(["git","remote","set-url","origin", orig]) print("Restored original origin URL.") except Exception: print("Warning: failed to restore original origin URL. Please check git remote manually.") if __name__ == '__main__': main()