+
+ >>> open("test-settings.substitution.ini", "w").write("config_var = 3")
+ >>> f = filename_regex_substitution('WIZARD_KEY', 'test-settings.substitution.ini', re.compile('^(config_var\s*=\s*)(.*)()$'))
+ >>> f(deploy.Deployment("."))
+ 1
+ >>> print open("test-settings.substitution.ini", "r").read()
+ config_var = WIZARD_KEY
+ >>> os.unlink("test-settings.substitution.ini")
+ """
+ if isinstance(files, str):
+ files = (files,)
+ def h(deployment):
+ base = deployment.location
+ subs = 0
+ for file in files:
+ file = os.path.join(base, file)
+ try:
+ contents = open(file, "r").read()
+ contents, n = regex.subn("\\1" + key + "\\3", contents)
+ subs += n
+ open(file, "w").write(contents)
+ except IOError:
+ pass
+ return subs
+ return h
+
+# XXX: rename to show that it's mysql specific
+def backup_database(outdir, deployment):
+ """
+ Generic database backup function for MySQL. Assumes that ``WIZARD_DBNAME``
+ is extractable, and that :func:`wizard.scripts.get_sql_credentials`
+ works.
+ """
+ sh = shell.Shell()
+ outfile = os.path.join(outdir, "db.sql")
+ try:
+ sh.call("mysqldump", "--compress", "-r", outfile, *get_mysql_args(deployment))
+ sh.call("gzip", "--best", outfile)
+ except shell.CallError as e:
+ shutil.rmtree(outdir)
+ raise BackupFailure(e.stderr)
+
+def restore_database(backup_dir, deployment):
+ """
+ Generic database restoration function for MySQL. See :func:`backup_database`
+ for the assumptions that we make.