Skip to content

Commit

Permalink
ci: add localization/ linting
Browse files Browse the repository at this point in the history
# Motivation

In order to ensure that we prefix localization
keys with the correct prefix `br_`, we need to
have a more automatic way of checking this.

Since the localization keys are stored in `YAML`
files, it should be pretty easy to simply check
the keys in those files with a simple script.

However, the code in many of those files is not
compliant with the `YAML` standard, so we need to
fix that first.

# Changes

This commit adds a Github Action workflow that
ensures that the `YAML` files in the
`localization/` does not have syntax errors and
follow the configuration in
`scripts/lint/.yamllint.yml`.

# Future work

A bunch of files are not compliant, and should be
removed from the `--paths-to-ignore` list.
  • Loading branch information
trym-b committed Mar 9, 2024
1 parent b58f142 commit 7fdd9fe
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Main

on: push

jobs:
yamllint:
name: Lint localization/ with yamllint
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Install requirements
run: pip install --requirement scripts/lint/requirements.txt
- name: Lint
run: python scripts/lint/yaml.py --directory localization/
--paths-to-ignore
"localization/korean/ib_l_korean .yml"
"localization/english/romaioi_events_l_english.yml"
"localization/english/br_hippodrome_l_english.yml"
"localization/english/romaioi_names_l_english.yml"
"localization/english/romaioi_misc_l_english.yml"
"localization/english/addons/warispolitics_l_english.yml"
"localization/english/addons/ib_l_english.yml"
"localization/english/replace/country_flavor_text_l_english.yml"
"localization/english/replace/cultures_l_english.yml"
"localization/english/replace/misc_l_english.yml"
"localization/english/replace/countries_l_english.yml"
"localization/english/replace/hub_names_l_english.yml"
"localization/english/replace/inventions_l_english.yml"
32 changes: 32 additions & 0 deletions scripts/lint/.yamllint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---

yaml-files:
- '*.yaml'
- '*.yml'
- '.yamllint'

rules:
anchors: enable
braces: enable
brackets: enable
colons: enable
commas: enable
comments: disable
comments-indentation: disable
document-end: disable
document-start: disable
empty-lines: disable
empty-values: disable
float-values: disable
hyphens: enable
indentation: disable
key-duplicates: enable
key-ordering: disable
line-length: disable
new-line-at-end-of-file: disable
new-lines: enable
octal-values: disable
quoted-strings: disable
trailing-spaces: disable
truthy:
level: warning
1 change: 1 addition & 0 deletions scripts/lint/requirements.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yamllint
12 changes: 12 additions & 0 deletions scripts/lint/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#
# This file is autogenerated by pip-compile with Python 3.12
# by the following command:
#
# pip-compile scripts/lint/requirements.in
#
pathspec==0.12.1
# via yamllint
pyyaml==6.0.1
# via yamllint
yamllint==1.35.1
# via -r scripts/lint/requirements.in
59 changes: 59 additions & 0 deletions scripts/lint/yaml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from argparse import ArgumentParser, Namespace
from pathlib import Path
from subprocess import run
from sys import exit


def _args() -> Namespace:
parser = ArgumentParser()
parser.add_argument(
"--directory", required=True, type=Path, help="Directory to check"
)
parser.add_argument(
"--paths-to-ignore", type=Path, nargs="*", help="Paths to ignore"
)
return parser.parse_args()


def _main() -> None:
args = _args()
all_files = list(args.directory.rglob("*.yaml")) + list(
args.directory.rglob("*.yml")
)

paths_to_lint = []

if args.paths_to_ignore is None:
paths_to_lint = all_files
else:
for path in all_files:
if path not in args.paths_to_ignore:
paths_to_lint.append(path)

config_file = (Path(__file__).parent / ".yamllint.yml").relative_to(Path.cwd())

for file in paths_to_lint:
print(f"Linting {file}", flush=True)
command = [
"yamllint",
file,
"--strict",
"--config-file",
config_file,
]
result = run(command, text=True)
if result.returncode != 0:
print(
"Linting errors found, see the error above for more details", flush=True
)
print(
f"To run this test locally, run the following command:\n{' '.join(map(str, command))}",
flush=True,
)
exit(result.returncode)

print("YAML linting complete", flush=True)


if __name__ == "__main__":
_main()

0 comments on commit 7fdd9fe

Please sign in to comment.