Skip to content

Instantly share code, notes, and snippets.

@ahamez
Created August 24, 2018 09:55
Show Gist options
  • Save ahamez/af7ee674518c1aa3b534c30cba950a39 to your computer and use it in GitHub Desktop.
Save ahamez/af7ee674518c1aa3b534c30cba950a39 to your computer and use it in GitHub Desktop.
Merge FOSS licenses from Yoco MANIFEST files.
// clang++ -I. -std=c++17 -Wall -Wextra yocto_merge_manifest_licenses.cc
#include <fstream>
#include <iostream>
#include <regex>
#include <set>
#include <string>
#include <tuple>
#include <boost/algorithm/string/replace.hpp>
const auto regex = std::regex{
"PACKAGE NAME: (.+)\\nPACKAGE VERSION: (.+)\\nRECIPE NAME: (.+)\\nLICENSE: (.+)\\n\n"
};
struct package
{
package(std::string n, std::string v, std::string l)
: name{n}, version{v}, license{l}
{}
std::string name;
std::string version;
std::string license;
friend
bool
operator<(const package& lhs, const package& rhs)
noexcept
{
return std::tie(lhs.name, lhs.version, lhs.license)
< std::tie(rhs.name, rhs.version, rhs.license);
}
};
std::string
process_license(std::string license)
{
boost::replace_all(license, "PD", "Public Domaine");
boost::replace_all(license, "&", "and");
boost::replace_all(license, "|", "or");
return license;
}
int main(int argc, const char** argv)
{
if (argc < 2)
{
std::cerr << argv[0] << " manifests+";
std::exit(1);
}
auto packages = std::set<package>{};
for (auto i = 1; i < argc; ++i)
{
if (auto file = std::ifstream{argv[i]}; not file.is_open())
{
std::cerr << "Cannot open " << argv[i] << '\n';
std::exit(1);
}
else
{
const auto str = std::string{
std::istreambuf_iterator<char>{file},
std::istreambuf_iterator<char>{}
};
for (auto begin = cbegin(str); begin != cend(str);)
{
const auto [name, version, license, length] = [&]
{
auto match = std::smatch{};
std::regex_search(begin, cend(str), match, regex);
return std::tuple{match[1].str(), match[2].str(), match[4].str(), match.length()};
}();
if (license != "CLOSED" and license != "Proprietary")
{
packages.emplace(name, version, process_license(license));
}
begin += length;
}
}
}
for (const auto& [name, version, license] : packages)
{
std::cout << name << ";" << version << ";" << license << '\n';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment