summaryrefslogtreecommitdiff
path: root/migrations/run-all
blob: 5a817ee536e39a18557556d411cc913be2dcf28e (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
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python
# Copyright (C) 2015  Codethink Limited
#
# 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; version 2 of the License.
#
# 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/>.


'''Run a set of migration scripts.

This script does exactly what `PYTHONPATH=. run-parts --exit-on-error` would
do. I avoided using 'run-parts' purely because the implementation in Fedora 22
doesn't have an '--exit-on-error' option. The Busybox and Debian
implementations do have that option.

Please fix run-parts in https://git.fedorahosted.org/cgit/crontabs.git/tree/ 
so we can simplify this script :-)

'''


import os
import subprocess
import sys


if len(sys.argv) == 2:
    migration_dir = sys.argv[1]
elif len(sys.argv) == 1:
    migration_dir = os.path.dirname(__file__)
else:
    sys.stderr.write("Usage: %s [MIGRATION_DIR]\n" % sys.argv[0])
    sys.exit(1)


def is_executable(fpath):
    return os.path.isfile(fpath) and os.access(fpath, os.X_OK)

env = os.environ
if 'PYTHONPATH' in env:
    env['PYTHONPATH'] = env['PYTHONPATH'] + ':' + migration_dir
else:
    env['PYTHONPATH'] = migration_dir

try:
    migrations_found = 0
    for fname in sorted(os.listdir(migration_dir)):
        migration_fpath = os.path.join(migration_dir, fname)
        if is_executable(migration_fpath):
            if not os.path.samefile(migration_fpath, __file__) and \
                    fname != 'indent':
                migrations_found += 1
                sys.stdout.write(migration_fpath + ":\n")
                subprocess.check_call(
                    migration_fpath, env=env)

    if migrations_found == 0:
        sys.stderr.write("No migration files found in '%s'\n" % migration_dir)
        sys.exit(1)
    else:
        sys.exit(0)

except (subprocess.CalledProcessError, RuntimeError) as e:
    sys.stderr.write(str(e) + '\n')
    sys.exit(1)