import os import re import logging from wizard import app, install, resolve, sql, util from wizard.app import php def make_filename_regex_define(var): return 'wp-config.php', php.re_define(var) seed = util.dictmap(make_filename_regex_define, { 'WIZARD_DBSERVER': 'DB_HOST', 'WIZARD_DBNAME': 'DB_NAME', 'WIZARD_DBUSER': 'DB_USER', 'WIZARD_DBPASSWORD': 'DB_PASSWORD', }) # XXX: I have omitted an implementation for table prefix, on grounds that we # do not permit it to be configured. If we do end up growing support for # arbitrary table prefixes this should be added. class Application(app.Application): parametrized_files = ['wp-config.php'] + php.parametrized_files extractors = app.make_extractors(seed) extractors.update(php.extractors) substitutions = app.make_substitutions(seed) substitutions.update(php.substitutions) install_schema = install.ArgSchema("mysql", "email", "title") def download(self, version): return "http://wordpress.org/wordpress-%s.tar.gz" % version def checkConfig(self, deployment): return os.path.isfile("wp-config.php") def detectVersion(self, deployment): # XXX: Very duplicate code with MediaWiki; refactor contents = deployment.read("wp-includes/version.php") match = php.re_var("wp_version").search(contents) if not match: return None return distutils.version.LooseVersion(match.group(2)[1:-1]) def prepareMerge(self, deployment): resolve.fix_newlines("wp-config.php") def install(self, version, options): # XXX: Hmm... we should figure out something about this try: os.unlink("wp-config.php") except OSError: pass post_setup_config = { 'dbhost': options.mysql_host, 'uname': options.mysql_user, 'dbname': options.mysql_db, 'pwd': options.mysql_password, 'prefix': '', 'submit': 'Submit', 'step': '2', } post_install = { 'weblog_title': options.title, 'admin_email': options.email, 'submit': 'Continue', 'step': '2', } old_mode = os.stat(".").st_mode os.chmod(".", 0777) # XXX: squick squick result = install.fetch(options, "wp-admin/setup-config.php?step=2", post_setup_config) logging.debug("setup-config.php output\n\n" + result) result = install.fetch(options, "wp-admin/install.php?step=2", post_install) logging.debug("install.php output\n\n" + result) os.chmod(".", old_mode) if "Finished" not in result: raise install.Failure() # not sure what to do about this meta = sql.mysql_connect(options) wp_options = meta.tables["wp_options"] wp_options.update().where(wp_options.c.option_name == 'siteurl').values(option_value=options.web_path).execute() wp_options.update().where(wp_options.c.option_name == 'home').values(option_value="http://%s%s" % (options.web_host, options.web_path)).execute() # XXX: what if missing leading slash; this should be put in a function