-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.py
52 lines (43 loc) · 1.74 KB
/
helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# -*- coding: utf-8 -*-
import os
import sys
sys.path[0:0] = [os.path.realpath('parts/omelette')]
from imio.pyutils.system import error
from imio.pyutils.system import read_file
from imio.pyutils.system import verbose
import argparse
def patch_instance(inst='instance-debug'):
idp = 'parts/{}/bin/interpreter'.format(inst)
if not os.path.exists(idp):
error("'{}' doesn't exist: cannot patch it".format(idp))
return False
lines = read_file(idp)
if 'ploneCustom.css' not in ''.join(lines):
sp = 0
for (i, line) in enumerate(lines):
if 'exec(_val)' in line:
nl = line.lstrip()
sp = len(line) - len(nl)
break
lines.insert(i, "{}{}".format(' ' * sp,
'_val = _val.replace("\'); from AccessControl.SpecialUsers import system '
'as user;", "/ploneCustom.css\'); from AccessControl.SpecialUsers import '
'system as user;")'))
verbose("=> Patching: '{}'".format(idp))
fh = open(idp, 'w')
fh.write('\n'.join(lines))
fh.close()
else:
verbose("=> Already patched: '{}'".format(idp))
functions = {'patch_instance': patch_instance}
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Call some helper function')
parser.add_argument('-f', '--function', dest='fct', help="Function to call",
choices=sorted(functions.keys()))
parser.add_argument('-i', '--instance', dest='inst', help="Instance to use")
ns = parser.parse_args()
if ns.fct:
kwargs = {}
if ns.inst:
kwargs['inst'] = ns.inst
functions[ns.fct](**kwargs)