summaryrefslogtreecommitdiff
path: root/check
blob: 37036dc65d7bf79bdc8658bf807b29b873cd5541 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# check for duplicate lorries
#
# Copyright © 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

from __future__ import print_function

import json
import glob
import sys

LC_CONF_FILE = 'lorry-controller.conf'

lorry_urls_to_names = {}    # Map urls to names
duplicates_found = False

def find_globs():
    globs = []

    with open(LC_CONF_FILE) as f:
        maps = json.load(f) # config is a list of maps
        for m in maps:
            globs += m['globs']

    return globs

def get_url_set(lorry):
    url_set = set()

    if 'url' in lorry:
        url_set.add(lorry['url'])
    else:
        # Bazaar
        for _, url in lorry['branches'].iteritems():
            url_set.add(url)

    return url_set

def check_for_duplicates(lorry_file, lorries):
    global duplicates_found

    def msg((lorry, lorry_file)):
        return "'%s' in '%s'" % (lorry, lorry_file)

    for lorry in lorries:
        url_set = get_url_set(lorries[lorry])
        if verbose:
            print("\tlorry '%s' has url %s" % (lorry, url_set))

        for url in url_set:
            if url in lorry_urls_to_names:
                print("%s\nand\n%s\nhave the same url: '%s'\n"
                      % (msg((lorry, lorry_file)),
                         msg(lorry_urls_to_names[url]), url))

                duplicates_found = True
            else:
                lorry_urls_to_names[url] = (lorry, lorry_file)

if not (len(sys.argv) == 1 or len(sys.argv) == 2 and sys.argv[1] == '-v'):
    print('usage: %s [-v]' % sys.argv[0], file=sys.stderr)
    sys.exit(1)

verbose = len(sys.argv) == 2

for g in find_globs():
    for lorry_file in glob.glob(g):
        if verbose:
            print(lorry_file)

        with open(lorry_file) as f:
            check_for_duplicates(lorry_file, json.load(f))

if duplicates_found:
    print('\nERROR: Duplicates found', file=sys.stderr)
    sys.exit(1)
else:
    print('No duplicates found')