+++ /dev/null
-#!/usr/bin/python
-
-# Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
-#
-# This file is part of Cookbook.
-#
-# Cookbook is free software: you can redistribute it and/or modify it
-# under the terms of the GNU General Public License as published by the
-# Free Software Foundation, either version 3 of the License, or (at your
-# option) any later version.
-#
-# Cookbook is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with Cookbook. If not, see <http://www.gnu.org/licenses/>.
-
-from __future__ import with_statement
-
-import os
-import os.path
-import uuid
-
-import cookbook
-from cookbook import Cookbook
-from cookbook.mom import MomParser
-from cookbook.cookbook import test as test_cookbook
-
-try:
- import jinja2
- import cherrypy
- from cookbook.server import Server
- from cookbook.server import test as test_server
-except ImportError, e:
- cherrypy = e
- def test_server():
- pass
-
-
-if __name__ == '__main__':
- import optparse
- import sys
-
- p = optparse.OptionParser()
- p.add_option('-t', '--test', dest='test', default=False,
- action='store_true', help='run internal tests and exit')
- p.add_option('-m', '--mom', dest='mom', metavar='PATH',
- help="load Mom's cookbook")
- p.add_option('-s', '--serve', dest='serve', default=False,
- action='store_true', help='serve cookbook')
- p.add_option('-a', '--address', dest='address', default='127.0.0.1',
- metavar='ADDR',
- help='address that the server will bind to (%default)')
- p.add_option('-p', '--port', dest='port', default='8080',
- metavar='PORT',
- help='port that the server will listen on (%default)')
-
- options,arguments = p.parse_args()
-
- if options.test == True:
- test_cookbook()
- test_server()
- sys.exit(0)
-
- if isinstance(cherrypy, ImportError):
- raise cherrypy
-
- sys.stderr.write('Loading cookbook\n')
- if options.mom == None:
- c = Cookbook()
- c.load()
- else:
- p = MomParser()
- c = p.parse(options.mom)
-
- if options.serve == False:
- sys.stderr.write('Saving cookbook\n')
- c.save('new-recipe')
- else:
- # HACK! to ensure we *always* get utf-8 output
- #reload(sys)
- #sys.setdefaultencoding('utf-8')
- sys.stderr.write('Serving cookbook\n')
- module_dir = os.path.dirname(os.path.abspath(cookbook.__file__))
- static_dir = os.path.join(module_dir, 'static')
- if not os.path.exists(static_dir):
- os.mkdir(static_dir)
- template_dir = os.path.join(module_dir, 'template')
- config = os.path.join(module_dir, 'config')
- s = Server(c, template_dir)
- if cherrypy.__version__.startswith('3.'):
- cherrypy.config.update({ # http://www.cherrypy.org/wiki/ConfigAPI
- 'server.socket_host': options.address,
- 'server.socket_port': int(options.port),
- 'tools.decode.on': True,
- 'tools.encode.on': True,
- 'tools.encode.encoding': 'utf8',
- 'tools.staticdir.root': static_dir,
- })
- if cherrypy.__version__.startswith('3.2'):
- get_ha1 = cherrypy.lib.auth_digest.get_ha1_file_htdigest(
- '.htaccess')
- digest_auth = {
- 'tools.auth_digest.on': True,
- 'tools.auth_digest.realm': 'cookbook',
- 'tools.auth_digest.get_ha1': get_ha1,
- 'tools.auth_digest.key': str(uuid.uuid4()),
- }
- else:
- passwds = {}
- with open('.htaccess', 'r') as f:
- for line in f:
- user,realm,ha1 = line.strip().split(':')
- passwds[user] = ha1 # use the ha1 as the password
- digest_auth = {
- 'tools.digest_auth.on': True,
- 'tools.digest_auth.realm': 'cookbook',
- 'tools.digest_auth.users': passwds,
- }
- print passwds
- del(passwds)
- app_config = {
- '/static': {
- 'tools.staticdir.on': True,
- 'tools.staticdir.dir': '',
- },
- '/edit': digest_auth,
- '/add_tag': digest_auth,
- '/remove_tag': digest_auth,
- }
- cherrypy.quickstart(root=s, config=app_config)
- elif cherrypy.__version__.startswith('2.'):
- cherrypy.root = s
- cherrypy.config.update({
- 'server.environment': 'production',
- 'server.socket_host': options.address,
- 'server.socket_port': int(options.port),
- 'decoding_filter.on': True,
- 'encoding_filter.on': True,
- 'encodinf_filter.encoding': 'utf8',
- 'static_filter.on': True,
- 'static_filter.dir': static_dir,
- })
- import pprint
- pprint.pprint(cherrypy.config.configs)
- cherrypy.server.start()
- s.cleanup()