From 80749e2b5b7a9ece444e41375809ce55bdd59a86 Mon Sep 17 00:00:00 2001 From: Nav Date: Tue, 1 Jun 2021 23:54:04 +0100 Subject: [PATCH] Added TDF validation script --- .../TargetDescriptionFile.php | 102 ++++++++++++++++++ .../TargetDescriptionFiles/Validate.php | 57 ++++++++++ .../TargetDescriptionFiles/Variant.php | 27 +++++ 3 files changed, 186 insertions(+) create mode 100644 build/scripts/TargetDescriptionFiles/TargetDescriptionFile.php create mode 100644 build/scripts/TargetDescriptionFiles/Validate.php create mode 100644 build/scripts/TargetDescriptionFiles/Variant.php diff --git a/build/scripts/TargetDescriptionFiles/TargetDescriptionFile.php b/build/scripts/TargetDescriptionFiles/TargetDescriptionFile.php new file mode 100644 index 00000000..27383456 --- /dev/null +++ b/build/scripts/TargetDescriptionFiles/TargetDescriptionFile.php @@ -0,0 +1,102 @@ +filePath = $filePath; + $this->init(); + } + + private function init() + { + if (!file_exists($this->filePath)) { + throw new Exception("Invalid TDF file path - file does not exist."); + } + + $xml = simplexml_load_file($this->filePath); + if ($xml === false) { + throw new Exception("Failed to parse TDF XML."); + } + + $this->xml = $xml; + + /** @var SimpleXMLElement[] $devices */ + $devices = (array) $xml->devices; + if (!empty($devices)) { + $device = reset($devices); + $deviceAttributes = $device->attributes(); + + if (!empty($deviceAttributes['name'])) { + $this->targetName = $device['name']; + } + + if (!empty($deviceAttributes['architecture'])) { + $this->targetArchitecture = $device['architecture']; + } + } + + $variantElements = $xml->xpath('variants/variant'); + + foreach ($variantElements as $variantElement) { + $variantAttributes = $variantElement->attributes(); + $variant = new Variant(); + + if (!empty($variantAttributes['ordercode'])) { + $variant->name = $variantAttributes['ordercode']; + } + + if (!empty($variantAttributes['name'])) { + $variant->name = $variantAttributes['name']; + } + + if (!empty($variantAttributes['package'])) { + $variant->package = $variantAttributes['package']; + } + + if (!empty($variantAttributes['pinout'])) { + $variant->pinout = $variantAttributes['pinout']; + } + + $this->variants[] = $variant; + } + } + + public function validate(): array + { + $failures = []; + + if (empty($this->targetName)) { + $failures[] = 'Target name not found'; + } + + if (empty($this->targetArchitecture)) { + $failures[] = 'Target architecture not found'; + } + + if (empty($this->variants)) { + $failures[] = 'Missing target variants'; + } + + foreach ($this->variants as $variant) { + $variantValidationFailures = $variant->validate(); + + if (!empty($variantValidationFailures)) { + $failures[] = 'Variant validation failures: ' . implode(", ", $variantValidationFailures); + } + } + + return $failures; + } +} diff --git a/build/scripts/TargetDescriptionFiles/Validate.php b/build/scripts/TargetDescriptionFiles/Validate.php new file mode 100644 index 00000000..1da30e59 --- /dev/null +++ b/build/scripts/TargetDescriptionFiles/Validate.php @@ -0,0 +1,57 @@ +isFile() && $entry->getExtension() == 'xml') { + $output[] = clone $entry; + + } else if ($entry->isDir() && !$entry->isDot()) { + $output = array_merge($output, getXmlFiles($entry->getPathname())); + } + } + + return $output; +} + +$xmlFiles = getXmlFiles(TDF_DIR_PATH); +$failedValidationCount = 0; + +foreach ($xmlFiles as $xmlFile) { + $targetDescriptionFile = new TargetDescriptionFile($xmlFile->getPathname()); + $validationFailures = $targetDescriptionFile->validate(); + + if (!empty($validationFailures)) { + $failedValidationCount++; + + print "\033[31m"; + print "Validation for " . $xmlFile->getFilename() . " failed.\n"; + print "Full path: " . $xmlFile->getRealPath() . "\n"; + print count($validationFailures) . " errors found:\n"; + print implode("\n", $validationFailures); + print "\n\n"; + print "\033[0m"; + + } else { + print "\033[32m"; + print "Validation for " . $xmlFile->getFilename() . " passed.\n"; + print "\033[0m"; + } +} + +print "\n\n"; +print "Validated " . count($xmlFiles) . " TDFs. "; +print (($failedValidationCount > 0) ? "\033[31m" : "\033[32m"); +print $failedValidationCount . " failures." . "\033[0m" . "\n"; +echo "Done\n"; diff --git a/build/scripts/TargetDescriptionFiles/Variant.php b/build/scripts/TargetDescriptionFiles/Variant.php new file mode 100644 index 00000000..a8a18f78 --- /dev/null +++ b/build/scripts/TargetDescriptionFiles/Variant.php @@ -0,0 +1,27 @@ +name)) { + $failures[] = 'Name not found'; + } + + if ($this->name == "standard") { + $failures[] = 'Name set to "standard" - needs attention'; + } + + if (empty($this->package)) { + $failures[] = 'Package not found'; + } + + return $failures; + } +}