From 9edf83ec0da2829a1a2792242fb92c6f1fe7a570 Mon Sep 17 00:00:00 2001 From: Laserlicht <13953785+Laserlicht@users.noreply.github.com> Date: Sat, 16 Sep 2023 20:24:15 +0200 Subject: [PATCH] json validation strict with py --- .github/ignore_json.py | 1 + .github/update_size.py | 4 ++-- .github/validate_json.py | 23 +++++++++++++++++++++++ .github/validate_mod_json.py | 6 +++--- .github/workflows/main.yml | 16 +++++++++------- 5 files changed, 38 insertions(+), 12 deletions(-) create mode 100644 .github/ignore_json.py create mode 100644 .github/validate_json.py diff --git a/.github/ignore_json.py b/.github/ignore_json.py new file mode 100644 index 0000000..4205d5b --- /dev/null +++ b/.github/ignore_json.py @@ -0,0 +1 @@ +ignore = [ "./github.json", "./vcmi-1.2.json", "./vcmi-1.3.json" ] \ No newline at end of file diff --git a/.github/update_size.py b/.github/update_size.py index 64ce921..a419b5a 100644 --- a/.github/update_size.py +++ b/.github/update_size.py @@ -4,7 +4,7 @@ import sys import urllib.request -ignore = [ "./github.json", "./vcmi-1.2.json", "./vcmi-1.3.json" ] +from ignore_json import ignore for filename in glob.glob(os.path.join('.', '*.json')): if filename not in ignore: @@ -12,7 +12,7 @@ filecontent = open(filename, "r").read() modlist = json.loads(filecontent) for mod, data in modlist.items(): - url = data["download"] + url = data["download"].replace(" ", "%20") print(f"Download {mod}: {url}") try: response = urllib.request.urlopen(url) diff --git a/.github/validate_json.py b/.github/validate_json.py new file mode 100644 index 0000000..6847d04 --- /dev/null +++ b/.github/validate_json.py @@ -0,0 +1,23 @@ +import json +import glob +import os +import sys +import urllib.request + +from ignore_json import ignore + +error = False + +for filename in glob.glob(os.path.join('.', '*.json')): + if filename not in ignore: + print(f"Opening: {filename}") + filecontent = open(filename, "r").read() + + try: + json.loads(filecontent) + except Exception as err: + print("Error: " + str(err)) + sys.exit(os.EX_SOFTWARE) + +print("Everything is ok!") +sys.exit(os.EX_OK) \ No newline at end of file diff --git a/.github/validate_mod_json.py b/.github/validate_mod_json.py index 7e80feb..300a499 100644 --- a/.github/validate_mod_json.py +++ b/.github/validate_mod_json.py @@ -4,7 +4,7 @@ import sys import urllib.request -ignore = [ "./github.json", "./vcmi-1.2.json", "./vcmi-1.3.json" ] +from ignore_json import ignore error = False @@ -14,7 +14,7 @@ filecontent = open(filename, "r").read() modlist = json.loads(filecontent) for mod, data in modlist.items(): - url = data["mod"] + url = data["mod"].replace(" ", "%20") print(f"Download {mod}: {url}") try: response = urllib.request.urlopen(url) @@ -28,7 +28,7 @@ json.loads(filecontent) except Exception as err: error = True - print("Error: " + err) + print("Error: " + str(err)) continue if error: sys.exit(os.EX_SOFTWARE) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index b9257c5..7dfc86f 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -12,16 +12,18 @@ on: workflow_dispatch: jobs: - check_json: + validate_json: runs-on: ubuntu-latest + defaults: + run: + shell: bash steps: - uses: actions/checkout@v3 - - name: json-syntax-check - uses: limitusus/json-syntax-check@v2 - with: - pattern: "\\.json$" + - name: Validate json + run: | + python3 .github/validate_json.py validate_mod_json: - needs: check_json + needs: validate_json runs-on: ubuntu-latest defaults: run: @@ -32,7 +34,7 @@ jobs: run: | python3 .github/validate_mod_json.py update_size: - needs: check_json + needs: validate_json runs-on: ubuntu-latest defaults: run: