From 4ee35ddfe6d67881c5c074caa760c9f9fd982d43 Mon Sep 17 00:00:00 2001
From: Mike McLean <mikem@redhat.com>
Date: Apr 20 2023 01:09:42 +0000
Subject: [PATCH 1/5] fix fakehub imports


---

diff --git a/devtools/fakehub b/devtools/fakehub
index 5d79200..814db24 100755
--- a/devtools/fakehub
+++ b/devtools/fakehub
@@ -12,9 +12,8 @@ import sys
 from urllib.parse import quote
 
 sys.path.insert(0, os.getcwd())
-sys.path.insert(1, os.path.join(os.getcwd(), 'kojihub'))
 import koji
-import kojixmlrpc
+from kojihub import kojixmlrpc
 import koji.xmlrpcplus
 
 

From dca12896a20ce8c5c640d70f4d6facb12db7f476 Mon Sep 17 00:00:00 2001
From: Mike McLean <mikem@redhat.com>
Date: Apr 20 2023 01:09:42 +0000
Subject: [PATCH 2/5] fakehub: drop into pdb on exception


---

diff --git a/devtools/fakehub b/devtools/fakehub
index 814db24..080a73d 100755
--- a/devtools/fakehub
+++ b/devtools/fakehub
@@ -17,6 +17,22 @@ from kojihub import kojixmlrpc
 import koji.xmlrpcplus
 
 
+# replace handle_rpc
+original_handle_rpc = kojixmlrpc.ModXMLRPCRequestHandler.handle_rpc
+
+def handle_rpc(self, environ):
+    try:
+        original_handle_rpc(self, environ)
+    except Exception:
+        import pdb
+        import traceback
+        etype, e, tb = sys.exc_info()
+        traceback.print_exc()
+        pdb.post_mortem(tb)
+
+kojixmlrpc.ModXMLRPCRequestHandler.handle_rpc = handle_rpc
+
+
 def get_url(environ):
     url = environ['wsgi.url_scheme']+'://'
 

From 63cd77f049d1169f8b82daee81a4bfffa8b7faee Mon Sep 17 00:00:00 2001
From: Mike McLean <mikem@redhat.com>
Date: Apr 20 2023 01:09:42 +0000
Subject: [PATCH 3/5] fakehub: avoid commits after debugging errors


---

diff --git a/devtools/fakehub b/devtools/fakehub
index 080a73d..0f9a435 100755
--- a/devtools/fakehub
+++ b/devtools/fakehub
@@ -29,6 +29,8 @@ def handle_rpc(self, environ):
         etype, e, tb = sys.exc_info()
         traceback.print_exc()
         pdb.post_mortem(tb)
+        # re-raise or the app handler will commit
+        raise
 
 kojixmlrpc.ModXMLRPCRequestHandler.handle_rpc = handle_rpc
 

From 5307b3d2c53e7aef3dcbfe3ea397c57f36c4e232 Mon Sep 17 00:00:00 2001
From: Mike McLean <mikem@redhat.com>
Date: Apr 20 2023 01:09:42 +0000
Subject: [PATCH 4/5] fakehub: fix typo


---

diff --git a/devtools/fakehub b/devtools/fakehub
index 0f9a435..a620c25 100755
--- a/devtools/fakehub
+++ b/devtools/fakehub
@@ -22,7 +22,7 @@ original_handle_rpc = kojixmlrpc.ModXMLRPCRequestHandler.handle_rpc
 
 def handle_rpc(self, environ):
     try:
-        original_handle_rpc(self, environ)
+        return original_handle_rpc(self, environ)
     except Exception:
         import pdb
         import traceback

From 128d02be797b3155b444f41f3d8f1edbc0d65b15 Mon Sep 17 00:00:00 2001
From: Mike McLean <mikem@redhat.com>
Date: Apr 20 2023 01:09:42 +0000
Subject: [PATCH 5/5] fakehub: --pdb option


---

diff --git a/devtools/fakehub b/devtools/fakehub
index a620c25..787bf43 100755
--- a/devtools/fakehub
+++ b/devtools/fakehub
@@ -4,6 +4,7 @@ from __future__ import absolute_import, print_function
 
 import ast
 import io
+import optparse
 import os
 import os.path
 import pprint
@@ -17,7 +18,7 @@ from kojihub import kojixmlrpc
 import koji.xmlrpcplus
 
 
-# replace handle_rpc
+# alternate handle_rpc, used when --pdb is given
 original_handle_rpc = kojixmlrpc.ModXMLRPCRequestHandler.handle_rpc
 
 def handle_rpc(self, environ):
@@ -32,8 +33,6 @@ def handle_rpc(self, environ):
         # re-raise or the app handler will commit
         raise
 
-kojixmlrpc.ModXMLRPCRequestHandler.handle_rpc = handle_rpc
-
 
 def get_url(environ):
     url = environ['wsgi.url_scheme']+'://'
@@ -64,20 +63,27 @@ def nice_literal(value):
         return value
 
 
-def get_request():
-    method = sys.argv[1]
-    args = []
+def get_options():
+    parser = optparse.OptionParser(usage='%prog [options] <policy_file>')
+    parser.add_option('--pdb', action='store_true',
+                      help='drop into pdb on error')
+    opts, args = parser.parse_args()
+
+    # parse request from args
+    method = args[0]
+    callargs = []
     kwargs = {}
-    for s in sys.argv[2:]:
+    for s in args[1:]:
         if '=' in s:
             k, v = s.split('=', 1)
             v = nice_literal(v)
             kwargs[k] = v
         else:
-            args.append(nice_literal(s))
-    args = koji.encode_args(*args, **kwargs)
-    request = koji.xmlrpcplus.dumps(args, method, allow_none=1)
-    return request
+            callargs.append(nice_literal(s))
+    callargs = koji.encode_args(*callargs, **kwargs)
+    opts.request = koji.xmlrpcplus.dumps(callargs, method, allow_none=1)
+
+    return opts
 
 
 def start_response(status, headers):
@@ -105,13 +111,17 @@ def set_config(environ):
 
 
 def main():
+    options = get_options()
+    if options.pdb:
+        kojixmlrpc.ModXMLRPCRequestHandler.handle_rpc = handle_rpc
+
     environ = {}
     environ['SCRIPT_FILENAME'] = kojixmlrpc.__file__
     environ['wsgi.url_scheme'] = 'https'
     environ['SERVER_NAME'] = 'myserver'
     environ['SERVER_PORT'] = '443'
     environ['REQUEST_URI'] = get_url(environ)
-    environ['wsgi.input'] = io.StringIO(get_request())
+    environ['wsgi.input'] = io.StringIO(options.request)
     environ['REQUEST_METHOD'] = 'POST'
     environ['CONTENT_TYPE'] = 'text/xml'
     set_config(environ)

