From 2a1c8f81e8ec30f3d53a817cab0e84b6d0339ac8 Mon Sep 17 00:00:00 2001 From: Richard Ipsum Date: Tue, 20 Jan 2015 11:15:51 +0000 Subject: Add script to check for duplicate lorries --- check | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100755 check diff --git a/check b/check new file mode 100755 index 0000000..37036dc --- /dev/null +++ b/check @@ -0,0 +1,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') -- cgit v1.2.1