source: appstream-generator/src/asgen/backends/archlinux/alpkgindex.d @ 4841

Last change on this file since 4841 was 4841, checked in by Juanma, 2 years ago

Initial release

File size: 4.9 KB
Line 
1/*
2 * Copyright (C) 2016 Matthias Klumpp <matthias@tenstral.net>
3 *
4 * Licensed under the GNU Lesser General Public License Version 3
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the license, or
9 * (at your option) any later version.
10 *
11 * This software is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this software.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20module asgen.backends.archlinux.alpkgindex;
21
22import std.stdio;
23import std.path;
24import std.string;
25import std.algorithm : canFind;
26import std.array : appender;
27static import std.file;
28
29import asgen.logging;
30import asgen.zarchive;
31import asgen.backends.interfaces;
32import asgen.backends.archlinux.alpkg;
33import asgen.backends.archlinux.listfile;
34
35
36final class ArchPackageIndex : PackageIndex
37{
38
39private:
40    string rootDir;
41    Package[][string] pkgCache;
42
43public:
44
45    this (string dir)
46    {
47        this.rootDir = dir;
48        if (!std.file.exists (dir))
49            throw new Exception ("Directory '%s' does not exist.", dir);
50    }
51
52    void release ()
53    {
54        pkgCache = null;
55    }
56
57    private void setPkgDescription (ArchPackage pkg, string pkgDesc)
58    {
59        if (pkgDesc is null)
60            return;
61
62        auto desc = "<p>%s</p>".format (pkgDesc);
63        pkg.setDescription (desc, "C");
64    }
65
66    private Package[] loadPackages (string suite, string section, string arch)
67    {
68        auto pkgRoot = buildPath (rootDir, suite, section, "os", arch);
69        auto listsTarFname = buildPath (pkgRoot, format ("%s.files.tar.gz", section));
70        if (!std.file.exists (listsTarFname)) {
71            logWarning ("Package lists tarball '%s' does not exist.", listsTarFname);
72            return [];
73        }
74
75        auto ad = new ArchiveDecompressor ();
76        ad.open (listsTarFname);
77        logDebug ("Opened: %s", listsTarFname);
78
79        ArchPackage[string] pkgsMap;
80        foreach (ref entry; ad.read ()) {
81
82            auto archPkid = dirName (entry.fname);
83            ArchPackage pkg;
84            if (archPkid in pkgsMap) {
85                pkg = pkgsMap[archPkid];
86            } else {
87                pkg = new ArchPackage ();
88                pkgsMap[archPkid] = pkg;
89            }
90
91            auto infoBaseName = baseName (entry.fname);
92            if (infoBaseName == "desc") {
93                // we have the description file, add information to this package
94                auto descF = new ListFile ();
95                descF.loadData (entry.data);
96                pkg.name = descF.getEntry ("NAME");
97                pkg.ver  = descF.getEntry ("VERSION");
98                pkg.arch = descF.getEntry ("ARCH");
99
100                pkg.maintainer = descF.getEntry ("PACKAGER");
101                pkg.filename = buildPath (pkgRoot, descF.getEntry ("FILENAME"));
102                setPkgDescription (pkg, descF.getEntry ("DESC"));
103            } else if (infoBaseName == "files") {
104                // we found a content index, add content information to the package
105                auto filesF = new ListFile ();
106                filesF.loadData (entry.data);
107
108                auto filesStr = filesF.getEntry ("FILES");
109                if (filesStr is null) {
110                    if (!pkg.name.canFind ("-meta")) {
111                        logWarning ("Package '%s' has no file list set. Ignoring it.", pkg.toString ());
112                        continue;
113                    }
114                }
115
116                auto contents = appender!(string[]);
117                foreach (l; filesStr.splitLines ())
118                    contents ~= "/" ~ l;
119                pkg.contents = contents.data;
120            }
121        }
122
123        // perform a sanity check, so we will never emit invalid packages
124        auto pkgs = appender!(Package[]);
125        foreach (ref pkg; pkgsMap.byValue ()) {
126            if (pkg.isValid)
127                pkgs ~= pkg;
128            else
129                logError ("Found an invalid package (name, architecture or version is missing). This is a bug.");
130        }
131
132        return pkgs.data;
133    }
134
135    Package[] packagesFor (string suite, string section, string arch)
136    {
137        if ((suite == "arch") || (suite == "archlinux"))
138            suite = "";
139
140        immutable id = "%s-%s-%s".format (suite, section, arch);
141        if (id !in pkgCache) {
142            auto pkgs = loadPackages (suite, section, arch);
143            synchronized (this) pkgCache[id] = pkgs;
144        }
145
146        return pkgCache[id];
147    }
148
149    bool hasChanges (DataStore dstore, string suite, string section, string arch)
150    {
151        return true;
152    }
153}
Note: See TracBrowser for help on using the repository browser.