91 lines
2.6 KiB
Python
91 lines
2.6 KiB
Python
import subprocess, time, os, threading
|
|
|
|
base = r"C:\Users\juanm\Documents\Github\INFRAESTRUCTURA\omniroute"
|
|
url_file = os.path.join(base, "oauth_url.txt")
|
|
code_file = os.path.join(base, "oauth_code.txt")
|
|
log_file = os.path.join(base, "oauth_helper.log")
|
|
exit_file = os.path.join(base, "oauth_helper.exit")
|
|
hb_file = os.path.join(base, "oauth_helper.hb")
|
|
|
|
for f in (url_file, code_file, log_file, exit_file, hb_file):
|
|
try:
|
|
os.remove(f)
|
|
except FileNotFoundError:
|
|
pass
|
|
|
|
proc = subprocess.Popen(
|
|
["omniroute.cmd", "oauth", "start", "--provider", "claude-code", "--no-browser"],
|
|
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
|
|
text=True, bufsize=1
|
|
)
|
|
|
|
log = open(log_file, "w", encoding="utf-8", buffering=1)
|
|
buf = ""
|
|
|
|
|
|
def reader():
|
|
global buf
|
|
while True:
|
|
ch = proc.stdout.read(1)
|
|
if ch == "":
|
|
if proc.poll() is not None:
|
|
break
|
|
time.sleep(0.05)
|
|
continue
|
|
log.write(ch)
|
|
buf += ch
|
|
if "https://claude.ai/oauth/authorize" in buf and not os.path.exists(url_file):
|
|
start = buf.find("https://claude.ai/oauth/authorize")
|
|
nl = buf.find("\n", start)
|
|
if nl != -1:
|
|
with open(url_file, "w", encoding="utf-8") as uf:
|
|
uf.write(buf[start:nl].strip())
|
|
if len(buf) > 200000:
|
|
buf = buf[-50000:]
|
|
|
|
|
|
threading.Thread(target=reader, daemon=True).start()
|
|
|
|
# Esperar la URL (max 2 min, es rapido)
|
|
wait_url = time.time() + 120
|
|
while time.time() < wait_url and not os.path.exists(url_file):
|
|
time.sleep(0.5)
|
|
|
|
# Sondear el codigo SIN limite de tiempo; hasta que el proceso termine
|
|
sent = False
|
|
while proc.poll() is None:
|
|
with open(hb_file, "w") as hb:
|
|
hb.write(str(time.time()))
|
|
if not sent and os.path.exists(code_file):
|
|
try:
|
|
with open(code_file, "r", encoding="utf-8") as cf:
|
|
code = cf.read().strip()
|
|
except Exception:
|
|
code = ""
|
|
if code:
|
|
try:
|
|
os.remove(code_file)
|
|
except FileNotFoundError:
|
|
pass
|
|
log.write("\n[helper] enviando codigo al proceso...\n")
|
|
try:
|
|
proc.stdin.write(code + "\n")
|
|
proc.stdin.flush()
|
|
except Exception as e:
|
|
log.write("[helper] fallo al escribir stdin: %r\n" % e)
|
|
sent = True
|
|
time.sleep(1)
|
|
|
|
try:
|
|
rest = proc.stdout.read()
|
|
if rest:
|
|
log.write(rest)
|
|
except Exception:
|
|
pass
|
|
|
|
log.write("\n=== PROCESO TERMINADO rc=%s ===\n" % proc.poll())
|
|
log.close()
|
|
|
|
with open(exit_file, "w") as ef:
|
|
ef.write("done")
|