Skip to content

Commit d39bca7

Browse files
committed
Validate schema before it is loaded
1 parent ae5fe0e commit d39bca7

4 files changed

Lines changed: 76 additions & 6 deletions

File tree

openandroidinstaller/installer_config.py

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
from pathlib import Path
1717
from typing import List, Optional
18+
import schema
19+
from schema import Regex, Schema, SchemaError
1820

1921
import yaml
2022
from loguru import logger
@@ -56,11 +58,16 @@ def from_file(cls, path):
5658
with open(path, "r") as stream:
5759
try:
5860
raw_config = yaml.safe_load(stream)
59-
config = dict(raw_config)
60-
raw_steps = config["steps"]
61-
metadata = config["metadata"]
61+
if validate_config(raw_config):
62+
config = dict(raw_config)
63+
raw_steps = config["steps"]
64+
metadata = config["metadata"]
65+
else:
66+
logger.info("Validation of config failed.")
67+
return None
6268
except yaml.YAMLError as exc:
6369
logger.info(exc)
70+
return None
6471

6572
if raw_steps.get("unlock_bootloader") is not None:
6673
unlock_bootloader = [
@@ -92,8 +99,42 @@ def _load_config(device_code: str, config_path: Path) -> Optional[InstallerConfi
9299
try:
93100
config = InstallerConfig.from_file(path)
94101
logger.info(f"Loaded device config from {path}.")
95-
logger.info(f"Config metadata: {config.metadata}.")
102+
if config:
103+
logger.info(f"Config metadata: {config.metadata}.")
96104
return config
97105
except FileNotFoundError:
98106
logger.info(f"No device config found for {path}.")
99107
return None
108+
109+
110+
def validate_config(config: str) -> bool:
111+
"""Validate the schema of the config."""
112+
113+
step_schema = {
114+
"title": str,
115+
"type": Regex(r"text|confirm_button|call_button|call_button_with_input"),
116+
"content": str,
117+
schema.Optional("command"): Regex(r"^adb\s|^fastboot\s|^heimdall\s"),
118+
schema.Optional("allow_skip"): bool,
119+
}
120+
121+
config_schema = Schema({
122+
"metadata": {
123+
"maintainer": str,
124+
"devicename": str,
125+
"devicecode": str,
126+
},
127+
"requirements": schema.Or(None, []),
128+
"steps": {
129+
"unlock_bootloader": schema.Or(None, [step_schema]),
130+
"flash_recovery": [step_schema],
131+
"install_os": [step_schema],
132+
}
133+
})
134+
try:
135+
config_schema.validate(config)
136+
logger.info("Config is valid.")
137+
return True
138+
except SchemaError as se:
139+
logger.info(f"Config is invalid. Error {se}")
140+
return False

openandroidinstaller/openandroidinstaller.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
# Toggle to True for development purposes
3535
DEVELOPMENT = True
36-
DEVELOPMENT_CONFIG = "FP2" # "a3y17lte" # "sargo"
36+
DEVELOPMENT_CONFIG = "sargo" # "a3y17lte" # "sargo"
3737

3838

3939
PLATFORM = sys.platform
@@ -217,6 +217,7 @@ def search_devices(self, e):
217217
else:
218218
# failed to load config
219219
logger.info(f"Failed to load config for {device_code}.")
220+
self.device_name.value = f"Failed to load config for {device_code}."
220221
self.view.update()
221222

222223

poetry.lock

Lines changed: 28 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ loguru = "^0.6.0"
1919
requests = "^2.28.1"
2020
pytest = "^7.1.3"
2121
regex = "^2022.9.13"
22+
schema = "^0.7.5"
2223

2324
[tool.poetry.dev-dependencies]
2425

0 commit comments

Comments
 (0)