1.1 --- a/tools/Apache/config.py Fri Jun 09 22:59:16 2006 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,195 +0,0 @@
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
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/tools/Apache/webstack_apache_config.py Fri Jun 09 22:59:35 2006 +0000
2.3 @@ -0,0 +1,195 @@
2.4 +#!/usr/bin/env python
2.5 +
2.6 +"Make a configuration file for an Apache-resident application."
2.7 +
2.8 +import os, sys
2.9 +import glob
2.10 +
2.11 +class ApacheServer:
2.12 + def __init__(self, apache_site_dir):
2.13 + self.apache_site_dir = apache_site_dir
2.14 + self.directories = [apache_site_dir, os.path.split(apache_site_dir)[0]]
2.15 +
2.16 + def get_user_from_config(self):
2.17 + return self.get_item_from_config("User")
2.18 +
2.19 + def get_server_root_from_config(self):
2.20 + return self.get_item_from_config("ServerRoot")
2.21 +
2.22 + def get_item_from_config(self, item_name):
2.23 + item = None
2.24 + for apache_dir in self.directories:
2.25 + for conf_filename in glob.glob(os.path.join(apache_dir, "*.conf")):
2.26 + conf_file = open(conf_filename)
2.27 + for line in conf_file.readlines():
2.28 + line_parts = self.parse_line(line)
2.29 + if len(line_parts) > 1 and line_parts[0] == item_name:
2.30 + item = line_parts[1]
2.31 + conf_file.close()
2.32 + return item
2.33 + conf_file.close()
2.34 + return None
2.35 +
2.36 + def parse_line(self, line):
2.37 + parts = line.split('"')
2.38 + new_parts = []
2.39 + for i in range(0, len(parts)):
2.40 + part = parts[i]
2.41 + if i % 2 == 0:
2.42 + new_parts += part.split()
2.43 + else:
2.44 + new_parts.append(part)
2.45 + return new_parts
2.46 +
2.47 +cgi_template = """
2.48 +ScriptAlias %s "%s"
2.49 +"""
2.50 +
2.51 +mod_python_template = """
2.52 +<Location "%s">
2.53 + SetHandler python-program
2.54 + PythonHandler %s
2.55 + PythonDebug On
2.56 + PythonPath "['%s'] + sys.path"
2.57 +</Location>
2.58 +"""
2.59 +
2.60 +if __name__ == "__main__":
2.61 + try:
2.62 + app_type = sys.argv[1]
2.63 + app_location = sys.argv[2]
2.64 + apache_site_dir = sys.argv[3]
2.65 + site_name = sys.argv[4]
2.66 + url_path = sys.argv[5]
2.67 + if app_type not in ("mod_python", "CGI"):
2.68 + print "Please specify either CGI or mod_python as the application type."
2.69 + sys.exit(1)
2.70 +
2.71 + except IndexError:
2.72 + print "config.py CGI|mod_python <app-location> <apache-site-dir> <site-name> <url-path>"
2.73 + print
2.74 + print "CGI configures a CGI application"
2.75 + print "mod_python configures a mod_python application"
2.76 + print
2.77 + print "<app-location> is the full path to your application"
2.78 + print "eg. %s/examples/CGI/SimpleHandler.py" % os.getcwd()
2.79 + print
2.80 + print "<apache-site-dir> is the directory where site configuration files are stored"
2.81 + print "eg. /etc/apache2/sites-available"
2.82 + print
2.83 + print "<site-name> is the name of the site within Apache"
2.84 + print "eg. simple"
2.85 + print
2.86 + print "<url-path> is the path at which your application will be published"
2.87 + print "eg. /cgi/simple"
2.88 + sys.exit(1)
2.89 +
2.90 + # Derived information.
2.91 +
2.92 + handler_dir, handler_name = os.path.split(app_location)
2.93 + handler_name, extension = os.path.splitext(handler_name)
2.94 + if handler_name == "" or extension != ".py":
2.95 + print "Please specify the path to actual handler module file."
2.96 + print "eg. %s/examples/ModPython/SimpleApp/SimpleHandler.py" % os.getcwd()
2.97 + sys.exit(1)
2.98 +
2.99 + # Initialise an object representing an Apache server.
2.100 +
2.101 + apache_server = ApacheServer(apache_site_dir)
2.102 +
2.103 + # Set up the template and the sessions directory location.
2.104 +
2.105 + if app_type == "CGI":
2.106 + template = cgi_template % (url_path, app_location)
2.107 + sessions_dir = os.path.join(handler_dir, "WebStack-sessions")
2.108 +
2.109 + elif app_type == "mod_python":
2.110 + template = mod_python_template % (url_path, handler_name, handler_dir)
2.111 + server_root = apache_server.get_server_root_from_config() or apache_site_dir
2.112 + sessions_dir = os.path.join(server_root, "WebStack-sessions")
2.113 +
2.114 + # Set up the site filename.
2.115 +
2.116 + site_filename = os.path.join(apache_site_dir, site_name)
2.117 +
2.118 + if os.path.exists(site_filename):
2.119 + answer = raw_input("Overwrite existing site file? (Y|N) ")
2.120 + if answer.upper() == "N":
2.121 + print "Not overwriting."
2.122 + sys.exit(1)
2.123 +
2.124 + # Write the site file.
2.125 +
2.126 + try:
2.127 + f = open(site_filename, "wb")
2.128 + f.write(template)
2.129 + f.close()
2.130 + except IOError:
2.131 + print "Could not write the site file. Check your user privileges."
2.132 + print
2.133 + raise
2.134 +
2.135 + # Set up the sessions directory.
2.136 +
2.137 + if not os.path.exists(sessions_dir):
2.138 + answer = raw_input("Create sessions directory at %s? (Y|N) " % sessions_dir)
2.139 + if answer.upper() == "Y":
2.140 + os.mkdir(sessions_dir)
2.141 +
2.142 + # Find the user who should own the sessions directory.
2.143 +
2.144 + if os.path.exists(sessions_dir):
2.145 + try:
2.146 + import pwd
2.147 + username = apache_server.get_user_from_config()
2.148 + if username is None:
2.149 + print "Not able to determine the Web server user."
2.150 + else:
2.151 + print "Found", username, "as the Web server user."
2.152 + try:
2.153 + t = pwd.getpwnam(username)
2.154 + uid, gid = t[2:4]
2.155 + answer = raw_input("Set %s, %s as user, group on the sessions directory? (Y|N) " % (uid, gid))
2.156 + if answer.upper() == "Y":
2.157 + os.chown(sessions_dir, uid, gid)
2.158 +
2.159 + except KeyError:
2.160 + print "User not found in the password database."
2.161 + except OSError:
2.162 + print "Not able to change the ownership. Check your user privileges."
2.163 +
2.164 + except ImportError:
2.165 + print "Not configuring the sessions directory ownership."
2.166 +
2.167 + # Check the permissions on the application.
2.168 +
2.169 + if app_type == "CGI":
2.170 + try:
2.171 + import stat
2.172 + details = os.stat(app_location)
2.173 + mode = stat.S_IMODE(details[stat.ST_MODE])
2.174 +
2.175 + # Check for incorrect permissions.
2.176 +
2.177 + flags = stat.S_IRUSR|stat.S_IXUSR|stat.S_IRGRP|stat.S_IXGRP|stat.S_IROTH|stat.S_IXOTH
2.178 +
2.179 + # Set correct permissions.
2.180 +
2.181 + if mode & flags == flags:
2.182 + print "Correct permissions found were", oct(mode), "for", app_location
2.183 + else:
2.184 + answer = raw_input("Change the permissions on %s? (Y|N) " % app_location)
2.185 + if answer.upper() == "Y":
2.186 + print "Setting mode", oct(flags), "on", app_location
2.187 + os.chmod(app_location, flags)
2.188 +
2.189 + except ImportError:
2.190 + print "Not changing the permissions on the application."
2.191 +
2.192 + print "--------"
2.193 + print "Configuration completed."
2.194 + print "You may need to run an administrative tool to add the new site '%s' to Apache." % site_name
2.195 + print "eg. a2ensite"
2.196 + print "You may also want to check any sys.path definitions in your application."
2.197 +
2.198 +# vim: tabstop=4 expandtab shiftwidth=4