]> scripts.mit.edu Git - wizard.git/blob - wizard/app/php.py
Update MediaWiki config regexes
[wizard.git] / wizard / app / php.py
1 """
2 Common data and functions for use in PHP applications.
3
4 .. testsetup:: *
5
6     from wizard.app.php import *
7 """
8
9 import re
10 import os
11
12 from wizard import app, util
13
14 def re_var(var):
15     """
16     Generates a regexp for assignment to ``var`` in PHP; the quoted
17     value is the second subpattern.
18
19     >>> re_var('key').search("$key = 'val';").group(2)
20     "'val'"
21     """
22     return re.compile('^(\$' + app.expand_re(var) + r'''\s*=\s*)(.*)(;)''', re.M)
23
24 def re_define(var):
25     """
26     Generates a regexp for the definition of a constant in PHP; the
27     quoted value is the second subpattern.
28
29     >>> re_define('FOO').search("define('FOO', 'bar');").group(2)
30     "'bar'"
31     """
32     return re.compile('^(define\([\'"]' + app.expand_re(var) + r'''['"]\s*,\s*)(.*)(\);)''', re.M)
33
34 def _make_filename_regex(var):
35     return 'php.ini', re.compile('^(' + app.expand_re(var) + r'\s*=\s*)(.*)()$', re.M)
36
37 def ini_replace_vars():
38     """
39     Replace ``WIZARD_TMPDIR`` and ``WIZARD_SESSIONNAME`` with with user-specific values.
40     """
41     text = open('php.ini', "r").read()
42     text = text.replace('WIZARD_TMPDIR', '/mit/%s/web_scripts_tmp' % os.environ['USER'])
43     text = text.replace('WIZARD_SESSIONNAME', '%s_SID' % os.environ['USER'])
44     open('php.ini', "w").write(text)
45
46 seed = util.dictmap(_make_filename_regex, {
47         'WIZARD_SESSIONNAME': 'session.name',
48         'WIZARD_TMPDIR': ('upload_tmp_dir', 'session.save_path'),
49         })
50
51 #: Common extractors for parameters in :file:`php.ini`.
52 extractors = app.make_extractors(seed)
53 #: Common substitutions for parameters in :file:`php.ini`.
54 substitutions = app.make_substitutions(seed)
55 #: A list containing :file:`php.ini`.
56 parametrized_files = ["php.ini"]
57 #: Nop for consistency.
58 deprecated_keys = set([])
59