Created
December 13, 2010 02:34
-
-
Save harryxu/738588 to your computer and use it in GitHub Desktop.
gitweb in django
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, sys, subprocess, httplib | |
from cStringIO import StringIO | |
from django.http import HttpResponse | |
def gitweb(request): | |
cigenv = { | |
'REQUEST_METHOD': request.method, | |
'QUERY_STRING': request.META['QUERY_STRING'], | |
} | |
os.environ.update(cigenv) | |
p = subprocess.Popen(['/usr/share/gitweb/index.cgi'], | |
stdin = subprocess.PIPE, | |
stdout = subprocess.PIPE, | |
stderr = subprocess.PIPE | |
) | |
stdout, stderr = p.communicate() | |
res = httplib.HTTPResponse(FakeSock(), method='GET') | |
res.fp = StringIO(stdout) | |
res.begin() | |
if not res.getheaders(): | |
msg = httplib.HTTPMessage(StringIO(stdout)) | |
else: | |
meg = res.msg | |
#remove headers | |
while True: | |
line = res.fp.readline() | |
if line == '\r\n': break | |
response = HttpResponse(res.read(), mimetype=msg.getheader('content-type')) | |
for item in msg.items(): | |
response[item[0]] = item[1] | |
return response | |
class FakeSock(object): | |
def makefile(self, mode, bufsize): | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment