From dd3e891e4510e1c6ecc10a47289ecbc8b6ee36c5 Mon Sep 17 00:00:00 2001 From: Mike McLean Date: Jun 27 2017 16:58:56 +0000 Subject: fakeweb tool for devel --- diff --git a/devtools/fakeweb b/devtools/fakeweb new file mode 100755 index 0000000..6caafa0 --- /dev/null +++ b/devtools/fakeweb @@ -0,0 +1,119 @@ +#!/usr/bin/python + +import ast +import logging +import os +import os.path +import pprint +import sys +import tempfile +import time +import xmlrpclib + +from urllib import quote +from wsgiref.util import setup_testing_defaults +from wsgiref.simple_server import make_server + +from koji.context import context + +CWD = os.getcwd() +sys.path.insert(0, CWD) +sys.path.insert(1, os.path.join(CWD, 'www/lib')) +sys.path.insert(1, os.path.join(CWD, 'www/kojiweb')) +import koji +import kojiweb +import wsgi_publisher + + +def get_url(environ): + url = environ['wsgi.url_scheme']+'://' + + if environ.get('HTTP_HOST'): + url += environ['HTTP_HOST'] + else: + url += environ['SERVER_NAME'] + + if environ['wsgi.url_scheme'] == 'https': + if environ['SERVER_PORT'] != '443': + url += ':' + environ['SERVER_PORT'] + else: + if environ['SERVER_PORT'] != '80': + url += ':' + environ['SERVER_PORT'] + + url += quote(environ.get('SCRIPT_NAME', '')) + url += quote(environ.get('PATH_INFO', '')) + if environ.get('QUERY_STRING'): + url += '?' + environ['QUERY_STRING'] + return url + +FIRST = True + + +def do_static(environ, start_response): + redirect = os.environ.get('STATIC_URL', '') + if redirect: + environ['STATIC_URL'] = redirect + return redirect_static(environ, start_response) + # otherwise serve our local static files + path = environ.get('PATH_INFO', '') + assert path.startswith('/koji-static') + path = path[12:] + path = path.lstrip('/') + fn = os.path.join(CWD, 'www/static', path) + if not os.path.exists(fn): + print "CWD: %s" % os.getcwd() + print "No such file: %s" % fn + return do_404() + size = os.path.getsize(fn) + headers = [ + ('Content-Length', str(size)), + ('Content-Type', "text/plain"), #XXX + ] + start_response('200 OK', headers) + return iter_file(fn) + + +def iter_file(fn): + with open(fn, 'rb') as fo: + while True: + chunk = fo.read(8192) + if not chunk: + break + yield chunk + + +def redirect_static(environ, start_response): + response = '' + headers = [ + ('Content-Length', str(len(response))), + ('Content-Type', "text/plain"), + ('Location', environ['STATIC_URL'] + environ['PATH_INFO']), + ] + start_response('302 Found', headers) + return [response] + + +def application(environ, start_response): + global FIRST + # provide some needed info + environ['SCRIPT_FILENAME'] = wsgi_publisher.__file__ + environ['REQUEST_URI'] = get_url(environ) + environ['koji.web.ConfigFile'] = "%s/devtools/fakeweb.conf" % os.getcwd() + environ['koji.web.ConfigDir'] = "%s/devtools/fakeweb.conf.d" % os.getcwd() + if FIRST: + pprint.pprint(environ) + FIRST = False + path = environ.get('PATH_INFO', '') + if path.startswith('/koji-static'): + return do_static(environ, start_response) + return wsgi_publisher.application(environ, start_response) + + +def main(): + #koji.add_file_logger('koji', 'fakeweb.log') + httpd = make_server('', 8000, application) + print "Serving on port 8000..." + httpd.serve_forever() + +if __name__ == '__main__': + main()