File: validate_json.py

package info (click to toggle)
cataclysm-dda 0.H-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 710,808 kB
  • sloc: cpp: 524,019; python: 11,580; sh: 1,228; makefile: 1,169; xml: 507; javascript: 150; sql: 56; exp: 41; perl: 37
file content (37 lines) | stat: -rwxr-xr-x 1,070 bytes parent folder | download
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
#!/usr/bin/env python3

# Validate that all JSON files are syntactically correct;
# does NOT check indentation styles.

import glob
import json
import sys


def main():
    json_files = glob.glob('**/**.json', recursive=True)
    errors = 0
    total = 0
    for file_path in json_files:
        if not file_path.startswith("android/app/src/main/assets"):
            total += 1
            try:
                with open(file_path, encoding="utf-8") as fp:
                    _ = json.load(fp)
            except OSError as e:
                print("Error opening {}: {}".format(file_path, e))
                errors += 1
            except json.JSONDecodeError as e:
                print("Error parsing {}: {}".format(file_path, e))
                errors += 1
    if errors == 0:
        print("All {} JSON files healthy.".format(total))
        return 0
    else:
        print("Found {} erroneous files among {} JSON files in the repository."
              .format(errors, total))
        return min(errors, 255)


if __name__ == '__main__':
    sys.exit(main())