Legoktm has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/192059

Change subject: Add jsonchecker.py
......................................................................

Add jsonchecker.py

Recursively scans directories for JSON files, verifies their syntax
and checks that they don't have any duplicate keys.

Upstream: https://github.com/legoktm/jsonchecker

Bug: T73284
Change-Id: Ife432a12932dcc1651cb6ef9d565d45f20501467
---
A bin/jsonchecker.py
1 file changed, 105 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/integration/jenkins 
refs/changes/59/192059/1

diff --git a/bin/jsonchecker.py b/bin/jsonchecker.py
new file mode 100755
index 0000000..c69983b
--- /dev/null
+++ b/bin/jsonchecker.py
@@ -0,0 +1,105 @@
+#!/usr/bin/env python
+"""
+Recursive JSON syntax checker that also checks
+for duplicate keys.
+
+https://github.com/legoktm/jsonchecker v0.7.0
+
+Released into the public domain.
+"""
+from __future__ import print_function
+
+from collections import defaultdict
+import json
+import os
+import sys
+
+
+class DuplicateKeyFinder:
+    def __init__(self, quiet=False):
+        self.errors = defaultdict(list)
+        self.invalids = {}
+        self.quiet = quiet
+
+    def mark_error(self, key, fname):
+        self.errors[fname].append(key)
+
+    def checker(self, seq, fname):
+        d = {}
+        for key, value in seq:
+            if key in d:
+                self.mark_error(key, fname)
+            else:
+                d[key] = value
+        return d
+
+    def check_directory(self, directory):
+        if directory == '.':
+            directory = os.getcwd()
+        if os.path.isdir(directory):
+            files = [os.path.join(directory, fname) for fname in 
os.listdir(directory)]
+        else:
+            files = [directory]
+        for fname in files:
+            if fname.startswith('.'):
+                continue
+            if os.path.isdir(fname):
+                self.check_directory(fname)
+                continue
+            if not (os.path.isfile(fname) and fname.endswith('.json')):
+                continue
+            if self.quiet:
+                print('.', end='')
+            else:
+                print('Checking %s...' % fname)
+            self.check_file(fname)
+
+    def check_file(self, fname):
+        """
+        Check the contents of the given file
+        """
+        with open(fname) as f:
+            text = f.read()
+        try:
+            json.loads(text, object_pairs_hook=lambda seq: self.checker(seq, 
fname))
+        except ValueError as e:
+            self.invalids[fname] = str(e)
+
+    def run(self, directories):
+        for directory in directories:
+            self.check_directory(directory)
+        self.exit()
+
+    def exit(self):
+        if self.errors or self.invalids:
+            if self.quiet:
+                print('')
+            for fname in self.errors:
+                print('----')
+                print('Duplicate keys found in %s:' % fname)
+                for key in self.errors[fname]:
+                    print('* %s' % key)
+            for fname, tb in self.invalids.items():
+                print('----')
+                print('Error while parsing %s:' % fname)
+                print(tb)
+            sys.exit(1)
+        else:
+            if self.quiet:
+                print('')
+            sys.exit(0)
+
+
+def main():
+    finder = DuplicateKeyFinder(quiet='--quiet' in sys.argv)
+    directories = []
+    for arg in sys.argv[1:]:
+        if not arg.startswith('--'):
+            directories.append(arg)
+    if not directories:
+        print('No files or directories provided')
+        sys.exit(1)
+    finder.run(directories)
+
+if __name__ == '__main__':
+    main()

-- 
To view, visit https://gerrit.wikimedia.org/r/192059
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ife432a12932dcc1651cb6ef9d565d45f20501467
Gerrit-PatchSet: 1
Gerrit-Project: integration/jenkins
Gerrit-Branch: master
Gerrit-Owner: Legoktm <legoktm.wikipe...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to