aboutsummaryrefslogtreecommitdiff
path: root/paludis/repositories/unwritten/unwritten_repository_file.cc
blob: cc6d6903ccb3f333d30daa83e7b846e521da7a0e (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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2008, 2009, 2010 Ciaran McCreesh
 *
 * 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/unwritten/unwritten_repository_file.hh>
#include <paludis/repositories/unwritten/unwritten_repository.hh>
#include <paludis/util/stringify.hh>
#include <paludis/util/log.hh>
#include <paludis/util/simple_parser.hh>
#include <paludis/util/make_named_values.hh>
#include <paludis/util/tokeniser.hh>
#include <paludis/util/join.hh>
#include <paludis/util/safe_ifstream.hh>
#include <paludis/util/wrapped_forward_iterator.hh>
#include <paludis/util/accept_visitor.hh>
#include <paludis/name.hh>
#include <paludis/version_spec.hh>
#include <paludis/literal_metadata_key.hh>
#include <paludis/metadata_key-fwd.hh>
#include <paludis/dep_spec.hh>
#include <paludis/formatter.hh>
#include <paludis/user_dep_spec.hh>
#include <paludis/spec_tree.hh>

#include <paludis/util/pimp-impl.hh>
#include <paludis/util/wrapped_forward_iterator-impl.hh>
#include <paludis/util/indirect_iterator-impl.hh>

#include <list>
#include <algorithm>

using namespace paludis;
using namespace paludis::unwritten_repository;

typedef std::list<UnwrittenRepositoryFileEntry> Entries;

namespace paludis
{
    template <>
    struct Imp<UnwrittenRepositoryFile>
    {
        Entries entries;
        bool is_graveyard;

        Imp() :
            is_graveyard(false)
        {
        }
    };

    template <>
    struct WrappedForwardIteratorTraits<UnwrittenRepositoryFile::ConstIteratorTag>
    {
        typedef Entries::const_iterator UnderlyingIterator;
    };
}

UnwrittenRepositoryFile::UnwrittenRepositoryFile(const FSPath & f) :
    Pimp<UnwrittenRepositoryFile>()
{
    _load(f);
}

UnwrittenRepositoryFile::~UnwrittenRepositoryFile()
{
}

UnwrittenRepositoryFile::ConstIterator
UnwrittenRepositoryFile::begin() const
{
    return ConstIterator(_imp->entries.begin());
}

UnwrittenRepositoryFile::ConstIterator
UnwrittenRepositoryFile::end() const
{
    return ConstIterator(_imp->entries.end());
}

namespace
{
    struct UnwrittenHomepagePrinter
    {
        std::stringstream s;
        const SimpleURISpecTree::ItemFormatter & formatter;

        UnwrittenHomepagePrinter(const SimpleURISpecTree::ItemFormatter & f) :
            formatter(f)
        {
        }

        void visit(const SimpleURISpecTree::NodeType<AllDepSpec>::Type & node)
        {
            std::for_each(indirect_iterator(node.begin()), indirect_iterator(node.end()), accept_visitor(*this));
        }

        void visit(const SimpleURISpecTree::NodeType<ConditionalDepSpec>::Type & node)
        {
            std::for_each(indirect_iterator(node.begin()), indirect_iterator(node.end()), accept_visitor(*this));
        }

        void visit(const SimpleURISpecTree::NodeType<SimpleURIDepSpec>::Type & node)
        {
            if (! s.str().empty())
                s << " ";
            s << formatter.format(*node.spec(), format::Plain());
        }
    };

    struct UnwrittenHomepageKey :
        MetadataSpecTreeKey<SimpleURISpecTree>
    {
        const std::shared_ptr<const SimpleURISpecTree> vv;

        const std::string _raw_name;
        const std::string _human_name;
        const MetadataKeyType _type;

        UnwrittenHomepageKey(const std::string & r, const std::string & h, const MetadataKeyType t,
                const std::shared_ptr<const SimpleURISpecTree> & v) :
            vv(v),
            _raw_name(r),
            _human_name(h),
            _type(t)
        {
        }

        const std::shared_ptr<const SimpleURISpecTree> value() const
        {
            return vv;
        }

        std::string pretty_print(const SimpleURISpecTree::ItemFormatter & f) const
        {
            UnwrittenHomepagePrinter p(f);
            value()->top()->accept(p);
            return p.s.str();
        }

        std::string pretty_print_flat(const SimpleURISpecTree::ItemFormatter & f) const
        {
            UnwrittenHomepagePrinter p(f);
            value()->top()->accept(p);
            return p.s.str();
        }

        virtual const std::string raw_name() const PALUDIS_ATTRIBUTE((warn_unused_result))
        {
            return _raw_name;
        }

        virtual const std::string human_name() const PALUDIS_ATTRIBUTE((warn_unused_result))
        {
            return _human_name;
        }

        virtual MetadataKeyType type() const PALUDIS_ATTRIBUTE((warn_unused_result))
        {
            return _type;
        }
    };
}

void
UnwrittenRepositoryFile::_load(const FSPath & f)
{
    SafeIFStream file(f);

    std::string line;
    while (std::getline(file, line))
    {
        if (line.empty())
            break;

        std::string key, value;
        SimpleParser line_parser(line);
        if (line_parser.consume(
                    (+simple_parser::any_except(" \t") >> key) &
                    (*simple_parser::any_of(" \t")) &
                    (simple_parser::exact("=")) &
                    (*simple_parser::any_of(" \t")) &
                    (*simple_parser::any_except("") >> value)
                    ))
        {
            if (key == "format")
            {
                if (value != "unwritten-1")
                    throw UnwrittenRepositoryConfigurationError(
                            "Unsupported format '" + value + "' in '" + stringify(f) + "'");
            }
            else if (key == "role")
            {
                if (value == "graveyard")
                    _imp->is_graveyard = true;
                else if (value == "unwritten")
                    _imp->is_graveyard = false;
                else
                    Log::get_instance()->message("unwritten_repository.file.unknown_key", ll_warning, lc_context)
                        << "Ignoring unknown key value '" << value << "' for key '" << key << "'";
            }
            else
                Log::get_instance()->message("unwritten_repository.file.unknown_key", ll_warning, lc_context)
                    << "Ignoring unknown key '" << key << "' with value '" << value << "'";
        }
        else
            throw UnwrittenRepositoryConfigurationError(
                    "Cannot parse header line '" + line + "' in '" + stringify(f) + "'");
    }

    CategoryNamePart category("x");
    PackageNamePart package("x");
    std::shared_ptr<MetadataValueKey<SlotName> > slot;
    VersionSpec version("0", { });
    std::shared_ptr<UnwrittenRepositoryFileEntry> entry;
    while (std::getline(file, line))
    {
        SimpleParser line_parser(line);

        std::string token, token2;
        if (line.empty())
        {
        }
        else if (line_parser.consume(
                    (*simple_parser::any_of(" \t")) &
                    (simple_parser::exact("#")) &
                    (*simple_parser::any_except(""))))
        {
        }
        else if (line_parser.consume(
                    (+simple_parser::any_except(" \t/") >> token) &
                    (simple_parser::exact("/"))
                    ))
        {
            if (! line_parser.eof())
                throw UnwrittenRepositoryConfigurationError(
                        "Cannot parse body category line '" + line + "' in '" + stringify(f) + "'");

            category = CategoryNamePart(token);
        }
        else if (line_parser.consume(
                    (+simple_parser::any_of(" \t")) &
                    (+simple_parser::any_except(" \t/") >> token) &
                    (simple_parser::exact("/"))
                    ))
        {
            if (! line_parser.eof())
                throw UnwrittenRepositoryConfigurationError(
                        "Cannot parse body package line '" + line + " in '" + stringify(f) + "'");

            package = PackageNamePart(token);
        }
        else if (line_parser.consume(
                    (+simple_parser::any_of(" \t")) &
                    (+simple_parser::exact(":")) &
                    (+simple_parser::any_except(" \t") >> token) &
                    (+simple_parser::any_of(" \t"))
                    ))
        {
            slot = std::make_shared<LiteralMetadataValueKey<SlotName>>("SLOT", "Slot", mkt_internal, SlotName(token));

            if (line_parser.consume(
                        (+simple_parser::any_except(" \t") >> token)
                        ))
                version = VersionSpec(token, user_version_spec_options());
            else
                throw UnwrittenRepositoryConfigurationError(
                        "Cannot parse body slot+version line '" + line + " in '" + stringify(f) + "'");

            if (! line_parser.eof())
                throw UnwrittenRepositoryConfigurationError(
                        "Cannot parse body slot+version line '" + line + " in '" + stringify(f) + "'");

            if (entry)
            {
                _imp->entries.push_back(*entry);
                entry.reset();
            }
        }
        else if (line_parser.consume(
                    (+simple_parser::any_of(" \t")) &
                    (+simple_parser::any_except(" \t") >> token) &
                    (+simple_parser::any_of(" \t")) &
                    (+simple_parser::exact("=")) &
                    (+simple_parser::any_of(" \t")) &
                    (+simple_parser::any_except("") >> token2)
                    ))
        {
            if (! line_parser.eof())
                throw UnwrittenRepositoryConfigurationError(
                        "Cannot parse body key = value line '" + line + " in '" + stringify(f) + "'");

            if (! entry)
                entry = std::make_shared<UnwrittenRepositoryFileEntry>(make_named_values<UnwrittenRepositoryFileEntry>(
                                n::added_by() = std::shared_ptr<const MetadataValueKey<std::string> >(),
                                n::bug_ids() = std::shared_ptr<const MetadataCollectionKey<Sequence<std::string> > >(),
                                n::comment() = std::shared_ptr<const MetadataValueKey<std::string> >(),
                                n::commit_id() = std::shared_ptr<const MetadataValueKey<std::string> >(),
                                n::description() = std::shared_ptr<const MetadataValueKey<std::string> >(),
                                n::homepage() = std::shared_ptr<const MetadataSpecTreeKey<SimpleURISpecTree> >(),
                                n::name() = category + package,
                                n::remote_ids() = std::shared_ptr<const MetadataCollectionKey<Sequence<std::string> > >(),
                                n::removed_by() = std::shared_ptr<const MetadataValueKey<std::string> >(),
                                n::removed_from() = std::shared_ptr<const MetadataCollectionKey<Set<std::string> > >(),
                                n::slot() = slot,
                                n::version() = version
                                ));

            if (token == "description")
                entry->description() = std::make_shared<LiteralMetadataValueKey<std::string>>("description", "Description", mkt_significant, token2);
            else if (token == "homepage")
            {
                std::shared_ptr<AllDepSpec> all_spec(std::make_shared<AllDepSpec>());
                std::shared_ptr<SimpleURISpecTree> tree(std::make_shared<SimpleURISpecTree>(all_spec));
                std::list<std::string> tokens;
                tokenise_whitespace(token2, std::back_inserter(tokens));
                for (std::list<std::string>::const_iterator t(tokens.begin()), t_end(tokens.end()) ;
                        t != t_end ; ++t)
                    tree->top()->append(std::make_shared<SimpleURIDepSpec>(*t));
                entry->homepage() = std::make_shared<UnwrittenHomepageKey>("homepage", "Homepage", mkt_normal, tree);
            }
            else if (token == "comment")
                entry->comment() = std::make_shared<LiteralMetadataValueKey<std::string>>("comment", "Comment", mkt_normal, token2);
            else if (token == "commit-id")
                entry->commit_id() = std::make_shared<LiteralMetadataValueKey<std::string>>("commit-id", "Commit ID", mkt_normal, token2);
            else if (token == "added-by")
                entry->added_by() = std::make_shared<LiteralMetadataValueKey<std::string>>("added-by", "Added by", mkt_author, token2);
            else if (token == "removed-by")
                entry->removed_by() = std::make_shared<LiteralMetadataValueKey<std::string>>("removed-by", "Removed by", mkt_author, token2);
            else if (token == "removed-from")
            {
                auto t2s(std::make_shared<Set<std::string> >());
                t2s->insert(token2);
                entry->removed_from() = std::make_shared<LiteralMetadataStringSetKey>("removed-from", "Removed from", mkt_author, t2s);
            }
            else if (token == "bug-ids")
            {
                std::shared_ptr<Sequence<std::string> > seq(std::make_shared<Sequence<std::string>>());
                std::list<std::string> tokens;
                tokenise_whitespace(token2, std::back_inserter(tokens));
                for (std::list<std::string>::const_iterator t(tokens.begin()), t_end(tokens.end()) ;
                        t != t_end ; ++t)
                    seq->push_back(*t);
                entry->bug_ids() = std::make_shared<LiteralMetadataStringSequenceKey>("bug-ids", "Bug IDs", mkt_normal, seq);
            }
            else if (token == "remote-ids")
            {
                std::shared_ptr<Sequence<std::string> > seq(std::make_shared<Sequence<std::string>>());
                std::list<std::string> tokens;
                tokenise_whitespace(token2, std::back_inserter(tokens));
                for (std::list<std::string>::const_iterator t(tokens.begin()), t_end(tokens.end()) ;
                        t != t_end ; ++t)
                    seq->push_back(*t);
                entry->remote_ids() = std::make_shared<LiteralMetadataStringSequenceKey>("remote-ids", "Remote IDs", mkt_internal, seq);
            }
            else
                Log::get_instance()->message("unwritten_repository.file.unknown_key", ll_warning, lc_context)
                    << "Ignoring unknown key '" << token << "' with value '" << token2 << "'";
        }
        else
            throw UnwrittenRepositoryConfigurationError(
                    "Cannot parse body line '" + line + " in '" + stringify(f) + "'");
    }

    if (entry)
        _imp->entries.push_back(*entry);
}

bool
UnwrittenRepositoryFile::is_graveyard() const
{
    return _imp->is_graveyard;
}

template class Pimp<UnwrittenRepositoryFile>;
template class WrappedForwardIterator<UnwrittenRepositoryFile::ConstIteratorTag,
         const UnwrittenRepositoryFileEntry>;