|
1 |
#!/usr/bin/python3
|
|
2 |
|
|
3 |
"""Ce script permet de modifier un fichier settings.php d’un site. Il prend le chemin du settings en paramètre.
|
|
4 |
"""
|
|
5 |
|
|
6 |
import re
|
|
7 |
import sys
|
|
8 |
import argparse
|
|
9 |
import os
|
|
10 |
|
|
11 |
def modify_settings(args):
|
|
12 |
settings_path = args.settings
|
|
13 |
settings_new_path = settings_path + '.new'
|
|
14 |
|
|
15 |
regexp = "'{}' => '(.*)'"
|
|
16 |
|
|
17 |
with open(settings_path, 'r') as settings:
|
|
18 |
with open(settings_new_path, 'w') as settings_new:
|
|
19 |
for ligne in settings:
|
|
20 |
ligne = re.sub(regexp.format('password'),\
|
|
21 |
"'password' => '{}'".format(args.password), ligne)
|
|
22 |
if args.database:
|
|
23 |
ligne = re.sub(regexp.format('database'),\
|
|
24 |
"'database' => '{}'".format(args.database), ligne)
|
|
25 |
ligne = re.sub(regexp.format('username'),\
|
|
26 |
"'username' => '{}'".format(args.user), ligne)
|
|
27 |
ligne = re.sub(regexp.format('host'),\
|
|
28 |
"'host' => '{}'".format(args.host), ligne)
|
|
29 |
if args.port:
|
|
30 |
ligne = re.sub(regexp.format('port'),\
|
|
31 |
"'port' => '{}'".format(args.port), ligne)
|
|
32 |
if args.prefix:
|
|
33 |
ligne = re.sub(regexp.format('prefix'),\
|
|
34 |
"'prefix' => '{}'".format(args.prefix), ligne)
|
|
35 |
if args.baseurl:
|
|
36 |
ligne = re.sub("\$base_url = (.*)",\
|
|
37 |
"$base_url = '{}'; // NO trailing slash!".format(args.baseurl), ligne)
|
|
38 |
settings_new.write(ligne)
|
|
39 |
os.chmod(settings_path, 700)
|
|
40 |
os.remove(settings_path)
|
|
41 |
os.rename(settings_new_path, settings_path)
|
|
42 |
os.chmod(settings_path, 600)
|
|
43 |
|
|
44 |
if __name__ == '__main__':
|
|
45 |
parser = argparse.ArgumentParser(description=__doc__)
|
|
46 |
parser.add_argument('settings', metavar='settings', help='The path to the settings.')
|
|
47 |
parser.add_argument('--user', '-u', dest='user', default='root', help='Database user')
|
|
48 |
parser.add_argument('--password', '-p', dest='password', default='', help='Database password')
|
|
49 |
parser.add_argument('--database', dest='database', help='New database name')
|
|
50 |
parser.add_argument('--host', dest='host', default='localhost', help='The new host of the database.')
|
|
51 |
parser.add_argument('--prefix', dest='prefix', help='The prefix for the database.')
|
|
52 |
parser.add_argument('--port', dest='port', help='The database port.')
|
|
53 |
parser.add_argument('--baseurl', dest='baseurl', help='The new base url')
|
|
54 |
args = parser.parse_args()
|
|
55 |
modify_settings(args)
|
These script synchronises the site and make the proper modification to its settings.php