git-instaweb: Add Python builtin http.server support
PATCH 1/1 git-instaweb: Add Python builtin http.server support
With this patch it is possible to launch git-instaweb by using Python 3 http.server CGI handler via -d python option.
git-instaweb generates a small wrapper around the http.server (in GIT_DIR/gitweb/) that address a limitation of the CGI handler where CGI scripts have to be in a cgi-bin subdirectory and directory index can't be easily changed. To keep the implementation small, gitweb is running on url /cgi-bin/gitweb.cgi and an automatic redirection is done when opening /.
Python 3 is by default installed on most modern Linux distributions which enables running git instaweb -d python without needing anything else.
---
Notes:
Base Ref: master
Web-Diff: https://github.com/artizirk/git/commit/a40d28ca58
Checkout: git fetch https://github.com/artizirk/git instaweb-python-v1 && git checkout a40d28ca58Documentation/git-instaweb.txt | 3 +- git-instaweb.sh | 114 ++++++++++++++++++++++++++++++++- 2 files changed, 115 insertions(+), 2 deletions(-)
Documentation/git-instaweb.txt +2 -1
@@ -29,7 +29,8 @@ OPTIONS The HTTP daemon command-line that will be executed. Command-line options may be specified here, and the configuration file will be added at the end of the command-line. - Currently apache2, lighttpd, mongoose, plackup and webrick are supported. + Currently apache2, lighttpd, mongoose, plackup, python and + webrick are supported. (Default: lighttpd) -m::
git-instaweb.sh +113 -1
138 lines not on this page · raw message
base-commit: 16a465bc018d09e9d7bbbdc5f40a7fb99c21f8ef
Signature · 1 line
2.20.1
Quoted 5 lines · Arti Zirk
With this patch it is possible to launch git-instaweb by using Python 3 http.server CGI handler via
-d pythonoption.git-instaweb generates a small wrapper around the http.server (in GIT_DIR/gitweb/) ...
I know this follows an existing pattern (psgi and webrick also writes nontrivial amount of code inside GIT_DIR/gitweb), but can we somehow clean it up so that we can do instaweb out of a repository without writing so much into it first, before adding yet another one to make the situation even worse, I wonder?
For now I'll queue this so that we won't lose sight of the topic, but I am not exactly enthused.
Quoted 10 lines · Arti Zirk
With this patch it is possible to launch git-instaweb by using Python 3 http.server CGI handler via
-d pythonoption.git-instaweb generates a small wrapper around the http.server (in GIT_DIR/gitweb/) that address a limitation of the CGI handler where CGI scripts have to be in a cgi-bin subdirectory and directory index can't be easily changed. To keep the implementation small, gitweb is running on url
/cgi-bin/gitweb.cgiand an automatic redirection is done when opening/.
Quoted 3 lines
Python 3 is by default installed on most modern Linux distributions which enables running
git instaweb -d pythonwithout needing anything else.
I'm glad we're using Python 3 here, but I wonder if the name "python" will lead people to think it will work with Python 2 as well. There are people using up-to-date Git on systems like CentOS 6 and 7, where Python 3 is not generally available.
Could we call this "python3" instead, or provide some other way to communicate this to the user?
Signature · 2 lines
brian m. carlson: Houston, Texas, US OpenPGP: https://keybase.io/bk2204
Quoted 7 lines · brian m. carlson
I'm glad we're using Python 3 here, but I wonder if the name "python" will lead people to think it will work with Python 2 as well. There are people using up-to-date Git on systems like CentOS 6 and 7, where Python 3 is not generally available.
Could we call this "python3" instead, or provide some other way to communicate this to the user?
Sounds like a good idea. Also would this be _the_ sole http server implementation Python3 users would choose, or is it just a possible one? What I am trying to get at is that we might need to be even more specific than just "python 3", but may need to convey that this is for "http.server using python 3". I dunno.
Quoted 9 lines
Quoted 3 lines
Could we call this "python3" instead, or provide some other way to communicate this to the user?
Sounds like a good idea. Also would this be _the_ sole http server implementation Python3 users would choose, or is it just a possible one? What I am trying to get at is that we might need to be even more specific than just "python 3", but may need to convey that this is for "http.server using python 3". I dunno.
This is the built in http server that Python comes with (like Ruby users have builtin webrick server). While it is possible to install something else, I don't think many casual git-instaweb users would do it.
I haven't looked in depth into it but I'm pretty sure that by simply changing the imports I could make this code also work in python2.
Upstream python2 support ends in ~11 months and would Red Hat/CentOS 7 users using new git releases really care about "git instaweb -d python" not working on installations without Python 3?
In the end I would like to keep the name just "python" to signal that it only needs standard Python installation and nothing else.
Quoted 11 lines · Arti Zirk
This is the built in http server that Python comes with (like Ruby users have builtin webrick server). While it is possible to install something else, I don't think many casual git-instaweb users would do it.
I haven't looked in depth into it but I'm pretty sure that by simply changing the imports I could make this code also work in python2.
Upstream python2 support ends in ~11 months and would Red Hat/CentOS 7 users using new git releases really care about "git instaweb -d python" not working on installations without Python 3?
I'm sensitive to the fact that upstream is dropping support for Python 2, and I have no objections to limiting this to Python 3. However, whether we like it or not, Red Hat/CentOS 7 is going to be around for four years after that.
Quoted 2 lines
In the end I would like to keep the name just "python" to signal that it only needs standard Python installation and nothing else.
Could you update the documentation to put "Python 3" in parentheses or otherwise make it clear in the documentation? My goal is to avoid confusing users who are on systems that still have Python 2.
Signature · 2 lines
brian m. carlson: Houston, Texas, US OpenPGP: https://keybase.io/bk2204
Quoted 18 lines · brian m. carlson
Quoted 11 lines · Arti Zirk
This is the built in http server that Python comes with (like Ruby users have builtin webrick server). While it is possible to install something else, I don't think many casual git-instaweb users would do it.
I haven't looked in depth into it but I'm pretty sure that by simply changing the imports I could make this code also work in python2.
Upstream python2 support ends in ~11 months and would Red Hat/CentOS 7 users using new git releases really care about "git instaweb -d python" not working on installations without Python 3?
I'm sensitive to the fact that upstream is dropping support for Python 2, and I have no objections to limiting this to Python 3. However, whether we like it or not, Red Hat/CentOS 7 is going to be around for four years after that.
Quoted 6 lines
Quoted 2 lines
In the end I would like to keep the name just "python" to signal that it only needs standard Python installation and nothing else.
Could you update the documentation to put "Python 3" in parentheses or otherwise make it clear in the documentation? My goal is to avoid confusing users who are on systems that still have Python 2.
Calling the option python3 (not python) would give readers the same message loud and clear in a simpler way, I would think. Is there any reasonable reason why we'd want to avoid saying python3? It's not like we are afraid of sending an message that we won't stay working with python4 that is in the near horizon ;-), and as Arti says, those who already live in Python3 world would know, when they see either "python" or "python3", that is what they have anyway no?
PATCH v2 1/1 git-instaweb: Add Python builtin http.server support
In reply to Arti ZirkWith this patch it is possible to launch git-instaweb by using Python http.server CGI handler via -d python option.
git-instaweb generates a small wrapper around the http.server (in GIT_DIR/gitweb/) that address a limitation of the CGI handler where CGI scripts have to be in a cgi-bin subdirectory and directory index can't be easily changed. To keep the implementation small, gitweb is running on url /cgi-bin/gitweb.cgi and an automatic redirection is done when opening /.
The generated wrapper is compatible with both Python 2 and 3.
Python is by default installed on most modern Linux distributions which enables running git instaweb -d python without needing anything else.
---
Changes v1..v2:
- Add compatibily for Python 2, tested with 2.7, 3.4, 3.5, 3.6, 3.7
Notes:
Base Ref: master
Web-Diff: https://github.com/artizirk/git/commit/fe30635765
Checkout: git fetch https://github.com/artizirk/git instaweb-python-v2 && git checkout fe30635765
### Interdiff (v1..v2):
diff --git a/git-instaweb.sh b/git-instaweb.sh
index c8f9f03447..7c55229773 100755
--- a/git-instaweb.sh
+++ b/git-instaweb.sh
@@ -614,10 +614,11 @@ python_conf() {
ln -sf "$root/gitweb.cgi" "$fqgitdir/gitweb/$httpd_only/cgi-bin/gitweb.cgi"
ln -sf "$root/static" "$fqgitdir/gitweb/$httpd_only/"
- # generate a standalone 'python3 http.server' script in $fqgitdir/gitweb
- # This asumes that python3 is in user's $PATH
+ # generate a standalone 'python http.server' script in $fqgitdir/gitweb
+ # This asumes that python is in user's $PATH
+ # This script is Python 2 and 3 compatible
cat > "$fqgitdir/gitweb/gitweb.py" <<EOF
-#!/usr/bin/env python3
+#!/usr/bin/env python
import os
import sys
@@ -639,7 +640,14 @@ os.dup2(errorlogfile.fileno(), _orig_stderr_fd)
sys.stderr = errorlogfile
from functools import partial
-from http.server import CGIHTTPRequestHandler, test
+
+if sys.version_info < (3, 0): # Python 2
+ from CGIHTTPServer import CGIHTTPRequestHandler
+ from BaseHTTPServer import HTTPServer as ServerClass
+else: # Python 3
+ from http.server import CGIHTTPRequestHandler
+ from http.server import HTTPServer as ServerClass
+
# Those environment variables will be passed to the cgi script
os.environ.update({
@@ -660,7 +668,7 @@ class GitWebRequestHandler(CGIHTTPRequestHandler):
def do_HEAD(self):
self.redirect_path()
- super().do_HEAD()
+ CGIHTTPRequestHandler.do_HEAD(self)
def do_GET(self):
if self.path == "/":
@@ -669,11 +677,11 @@ class GitWebRequestHandler(CGIHTTPRequestHandler):
self.end_headers()
return
self.redirect_path()
- super().do_GET()
+ CGIHTTPRequestHandler.do_GET(self)
def do_POST(self):
self.redirect_path()
- super().do_POST()
+ CGIHTTPRequestHandler.do_POST(self)
# rewrite path of every request that is not gitweb.cgi to out of cgi-bin
def redirect_path(self):
@@ -685,7 +693,7 @@ class GitWebRequestHandler(CGIHTTPRequestHandler):
def is_cgi(self):
result = False
if self.path.startswith('/cgi-bin/gitweb.cgi'):
- result = super().is_cgi()
+ result = CGIHTTPRequestHandler.is_cgi(self)
return result
@@ -698,7 +706,12 @@ if "$local" == "true":
# as this was added to SimpleHTTPRequestHandler in Python 3.7
os.chdir("$fqgitdir/gitweb/$httpd_only/")
-test(HandlerClass=GitWebRequestHandler, port=$port, bind=bind)
+GitWebRequestHandler.protocol_version = "HTTP/1.0"
+httpd = ServerClass((bind, $port), GitWebRequestHandler)
+
+sa = httpd.socket.getsockname()
+print("Serving HTTP on", sa[0], "port", sa[1], "...")
+httpd.serve_forever()
EOF
chmod a+x "$fqgitdir/gitweb/gitweb.py"
### PatchesDocumentation/git-instaweb.txt | 3 +- git-instaweb.sh | 127 ++++++++++++++++++++++++++++++++- 2 files changed, 128 insertions(+), 2 deletions(-)
Documentation/git-instaweb.txt +2 -1
@@ -29,7 +29,8 @@ OPTIONS The HTTP daemon command-line that will be executed. Command-line options may be specified here, and the configuration file will be added at the end of the command-line. - Currently apache2, lighttpd, mongoose, plackup and webrick are supported. + Currently apache2, lighttpd, mongoose, plackup, python and + webrick are supported. (Default: lighttpd) -m::
git-instaweb.sh +126 -1
151 lines not on this page · raw message
base-commit: 16a465bc018d09e9d7bbbdc5f40a7fb99c21f8ef --
2.20.1
git-instaweb.sh EOF · line 621
Quoted 5 lines · Arti Zirk
git-instaweb.sh +5
-0+ # generate a standalone 'python http.server' script in $fqgitdir/gitweb + # This asumes that python is in user's $PATH + # This script is Python 2 and 3 compatible + cat > "$fqgitdir/gitweb/gitweb.py" <<EOF +#!/usr/bin/env python
I will point out, that despite what the PEPs say, on Debian and derivatives, "python" will always invoke Python 2, and never Python 3.
This is probably fine for now, but we'll need to reconsider it before 2020.
Signature · 2 lines
brian m. carlson: Houston, Texas, US OpenPGP: https://keybase.io/bk2204
Quoted 6 lines
I will point out, that despite what the PEPs say, on Debian and derivatives, "python" will always invoke Python 2, and never Python 3.
This is probably fine for now, but we'll need to reconsider it before 2020.
In this case it shouldn't be a problem because I have made sure that it works in both cases.
Quoted 1 line · Arti Zirk
The generated wrapper is compatible with both Python 2 and 3.
;-)
Yup, that obviously is the best way to settle "should we make it clear that this would not work with Python 2?" question.