/[glsr]/trunk/setup/setup.py
Gentoo

Contents of /trunk/setup/setup.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 24 - (show annotations) (download) (as text)
Thu Jun 30 05:18:08 2005 UTC (12 years, 11 months ago) by hadfield
File MIME type: text/x-python
File size: 6418 byte(s)
imported domain_modules package and setup package from the src-whoa cvs tree
1 #!/usr/bin/env python2
2
3 # Copyright 2005 Gentoo Foundation
4 # Distributed under the terms of the GNU General Public License v2
5 #
6
7 """The GLSR Setup Script.
8
9 Sets up the script repository. Performing the following actions:
10 - Creation of web server owned directories
11 - Addition of admin users to the database
12 - Creation of the database and tables
13
14 Note that this script will need the root db username and password to create
15 the database and the database user. (This isn't actually implemented yet)
16
17 TODO:
18 - Implement code that will create the database and db user permissions.
19 - Don't try to create the database or tables if they already exist. Or at
20 least drop them first.
21 """
22
23 __revision__ = '$Id$'
24 __modulename__ = 'setup'
25
26 import os
27 import re
28 import sys
29 sys.path.insert(0, "../")
30
31 import config
32 from core.template import Template as template_handler
33 from core.db.mysql import SQLdb as db_handler
34
35 ###############################################################################
36 # Setup DB regular expressions
37 # These expressions are used for parsing the db.schema file. To add support
38 # for an additional database all you need to do is add two new regular
39 # expressions for that db.
40
41 # Create the database handler object.
42 _db_obj = db_handler(config.db)
43
44 # Database specific db matching regex's.
45 _mysql_db_re = r'(?is)CREATE\s+DATABASE\s+\w+\s*;'
46
47 # Database specific table matching regex's. Used for getting a table definition
48 # from the db schema. Note that %s represents the table name.
49 _mysql_table_re = r'(?is)CREATE\s+TABLE\s+(%s)\s*\([^;]*;'
50
51 # Set the table name and db matching regex to the appropriate database.
52 _db_re = eval("_%s_db_re" % config.db["db_type"])
53 _table_re = eval("_%s_table_re" % config.db["db_type"])
54
55 def load_schema(schema_loc, values):
56 """Load and compile the SQL schema template."""
57
58 schema_tmpl = template_handler()
59 schema_tmpl.param("TBL_PREFIX", config.db["prefix"])
60 schema_tmpl.param("DB_NAME", config.db["db"])
61 schema_tmpl.param("LANGUAGE_LOOP", [], "loop")
62
63 if values.has_key("USER_LOOP"):
64 schema_tmpl.param("USER_LOOP", values["USER_LOOP"], "loop")
65 else:
66 schema_tmpl.param("USER_LOOP", [], "loop")
67
68 schema_tmpl.compile(schema_loc, cache = False)
69 return schema_tmpl.output()
70
71 def add_user(user_type = ""):
72 """Prompts for user information and returns the info in a dict."""
73
74 from getpass import getpass
75
76 if user_type != "":
77 user_type += " "
78
79 alias = raw_input("\n%suser alias: " % user_type)
80 if len(alias) == 0:
81 print "ERROR: Zero length username"
82 sys.exit(1)
83
84 passwd = getpass("Password: ")
85 if len(passwd) == 0:
86 print "ERROR: Zero length password"
87 sys.exit(1)
88
89 passwd2 = getpass("Confirm password: ")
90 if passwd != passwd2:
91 print "ERROR: passwords do not match"
92 sys.exit(1)
93
94 fullname = raw_input("Full Name: ")
95 email = raw_input("Email: ")
96
97 return {"alias": alias, "passwd": passwd, "fullname": fullname,
98 "email": email}
99
100 ###############################################################################
101 # The database building/creation methods
102
103 def _list_tables(schema):
104 """Returns a list of table names in the specified schema."""
105
106 # This assumes that all databases have table names that are alphanumeric
107 tbl_expr = re.compile(_table_re % "\w+")
108
109 table_list = []
110 start_pos = 0
111
112 while True:
113 # Loop through the schema to find all of the table names.
114 # match.group(0) will match the entire table definition.
115 # match.group(1) will match the table name.
116
117 match = tbl_expr.search(schema, start_pos)
118 if match is None:
119 break
120
121 start_pos = schema.find(match.group(0)) + len(match.group(0))
122 table_list.append(match.group(1))
123
124 return table_list
125
126 def create_database(schema):
127 """Create the glsr database."""
128
129 db_match = re.search(_db_re, schema)
130
131 if db_match is None:
132 raise SetupError("DB create not defined in schema")
133
134 print db_match.group(0)
135
136 _db_obj.query(db_match.group(0), fetch="none")
137
138
139 def create_tables(tables, schema):
140 """Create all tables in 'tables'. Each table must be defined in schema."""
141
142 for table in tables:
143
144 print " >>> " + table
145 table_match = re.search(_table_re % table, schema)
146
147 if table_match is None:
148 raise SetupError("Table '%s' not defined in schema" % table)
149
150 #print str_indent(table_str.group(0), 4)
151 #print ""
152
153 _db_obj.query(table_match.group(0), fetch="none")
154
155 def insert_data(table, schema):
156 """Insert additional SQL data."""
157
158 start = schema.find("# begin %s-data-insert" % table)
159 start = start + len("# begin %s-data-insert" % table)
160 end = schema.find("# end %s-data-insert" % table)
161
162 query_str = schema[start:end].strip()
163 if -1 not in (start, end) and query_str != "":
164 print " >>> " + table
165 _db_obj.query(schema[start:end].strip(), fetch = "none")
166
167 class SetupError(Exception): pass
168
169
170 if __name__ == '__main__':
171
172 # Setup directories and files
173 print "Creating script repository directories and files:"
174 print " Creating log folder (%s)" % config.logging_loc
175 if not os.path.exists(config.logging_loc):
176 os.makedirs(config.logging_loc)
177
178 print " Creating template cache folder (%s)" % config.template_cache
179 if not os.path.exists(config.template_cache):
180 os.makedirs(config.template_cache)
181
182 _user_loop = []
183 admin_info = add_user("Admin")
184 admin_info["rank"] = 0
185 admin_info["type"] = 3
186 _user_loop.append(admin_info)
187
188 # Load the schema file
189 _schema_loc = config.root + "setup/schema/%s.schema" % config.db["db_type"]
190 _schema = load_schema(_schema_loc, {"USER_LOOP": _user_loop})
191
192 # Create the database and tables
193 #print "Creating GLSR %s database" % config.db["db_type"]
194 #create_database(schema)
195
196 print "\nCreating %s tables" % config.db["db_type"]
197 create_tables(_list_tables(_schema), _schema)
198
199 print "\nInserting default data:"
200 insert_data("state", _schema)
201
202 print "\nInserting language data:"
203 insert_data("language", _schema)
204
205 # Add the admin user
206 print "\nCreating admin user %s..." % (admin_info["alias"])
207 insert_data("user", _schema)
208
209 print

Properties

Name Value
svn:executable *
svn:keywords Id

  ViewVC Help
Powered by ViewVC 1.1.20