X-Git-Url: https://scripts.mit.edu/gitweb/wizard.git/blobdiff_plain/48a6b1d128d5f1841f3f96e9f8209b418712e2ef..db709749a3e3b1cba85a6869c87af925ae33d441:/wizard/app/php.py diff --git a/wizard/app/php.py b/wizard/app/php.py index 5eb5130..b617195 100644 --- a/wizard/app/php.py +++ b/wizard/app/php.py @@ -1,14 +1,59 @@ +""" +Common data and functions for use in PHP applications. + +.. testsetup:: * + + from wizard.app.php import * +""" + import re +import os from wizard import app, util -def make_filename_regex(var): - return 'php.ini', re.compile('^(' + re.escape(var) + r'\s*=\s*)(.*)()$', re.M) +def re_var(var): + """ + Generates a regexp for assignment to ``var`` in PHP; the quoted + value is the second subpattern. + + >>> re_var('key').search("$key = 'val';").group(2) + "'val'" + """ + return re.compile('^(\$' + app.expand_re(var) + r'''\s*=\s*)(.*)(;)''', re.M) -make_extractor = app.filename_regex_extractor(make_filename_regex) +def re_define(var): + """ + Generates a regexp for the definition of a constant in PHP; the + quoted value is the second subpattern. -extractors = util.dictmap(make_extractor, - {'WIZARD_SESSIONNAME': 'session.name' - ,'WIZARD_TMPDIR': 'upload_tmp_dir' + >>> re_define('FOO').search("define('FOO', 'bar');").group(2) + "'bar'" + """ + return re.compile('^(define\([\'"]' + app.expand_re(var) + r'''['"]\s*,\s*)(.*)(\);)''', re.M) + +def _make_filename_regex(var): + return 'php.ini', re.compile('^(' + app.expand_re(var) + r'\s*=\s*)(.*)()$', re.M) + +def ini_replace_vars(): + """ + Replace ``WIZARD_TMPDIR`` and ``WIZARD_SESSIONNAME`` with with user-specific values. + """ + text = open('php.ini', "r").read() + text = text.replace('WIZARD_TMPDIR', '/mit/%s/web_scripts_tmp' % os.environ['USER']) + text = text.replace('WIZARD_SESSIONNAME', '%s_SID' % os.environ['USER']) + open('php.ini', "w").write(text) + +seed = util.dictmap(_make_filename_regex, { + 'WIZARD_SESSIONNAME': 'session.name', + 'WIZARD_TMPDIR': ('upload_tmp_dir', 'session.save_path'), }) +#: Common extractors for parameters in :file:`php.ini`. +extractors = app.make_extractors(seed) +#: Common substitutions for parameters in :file:`php.ini`. +substitutions = app.make_substitutions(seed) +#: A list containing :file:`php.ini`. +parametrized_files = ["php.ini"] +#: Nop for consistency. +deprecated_keys = set([]) +