aboutsummaryrefslogtreecommitdiff
path: root/paludis/repositories/gems/gem_specification.cc
blob: 37d6c4b28f4d0f36051ca47c905bc9ede7a6a673 (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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2007, 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/gems/gem_specification.hh>
#include <paludis/repositories/gems/yaml.hh>
#include <paludis/util/private_implementation_pattern-impl.hh>
#include <paludis/util/mutex.hh>
#include <paludis/util/stringify.hh>
#include <paludis/util/make_named_values.hh>
#include <paludis/name.hh>
#include <paludis/version_spec.hh>
#include <paludis/repository.hh>
#include <paludis/metadata_key.hh>
#include <paludis/action.hh>
#include <paludis/environment.hh>
#include <paludis/literal_metadata_key.hh>
#include <paludis/mask.hh>
#include <paludis/user_dep_spec.hh>
#include <tr1/functional>

using namespace paludis;
using namespace paludis::gems;

namespace paludis
{
    template <>
    struct Implementation<GemSpecification>
    {
        mutable Mutex mutex;

        std::string name_part;
        std::string version;
        std::string date;
        std::string platform;
        std::string homepage;

        std::tr1::shared_ptr<LiteralMetadataValueKey<std::string> > description_key;
        std::tr1::shared_ptr<LiteralMetadataValueKey<std::string> > summary_key;
        std::tr1::shared_ptr<LiteralMetadataValueKey<std::string> > authors_key;
        std::tr1::shared_ptr<LiteralMetadataValueKey<std::string> > rubyforge_project_key;
        std::tr1::shared_ptr<LiteralMetadataValueKey<FSEntry> > fs_location_key;
        std::tr1::shared_ptr<LiteralMetadataValueKey<SlotName> > slot_key;

        std::tr1::shared_ptr<const FSEntry> load_from_file;

        const Environment * const environment;
        const std::tr1::shared_ptr<const Repository> repository;

        mutable bool has_masks;

        Implementation(const Environment * const e, const std::tr1::shared_ptr<const Repository> & r) :
            environment(e),
            repository(r),
            has_masks(false)
        {
        }
    };
}

namespace
{
    std::string extract_text_only(const yaml::Node & n, const std::string & extra);

    struct VersionVisitor
    {
        std::string text;

        void visit(const yaml::StringNode & n) PALUDIS_ATTRIBUTE((noreturn));
        void visit(const yaml::SequenceNode & n) PALUDIS_ATTRIBUTE((noreturn));

        void visit(const yaml::MapNode & n)
        {
            yaml::MapNode::ConstIterator i(n.find("version"));
            if (i == n.end())
                throw BadSpecificationError("Version has no version: key");
            text = extract_text_only(*i->second, "for Version version: key");
        }
    };

    void VersionVisitor::visit(const yaml::StringNode &)
    {
        throw BadSpecificationError("Version child node is string, not map");
    }

    void VersionVisitor::visit(const yaml::SequenceNode &)
    {
        throw BadSpecificationError("Version child node is sequence, not map");
    }

    struct ExtractTextVisitor
    {
        const std::string extra;
        const bool accept_sequence;
        std::string result;

        ExtractTextVisitor(const std::string & s, const bool b) :
            extra(s),
            accept_sequence(b)
        {
        }

        void visit(const yaml::StringNode & n)
        {
            result = n.text();
        }

        void visit(const yaml::SequenceNode & s)
        {
            if (! accept_sequence)
                throw BadSpecificationError("Found sequence rather than text " + extra);

            bool w(false);
            for (yaml::SequenceNode::ConstIterator i(s.begin()), i_end(s.end()) ; i != i_end ; ++i)
            {
                if (w)
                    result.append(", ");
                result.append(extract_text_only(**i, extra));
                w = true;
            }
        }

        void visit(const yaml::MapNode &) PALUDIS_ATTRIBUTE((noreturn));
    };

    void ExtractTextVisitor::visit(const yaml::MapNode &)
    {
        throw BadSpecificationError("Found map rather than text " + extra);
    }

    std::string extract_text_only(const yaml::Node & n, const std::string & extra)
    {
        ExtractTextVisitor v(extra, false);
        n.accept(v);
        return v.result;
    }

    std::string extract_text_sequence(const yaml::Node & n, const std::string & extra)
    {
        ExtractTextVisitor v(extra, true);
        n.accept(v);
        return v.result;
    }

    std::string required_text_only_key(const yaml::MapNode & n, const std::string & k)
    {
        yaml::MapNode::ConstIterator i(n.find(k));
        if (i == n.end())
            throw BadSpecificationError("Key '" + k + "' not defined");
        return extract_text_only(*i->second, "for key '" + k + "'");
    }

    std::string optional_text_sequence_key(const yaml::MapNode & n, const std::string & k)
    {
        yaml::MapNode::ConstIterator i(n.find(k));
        if (i == n.end())
            return "";
        return extract_text_sequence(*i->second, "for key '" + k + "'");
    }

    std::string optional_text_only_key(const yaml::MapNode & n, const std::string & k)
    {
        yaml::MapNode::ConstIterator i(n.find(k));
        if (i == n.end())
            return "";
        return extract_text_only(*i->second, "for key '" + k + "'");
    }

    std::string required_version(const yaml::MapNode & n, const std::string & k)
    {
        yaml::MapNode::ConstIterator i(n.find(k));
        if (i == n.end())
            throw BadSpecificationError("Key '" + k + "' not defined");

        VersionVisitor v;
        i->second->accept(v);
        return v.text;
    }

    struct TopVisitor
    {
        Implementation<GemSpecification> * const _imp;

        TopVisitor(Implementation<GemSpecification> * const i) :
            _imp(i)
        {
        }

        void visit(const yaml::MapNode & n)
        {
            std::string summary(required_text_only_key(n, "summary"));
            if (! summary.empty())
                _imp->summary_key.reset(new LiteralMetadataValueKey<std::string> ("summary", "Summary", mkt_significant, summary));

            std::string description(optional_text_only_key(n, "description"));
            if (! description.empty())
                _imp->description_key.reset(new LiteralMetadataValueKey<std::string> ("description", "Description", mkt_normal, description));

            std::string authors(optional_text_sequence_key(n, "authors"));
            if (! authors.empty())
                _imp->authors_key.reset(new LiteralMetadataValueKey<std::string> ("authors", "Authors", mkt_normal, authors));

            std::string rubyforge_project(optional_text_sequence_key(n, "rubyforge_project"));
            if (! rubyforge_project.empty())
                _imp->rubyforge_project_key.reset(new LiteralMetadataValueKey<std::string> ("rubyforge_project", "Rubyforge Project",
                            mkt_normal, rubyforge_project));

            _imp->date = required_text_only_key(n, "date");
            _imp->platform = required_text_only_key(n, "platform");
            _imp->name_part = required_text_only_key(n, "name");
            _imp->version = required_version(n, "version");
        }

        void visit(const yaml::SequenceNode & n) PALUDIS_ATTRIBUTE((noreturn));

        void visit(const yaml::StringNode & n) PALUDIS_ATTRIBUTE((noreturn));
    };

    void TopVisitor::visit(const yaml::SequenceNode &)
    {
        throw BadSpecificationError("Top level node is sequence, not map");
    }

    void TopVisitor::visit(const yaml::StringNode & n)
    {
        throw BadSpecificationError("Top level node is text '" + n.text() + "', not map");
    }
}

GemSpecification::GemSpecification(const Environment * const e,
        const std::tr1::shared_ptr<const Repository> & r, const yaml::Node & node) :
    PrivateImplementationPattern<GemSpecification>(new Implementation<GemSpecification>(e, r)),
    _imp(PrivateImplementationPattern<GemSpecification>::_imp)
{
    TopVisitor v(_imp.get());
    node.accept(v);

    if (_imp->summary_key)
        add_metadata_key(_imp->summary_key);

    if (_imp->description_key)
        add_metadata_key(_imp->description_key);

    if (_imp->authors_key)
        add_metadata_key(_imp->authors_key);

    if (_imp->rubyforge_project_key)
        add_metadata_key(_imp->rubyforge_project_key);
}


GemSpecification::GemSpecification(const Environment * const e, const std::tr1::shared_ptr<const Repository> & r,
        const PackageNamePart & q, const VersionSpec & v, const FSEntry & f) :
    PrivateImplementationPattern<GemSpecification>(new Implementation<GemSpecification>(e, r)),
    _imp(PrivateImplementationPattern<GemSpecification>::_imp)
{
    _imp->name_part = stringify(q);
    _imp->version = stringify(v);
    _imp->load_from_file.reset(new FSEntry(f));
    _imp->fs_location_key.reset(new LiteralMetadataValueKey<FSEntry> ("GEM", "Gem Location", mkt_internal, f));
    add_metadata_key(_imp->fs_location_key);
    _imp->slot_key.reset(new LiteralMetadataValueKey<SlotName>("SLOT", "Slot", mkt_internal, SlotName(stringify(v))));
    add_metadata_key(_imp->slot_key);
}

GemSpecification::~GemSpecification()
{
}

BadSpecificationError::BadSpecificationError(const std::string & s) throw () :
    Exception("Bad gem specification: " + s)
{
}

const std::string
GemSpecification::canonical_form(const PackageIDCanonicalForm f) const
{
    switch (f)
    {
        case idcf_full:
            return stringify(name()) + "-" + stringify(version()) + "::" + stringify(repository()->name());

        case idcf_version:
            return stringify(version());

        case idcf_no_version:
            return stringify(name()) + "::" + stringify(_imp->repository->name());

        case idcf_no_name:
            return stringify(version()) + "::" + stringify(repository()->name());

        case last_idcf:
            break;
    }

    throw InternalError(PALUDIS_HERE, "Bad PackageIDCanonicalForm");
}

PackageDepSpec
GemSpecification::uniquely_identifying_spec() const
{
    return parse_user_package_dep_spec("=" + stringify(name()) + "-" + stringify(version()) + "::" + stringify(repository()->name()),
            _imp->environment, UserPackageDepSpecOptions());
}

const QualifiedPackageName
GemSpecification::name() const
{
    return QualifiedPackageName(CategoryNamePart("gems") + PackageNamePart(_imp->name_part));
}

const VersionSpec
GemSpecification::version() const
{
    return VersionSpec(_imp->version, VersionSpecOptions());
}

const std::tr1::shared_ptr<const Repository>
GemSpecification::repository() const
{
    return _imp->repository;
}

const std::tr1::shared_ptr<const MetadataValueKey<std::tr1::shared_ptr<const PackageID> > >
GemSpecification::virtual_for_key() const
{
    return std::tr1::shared_ptr<const MetadataValueKey<std::tr1::shared_ptr<const PackageID> > >();
}

const std::tr1::shared_ptr<const MetadataCollectionKey<KeywordNameSet> >
GemSpecification::keywords_key() const
{
    return std::tr1::shared_ptr<const MetadataCollectionKey<KeywordNameSet> >();
}

const std::tr1::shared_ptr<const MetadataSpecTreeKey<ProvideSpecTree> >
GemSpecification::provide_key() const
{
    return std::tr1::shared_ptr<const MetadataSpecTreeKey<ProvideSpecTree> >();
}

const std::tr1::shared_ptr<const MetadataSpecTreeKey<DependencySpecTree> >
GemSpecification::dependencies_key() const
{
    return std::tr1::shared_ptr<const MetadataSpecTreeKey<DependencySpecTree> >();
}

const std::tr1::shared_ptr<const MetadataSpecTreeKey<DependencySpecTree> >
GemSpecification::build_dependencies_key() const
{
    return std::tr1::shared_ptr<const MetadataSpecTreeKey<DependencySpecTree> >();
}

const std::tr1::shared_ptr<const MetadataSpecTreeKey<DependencySpecTree> >
GemSpecification::run_dependencies_key() const
{
    return std::tr1::shared_ptr<const MetadataSpecTreeKey<DependencySpecTree> >();
}

const std::tr1::shared_ptr<const MetadataSpecTreeKey<DependencySpecTree> >
GemSpecification::post_dependencies_key() const
{
    return std::tr1::shared_ptr<const MetadataSpecTreeKey<DependencySpecTree> >();
}

const std::tr1::shared_ptr<const MetadataSpecTreeKey<FetchableURISpecTree> >
GemSpecification::fetches_key() const
{
    return std::tr1::shared_ptr<const MetadataSpecTreeKey<FetchableURISpecTree> >();
}

const std::tr1::shared_ptr<const MetadataSpecTreeKey<SimpleURISpecTree> >
GemSpecification::homepage_key() const
{
    return std::tr1::shared_ptr<const MetadataSpecTreeKey<SimpleURISpecTree> >();
}

const std::tr1::shared_ptr<const MetadataSpecTreeKey<DependencySpecTree> >
GemSpecification::suggested_dependencies_key() const
{
    return std::tr1::shared_ptr<const MetadataSpecTreeKey<DependencySpecTree> >();
}

const std::tr1::shared_ptr<const MetadataValueKey<std::string> >
GemSpecification::short_description_key() const
{
    return _imp->summary_key;
}

const std::tr1::shared_ptr<const MetadataValueKey<std::string> >
GemSpecification::long_description_key() const
{
    return _imp->description_key;
}

const std::tr1::shared_ptr<const MetadataValueKey<FSEntry> >
GemSpecification::fs_location_key() const
{
    return _imp->fs_location_key;
}

const std::tr1::shared_ptr<const MetadataValueKey<std::tr1::shared_ptr<const Contents> > >
GemSpecification::contents_key() const
{
    return std::tr1::shared_ptr<const MetadataValueKey<std::tr1::shared_ptr<const Contents> > >();
}

const std::tr1::shared_ptr<const MetadataTimeKey>
GemSpecification::installed_time_key() const
{
    return std::tr1::shared_ptr<const MetadataTimeKey>();
}

const std::tr1::shared_ptr<const MetadataCollectionKey<Set<std::string> > >
GemSpecification::from_repositories_key() const
{
    return std::tr1::shared_ptr<const MetadataCollectionKey<Set<std::string> > >();
}

const std::tr1::shared_ptr<const MetadataCollectionKey<PackageIDSequence> >
GemSpecification::contains_key() const
{
    return std::tr1::shared_ptr<const MetadataCollectionKey<PackageIDSequence> >();
}

const std::tr1::shared_ptr<const MetadataValueKey<std::tr1::shared_ptr<const PackageID> > >
GemSpecification::contained_in_key() const
{
    return std::tr1::shared_ptr<const MetadataValueKey<std::tr1::shared_ptr<const PackageID> > >();
}

const std::tr1::shared_ptr<const MetadataCollectionKey<Set<std::string> > >
GemSpecification::behaviours_key() const
{
    return make_null_shared_ptr();
}

const std::tr1::shared_ptr<const MetadataValueKey<SlotName> >
GemSpecification::slot_key() const
{
    return _imp->slot_key;
}

bool
GemSpecification::arbitrary_less_than_comparison(const PackageID &) const
{
    return false;
}

std::size_t
GemSpecification::extra_hash_value() const
{
    return 0;
}

void
GemSpecification::need_keys_added() const
{
    if (_imp->load_from_file)
        throw InternalError(PALUDIS_HERE, "Got to do the load from file thing");
}

#if 0
InstalledGemsRepository::need_version_metadata(const QualifiedPackageName & q, const VersionSpec & v) const
{
    MetadataMap::const_iterator i(_imp->metadata.find(std::make_pair(q, v)));
    if (i != _imp->metadata.end())
        return;

    Context c("When loading version metadata for '" + stringify(PackageDatabaseEntry(q, v, name())) + "':");

    std::tr1::shared_ptr<gems::InstalledGemMetadata> m(new gems::InstalledGemMetadata(v));
    _imp->metadata.insert(std::make_pair(std::make_pair(q, v), m));

    Command cmd(getenv_with_default("PALUDIS_GEMS_DIR", LIBEXECDIR "/paludis") +
            "/gems/gems.bash specification '" + stringify(q.package) + "' '" + stringify(v) + "'");
    cmd.with_stderr_prefix(stringify(q) + "-" + stringify(v) + "::" + stringify(name()) + "> ");
    cmd.with_sandbox();
    cmd.with_uid_gid(_imp->params.environment->reduced_uid(), _imp->params.environment->reduced_gid());

    PStream p(cmd);
    std::string output((std::istreambuf_iterator<char>(p)), std::istreambuf_iterator<char>());

    if (0 != p.exit_status())
    {
        Log::get_instance()->message(ll_warning, lc_context) << "Version metadata extraction returned non-zero";
        return;
    }

    yaml::Document spec_doc(output);
    gems::GemSpecification spec(*spec_doc.top());
    m->populate_from_specification(spec);
    m->eapi = EAPIData::get_instance()->eapi_from_string("gems-1");
}
#endif

bool
GemSpecification::supports_action(const SupportsActionTestBase & b) const
{
    return repository()->some_ids_might_support_action(b);
}

namespace
{
    struct PerformAction
    {
        const PackageID * const id;

        PerformAction(const PackageID * const i) :
            id(i)
        {
        }

        void visit(const InstallAction & a)
        {
            SupportsActionTest<InstallAction> t;
            if (! id->repository()->some_ids_might_support_action(t))
                throw ActionFailedError("Unsupported action '" + a.simple_name() + "'");
        }

        void visit(const UninstallAction & a)
        {
            SupportsActionTest<UninstallAction> t;
            if (! id->repository()->some_ids_might_support_action(t))
                throw ActionFailedError("Unsupported action '" + a.simple_name() + "'");
        }

        void visit(const ConfigAction & a)
        {
            SupportsActionTest<ConfigAction> t;
            if (! id->repository()->some_ids_might_support_action(t))
                throw ActionFailedError("Unsupported action '" + a.simple_name() + "'");
        }

        void visit(const FetchAction & a)
        {
            SupportsActionTest<FetchAction> t;
            if (! id->repository()->some_ids_might_support_action(t))
                throw ActionFailedError("Unsupported action '" + a.simple_name() + "'");
        }

        void visit(const InfoAction & a)
        {
            SupportsActionTest<InfoAction> t;
            if (! id->repository()->some_ids_might_support_action(t))
                throw ActionFailedError("Unsupported action '" + a.simple_name() + "'");
        }

        void visit(const PretendAction & a)
        {
            SupportsActionTest<PretendAction> t;
            if (! id->repository()->some_ids_might_support_action(t))
                throw ActionFailedError("Unsupported action '" + a.simple_name() + "'");
        }

        void visit(const PretendFetchAction & a)
        {
            SupportsActionTest<PretendFetchAction> t;
            if (! id->repository()->some_ids_might_support_action(t))
                throw ActionFailedError("Unsupported action '" + a.simple_name() + "'");
        }
    };
}

void
GemSpecification::perform_action(Action & a) const
{
    PerformAction b(this);
    a.accept(b);
}

void
GemSpecification::need_masks_added() const
{
    Lock l(_imp->mutex);

    if (_imp->has_masks)
        return;

    _imp->has_masks = true;

    Context context("When generating masks for ID '" + canonical_form(idcf_full) + "':");

    if (! _imp->environment->unmasked_by_user(*this))
    {
        /* user */
        std::tr1::shared_ptr<const Mask> user_mask(_imp->environment->mask_for_user(*this, false));
        if (user_mask)
            add_mask(user_mask);
    }
    else
    {
        std::tr1::shared_ptr<const Mask> user_mask(_imp->environment->mask_for_user(*this, true));
        if (user_mask)
            add_overridden_mask(make_shared_ptr(new OverriddenMask(
                            make_named_values<OverriddenMask>(
                                value_for<n::mask>(user_mask),
                                value_for<n::override_reason>(mro_overridden_by_user)
                                ))));
    }

    /* break portage */
    std::tr1::shared_ptr<const Mask> breaks_mask(_imp->environment->mask_for_breakage(*this));
    if (breaks_mask)
        add_mask(breaks_mask);
}

void
GemSpecification::invalidate_masks() const
{
    Lock l(_imp->mutex);

    if (! _imp->has_masks)
        return;

    _imp->has_masks = false;
    PackageID::invalidate_masks();
}

std::tr1::shared_ptr<const Set<std::string> >
GemSpecification::breaks_portage() const
{
    std::tr1::shared_ptr<Set<std::string> > why(new Set<std::string>);
    why->insert("format");
    return why;
}

const std::tr1::shared_ptr<const MetadataValueKey<std::tr1::shared_ptr<const Choices> > >
GemSpecification::choices_key() const
{
    return std::tr1::shared_ptr<const MetadataValueKey<std::tr1::shared_ptr<const Choices> > >();
}