#!/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')