1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/tools/Apache/webstack_apache_config.py Fri Jun 09 22:59:35 2006 +0000
1.3 @@ -0,0 +1,195 @@
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 +<Location "%s">
1.53 + SetHandler python-program
1.54 + PythonHandler %s
1.55 + PythonDebug On
1.56 + PythonPath "['%s'] + sys.path"
1.57 +</Location>
1.58 +"""
1.59 +
1.60 +if __name__ == "__main__":
1.61 + try:
1.62 + app_type = sys.argv[1]
1.63 + app_location = sys.argv[2]
1.64 + apache_site_dir = sys.argv[3]
1.65 + site_name = sys.argv[4]
1.66 + url_path = sys.argv[5]
1.67 + if app_type not in ("mod_python", "CGI"):
1.68 + print "Please specify either CGI or mod_python as the application type."
1.69 + sys.exit(1)
1.70 +
1.71 + except IndexError:
1.72 + print "config.py CGI|mod_python <app-location> <apache-site-dir> <site-name> <url-path>"
1.73 + print
1.74 + print "CGI configures a CGI application"
1.75 + print "mod_python configures a mod_python application"
1.76 + print
1.77 + print "<app-location> is the full path to your application"
1.78 + print "eg. %s/examples/CGI/SimpleHandler.py" % os.getcwd()
1.79 + print
1.80 + print "<apache-site-dir> is the directory where site configuration files are stored"
1.81 + print "eg. /etc/apache2/sites-available"
1.82 + print
1.83 + print "<site-name> is the name of the site within Apache"
1.84 + print "eg. simple"
1.85 + print
1.86 + print "<url-path> is the path at which your application will be published"
1.87 + print "eg. /cgi/simple"
1.88 + sys.exit(1)
1.89 +
1.90 + # Derived information.
1.91 +
1.92 + handler_dir, handler_name = os.path.split(app_location)
1.93 + handler_name, extension = os.path.splitext(handler_name)
1.94 + if handler_name == "" or extension != ".py":
1.95 + print "Please specify the path to actual handler module file."
1.96 + print "eg. %s/examples/ModPython/SimpleApp/SimpleHandler.py" % os.getcwd()
1.97 + sys.exit(1)
1.98 +
1.99 + # Initialise an object representing an Apache server.
1.100 +
1.101 + apache_server = ApacheServer(apache_site_dir)
1.102 +
1.103 + # Set up the template and the sessions directory location.
1.104 +
1.105 + if app_type == "CGI":
1.106 + template = cgi_template % (url_path, app_location)
1.107 + sessions_dir = os.path.join(handler_dir, "WebStack-sessions")
1.108 +
1.109 + elif app_type == "mod_python":
1.110 + template = mod_python_template % (url_path, handler_name, handler_dir)
1.111 + server_root = apache_server.get_server_root_from_config() or apache_site_dir
1.112 + sessions_dir = os.path.join(server_root, "WebStack-sessions")
1.113 +
1.114 + # Set up the site filename.
1.115 +
1.116 + site_filename = os.path.join(apache_site_dir, site_name)
1.117 +
1.118 + if os.path.exists(site_filename):
1.119 + answer = raw_input("Overwrite existing site file? (Y|N) ")
1.120 + if answer.upper() == "N":
1.121 + print "Not overwriting."
1.122 + sys.exit(1)
1.123 +
1.124 + # Write the site file.
1.125 +
1.126 + try:
1.127 + f = open(site_filename, "wb")
1.128 + f.write(template)
1.129 + f.close()
1.130 + except IOError:
1.131 + print "Could not write the site file. Check your user privileges."
1.132 + print
1.133 + raise
1.134 +
1.135 + # Set up the sessions directory.
1.136 +
1.137 + if not os.path.exists(sessions_dir):
1.138 + answer = raw_input("Create sessions directory at %s? (Y|N) " % sessions_dir)
1.139 + if answer.upper() == "Y":
1.140 + os.mkdir(sessions_dir)
1.141 +
1.142 + # Find the user who should own the sessions directory.
1.143 +
1.144 + if os.path.exists(sessions_dir):
1.145 + try:
1.146 + import pwd
1.147 + username = apache_server.get_user_from_config()
1.148 + if username is None:
1.149 + print "Not able to determine the Web server user."
1.150 + else:
1.151 + print "Found", username, "as the Web server user."
1.152 + try:
1.153 + t = pwd.getpwnam(username)
1.154 + uid, gid = t[2:4]
1.155 + answer = raw_input("Set %s, %s as user, group on the sessions directory? (Y|N) " % (uid, gid))
1.156 + if answer.upper() == "Y":
1.157 + os.chown(sessions_dir, uid, gid)
1.158 +
1.159 + except KeyError:
1.160 + print "User not found in the password database."
1.161 + except OSError:
1.162 + print "Not able to change the ownership. Check your user privileges."
1.163 +
1.164 + except ImportError:
1.165 + print "Not configuring the sessions directory ownership."
1.166 +
1.167 + # Check the permissions on the application.
1.168 +
1.169 + if app_type == "CGI":
1.170 + try:
1.171 + import stat
1.172 + details = os.stat(app_location)
1.173 + mode = stat.S_IMODE(details[stat.ST_MODE])
1.174 +
1.175 + # Check for incorrect permissions.
1.176 +
1.177 + flags = stat.S_IRUSR|stat.S_IXUSR|stat.S_IRGRP|stat.S_IXGRP|stat.S_IROTH|stat.S_IXOTH
1.178 +
1.179 + # Set correct permissions.
1.180 +
1.181 + if mode & flags == flags:
1.182 + print "Correct permissions found were", oct(mode), "for", app_location
1.183 + else:
1.184 + answer = raw_input("Change the permissions on %s? (Y|N) " % app_location)
1.185 + if answer.upper() == "Y":
1.186 + print "Setting mode", oct(flags), "on", app_location
1.187 + os.chmod(app_location, flags)
1.188 +
1.189 + except ImportError:
1.190 + print "Not changing the permissions on the application."
1.191 +
1.192 + print "--------"
1.193 + print "Configuration completed."
1.194 + print "You may need to run an administrative tool to add the new site '%s' to Apache." % site_name
1.195 + print "eg. a2ensite"
1.196 + print "You may also want to check any sys.path definitions in your application."
1.197 +
1.198 +# vim: tabstop=4 expandtab shiftwidth=4