Spaces:
Sleeping
Sleeping
feat: bidirectional GitHub sync with background pull/push
Browse files
app.py
CHANGED
|
@@ -4,18 +4,65 @@ import subprocess
|
|
| 4 |
import shlex
|
| 5 |
import os
|
| 6 |
import sys
|
|
|
|
|
|
|
| 7 |
|
| 8 |
print(">>> app.py loading", file=sys.stderr)
|
| 9 |
sys.stderr.flush()
|
| 10 |
|
| 11 |
SAFE_COMMANDS = "*"
|
| 12 |
-
|
| 13 |
ALLOWLIST = {}
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
def
|
| 18 |
-
return "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
|
| 21 |
def is_allowed(args):
|
|
@@ -27,7 +74,7 @@ def is_allowed(args):
|
|
| 27 |
return base in SAFE_COMMANDS
|
| 28 |
|
| 29 |
|
| 30 |
-
def spawn(command: str):
|
| 31 |
if not command:
|
| 32 |
return ""
|
| 33 |
try:
|
|
@@ -38,17 +85,19 @@ def spawn(command: str):
|
|
| 38 |
if not is_allowed(args):
|
| 39 |
return f"Error: command '{args[0]}' not allowed in demo container\n"
|
| 40 |
|
| 41 |
-
|
| 42 |
-
if
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
| 45 |
try:
|
| 46 |
result = subprocess.run(
|
| 47 |
-
|
| 48 |
capture_output=True,
|
| 49 |
text=True,
|
| 50 |
timeout=10,
|
| 51 |
-
cwd=
|
| 52 |
)
|
| 53 |
return (result.stdout + result.stderr) or "(no output)"
|
| 54 |
except subprocess.TimeoutExpired:
|
|
@@ -57,6 +106,71 @@ def spawn(command: str):
|
|
| 57 |
return f"Error: {e}\n"
|
| 58 |
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
print(">>> building demo", file=sys.stderr)
|
| 61 |
sys.stderr.flush()
|
| 62 |
|
|
@@ -64,9 +178,8 @@ demo = gr.Interface(
|
|
| 64 |
fn=spawn,
|
| 65 |
inputs=gr.Textbox(label="command", lines=1),
|
| 66 |
outputs=gr.Textbox(label="output", lines=10),
|
| 67 |
-
api_name="
|
| 68 |
)
|
| 69 |
-
|
| 70 |
demo.queue()
|
| 71 |
|
| 72 |
if __name__ == "__main__":
|
|
|
|
| 4 |
import shlex
|
| 5 |
import os
|
| 6 |
import sys
|
| 7 |
+
import time
|
| 8 |
+
import threading
|
| 9 |
|
| 10 |
print(">>> app.py loading", file=sys.stderr)
|
| 11 |
sys.stderr.flush()
|
| 12 |
|
| 13 |
SAFE_COMMANDS = "*"
|
|
|
|
| 14 |
ALLOWLIST = {}
|
| 15 |
|
| 16 |
+
GITHUB_TOKEN = os.environ.get("GITHUB_TOKEN", "")
|
| 17 |
+
LINKED_REPO_OWNER = os.environ.get("LINKED_REPO_OWNER", "")
|
| 18 |
+
LINKED_REPO_NAME = os.environ.get("LINKED_REPO_NAME", "")
|
| 19 |
+
LINKED_REPO_URL = os.environ.get("LINKED_REPO_URL", "")
|
| 20 |
+
REPO_ROOT = "/home/user/repos"
|
| 21 |
+
SYNC_INTERVAL = 30
|
| 22 |
+
_repo_mtimes: dict[str, float] = {}
|
| 23 |
+
_repo_lock = threading.Lock()
|
| 24 |
|
| 25 |
+
|
| 26 |
+
def repo_path(owner: str, repo: str) -> str:
|
| 27 |
+
return os.path.join(REPO_ROOT, f"{owner}_{repo}")
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def set_git_config(repo_dir: str):
|
| 31 |
+
subprocess.run(["git", "config", "user.email", "codexa-agent@users.noreply.github.com"], cwd=repo_dir, capture_output=True)
|
| 32 |
+
subprocess.run(["git", "config", "user.name", "Codexa Agent"], cwd=repo_dir, capture_output=True)
|
| 33 |
+
if GITHUB_TOKEN:
|
| 34 |
+
subprocess.run(["git", "config", "credential.helper", ""], cwd=repo_dir, capture_output=True)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def init_repo(owner: str, repo: str, remote_url: str = "") -> str:
|
| 38 |
+
path = repo_path(owner, repo)
|
| 39 |
+
os.makedirs(path, exist_ok=True)
|
| 40 |
+
|
| 41 |
+
if remote_url:
|
| 42 |
+
pass
|
| 43 |
+
elif LINKED_REPO_URL:
|
| 44 |
+
remote_url = LINKED_REPO_URL
|
| 45 |
+
else:
|
| 46 |
+
remote_url = f"https://github.com/{owner}/{repo}.git"
|
| 47 |
+
|
| 48 |
+
if GITHUB_TOKEN and "github.com" in remote_url:
|
| 49 |
+
auth_url = remote_url.replace("https://", f"https://{GITHUB_TOKEN}@")
|
| 50 |
+
else:
|
| 51 |
+
auth_url = remote_url
|
| 52 |
+
|
| 53 |
+
if not os.path.exists(os.path.join(path, ".git")):
|
| 54 |
+
result = subprocess.run(
|
| 55 |
+
["git", "clone", auth_url, path],
|
| 56 |
+
capture_output=True, text=True, timeout=120, cwd=path
|
| 57 |
+
)
|
| 58 |
+
if result.returncode != 0:
|
| 59 |
+
subprocess.run(["git", "init", "-b", "main"], cwd=path, capture_output=True)
|
| 60 |
+
subprocess.run(["git", "remote", "add", "origin", auth_url], cwd=path, capture_output=True)
|
| 61 |
+
subprocess.run(["git", "add", "."], cwd=path, capture_output=True)
|
| 62 |
+
subprocess.run(["git", "commit", "-m", "init"], cwd=path, capture_output=True)
|
| 63 |
+
|
| 64 |
+
set_git_config(path)
|
| 65 |
+
return path
|
| 66 |
|
| 67 |
|
| 68 |
def is_allowed(args):
|
|
|
|
| 74 |
return base in SAFE_COMMANDS
|
| 75 |
|
| 76 |
|
| 77 |
+
def spawn(command: str, owner: str = "", repo: str = ""):
|
| 78 |
if not command:
|
| 79 |
return ""
|
| 80 |
try:
|
|
|
|
| 85 |
if not is_allowed(args):
|
| 86 |
return f"Error: command '{args[0]}' not allowed in demo container\n"
|
| 87 |
|
| 88 |
+
cwd = os.environ.get("SPACE_ROOT", "/home/user")
|
| 89 |
+
if owner and repo:
|
| 90 |
+
rpath = repo_path(owner, repo)
|
| 91 |
+
if not os.path.exists(rpath):
|
| 92 |
+
init_repo(owner, repo)
|
| 93 |
+
cwd = rpath
|
| 94 |
try:
|
| 95 |
result = subprocess.run(
|
| 96 |
+
['sh', '-lc', command],
|
| 97 |
capture_output=True,
|
| 98 |
text=True,
|
| 99 |
timeout=10,
|
| 100 |
+
cwd=cwd,
|
| 101 |
)
|
| 102 |
return (result.stdout + result.stderr) or "(no output)"
|
| 103 |
except subprocess.TimeoutExpired:
|
|
|
|
| 106 |
return f"Error: {e}\n"
|
| 107 |
|
| 108 |
|
| 109 |
+
def link_repo(owner: str, repo: str, remote_url: str = ""):
|
| 110 |
+
path = init_repo(owner, repo, remote_url)
|
| 111 |
+
with _repo_lock:
|
| 112 |
+
_repo_mtimes[path] = 0
|
| 113 |
+
return f"Linked {owner}/{repo} -> {path}"
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
def sync_status(owner: str, repo: str):
|
| 117 |
+
path = repo_path(owner, repo)
|
| 118 |
+
if not os.path.exists(path) or not os.path.exists(os.path.join(path, ".git")):
|
| 119 |
+
return "Not initialized"
|
| 120 |
+
result = subprocess.run(["git", "status", "--short", "--branch"], cwd=path, capture_output=True, text=True)
|
| 121 |
+
return result.stdout or "Clean, up to date"
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
def _recent_mtime(path: str) -> float:
|
| 125 |
+
latest = 0.0
|
| 126 |
+
for root, dirs, files in os.walk(path):
|
| 127 |
+
dirs[:] = [d for d in dirs if d != ".git"]
|
| 128 |
+
for f in files:
|
| 129 |
+
try:
|
| 130 |
+
m = os.path.getmtime(os.path.join(root, f))
|
| 131 |
+
if m > latest:
|
| 132 |
+
latest = m
|
| 133 |
+
except OSError:
|
| 134 |
+
pass
|
| 135 |
+
return latest
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
def background_sync():
|
| 139 |
+
while True:
|
| 140 |
+
time.sleep(SYNC_INTERVAL)
|
| 141 |
+
if not os.path.isdir(REPO_ROOT):
|
| 142 |
+
continue
|
| 143 |
+
for name in os.listdir(REPO_ROOT):
|
| 144 |
+
path = os.path.join(REPO_ROOT, name)
|
| 145 |
+
if not os.path.isdir(path) or not os.path.exists(os.path.join(path, ".git")):
|
| 146 |
+
continue
|
| 147 |
+
try:
|
| 148 |
+
subprocess.run(["git", "pull", "--rebase", "origin", "main"], cwd=path, capture_output=True, text=True, timeout=30)
|
| 149 |
+
mtime = _recent_mtime(path)
|
| 150 |
+
with _repo_lock:
|
| 151 |
+
prev = _repo_mtimes.get(path, 0)
|
| 152 |
+
_repo_mtimes[path] = mtime
|
| 153 |
+
if mtime > prev:
|
| 154 |
+
subprocess.run(["git", "add", "-A"], cwd=path, capture_output=True)
|
| 155 |
+
subprocess.run(["git", "commit", "-m", "codexa: auto-sync"], cwd=path, capture_output=True)
|
| 156 |
+
subprocess.run(["git", "push", "origin", "HEAD"], cwd=path, capture_output=True, text=True, timeout=30)
|
| 157 |
+
except Exception:
|
| 158 |
+
pass
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
@spaces.GPU
|
| 162 |
+
def _gpu_marker():
|
| 163 |
+
return "ok"
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
if LINKED_REPO_OWNER and LINKED_REPO_NAME:
|
| 167 |
+
init_repo(LINKED_REPO_OWNER, LINKED_REPO_NAME)
|
| 168 |
+
|
| 169 |
+
if GITHUB_TOKEN:
|
| 170 |
+
t = threading.Thread(target=background_sync, daemon=True)
|
| 171 |
+
t.start()
|
| 172 |
+
|
| 173 |
+
|
| 174 |
print(">>> building demo", file=sys.stderr)
|
| 175 |
sys.stderr.flush()
|
| 176 |
|
|
|
|
| 178 |
fn=spawn,
|
| 179 |
inputs=gr.Textbox(label="command", lines=1),
|
| 180 |
outputs=gr.Textbox(label="output", lines=10),
|
| 181 |
+
api_name="exec_ctx",
|
| 182 |
)
|
|
|
|
| 183 |
demo.queue()
|
| 184 |
|
| 185 |
if __name__ == "__main__":
|