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 <Location "%s"> 50 SetHandler python-program 51 PythonHandler %s 52 PythonDebug On 53 PythonPath "['%s'] + sys.path" 54 </Location> 55 """ 56 57 if __name__ == "__main__": 58 try: 59 app_type = sys.argv[1] 60 app_location = sys.argv[2] 61 apache_site_dir = sys.argv[3] 62 site_name = sys.argv[4] 63 url_path = sys.argv[5] 64 if app_type not in ("mod_python", "CGI"): 65 print "Please specify either CGI or mod_python as the application type." 66 sys.exit(1) 67 68 except IndexError: 69 print "config.py CGI|mod_python <app-location> <apache-site-dir> <site-name> <url-path>" 70 print 71 print "CGI configures a CGI application" 72 print "mod_python configures a mod_python application" 73 print 74 print "<app-location> is the full path to your application" 75 print "eg. %s/examples/CGI/SimpleHandler.py" % os.getcwd() 76 print 77 print "<apache-site-dir> is the directory where site configuration files are stored" 78 print "eg. /etc/apache2/sites-available" 79 print 80 print "<site-name> is the name of the site within Apache" 81 print "eg. simple" 82 print 83 print "<url-path> is the path at which your application will be published" 84 print "eg. /cgi/simple" 85 sys.exit(1) 86 87 # Derived information. 88 89 handler_dir, handler_name = os.path.split(app_location) 90 handler_name, extension = os.path.splitext(handler_name) 91 if handler_name == "" or extension != ".py": 92 print "Please specify the path to actual handler module file." 93 print "eg. %s/examples/ModPython/SimpleApp/SimpleHandler.py" % os.getcwd() 94 sys.exit(1) 95 96 # Initialise an object representing an Apache server. 97 98 apache_server = ApacheServer(apache_site_dir) 99 100 # Set up the template and the sessions directory location. 101 102 if app_type == "CGI": 103 template = cgi_template % (url_path, app_location) 104 sessions_dir = os.path.join(handler_dir, "WebStack-sessions") 105 106 elif app_type == "mod_python": 107 template = mod_python_template % (url_path, handler_name, handler_dir) 108 server_root = apache_server.get_server_root_from_config() or apache_site_dir 109 sessions_dir = os.path.join(server_root, "WebStack-sessions") 110 111 # Set up the site filename. 112 113 site_filename = os.path.join(apache_site_dir, site_name) 114 115 if os.path.exists(site_filename): 116 answer = raw_input("Overwrite existing site file? (Y|N) ") 117 if answer.upper() == "N": 118 print "Not overwriting." 119 sys.exit(1) 120 121 # Write the site file. 122 123 try: 124 f = open(site_filename, "wb") 125 f.write(template) 126 f.close() 127 except IOError: 128 print "Could not write the site file. Check your user privileges." 129 print 130 raise 131 132 # Set up the sessions directory. 133 134 if not os.path.exists(sessions_dir): 135 answer = raw_input("Create sessions directory at %s? (Y|N) " % sessions_dir) 136 if answer.upper() == "Y": 137 os.mkdir(sessions_dir) 138 139 # Find the user who should own the sessions directory. 140 141 if os.path.exists(sessions_dir): 142 try: 143 import pwd 144 username = apache_server.get_user_from_config() 145 if username is None: 146 print "Not able to determine the Web server user." 147 else: 148 print "Found", username, "as the Web server user." 149 try: 150 t = pwd.getpwnam(username) 151 uid, gid = t[2:4] 152 answer = raw_input("Set %s, %s as user, group on the sessions directory? (Y|N) " % (uid, gid)) 153 if answer.upper() == "Y": 154 os.chown(sessions_dir, uid, gid) 155 156 except KeyError: 157 print "User not found in the password database." 158 except OSError: 159 print "Not able to change the ownership. Check your user privileges." 160 161 except ImportError: 162 print "Not configuring the sessions directory ownership." 163 164 # Check the permissions on the application. 165 166 if app_type == "CGI": 167 try: 168 import stat 169 details = os.stat(app_location) 170 mode = stat.S_IMODE(details[stat.ST_MODE]) 171 172 # Check for incorrect permissions. 173 174 flags = stat.S_IRUSR|stat.S_IXUSR|stat.S_IRGRP|stat.S_IXGRP|stat.S_IROTH|stat.S_IXOTH 175 176 # Set correct permissions. 177 178 if mode & flags == flags: 179 print "Correct permissions found were", oct(mode), "for", app_location 180 else: 181 answer = raw_input("Change the permissions on %s? (Y|N) " % app_location) 182 if answer.upper() == "Y": 183 print "Setting mode", oct(flags), "on", app_location 184 os.chmod(app_location, flags) 185 186 except ImportError: 187 print "Not changing the permissions on the application." 188 189 print "--------" 190 print "Configuration completed." 191 print "You may need to run an administrative tool to add the new site '%s' to Apache." % site_name 192 print "eg. a2ensite" 193 print "You may also want to check any sys.path definitions in your application." 194 195 # vim: tabstop=4 expandtab shiftwidth=4