From 614a5d25c175112a985f230ba6a64a65af38973e Mon Sep 17 00:00:00 2001 From: Benoit Chesneau Date: Tue, 2 Feb 2010 18:23:37 +0100 Subject: [PATCH] replace /bin/* py setup entry-points --- bin/gunicorn | 28 ------------- bin/gunicorn_django | 34 ---------------- bin/gunicorn_paste | 59 ---------------------------- gunicorn/main.py | 96 ++++++++++++++++++++++++++++++++++++++++++++- setup.py | 11 ++++-- 5 files changed, 103 insertions(+), 125 deletions(-) delete mode 100755 bin/gunicorn delete mode 100755 bin/gunicorn_django delete mode 100644 bin/gunicorn_paste diff --git a/bin/gunicorn b/bin/gunicorn deleted file mode 100755 index c59372e1..00000000 --- a/bin/gunicorn +++ /dev/null @@ -1,28 +0,0 @@ -#! /usr/bin/env python -# -*- coding: utf-8 - -# -# This file is part of gunicorn released under the MIT license. -# See the NOTICE for more information. - - -import os -import sys - -sys.path.insert(0, os.getcwd()) - -from gunicorn.main import main -from gunicorn.util import import_app - -__usage__ = "%prog [OPTIONS] APP_MODULE" - -def get_app(parser, opts, args): - if len(args) != 1: - parser.error("No application module specified.") - - try: - return import_app(args[0]) - except: - parser.error("Failed to import application module.") - -main(__usage__, get_app) - diff --git a/bin/gunicorn_django b/bin/gunicorn_django deleted file mode 100755 index c2e67795..00000000 --- a/bin/gunicorn_django +++ /dev/null @@ -1,34 +0,0 @@ -#! /usr/bin/env python -# -*- coding: utf-8 - -# -# This file is part of gunicorn released under the MIT license. -# See the NOTICE for more information. - - -import os -import sys - -from django.core.handlers.wsgi import WSGIHandler -from gunicorn.main import main - -__usage__ = "%prog [OPTIONS]" - -PROJECT_PATH = os.getcwd() -if not os.path.isfile(os.path.join(PROJECT_PATH, "settings.py")): - print >>sys.stderr, "settings file not found." - sys.exit(1) - -PROJECT_NAME = os.path.split(PROJECT_PATH)[-1] - -sys.path.insert(0, PROJECT_PATH) -sys.path.append(os.path.join(PROJECT_PATH, os.pardir)) - -# set environ -os.environ['DJANGO_SETTINGS_MODULE'] = '%s.settings' % PROJECT_NAME - - -def get_app(parser, opts, args): - # django wsgi app - return WSGIHandler() - -main(__usage__, get_app) diff --git a/bin/gunicorn_paste b/bin/gunicorn_paste deleted file mode 100644 index a7711e31..00000000 --- a/bin/gunicorn_paste +++ /dev/null @@ -1,59 +0,0 @@ -#! /usr/bin/env python -# -*- coding: utf-8 - -# -# This file is part of gunicorn released under the MIT license. -# See the NOTICE for more information. - - -import os -import pkg_resources -import re -import sys -from paste.deploy import loadapp, loadwsgi -from gunicorn.main import main - -__usage__ = "%prog [OPTIONS] APP_MODULE" - -_scheme_re = re.compile(r'^[a-z][a-z]+:', re.I) - - -def get_app(parser, opts, args): - if len(args) != 1: - parser.error("No applicantion name specified.") - - config_file = os.path.abspath(os.path.normpath( - os.path.join(os.getcwd(), args[0]))) - - if not os.path.exists(config_file): - parser.error("Config file not found.") - - config_url = 'config:%s' % config_file - relative_to = os.path.dirname(config_file) - - # load module in sys path - sys.path.insert(0, relative_to) - - # add to eggs - pkg_resources.working_set.add_entry(relative_to) - ctx = loadwsgi.loadcontext(loadwsgi.SERVER, config_url, - relative_to=relative_to) - - if opts.workers: - workers = opts.workers - else: - workers = int(ctx.local_conf.get('workers', 1)) - - opts.host = opts.host or ctx.local_conf.get('host', '127.0.0.1') - opts.port = opts.port or int(ctx.local_conf.get('port', 8000)) - - debug = ctx.global_conf.get('debug') == "true" - if debug: - # we force to one worker in debug mode. - workers = 1 - - opts.workers=workers - - app = loadapp(config_url, relative_to=relative_to) - return app - -main(__usage__, get_app) diff --git a/gunicorn/main.py b/gunicorn/main.py index e05c836a..2b2b3eb9 100644 --- a/gunicorn/main.py +++ b/gunicorn/main.py @@ -7,11 +7,15 @@ import logging import optparse as op import os +import pkg_resources +import re import sys from gunicorn.arbiter import Arbiter from gunicorn import util +__usage__ = "%prog [OPTIONS]" + LOG_LEVELS = { "critical": logging.CRITICAL, "error": logging.ERROR, @@ -149,4 +153,94 @@ def paste_server(app, global_conf=None, host="127.0.0.1", port=None, daemonize() else: os.setpgrp() - arbiter.run() \ No newline at end of file + arbiter.run() + +def run(): + sys.path.insert(0, os.getcwd()) + + def get_app(parser, opts, args): + if len(args) != 1: + parser.error("No application module specified.") + + try: + return util.import_app(args[0]) + except: + parser.error("Failed to import application module.") + + main(__usage__, get_app) + +def run_django(): + from django.core.handlers.wsgi import WSGIHandler + + PROJECT_PATH = os.getcwd() + if not os.path.isfile(os.path.join(PROJECT_PATH, "settings.py")): + print >>sys.stderr, "settings file not found." + sys.exit(1) + + PROJECT_NAME = os.path.split(PROJECT_PATH)[-1] + + sys.path.insert(0, PROJECT_PATH) + sys.path.append(os.path.join(PROJECT_PATH, os.pardir)) + + # set environ + os.environ['DJANGO_SETTINGS_MODULE'] = '%s.settings' % PROJECT_NAME + + + def get_app(parser, opts, args): + # django wsgi app + return WSGIHandler() + + main(__usage__, get_app) + +def run_paster(): + + import os + + from paste.deploy import loadapp, loadwsgi + + __usage__ = "%prog [OPTIONS] APP_MODULE" + + _scheme_re = re.compile(r'^[a-z][a-z]+:', re.I) + + + def get_app(parser, opts, args): + if len(args) != 1: + parser.error("No applicantion name specified.") + + config_file = os.path.abspath(os.path.normpath( + os.path.join(os.getcwd(), args[0]))) + + if not os.path.exists(config_file): + parser.error("Config file not found.") + + config_url = 'config:%s' % config_file + relative_to = os.path.dirname(config_file) + + # load module in sys path + sys.path.insert(0, relative_to) + + # add to eggs + pkg_resources.working_set.add_entry(relative_to) + ctx = loadwsgi.loadcontext(loadwsgi.SERVER, config_url, + relative_to=relative_to) + + if opts.workers: + workers = opts.workers + else: + workers = int(ctx.local_conf.get('workers', 1)) + + opts.host = opts.host or ctx.local_conf.get('host', '127.0.0.1') + opts.port = opts.port or int(ctx.local_conf.get('port', 8000)) + + debug = ctx.global_conf.get('debug') == "true" + if debug: + # we force to one worker in debug mode. + workers = 1 + + opts.workers=workers + + app = loadapp(config_url, relative_to=relative_to) + return app + + main(__usage__, get_app) + \ No newline at end of file diff --git a/setup.py b/setup.py index 39035d36..13bcd8ba 100644 --- a/setup.py +++ b/setup.py @@ -9,7 +9,7 @@ from setuptools import setup, find_packages setup( name = 'gunicorn', - version = '0.3', + version = '0.3.1', description = 'WSGI HTTP Server for UNIX', long_description = file( @@ -38,9 +38,14 @@ setup( packages = ['gunicorn'], include_package_data = True, - scripts = ['bin/gunicorn', 'bin/gunicorn_django', 'bin/gunicorn_paste'], - + entry_points=""" + + [console_scripts] + gunicorn=gunicorn.main:run + gunicorn_django=gunicorn.main:run_django + gunicorn_paster=gunicorn.main:run_paster + [paste.server_runner] main=gunicorn.main:paste_server """,