summaryrefslogtreecommitdiff
path: root/scripts/check-unpetrify-refs.py
blob: 36eaa2d7c90245bb125c6423f0858a506b384997 (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
74
75
#!/usr/bin/env python
# Copyright (C) 2016 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import os
import sys
import glob
import subprocess

import scriptslib


'''
Script for checking unpetrify-refs in strata.

Without args this script will check everything in strata/, or each stratum
given on the command-line and will print on stdout whether a chunk has
a missing or non-existent unpetrify-ref and if it fails to check the remote
(missing repo).
'''

strata_dir = "strata"
trove_host = "git.baserock.org"

def ref_exists(remote, ref):
    output = subprocess.check_output(
        ["git", "ls-remote", remote, str(ref)],
        stderr=subprocess.STDOUT).strip()
    return True if output else False

def main(args):
    global trove_host
    opt = next(((i, j.split('=')[1]) for i, j in enumerate(args)
              if j.startswith("--trove-host=")), None)
    if opt:
        trove_host = opt[1]
        del args[opt[0]]
    if args:
        strata = args
    else:
        strata_path = os.path.join(scriptslib.definitions_root(), strata_dir)
        strata = glob.glob("%s/*.morph" % strata_path)

    for stratum in strata:
        path = os.path.relpath(stratum)
        morphology = scriptslib.load_yaml_file(stratum)
        for chunk in morphology['chunks']:
            unpetrify_ref = chunk.get("unpetrify-ref")
            if not unpetrify_ref:
                print ("%s: '%s' has no unpetrify-ref!" %
                       (path, chunk['name']))
                continue
            remote = scriptslib.parse_repo_alias(chunk['repo'], trove_host)
            try:
                if not ref_exists(remote, unpetrify_ref):
                    print ("%s: unpetrify-ref for '%s' is not present on the "
                           "remote (%s)!" % (path, chunk['name'], remote))
            except subprocess.CalledProcessError as e:
                print ("%s: failed to ls-remote (%s) for chunk '%s':\n%s" %
                       (path, remote, chunk['name'], e.output.strip()))

if __name__ == "__main__":
    main(sys.argv[1:])