1 #!/usr/bin/env python 2 3 "Make a configuration file for an Apache-resident application." 4 5 import os, sys 6 import glob 7 8 class ApacheServer: 9 def __init__(self, apache_site_dir): 10 self.apache_site_dir = apache_site_dir 11 self.directories = [apache_site_dir, os.path.split(apache_site_dir)[0]] 12 13 def get_user_from_config(self): 14 return self.get_item_from_config("User") 15 16 def get_server_root_from_config(self): 17 return self.get_item_from_config("ServerRoot") 18 19 def get_item_from_config(self, item_name): 20 item = None 21 for apache_dir in self.directories: 22 for conf_filename in glob.glob(os.path.join(apache_dir, "*.conf")): 23 conf_file = open(conf_filename) 24 for line in conf_file.readlines(): 25 line_parts = self.parse_line(line) 26 if len(line_parts) > 1 and line_parts[0] == item_name: 27 item = line_parts[1] 28 conf_file.close() 29 return item 30 conf_file.close() 31 return None 32 33 def parse_line(self, line): 34 parts = line.split('"') 35 new_parts = [] 36 for i in range(0, len(parts)): 37 part = parts[i] 38 if i % 2 == 0: 39 new_parts += part.split() 40 else: 41 new_parts.append(part) 42 return new_parts 43 44 cgi_template = """ 45 ScriptAlias %s "%s" 46 """ 47 48 mod_python_template = """ 49 Alias %s "%s" 50 51 <Directory "%s"> 52 AddHandler python-program %s 53 PythonHandler %s 54 PythonDebug On 55 </Directory> 56 """ 57 58 if __name__ == "__main__": 59 try: 60 app_type = sys.argv[1] 61 app_location = sys.argv[2] 62 apache_site_dir = sys.argv[3] 63 site_name = sys.argv[4] 64 url_path = sys.argv[5] 65 if app_type == "mod_python": 66 suffix = sys.argv[6] 67 elif app_type != "CGI": 68 print "Please specify either CGI or mod_python as the application type." 69 sys.exit(1) 70 71 except IndexError: 72 print "config.py CGI|mod_python <app-location> <apache-site-dir> <site-name> <url-path> [<suffix>]" 73 print 74 print "CGI configures a CGI application" 75 print "mod_python configures a mod_python application" 76 print 77 print "<app-location> is the full path to your application" 78 print "eg. %s/examples/CGI/SimpleHandler.py" % os.getcwd() 79 print 80 print "<apache-site-dir> is the directory where site configuration files are stored" 81 print "eg. /etc/apache2/sites-available" 82 print 83 print "<site-name> is the name of the site within Apache" 84 print "eg. simple" 85 print 86 print "<url-path> is the path at which your application will be published" 87 print "eg. /cgi/simple" 88 print 89 print "mod_python options:" 90 print 91 print "<suffix> is the ending which published resources in the application should have" 92 print "eg. .simple" 93 sys.exit(1) 94 95 # Derived information. 96 97 handler_dir, handler_name = os.path.split(app_location) 98 handler_name, extension = os.path.splitext(handler_name) 99 if handler_name == "" or extension != ".py": 100 print "Please specify the path to actual handler module file." 101 print "eg. %s/examples/ModPython/SimpleApp/SimpleHandler.py" % os.getcwd() 102 sys.exit(1) 103 104 # Initialise an object representing an Apache server. 105 106 apache_server = ApacheServer(apache_site_dir) 107 108 # Set up the template and the sessions directory location. 109 110 if app_type == "CGI": 111 template = cgi_template % (url_path, app_location) 112 sessions_dir = os.path.join(handler_dir, "WebStack-sessions") 113 114 elif app_type == "mod_python": 115 template = mod_python_template % (url_path, handler_dir, handler_dir, suffix, handler_name) 116 server_root = apache_server.get_server_root_from_config() or apache_site_dir 117 sessions_dir = os.path.join(server_root, "WebStack-sessions") 118 119 # Set up the site filename. 120 121 site_filename = os.path.join(apache_site_dir, site_name) 122 123 if os.path.exists(site_filename): 124 answer = raw_input("Overwrite existing site file? (Y|N) ") 125 if answer.upper() == "N": 126 print "Not overwriting." 127 sys.exit(1) 128 129 # Write the site file. 130 131 try: 132 f = open(site_filename, "wb") 133 f.write(template) 134 f.close() 135 except IOError: 136 print "Could not write the site file. Check your user privileges." 137 print 138 raise 139 140 # Set up the sessions directory. 141 142 if not os.path.exists(sessions_dir): 143 answer = raw_input("Create sessions directory at %s? (Y|N) " % sessions_dir) 144 if answer.upper() == "Y": 145 os.mkdir(sessions_dir) 146 147 # Find the user who should own the sessions directory. 148 149 if os.path.exists(sessions_dir): 150 try: 151 import pwd 152 username = apache_server.get_user_from_config() 153 if username is None: 154 print "Not able to determine the Web server user." 155 else: 156 print "Found", username, "as the Web server user." 157 try: 158 t = pwd.getpwnam(username) 159 uid, gid = t[2:4] 160 answer = raw_input("Set %s, %s as user, group on the sessions directory? (Y|N) " % (uid, gid)) 161 if answer.upper() == "Y": 162 os.chown(sessions_dir, uid, gid) 163 164 except KeyError: 165 print "User not found in the password database." 166 except OSError: 167 print "Not able to change the ownership. Check your user privileges." 168 169 except ImportError: 170 print "Not configuring the sessions directory ownership." 171 172 # Check the permissions on the application. 173 174 if app_type == "CGI": 175 try: 176 import stat 177 details = os.stat(app_location) 178 mode = stat.S_IMODE(details[stat.ST_MODE]) 179 180 # Check for incorrect permissions. 181 182 flags = stat.S_IRUSR|stat.S_IXUSR|stat.S_IRGRP|stat.S_IXGRP|stat.S_IROTH|stat.S_IXOTH 183 184 # Set correct permissions. 185 186 if mode & flags == flags: 187 print "Correct permissions found were", oct(mode), "for", app_location 188 else: 189 answer = raw_input("Change the permissions on %s? (Y|N) " % app_location) 190 if answer.upper() == "Y": 191 print "Setting mode", oct(flags), "on", app_location 192 os.chmod(app_location, flags) 193 194 except ImportError: 195 print "Not changing the permissions on the application." 196 197 print "--------" 198 print "Configuration completed." 199 print "You may need to run an administrative tool to add the new site '%s' to Apache." % site_name 200 print "eg. a2ensite" 201 print "You may also want to check any sys.path definitions in your application." 202 203 # vim: tabstop=4 expandtab shiftwidth=4