Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

packlist: validate versions_url links #181

Merged
merged 5 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Install dependencies
run: |
sudo apt-get install -y python3-pip
pip3 install -r util/requirements.txt
- name: Validate examples
run: |
python3 util/validate-packs.py examples/packs.json
python3 util/validate-versions.py examples/versions.json
- name: Validate community-packs.json
run: |
python3 util/validate-packs.py community-packs.json
python3 util/validate-packs.py --follow-links community-packs.json
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# PopTracker Pack List

List of packs and their update URLs.
List of packs and their update URLs provided by the community.

Send PRs for `community-packs.json` on github to add your packs.
Send PRs for `community-packs.json` on GitHub to add your pack or remove a pack with wrong or outdated authorship.
You can use the pencil button to create a PR directly on GitHub.

## packs.json

Expand Down
51 changes: 41 additions & 10 deletions util/validate-packs.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,52 @@
#!/usr/bin/python

import json
from urllib.request import urlopen

import jsonschema

def validate(instance, schemafilename=None):
if not schemafilename:
schemafilename = os.path.join(os.path.dirname(__file__), '../schema/packs.schema.json')
with open(schemafilename, 'r') as schemafile:
jsonschema.validate(instance=instance, schema=json.load(schemafile))

def validate(instance, schema_filename=None, follow_links=False, versions_schema_filename=None, *,
instance_filename=None, verbose=False):
if not schema_filename:
schema_filename = os.path.join(os.path.dirname(__file__), "../schema/packs.schema.json")
if not versions_schema_filename:
versions_schema_filename = os.path.join(os.path.dirname(__file__), "../schema/versions.schema.json")
with open(schema_filename, "r") as schema_file:
if verbose:
print(f"Validating {instance_filename or 'json'}")
jsonschema.validate(instance=instance, schema=json.load(schema_file))
if follow_links:
with open(versions_schema_filename, "r") as versions_schema_file:
versions_schema = json.load(versions_schema_file)
for uid, pack in instance.items():
try:
versions_url = pack["versions_url"]
if verbose:
print(f"Validating {uid} versions: {versions_url}")
versions_data = json.load(urlopen(versions_url))
jsonschema.validate(instance=versions_data, schema=versions_schema)
except Exception as e:
raise ValueError(f"Error validating {versions_url} for {uid}") from e

return True

if __name__ == '__main__':

if __name__ == "__main__":
import argparse
import os
parser = argparse.ArgumentParser(description='Validate packs.json')
parser.add_argument('infile', type=argparse.FileType('r'))
parser.add_argument('--schema', type=str)

parser = argparse.ArgumentParser(description="Validate packs.json")
parser.add_argument("infile", type=argparse.FileType("r"))
parser.add_argument("--schema", type=str,
help="schema to use for validation")
parser.add_argument("--versions-schema", type=str, dest="versions_schema",
help="schema to use for version.json files if --follow-links is used")
parser.add_argument("--follow-links", action="store_true", dest="follow_links",
help="follow links and validate linked files.")
parser.add_argument("--verbose", action="store_true")
args = parser.parse_args()
validate(json.load(args.infile), args.schema)
validate(json.load(args.infile), args.schema, args.follow_links, args.versions_schema,
instance_filename=args.infile.name,
verbose=args.verbose, )
exit(0)