summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Ipsum <richard.ipsum@codethink.co.uk>2015-01-20 11:15:51 +0000
committerRichard Ipsum <richard.ipsum@codethink.co.uk>2015-01-20 15:30:48 +0000
commit2a1c8f81e8ec30f3d53a817cab0e84b6d0339ac8 (patch)
tree71d483ff38f18247eeee1d4f5e221eda851b43ec
parent6dd07c2310eac603515140d12a0380b8274816e3 (diff)
downloadlorries-2a1c8f81e8ec30f3d53a817cab0e84b6d0339ac8.tar.gz
Add script to check for duplicate lorries
-rwxr-xr-xcheck93
1 files changed, 93 insertions, 0 deletions
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')