160 lines
5.5 KiB
Python
160 lines
5.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Remove Cursor relay/copy artifacts from remote hosts and sync clean push code."""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import glob
|
|
import json
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import paramiko
|
|
|
|
ROOT = Path(__file__).resolve().parent.parent
|
|
LOCAL_PUSH = ROOT / "douyin_youtube_ffplay.py"
|
|
LOCAL_PROBE = ROOT / "scripts" / "hardware_probe.py"
|
|
|
|
|
|
def run(ssh: paramiko.SSHClient, cmd: str, timeout: int = 120) -> str:
|
|
_, stdout, stderr = ssh.exec_command(cmd, timeout=timeout, get_pty=True)
|
|
out = (stdout.read() + stderr.read()).decode("utf-8", "replace")
|
|
return out.strip()
|
|
|
|
|
|
def sftp_put(sftp: paramiko.SFTPClient, local: Path, remote: str) -> None:
|
|
sftp.put(str(local), remote)
|
|
print(f"[ok] uploaded {local.name} -> {remote}")
|
|
|
|
|
|
def patch_ini_files(base: str) -> int:
|
|
removed = 0
|
|
for path in sorted(glob.glob(f"{base}/config/youtube.youtube2__*.ini")):
|
|
lines_out: list[str] = []
|
|
for line in Path(path).read_text(encoding="utf-8-sig").splitlines():
|
|
key = line.split("=", 1)[0].strip().lower()
|
|
if key in {"video_mode", "bitrate"}:
|
|
removed += 1
|
|
continue
|
|
lines_out.append(line)
|
|
Path(path).write_text("\n".join(lines_out).rstrip() + "\n", encoding="utf-8")
|
|
return removed
|
|
|
|
|
|
def patch_hardware_profile(base: str) -> bool:
|
|
profile_path = Path(f"{base}/config/hardware-profile.json")
|
|
if not profile_path.is_file():
|
|
return False
|
|
data = json.loads(profile_path.read_text(encoding="utf-8"))
|
|
if isinstance(data, dict) and "relay_strategy" in data:
|
|
data.pop("relay_strategy", None)
|
|
profile_path.write_text(json.dumps(data, indent=2, ensure_ascii=False) + "\n", encoding="utf-8")
|
|
return True
|
|
return False
|
|
|
|
|
|
def cleanup_hardware_env(base: str) -> int:
|
|
env_path = Path(f"{base}/config/hardware.env")
|
|
if not env_path.is_file():
|
|
return 0
|
|
kept: list[str] = []
|
|
removed = 0
|
|
for line in env_path.read_text(encoding="utf-8-sig").splitlines():
|
|
key = line.split("=", 1)[0].strip()
|
|
if key.startswith("LIVE_RELAY_"):
|
|
removed += 1
|
|
continue
|
|
kept.append(line)
|
|
env_path.write_text("\n".join(kept).rstrip() + "\n", encoding="utf-8")
|
|
return removed
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("host")
|
|
parser.add_argument("--user", default="eric")
|
|
parser.add_argument("--password", default=os.environ.get("LIVE_DEPLOY_PASS", "4669805"))
|
|
parser.add_argument("--base", required=True, help="Remote repo base path")
|
|
parser.add_argument("--restart", action="store_true")
|
|
args = parser.parse_args()
|
|
|
|
if not LOCAL_PUSH.is_file():
|
|
print(f"missing {LOCAL_PUSH}", file=sys.stderr)
|
|
return 2
|
|
|
|
ssh = paramiko.SSHClient()
|
|
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
ssh.connect(args.host, username=args.user, password=args.password, timeout=20)
|
|
sftp = ssh.open_sftp()
|
|
|
|
remote_push = f"{args.base}/douyin_youtube_ffplay.py"
|
|
remote_probe = f"{args.base}/scripts/hardware_probe.py"
|
|
sftp_put(sftp, LOCAL_PUSH, remote_push)
|
|
if LOCAL_PROBE.is_file():
|
|
sftp_put(sftp, LOCAL_PROBE, remote_probe)
|
|
sftp.close()
|
|
|
|
# Run cleanup on remote via inline python (paths are on remote)
|
|
cleanup_py = f"""
|
|
import glob, json
|
|
from pathlib import Path
|
|
base = {args.base!r}
|
|
|
|
def patch_ini():
|
|
removed = 0
|
|
for path in sorted(glob.glob(f"{{base}}/config/youtube.youtube2__*.ini")):
|
|
lines_out = []
|
|
for line in Path(path).read_text(encoding='utf-8-sig').splitlines():
|
|
key = line.split('=', 1)[0].strip().lower()
|
|
if key in {{'video_mode', 'bitrate'}}:
|
|
removed += 1
|
|
continue
|
|
lines_out.append(line)
|
|
Path(path).write_text('\\n'.join(lines_out).rstrip() + '\\n', encoding='utf-8')
|
|
return removed
|
|
|
|
def patch_profile():
|
|
p = Path(f"{{base}}/config/hardware-profile.json")
|
|
if not p.is_file():
|
|
return False
|
|
data = json.loads(p.read_text(encoding='utf-8'))
|
|
if isinstance(data, dict) and 'relay_strategy' in data:
|
|
data.pop('relay_strategy', None)
|
|
p.write_text(json.dumps(data, indent=2, ensure_ascii=False) + '\\n', encoding='utf-8')
|
|
return True
|
|
return False
|
|
|
|
def patch_env():
|
|
p = Path(f"{{base}}/config/hardware.env")
|
|
if not p.is_file():
|
|
return 0
|
|
kept = []
|
|
removed = 0
|
|
for line in p.read_text(encoding='utf-8-sig').splitlines():
|
|
key = line.split('=', 1)[0].strip()
|
|
if key.startswith('LIVE_RELAY_'):
|
|
removed += 1
|
|
continue
|
|
kept.append(line)
|
|
p.write_text('\\n'.join(kept).rstrip() + '\\n', encoding='utf-8')
|
|
return removed
|
|
|
|
print('ini_removed', patch_ini())
|
|
print('relay_strategy_removed', patch_profile())
|
|
print('relay_env_removed', patch_env())
|
|
has_copy_fn = '_resolve_stream_copy_plan' in Path(f"{{base}}/douyin_youtube_ffplay.py").read_text(encoding='utf-8', errors='ignore')
|
|
print('has_stream_copy_fn', has_copy_fn)
|
|
"""
|
|
print(run(ssh, f"python3 -c {json.dumps(cleanup_py)}"))
|
|
|
|
if args.restart:
|
|
print(run(ssh, f"cd {args.base} && pm2 restart youtube2__ch_mpwri36r_z5lm2 youtube2__ch_mpyvqkc0_gj63k 2>/dev/null || true"))
|
|
print(run(ssh, f"grep -hE 'LIVE_VIDEO_ENCODER|YouTube Live profile|stream copy|libx264' {args.base}/.pm2/logs/youtube2__*-out.log 2>/dev/null | tail -8"))
|
|
|
|
ssh.close()
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|