From bb250f9978798fdd9b64239dda981c290080ca88 Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Mon, 28 Apr 2008 12:12:32 +0200 Subject: [PATCH] (hopefully) fixed weird behaviour of setuptools' easy_install. added option to specify an own version of gitweb.cgi added option to listen only locally (not on hostname) made resource handling (media files) more robust using pkg_resources --- README.txt | 9 +++- setup.py | 32 ++++++++++--- src/gitserve/__init__.py | 56 ++++++++++++++-------- src/gitserve/{gitweb.cgi.so => gitweb.cgi} | 0 4 files changed, 68 insertions(+), 29 deletions(-) rename src/gitserve/{gitweb.cgi.so => gitweb.cgi} (100%) diff --git a/README.txt b/README.txt index 0f3df8f..f2b0f6a 100644 --- a/README.txt +++ b/README.txt @@ -31,10 +31,12 @@ Usage pretty easy:: -q, --quiet don't print anything to stdout -p PORT, --port=PORT port to listen on (default: 8000) -a ADDRESS, --address=ADDRESS - address to listen on (default: all interfaces) + address to listen on (default: hostname) + -l, --local only listen on 127.0.0.1 -b, --browser open default browser automatically -d, --daemon detach from terminal and become a daemon --pid-file=PIDFILE write the spawned process-id to this file + --gitweb=GITWEB use this gitweb cgi file instead of the included As the only argument you can specify a directory that contains your git projects. If you leave this argument blank ``git-serve`` will automatically uses @@ -52,7 +54,8 @@ port 8000, for example: http://127.0.0.1:8000/ If you provide a ``--port`` or ``--address`` option while starting ``git-serve`` you can have ``git-serve`` listen on your choices. You need to be root to run it -on port 80 or any other port below 1024. +on port 80 or any other port below 1024. The ``--local`` option tells +``git-serve`` to listen only on ``127.0.0.1``. The ``--browser`` option tells ``git-serve`` to automatically start your system's default web browser with the URL of the ``git-serve`` server while starting it. @@ -62,3 +65,5 @@ current shell session, becoming a daemon process that runs in background. This is very useful in combination with the ``--pid-file`` option that write the process id in the given file. +You can specify the location of the gitweb.cgi file that ``git-serve`` uses +with the ``--gitweb`` option (e.g. /home/jannis/lib/git/gitweb.cgi). diff --git a/setup.py b/setup.py index 53a5d7b..728baa5 100644 --- a/setup.py +++ b/setup.py @@ -1,14 +1,33 @@ #!/usr/bin/env python -import os, sys, stat -from setuptools import setup, find_packages +import os, sys +try: + from setuptools import find_packages, setup + from setuptools.command.easy_install import easy_install +except ImportError: + sys.exit("Please install a recent version of setuptools") + +easy_install.real_process_distribution = easy_install.process_distribution +def process_distribution(self, *args, **kwargs): + """Brutally ugly hack to have post_install functionality. oh. my. god.""" + easy_install.real_process_distribution(self, *args, **kwargs) + + import pkg_resources + try: + pkg_resources.require("gitserve") + gitweb_cgi = pkg_resources.resource_filename("gitserve", "gitweb.cgi") + os.chmod(gitweb_cgi, 0755) + except: + print "Chmodding failed. Try 'chmod +x /path/to/gitserve/gitweb.cgi'" +easy_install.process_distribution = process_distribution setup( name='gitserve', - version='0.1.1', - license='GPL2', + version='0.2.0', + license='GPL-2', description="A helper tool for git that mimics mercurial\'s serve command", - long_description=open(os.path.join(os.path.dirname(__file__), 'README.txt')).read(), + long_description=open('README.txt', 'r').read(), + maintainer='Jannis Leidel', author='Jannis Leidel', author_email='jannis@leidel.info', url='http://github.com/jezdez/git-serve/', @@ -26,10 +45,9 @@ 'Topic :: Internet :: WWW/HTTP :: Dynamic Content', 'Topic :: Internet :: WWW/HTTP :: HTTP Servers', ], - install_requires=["setuptools"], packages=find_packages('src'), package_dir={'':'src'}, - package_data={'': ['media/*.*', '*.so', '*.conf'],}, + package_data={'': ['media/*.*', '*.cgi', '*.conf'],}, entry_points={'console_scripts': ['git-serve = gitserve:main',],}, zip_safe=False, include_package_data = True, diff --git a/src/gitserve/__init__.py b/src/gitserve/__init__.py index 9487c42..30391ac 100644 --- a/src/gitserve/__init__.py +++ b/src/gitserve/__init__.py @@ -1,7 +1,7 @@ #!/usr/bin/python # encoding: utf-8 -__version__ = '0.1.1' +__version__ = '0.2.0' import os import sys @@ -11,10 +11,10 @@ from urlparse import urljoin from optparse import OptionParser from BaseHTTPServer import HTTPServer -from socket import gethostname, gethostbyaddr +from pkg_resources import resource_filename from CGIHTTPServer import CGIHTTPRequestHandler - -gitserve_dir = os.path.dirname(os.path.abspath(__file__)) +from socket import error as SocketError +from socket import gethostname, gethostbyaddr def become_daemon(home='.', out_log='/dev/null', err_log='/dev/null'): "Robustly turn into a UNIX daemon, running in our_home_dir." @@ -48,9 +48,8 @@ def become_daemon(home='.', out_log='/dev/null', err_log='/dev/null'): class GitWebRequestHandler(CGIHTTPRequestHandler): cgi_directories = [] - aliases = [ - ('/media', os.path.join(gitserve_dir, "media")), - ] + gitserve_media = resource_filename("gitserve", "media") + aliases = [('/media', gitserve_media),] verbose = False def log_message(self, format, *args): @@ -119,7 +118,10 @@ def main(): help="port to listen on (default: 8000)", default=8000) parser.add_option("-a", "--address", dest="address", default="", - help="address to listen on (default: all interfaces)") + help="address to listen on (default: hostname)") + parser.add_option("-l", "--local", + action="store_true", dest="local", default=False, + help="only listen on 127.0.0.1") parser.add_option("-b", "--browser", action="store_true", dest="browser", default=False, help="open default browser automatically") @@ -129,8 +131,18 @@ def main(): parser.add_option("--pid-file", dest="pidfile", default="", help="write the spawned process-id to this file") + parser.add_option("--gitweb", + dest="gitweb", default="", + help="use this gitweb cgi file instead of the included version") (options, args) = parser.parse_args() + # get path to gitweb.cgi file + gitweb_cgi = options.gitweb + if not gitweb_cgi: + gitweb_cgi = resource_filename('gitserve', 'gitweb.cgi') + if not os.access(gitweb_cgi, os.X_OK): + parser.error("Your gitweb.cgi is not executable. Try 'chmod +x %s'" % gitweb_cgi) + if len(args) > 1: parser.error("incorrect number of arguments") if args: @@ -155,19 +167,21 @@ def main(): if os.path.exists(os.path.expanduser('~/.gitwebconfig')): gitweb_conf = os.path.expanduser('~/.gitwebconfig') else: - gitweb_conf = os.path.join(gitserve_dir, 'gitweb.conf') + gitweb_conf = resource_filename('gitserve', 'gitweb.conf') os.environ['GITWEB_CONFIG'] = gitweb_conf - # get hostname from the system and build urls and path to cgi - if not options.address: - options.address = gethostname() - else: - options.address = gethostbyaddr(options.address)[0] - - listen = options.address, options.port + # get hostname from the system and build url + try: + if options.local: + options.address = '127.0.0.1' + elif not options.address: + options.address = gethostname() + else: + options.address = gethostbyaddr(options.address)[0] + except SocketError, e: + parser.error(e) gitweb_url = "http://%s:%d/%s/" % (options.address, options.port, repo_name) - gitweb_cgi = os.path.join(gitserve_dir, 'gitweb.cgi.so') - + # start daemon mode if options.daemon: options.verbose = False @@ -183,8 +197,7 @@ def main(): GitWebRequestHandler.verbose = options.verbose GitWebRequestHandler.cgi_directories.append('/%s' % repo_name) GitWebRequestHandler.aliases.append(('/%s' % repo_name, gitweb_cgi)) - - httpd = HTTPServer(listen, GitWebRequestHandler) + httpd = HTTPServer((options.address, options.port), GitWebRequestHandler) if options.verbose: print "starting gitweb at: %s" % gitweb_url @@ -197,6 +210,9 @@ def main(): httpd.serve_forever() except KeyboardInterrupt: pass + except SocketError: + if options.verbose: + raise if __name__ == "__main__": main() diff --git a/src/gitserve/gitweb.cgi.so b/src/gitserve/gitweb.cgi similarity index 100% rename from src/gitserve/gitweb.cgi.so rename to src/gitserve/gitweb.cgi