1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
#include <QDebug>
#include <QLocale>
#include "aptpackagebuilder.h"
#include "aptcacheparser.h"
#include "dpkgparser.h"
#include "helpers.h"
#include "runcommandforparsing.h"
#include "packagename.h"
#include "packagenotfoundexception.h"
namespace NApt {
AptCacheParser::AptCacheParser() {}
// splits into section and content
pair<string, string> splitSectionLine(const string& line) {
auto pos = line.find(": ");
return make_pair(line.substr(0, pos), line.substr(pos + 2));
}
void assignValueToSection(AptPackageBuilder& builder, const string& section, const string& content) {
static auto languageCode = QLocale::system().name().first(2);
static string localDescriptionSection = ("Description-" + languageCode).toStdString();
if (section == "Package") {
builder.setName(toQString(content));
} else if (section == "Name") {
builder.setName(toQString(content));
} else if (section == "Architecture") {
builder.setArchitecture(toQString(content));
} else if (
section == "Description" ||
section == "Description-en" ||
section == localDescriptionSection
) {
builder.setDescription(toQString(content));
} else if (section == "Version") {
builder.setVersion(toQString(content));
} else if (section == "Status") {
if (content.find("installed") != std::string::npos)
builder.setInstalledState(Package::INSTALLED);
else
builder.setInstalledState(Package::NOT_INSTALLED);
} else if (section == "Source") {
builder.setSource(toQString(content));
} else if (section == "Installed-Size") {
builder.setInstalledSize(toQString(content).toUInt());
} else if (section == "Maintainer") {
builder.setMaintainer(toQString(content));
} else if (section == "Depends") {
builder.setDepends(toQString(content));
} else if (section == "Replaces") {
builder.setReplaces(toQString(content));
} else if (section == "Provides") {
builder.setProvides(toQString(content));
} else if (section == "Pre-Depends") {
builder.setPreDepends(toQString(content));
} else if (section == "Recommends") {
builder.setRecommends(toQString(content));
} else if (section == "Suggests") {
builder.setSuggests(toQString(content));
} else if (section == "Conflicts") {
builder.setConflicts(toQString(content));
} else if (section == "Breaks") {
builder.setBreaks(toQString(content));
} else if (section == "Homepage") {
builder.setHomepage(toQString(content));
} else if (section == "Section") {
builder.setSection(toQString(content));
} else if (section == "Priority") {
builder.setPriority(toQString(content));
} else if (section == "Filename") {
builder.setFilename(toQString(content));
} else if (section == "Size") {
builder.setSize(toQString(content).toUInt());
} else if (section == "MD5sum") {
builder.setMd5sum(toQString(content));
} else if (section == "SHA256") {
builder.setSha256(toQString(content));
} else if (section == "Conffiles") {
builder.setConffiles(toQString(content));
} else if (section == "Description-md5") {
} else if (section == "Multi-Arch") {
} else if (section == "Tag") {
} else if (section == "Built-Using") {
} else if (section == "Enhances") {
} else if (section == "Sha1") {
} else if (section == "Sha512") {
} else if (section == "Enhances") {
} else if (section == "Bugs") {
} else if (true) {
// Those and some more like Static-Built-Using, X-Cargo-Built-Using, Gstreamer-Elements, ...
// Intentionally ignored:
// qDebug() << "Ignored section " << toQString(section);
}
}
void AptCacheParser::mergeIntoPackageMap(
const AptPackageBuilder& builder,
map<QString, unique_ptr<const IPackage>>& target
) {
static QString defaultArchitecture = toQString(PackageName::defaultArchitecture());
if (target.find(builder.name()) == target.end()){
target.insert({builder.name(), builder.createPackage()});
} else {
// prefer default architecture
if (builder.architecture() == defaultArchitecture) {
target.insert_or_assign(builder.name(), builder.createPackage());
}
}
}
map<QString, unique_ptr<const IPackage>> AptCacheParser::parseDumpAvail(
int packageCount,
const map<string, const DpkgParser::PackageInformation>& dpkgPackage,
NUtil::IProgressObserver& progressObserver
) {
auto packages = map<QString, unique_ptr<const IPackage>>();
auto lineProcessor = [&packages, &dpkgPackage, packageCount, &progressObserver](const string& line) {
static auto builder = AptPackageBuilder(dpkgPackage);
static string currentSection = "";
static string currentContent = "";
static int parsedPackageCount = 0;
if (line.empty()) {
assignValueToSection(builder, currentSection, currentContent);
currentSection = "";
currentContent = "";
mergeIntoPackageMap(builder, packages);
builder.clear();
++parsedPackageCount;
if (parsedPackageCount%500 == 0) {
progressObserver.setProgress(100 * parsedPackageCount / packageCount);
}
} else if (line[0] == ' ') {
// ignore multi-line content for dump-avail, there should be no
// such content expect for debtags or long description
// usually from third-party repos
} else {
auto sectionAndContent = splitSectionLine(line);
if (!currentSection.empty() && sectionAndContent.first != currentSection) {
assignValueToSection(builder, currentSection, currentContent);
currentSection = "";
currentContent = "";
}
currentSection = sectionAndContent.first;
currentContent = sectionAndContent.second;
}
};
NApplication::runCommandForParsing(
"apt-cache dumpavail |grep -E \"(^(Package:|Version:|Architecture:|Status:|Description:))|^$\"",
lineProcessor
);
return packages;
}
PackageDetails mergePackageDetails(const list<AptPackageBuilder>& input) {
assert(input.size() > 0);
if (input.size() == 1) return input.front().createPackageDetails();
auto installedPackage = std::find_if(input.begin(), input.end(), [](const AptPackageBuilder& builder) {
return builder.isInstalled();
});
// if we did find an installed package
if (installedPackage != input.end()) return installedPackage->createPackageDetails();
// else any guess is good, so take the first package in the list
else return input.front().createPackageDetails();
}
PackageDetails AptCacheParser::parseCacheShow(const::string& packageName) {
auto packageBuilders = list<AptPackageBuilder>();
auto lineProcessor = [&packageBuilders](const string& line) {
static auto builder = AptPackageBuilder();
static string currentSection = "";
static string currentContent = "";
if (line.empty()) {
assignValueToSection(builder, currentSection, currentContent);
currentSection = "";
currentContent = "";
packageBuilders.push_back(builder);
builder.clear();
} else // this line is a newline of a multiline entry
if (line[0] == ' ') {
currentContent += "\n" + line;
} else {
auto sectionAndContent = splitSectionLine(line);
if (!currentSection.empty() && sectionAndContent.first != currentSection) {
assignValueToSection(builder, currentSection, currentContent);
currentSection = "";
currentContent = "";
}
currentSection = sectionAndContent.first;
currentContent = sectionAndContent.second;
}
};
NApplication::runCommandForParsing(string("apt-cache show ") + packageName, lineProcessor);
if (packageBuilders.empty()) throw NPlugin::PackageNotFoundException(packageName);
return mergePackageDetails(packageBuilders);
}
}
|