summaryrefslogtreecommitdiff
path: root/ql-ikiwiki-publish
blob: 927c83e51b34d487630a56080ec277271273d22b (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
57
58
59
60
61
#!/usr/bin/env python
# Copyright 2016  QvarnLabs Ab
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# =*= License: GPL-3+ =*=


import os
import shutil
import subprocess
import sys
import tempfile


def mangle_setup(src, dest, html):
    with open(src) as f:
        text = f.read()

    lines = [
        line
        for line in text.splitlines()
        if not line.startswith('destdir:')
    ]

    lines.append('destdir: {}'.format(html))

    mangled = ''.join(line + '\n' for line in lines)

    with open(dest, 'w') as f:
        f.write(mangled)


static_http = sys.argv[1]
dirname = sys.argv[2]
rsync_target = 'static@{}:/srv/http/{}/.'.format(static_http, dirname)

T = tempfile.mkdtemp()
try:
    setup = os.path.join(T, 'ikiwiki.setup')
    html = os.path.join(T, 'html')
    mangle_setup('ikiwiki.setup', setup, html)
    subprocess.check_call(['ikiwiki', '--setup', setup])
    subprocess.check_call(
        ['rsync', '-ahHSvs', '--delete', html + '/.', rsync_target])
except BaseException as e:
    shutil.rmtree(T)
    raise
else:
    shutil.rmtree(T)