From e004cebd17f4414cc67eafe6588078c4042570f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucie=20G=C3=A9rard?= Date: Thu, 31 Aug 2023 13:22:26 +0200 Subject: Change qtattributionsscanner to accept string arrays for Copyright Task-number: QTBUG-111873 Change-Id: Ibdc03955d6e5942deb6dab37a70fdaefec68ccec Reviewed-by: Joerg Bornemann (cherry picked from commit 687bbd584cc36fb8d213cb99a9098178e3701ce8) Reviewed-by: Qt Cherry-pick Bot --- src/qtattributionsscanner/scanner.cpp | 36 +++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/src/qtattributionsscanner/scanner.cpp b/src/qtattributionsscanner/scanner.cpp index 460e21a7f..f936ec0d2 100644 --- a/src/qtattributionsscanner/scanner.cpp +++ b/src/qtattributionsscanner/scanner.cpp @@ -124,6 +124,20 @@ static std::optional toStringList(const QJsonValue &value) return result; } +static std::optional arrayToMultiLineString(const QJsonValue &value) +{ + if (!value.isArray()) + return std::nullopt; + QString result; + for (const auto &iter : value.toArray()) { + if (iter.type() != QJsonValue::String) + return std::nullopt; + result.append(iter.toString()); + result.append(QLatin1StringView("\n")); + } + return result; +} + // Extracts SPDX license ids from a SPDX license expression. // For "(BSD-3-Clause AND BeerWare)" this function returns { "BSD-3-Clause", "BeerWare" }. static QStringList extractLicenseIdsFromSPDXExpression(QString expression) @@ -211,7 +225,8 @@ static std::optional readPackage(const QJsonObject &object, const QStri const QString key = iter.key(); if (!iter.value().isString() && key != "QtParts"_L1 && key != "SecurityCritical"_L1 - && key != "Files"_L1 && key != "LicenseFiles"_L1 && key != "Comment"_L1) { + && key != "Files"_L1 && key != "LicenseFiles"_L1 && key != "Comment"_L1 + && key != "Copyright"_L1) { if (logLevel != SilentLog) std::cerr << qPrintable(tr("File %1: Expected JSON string as value of %2.").arg( QDir::toNativeSeparators(filePath), key)) << std::endl; @@ -273,7 +288,24 @@ static std::optional readPackage(const QJsonObject &object, const QStri for (const auto &iter : std::as_const(strings.value())) p.licenseFiles.push_back(dir.absoluteFilePath(iter)); } else if (key == "Copyright"_L1) { - p.copyright = value; + QJsonValueConstRef jsonValue = iter.value(); + if (jsonValue.isArray()) { + // Array joined with new lines + auto maybeString = arrayToMultiLineString(jsonValue); + if (maybeString) + p.copyright = maybeString.value(); + } else if (jsonValue.isString()) { + // Legacy format: multiple values separated by space in one string. + p.copyright = value; + } else { + if (logLevel != SilentLog) { + std::cerr << qPrintable(tr("File %1: Expected JSON array of string or" + "string as value of %2.").arg( + QDir::toNativeSeparators(filePath), key)) << std::endl; + validPackage = false; + continue; + } + } } else if (key == "CopyrightFile"_L1) { p.copyrightFile = QDir(directory).absoluteFilePath(value); } else if (key == "PackageComment"_L1) { -- cgit v1.2.3