import http.server, ssl, subprocess, pathlib, base64, os
root=pathlib.Path(__file__).parent
remote=root/'remote'
def git(*args): return subprocess.check_output(['git',*args],cwd=remote).decode().strip()
remote.mkdir(exist_ok=True)
git('init','-q','--initial-branch=main')
(remote/'README.md').write_text('Synthetic private repository for askpass proof.\n')
git('add','.'); git('-c','user.name=Scenario','-c','user.email=scenario@example.invalid','commit','-qm','Remote initial commit'); git('update-server-info')
subprocess.run(['openssl','req','-x509','-newkey','rsa:2048','-nodes','-keyout',str(root/'key.pem'),'-out',str(root/'cert.pem'),'-days','1','-subj','/CN=127.0.0.1','-addext','subjectAltName=IP:127.0.0.1'],check=True,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
(root/'askpass.sh').write_text('#!/bin/sh\nprintf "invoked\\n" >> "$0.log"\nprintf "fixture\\n"\n'); (root/'askpass.sh').chmod(0o700)
class Handler(http.server.SimpleHTTPRequestHandler):
 def __init__(self,*a,**kw): super().__init__(*a,directory=str(remote/'.git'),**kw)
 def do_GET(self):
  if self.headers.get('Authorization')!='Basic '+base64.b64encode(b'fixture:fixture').decode():
   self.send_response(401); self.send_header('WWW-Authenticate','Basic realm="Synthetic private Git"'); self.end_headers(); return
  super().do_GET()
 def log_message(self,*a): pass
server=http.server.ThreadingHTTPServer(('127.0.0.1',0),Handler)
ctx=ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER); ctx.load_cert_chain(root/'cert.pem',root/'key.pem'); server.socket=ctx.wrap_socket(server.socket,server_side=True)
(root/'remote-url').write_text('https://127.0.0.1:'+str(server.server_port)+'/')
print('Synthetic authenticated HTTPS remote ready',flush=True)
server.serve_forever()
