aboutsummaryrefslogtreecommitdiff
path: root/paludis/repositories/gentoo/portage_repository_news.cc
blob: 4f89bac60fb48626d6b3309f4ac5e581b2b32454 (plain)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2006, 2007 Ciaran McCreesh <ciaranm@ciaranm.org>
 *
 * This file is part of the Paludis package manager. Paludis is free software;
 * you can redistribute it and/or modify it under the terms of the GNU General
 * Public License version 2, as published by the Free Software Foundation.
 *
 * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <paludis/repositories/gentoo/portage_repository.hh>
#include <paludis/repositories/gentoo/portage_repository_news.hh>

#include <paludis/config_file.hh>
#include <paludis/environment.hh>
#include <paludis/util/dir_iterator.hh>
#include <paludis/util/fs_entry.hh>
#include <paludis/util/log.hh>
#include <paludis/util/strip.hh>
#include <paludis/query.hh>

#include <set>
#include <ostream>
#include <fstream>
#include <list>

using namespace paludis;

namespace paludis
{
    /**
     * Implementation data for PortageRepositoryNews.
     *
     * \ingroup grpportagerepository
     */
    template<>
    struct Implementation<PortageRepositoryNews>
    {
        const Environment * const environment;
        const PortageRepository * const portage_repository;
        const PortageRepositoryParams params;

        const FSEntry skip_file;
        const FSEntry unread_file;

        Implementation(const Environment * const e, const PortageRepository * const p,
                const PortageRepositoryParams & k) :
            environment(e),
            portage_repository(p),
            params(k),
            skip_file(e->root() / "var" / "lib" / "paludis" / "news" /
                    ("news-" + stringify(portage_repository->name()) + ".skip")),
            unread_file(e->root() / "var" / "lib" / "paludis" / "news" /
                    ("news-" + stringify(portage_repository->name()) + ".unread"))
        {
        }
    };
}

PortageRepositoryNews::PortageRepositoryNews(const Environment * const e, const PortageRepository * const p,
        const PortageRepositoryParams & k) :
    PrivateImplementationPattern<PortageRepositoryNews>(new Implementation<PortageRepositoryNews>(e, p, k))
{
}

PortageRepositoryNews::~PortageRepositoryNews()
{
}

void
PortageRepositoryNews::update_news() const
{
    Context context("When updating news at location '" +
            stringify(_imp->params.newsdir) + "' for repository '" +
            stringify(_imp->portage_repository->name()) + "':");

    if (! _imp->params.newsdir.is_directory())
        return;

    std::set<std::string> skip;

    if (_imp->skip_file.is_regular_file())
    {
        Context local_context("When handling news skip file '" + stringify(
                _imp->skip_file) + "':");
        LineConfigFile s(_imp->skip_file);
        std::copy(s.begin(), s.end(), std::inserter(skip, skip.end()));
    }

    for (DirIterator d(_imp->params.newsdir), d_end ; d != d_end ; ++d)
    {
        Context local_context("When handling news entry '" + stringify(*d) + "':");

        if (! d->is_directory())
            continue;
        if (! (*d / (d->basename() + ".en.txt")).is_regular_file())
            continue;

        if (skip.end() != skip.find(d->basename()))
            continue;

        try
        {
            NewsFile news(*d / (d->basename() + ".en.txt"));
            bool show(true);

            if (news.begin_display_if_installed() != news.end_display_if_installed())
            {
                bool local_show(false);
                for (NewsFile::DisplayIfInstalledIterator i(news.begin_display_if_installed()),
                        i_end(news.end_display_if_installed()) ; i != i_end ; ++i)
                    if (! _imp->environment->package_database()->query(
                                query::Package(*i) & query::RepositoryHasInstalledInterface(),
                                qo_whatever)->empty())
                        local_show = true;
                show &= local_show;
            }

            if (news.begin_display_if_keyword() != news.end_display_if_keyword())
            {
                bool local_show(false);
                for (NewsFile::DisplayIfKeywordIterator i(news.begin_display_if_keyword()),
                        i_end(news.end_display_if_keyword()) ; i != i_end && ! local_show ; ++i)
                    if (*i == _imp->portage_repository->profile_variable("ARCH"))
                        local_show = true;
                show &= local_show;
            }

            if (news.begin_display_if_profile() != news.end_display_if_profile())
            {
                bool local_show(false);
                std::tr1::shared_ptr<const FSEntryCollection> c(_imp->params.profiles);
                for (FSEntryCollection::Iterator p(c->begin()), p_end(c->end()) ; p != p_end ; ++p)
                {
                    std::string profile(strip_leading_string(strip_trailing_string(
                                strip_leading_string(stringify(p->realpath()),
                                    stringify(p->realpath())), "/"), "/"));
                    Log::get_instance()->message(ll_debug, lc_no_context,
                            "Profile path is '" + profile + "'");
                    for (NewsFile::DisplayIfProfileIterator i(news.begin_display_if_profile()),
                            i_end(news.end_display_if_profile()) ; i != i_end ; ++i)
                        if (profile == *i)
                            local_show = true;
                }
                show &= local_show;
            }

            if (show)
            {
                std::ofstream s(stringify(_imp->skip_file).c_str(),
                        std::ios::out | std::ios::app);
                if (! s)
                    Log::get_instance()->message(ll_warning, lc_no_context,
                            "Cannot append to news skip file '"
                            + stringify(_imp->skip_file) +
                            "', skipping news item '" + stringify(*d) + "'");

                std::ofstream t(stringify(_imp->unread_file).c_str(),
                        std::ios::out | std::ios::app);
                if (! t)
                    Log::get_instance()->message(ll_warning, lc_no_context,
                            "Cannot append to unread file '"
                            + stringify(_imp->unread_file) +
                            "', skipping news item '" + stringify(*d) + "'");

                if (s && t)
                {
                    s << d->basename() << std::endl;
                    t << d->basename() << std::endl;
                }
            }
        }
        catch (const ConfigFileError & e)
        {
            Log::get_instance()->message(ll_warning, lc_no_context,
                    "Skipping news item '"
                    + stringify(*d) + "' because of exception '" + e.message() + "' ("
                    + e.what() + ")");
        }
    }

}

