Runner: Merge development into master for release
[slapos.git] / software / unstable / mioga / mioga-hooks.py
1
2 import fnmatch
3 import grp
4 import os
5 import pprint
6 import pwd
7 import re
8 import shutil
9 import sys
10
11 class FileModifier:
12 def __init__(self, filename):
13 self.filename = filename
14 f = open(filename, 'rb')
15 self.content = f.read()
16 f.close()
17
18 def modify(self, key, value):
19 (self.content, count) = re.subn(
20 r'(<parameter[^>]*\sname\s*=\s*"' + re.escape(key) + r'"[^>]*\sdefault\s*=\s*")[^"]*',
21 r"\g<1>" + value,
22 self.content)
23 return count
24
25 def save(self):
26 f = open(self.filename, 'w')
27 f.write(self.content)
28 f.close()
29
30
31 def pre_configure_hook(options, bo, env):
32 location = options['location']
33
34 # TODO: double-check which one of these values must be set
35 # at instantiation time!
36
37 fm = FileModifier('conf/Config.xml')
38 fm.modify('apache_user', pwd.getpwuid(os.getuid())[0])
39 fm.modify('apache_group', grp.getgrgid(os.getgid())[0])
40 mioga_base = os.path.join(location, 'var', 'lib', 'Mioga2')
41 fm.modify('install_dir', mioga_base)
42 fm.modify('tmp_dir', os.path.join(mioga_base, 'tmp'))
43 fm.modify('search_tmp_dir', os.path.join(mioga_base, 'mioga_search'))
44 fm.modify('maildir', os.path.join(location, 'var', 'spool', 'mioga', 'maildir'))
45 fm.modify('maildirerror', os.path.join(location, 'var', 'spool', 'mioga', 'error'))
46 fm.modify('mailfifo', os.path.join(location, 'var', 'spool', 'mioga', 'fifo'))
47 fm.save()
48
49 # TODO: mail settings are certainly wrong, what is the domain name?
50
51 # Correct shebangs to the right Perl
52 for root, dirnames, filenames in os.walk('.'):
53 for filename in fnmatch.filter(filenames, '*.pl'):
54 with open(os.path.join(root, filename), "r+") as f:
55 lines = f.readlines()
56 (lines[0], count) = re.subn(r'^#!/usr/bin/perl',
57 '#!' + options['perl-binary'],
58 lines[0], 1)
59 if count > 0:
60 f.seek(0)
61 f.writelines(lines)
62 print "Corrected interpreter for script "+filename
63
64
65
66 # def post_make_hook(options, buildout):
67 # location = options['location']
68 # print "Mioga - postmakehook"
69 # print "We are currently in", os.getcwd()
70 # shutil.move("var", location)
71 # return None