import http.server,socketserver,json,os,re,pathlib,sys
root=pathlib.Path(sys.argv[1])
class Handler(http.server.BaseHTTPRequestHandler):
 def log_message(self,*a): pass
 def do_GET(self): self.handle_request()
 def do_POST(self): self.handle_request()
 def handle_request(self):
  raw=self.rfile.read(int(self.headers.get('Content-Length',0))); data=json.loads(raw or '{}'); path=self.path
  with open(root/'fixture-requests.jsonl','a') as f: f.write(json.dumps({'method':self.command,'path':path,'data':data})+'\n')
  status=200
  repo={'id':1,'name':'attribution-demo','full_name':'scenario/attribution-demo','owner':{'login':'scenario'},'default_branch':'main','fork':False,'html_url':'https://github.com/scenario/attribution-demo','clone_url':'https://github.com/scenario/attribution-demo.git','permissions':{'push':True,'admin':True}}
  if path=='/graphql':
   query=data.get('query',''); aliases=re.findall(r'(\w+):repository\(',query)
   answer={'data':{k:{'pullRequests':{'nodes':[]}} for k in aliases}} if aliases else {'data':{'repository':{'pullRequests':{'nodes':[]}}}}
  elif path.endswith('/pulls') and self.command=='POST':
   (root/'generated-pr.json').write_text(json.dumps(data,indent=2)); (root/'generated-description.md').write_text(data['body'])
   status=422;answer={'message':'Proof captured locally; publication deliberately disabled.'}
  elif '/pulls' in path: answer=[]
  elif '/repos/' in path: answer=repo
  elif path=='/user':answer={'login':'scenario','id':1}
  else: answer={}
  body=json.dumps(answer).encode();self.send_response(status);self.send_header('Content-Type','application/json');self.send_header('Content-Length',str(len(body)));self.send_header('x-ratelimit-limit','5000');self.send_header('x-ratelimit-remaining','4999');self.end_headers();self.wfile.write(body)
class Server(socketserver.UnixStreamServer): pass
Server(str(root/'github.sock'),Handler).serve_forever()
