pungi/bin/pungi-config-validate
Lubomír Sedlář f9a6c8418f Add JSON Schema for configuration
The schema is written in Python to reduce duplication. When
configuration is loaded, the validation checks if it's correct and fills
in default values.

There is a custom extension to the schema to report deprecated options.

The config dependencies are implemented as a separate pass. While it's
technically possible to express the dependencies in the schema itself,
the error messages are not very helpful and it makes the schema much
harder to read.

Phases no longer define `config_options`. New options should be added to
the schema. Since the default values are populated automatically during
validation, there is no need to duplicate them into the code.

The `pungi-config-validate` script is updated to use the schema and
report errors even for deeply nested fields.

The dependencies are updated: pungi now depends on `python-jsonschema`
(which is already available in Fedora).

Signed-off-by: Lubomír Sedlář <lsedlar@redhat.com>
2016-09-01 10:56:15 +02:00

127 lines
3.1 KiB
Python
Executable File

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import argparse
import contextlib
import kobo.conf
import os
import shutil
import sys
import tempfile
here = sys.path[0]
if here != '/usr/bin':
# Git checkout
sys.path[0] = os.path.dirname(here)
import pungi.compose
import pungi.checks
import pungi.paths
import pungi.phases
class ValidationCompose(pungi.compose.Compose):
def __init__(self, conf, has_old, topdir):
self.topdir = topdir
self.conf = conf
self._logger = None
self.just_phases = []
self.skip_phases = []
self.has_old_composes = has_old
self.paths = pungi.paths.Paths(self)
@property
def old_composes(self):
return '/dummy' if self.has_old_composes else None
@property
def compose_id(self):
return 'Dummy-1.0-20160811.t.0'
@property
def compose_type(self):
return 'test'
@property
def compose_date(self):
return '20160811'
@property
def compose_respin(self):
return '0'
@contextlib.contextmanager
def in_temp_dir():
tempdir = tempfile.mkdtemp()
yield tempdir
shutil.rmtree(tempdir)
def run(config, topdir, has_old):
conf = kobo.conf.PyConfigParser()
conf.load_from_file(config)
errors = pungi.checks.validate(conf)
if errors:
for error in errors:
print(error)
sys.exit(1)
compose = ValidationCompose(conf, has_old, topdir)
pkgset_phase = pungi.phases.PkgsetPhase(compose)
phases = [
pungi.phases.InitPhase(compose),
pungi.phases.BuildinstallPhase(compose),
pkgset_phase,
pungi.phases.GatherPhase(compose, pkgset_phase),
pungi.phases.ExtraFilesPhase(compose, pkgset_phase),
pungi.phases.CreaterepoPhase(compose),
pungi.phases.OstreeInstallerPhase(compose),
pungi.phases.OSTreePhase(compose),
pungi.phases.ProductimgPhase(compose, pkgset_phase),
pungi.phases.CreateisoPhase(compose),
pungi.phases.LiveImagesPhase(compose),
pungi.phases.LiveMediaPhase(compose),
pungi.phases.ImageBuildPhase(compose),
pungi.phases.ImageChecksumPhase(compose),
pungi.phases.TestPhase(compose),
]
errors = []
for phase in phases:
if phase.skip():
continue
try:
phase.validate()
except ValueError as ex:
for i in str(ex).splitlines():
errors.append("%s: %s" % (phase.name.upper(), i))
return errors
def main(args=None):
parser = argparse.ArgumentParser()
parser.add_argument('config', metavar='CONFIG',
help='configuration file to validate')
parser.add_argument('--old-composes', action='store_true',
help='indicate if pungi-koji will be run with --old-composes option')
opts = parser.parse_args(args)
with in_temp_dir() as topdir:
errors = run(opts.config, topdir, opts.old_composes)
for msg in errors:
print(msg)
return bool(errors)
if __name__ == '__main__':
if main():
sys.exit(1)