1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/tools/Apache/config.py Sun May 01 12:56:29 2005 +0000
1.3 @@ -0,0 +1,175 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"Make a configuration file for an Apache-resident application."
1.7 +
1.8 +import os, sys
1.9 +import glob
1.10 +
1.11 +class ApacheServer:
1.12 + def __init__(self, apache_site_dir):
1.13 + self.apache_site_dir = apache_site_dir
1.14 + self.directories = [apache_site_dir, os.path.split(apache_site_dir)[0]]
1.15 +
1.16 + def get_user_from_config(self):
1.17 + return self.get_item_from_config("User")
1.18 +
1.19 + def get_server_root_from_config(self):
1.20 + return self.get_item_from_config("ServerRoot")
1.21 +
1.22 + def get_item_from_config(self, item_name):
1.23 + item = None
1.24 + for apache_dir in self.directories:
1.25 + for conf_filename in glob.glob(os.path.join(apache_dir, "*.conf")):
1.26 + conf_file = open(conf_filename)
1.27 + for line in conf_file.readlines():
1.28 + line_parts = self.parse_line(line)
1.29 + if len(line_parts) > 1 and line_parts[0] == item_name:
1.30 + item = line_parts[1]
1.31 + conf_file.close()
1.32 + return item
1.33 + conf_file.close()
1.34 + return None
1.35 +
1.36 + def parse_line(self, line):
1.37 + parts = line.split('"')
1.38 + new_parts = []
1.39 + for i in range(0, len(parts)):
1.40 + part = parts[i]
1.41 + if i % 2 == 0:
1.42 + new_parts += part.split()
1.43 + else:
1.44 + new_parts.append(part)
1.45 + return new_parts
1.46 +
1.47 +cgi_template = """
1.48 +ScriptAlias %s "%s"
1.49 +"""
1.50 +
1.51 +mod_python_template = """
1.52 +Alias %s "%s"
1.53 +
1.54 +<Directory "%s">
1.55 + AddHandler python-program %s
1.56 + PythonHandler %s
1.57 + PythonDebug On
1.58 +</Directory>
1.59 +"""
1.60 +
1.61 +if __name__ == "__main__":
1.62 + try:
1.63 + app_type = sys.argv[1]
1.64 + app_location = sys.argv[2]
1.65 + apache_site_dir = sys.argv[3]
1.66 + site_name = sys.argv[4]
1.67 + url_path = sys.argv[5]
1.68 + if app_type == "mod_python":
1.69 + suffix = sys.argv[6]
1.70 + elif app_type != "CGI":
1.71 + print "Please specify either CGI or mod_python as the application type."
1.72 + sys.exit(1)
1.73 +
1.74 + except IndexError:
1.75 + print "config.py CGI|mod_python <app-location> <apache-site-dir> <site-name> <url-path> [<suffix>]"
1.76 + print
1.77 + print "CGI configures a CGI application"
1.78 + print "mod_python configures a mod_python application"
1.79 + print
1.80 + print "<app-location> is the full path to your application"
1.81 + print "eg. %s/examples/CGI/SimpleHandler.py" % os.getcwd()
1.82 + print
1.83 + print "<apache-site-dir> is the directory where site configuration files are stored"
1.84 + print "eg. /etc/apache2/sites-available"
1.85 + print
1.86 + print "<site-name> is the name of the site within Apache"
1.87 + print "eg. simple"
1.88 + print
1.89 + print "<url-path> is the path at which your application will be published"
1.90 + print "eg. /cgi/simple"
1.91 + print
1.92 + print "mod_python options:"
1.93 + print
1.94 + print "<suffix> is the ending which published resources in the application should have"
1.95 + print "eg. .simple"
1.96 + sys.exit(1)
1.97 +
1.98 + # Derived information.
1.99 +
1.100 + handler_dir, handler_name = os.path.split(app_location)
1.101 + handler_name, extension = os.path.splitext(handler_name)
1.102 + if handler_name == "" or extension != ".py":
1.103 + print "Please specify the path to actual handler module file."
1.104 + print "eg. %s/examples/ModPython/SimpleApp/SimpleHandler.py" % os.getcwd()
1.105 + sys.exit(1)
1.106 +
1.107 + # Initialise an object representing an Apache server.
1.108 +
1.109 + apache_server = ApacheServer(apache_site_dir)
1.110 +
1.111 + # Set up the template and the sessions directory location.
1.112 +
1.113 + if app_type == "CGI":
1.114 + template = cgi_template % (url_path, app_location)
1.115 + sessions_dir = os.path.join(handler_dir, "WebStack-sessions")
1.116 +
1.117 + elif app_type == "mod_python":
1.118 + template = mod_python_template % (url_path, handler_dir, handler_dir, suffix, handler_name)
1.119 + server_root = apache_server.get_server_root_from_config() or apache_site_dir
1.120 + sessions_dir = os.path.join(server_root, "WebStack-sessions")
1.121 +
1.122 + # Set up the site filename.
1.123 +
1.124 + site_filename = os.path.join(apache_site_dir, site_name)
1.125 +
1.126 + if os.path.exists(site_filename):
1.127 + answer = raw_input("Overwrite existing site file? (Y|N) ")
1.128 + if answer.upper() == "N":
1.129 + print "Not overwriting."
1.130 + sys.exit(1)
1.131 +
1.132 + # Write the site file.
1.133 +
1.134 + try:
1.135 + f = open(site_filename, "wb")
1.136 + f.write(template)
1.137 + f.close()
1.138 + except IOError:
1.139 + print "Could not write the site file. Check your user privileges."
1.140 + print
1.141 + raise
1.142 +
1.143 + # Set up the sessions directory.
1.144 +
1.145 + if not os.path.exists(sessions_dir):
1.146 + answer = raw_input("Create sessions directory at %s? (Y|N) " % sessions_dir)
1.147 + if answer.upper() == "Y":
1.148 + os.mkdir(sessions_dir)
1.149 +
1.150 + # Find the user who should own the sessions directory.
1.151 +
1.152 + try:
1.153 + import pwd
1.154 + username = apache_server.get_user_from_config()
1.155 + if username is None:
1.156 + print "Not able to determine the Web server user."
1.157 + else:
1.158 + print "Found", username, "as the Web server user."
1.159 + try:
1.160 + t = pwd.getpwnam(username)
1.161 + uid, gid = t[2:4]
1.162 + answer = raw_input("Set %s, %s as user, group on the sessions directory? (Y|N) " % (uid, gid))
1.163 + if answer.upper() == "Y":
1.164 + os.chown(sessions_dir, uid, gid)
1.165 +
1.166 + except KeyError:
1.167 + print "User not found in the password database."
1.168 + except OSError:
1.169 + print "Not able to change the ownership. Check your user privileges."
1.170 +
1.171 + except ImportError:
1.172 + print "Not configuring the sessions directory ownership."
1.173 +
1.174 + print "Configuration completed."
1.175 + print "You may need to run an administrative tool to add the new site '%s' to Apache." % site_name
1.176 + print "eg. a2ensite"
1.177 +
1.178 +# vim: tabstop=4 expandtab shiftwidth=4