Skip to content

Commit

Permalink
Add validation for paths in config
Browse files Browse the repository at this point in the history
Previously, if a filename referred to in the configuration does not
exist, that would only be discovered when the hook which uses that key
runs, which may be minutes or hours into the build.

Add a way to specify in the schema that certain keys must be set to a
single path to an extant file, or a list of paths to extant files.

https://phabricator.endlessm.com/T35517
  • Loading branch information
wjt committed Jun 21, 2024
1 parent 27ff146 commit 1b9cb13
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ with key suffixes as follows:
* `_required`: `true` means that the key must be set
* `_values`: the value, if set, must be within the space-separated list
of values here
* `_type`: `path` means the value, if set, must be the path to a file which
exists
* `_type`: `paths` means the value, if set, must be a space-separated list of
path to files which exist

Merged options
--------------
Expand Down
3 changes: 3 additions & 0 deletions config/schema.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
# _required = true means that the key must be set
# _values means the value, if set, must be within the space-separated list of
# values here
# _type = path means the value, if set, must be the path to a file which exists
# _type = paths means the value, if set, must be a space-separated list of
# path to files which exist

[image]
compression_required = true
Expand Down
21 changes: 21 additions & 0 deletions run-build
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,27 @@ class ImageBuilder(object):
raise eib.ImageBuildError(
'Configuration key [%s] %s has invalid value: %s'
% (section, option, self.config[section][option]))
elif option.endswith('_type'):
real_option = option[:-len('_type')]
if real_option in self.config[section]:
real_value = self.config[section][real_option].strip()
match value:
case "path":
paths = [real_value] if real_value else []
case "paths":
paths = real_value.split()
case other:
raise eib.ImageBuildError(
f'Schema key [{section}] {option} has invalid value: '
f'{value}'
)

for path in paths:
if not os.path.exists(path):
raise eib.ImageBuildError(
f'Configuration key [{section}] {real_option} refers to '
f'nonexistent path: {path}'
)

def check_config(self):
"""Check loaded configuration against schema for validity."""
Expand Down

0 comments on commit 1b9cb13

Please sign in to comment.