summaryrefslogtreecommitdiffstats
path: root/grandlyon/scripts/copy-wcs.py
blob: 84a7667acc2d84db07b6cde11afcfc6f03d533a7 (plain)
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
53
54
55
56
#! /usr/bin/env python
# usage: sudo -u wcs /usr/bin/wcsctl -f /etc/wcs/wcs-au-quotidien.cfg runscript --vhost=demarches-<...>.guichet-recette.grandlyon.com copy-wcs.py Oullins Genay

from quixote import get_publisher
from wcs.formdef import FormDef

import json
import os
import subprocess
import sys

from django.utils.text import slugify

orig_city = sys.argv[1]
slug_orig_city = slugify(orig_city)
dest_city = sys.argv[2]
slug_dest_city = slugify(dest_city)

if os.path.exists('/var/lib/combo/tenants/www.toodego.com'):
    orig_site = 'demarches-%s.toodego.com' % slug_orig_city
    dest_site = 'demarches-%s.toodego.com' % slug_dest_city
else:
    orig_site = 'demarches-%s.guichet-recette.grandlyon.com' % slug_orig_city
    dest_site = 'demarches-%s.guichet-recette.grandlyon.com' % slug_dest_city

roles_mapping = json.load(open('/tmp/roles.mapping.json'))

def copy(orig, dest, dest_city):
    if os.path.exists(dest):
        return  # do not overwrite
    if not os.path.isfile(orig):
        return  # ignore non files
    src = open(orig, 'rb').read()
    for role_id in roles_mapping.keys():
        src = src.replace(role_id.encode('ascii'), roles_mapping[role_id][dest_city].encode('ascii'))
    fd = open(dest, 'wb')
    fd.write(src)
    fd.close()

for object_type in ('formdefs', 'workflows', 'categories', 'datasources', 'models', 'wcscalls'):
    orig_dir = os.path.join('/var/lib/wcs/%s/%s/' % (orig_site, object_type))
    dest_dir = os.path.join('/var/lib/wcs/%s/%s/' % (dest_site, object_type))
    if not os.path.exists(orig_dir):
        continue
    if not os.path.exists(dest_dir):
        os.mkdir(dest_dir)
    for object_id in os.listdir(orig_dir):
        copy(os.path.join(orig_dir, object_id),
             os.path.join(dest_dir, object_id),
             dest_city)

# iterate over all objects and store them again to make sure side-code is run.
for formdef in FormDef.select():
    if orig_city in formdef.name:
        formdef.name = formdef.name.replace(orig_city, dest_city)
    formdef.store()