namespace paludis
{
    /**
     * Implementation data for NewsFile.
     *
     * \ingroup grpnewsconfigfile
     */
    template<>
    struct Implementation<NewsFile>
    {
        mutable bool in_header;
        mutable std::list<std::string> display_if_installed;
        mutable std::list<std::string> display_if_keyword;
        mutable std::list<std::string> display_if_profile;

        Implementation() :
            in_header(true)
        {
        }
    };
}

NewsFile::NewsFile(const FSEntry & our_filename) :
    ConfigFile(our_filename),
    PrivateImplementationPattern<NewsFile>(new Implementation<NewsFile>)
{
    need_lines();
}

NewsFile::~NewsFile()
{
}

void
NewsFile::accept_line(const std::string & line) const
{
    if (_imp->in_header)
    {
        std::string::size_type p(line.find(':'));
        if (std::string::npos == p)
            _imp->in_header = false;
        else
        {
            std::string k(strip_leading(strip_trailing(line.substr(0, p), " \t\n"), " \t\n"));
            std::string v(strip_leading(strip_trailing(line.substr(p + 1), " \t\n"), " \t\n"));
            if (k == "Display-If-Installed")
                _imp->display_if_installed.push_back(v);
            else if (k == "Display-If-Keyword")
                _imp->display_if_keyword.push_back(v);
            if (k == "Display-If-Profile")
                _imp->display_if_profile.push_back(v);
        }
    }
}

NewsFile::DisplayIfInstalledIterator
NewsFile::begin_display_if_installed() const
{
    return DisplayIfInstalledIterator(_imp->display_if_installed.begin());
}

NewsFile::DisplayIfInstalledIterator
NewsFile::end_display_if_installed() const
{
    return DisplayIfInstalledIterator(_imp->display_if_installed.end());
}

NewsFile::DisplayIfKeywordIterator
NewsFile::begin_display_if_keyword() const
{
    return DisplayIfKeywordIterator(_imp->display_if_keyword.begin());
}

NewsFile::DisplayIfKeywordIterator
NewsFile::end_display_if_keyword() const
{
    return DisplayIfKeywordIterator(_imp->display_if_keyword.end());
}

NewsFile::DisplayIfProfileIterator
NewsFile::begin_display_if_profile() const
{
    return DisplayIfProfileIterator(_imp->display_if_profile.begin());
}

NewsFile::DisplayIfProfileIterator
NewsFile::end_display_if_profile() const
{
    return DisplayIfProfileIterator(_imp->display_if_profile.end());
}