aboutsummaryrefslogtreecommitdiff
path: root/paludis/repositories/virtuals/package_id.cc
blob: 3a4c591a0382de389688bc7eff3dcde0a171a5c2 (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
/* 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/virtuals/package_id.hh>
#include <paludis/repositories/virtuals/installed_virtuals_repository.hh>
#include <paludis/repositories/virtuals/virtuals_repository.hh>
#include <paludis/util/stringify.hh>
#include <paludis/util/make_shared_ptr.hh>
#include <paludis/util/private_implementation_pattern-impl.hh>
#include <paludis/util/mutex.hh>
#include <paludis/util/hashes.hh>
#include <paludis/util/make_named_values.hh>
#include <paludis/util/return_literal_function.hh>
#include <paludis/name.hh>
#include <paludis/dep_spec.hh>
#include <paludis/version_spec.hh>
#include <paludis/environment.hh>
#include <paludis/version_requirements.hh>
#include <paludis/metadata_key.hh>
#include <paludis/formatter.hh>
#include <paludis/action.hh>
#include <paludis/mask.hh>
#include <paludis/package_database.hh>
#include <paludis/literal_metadata_key.hh>
#include <paludis/user_dep_spec.hh>
#include <paludis/selection.hh>
#include <paludis/generator.hh>
#include <paludis/filter.hh>
#include <paludis/filtered_generator.hh>

using namespace paludis;
using namespace paludis::virtuals;

namespace paludis
{
    template <>
    struct Implementation<VirtualsDepKey>
    {
        const Environment * const env;
        const std::tr1::shared_ptr<DependencySpecTree> value;
        const std::tr1::shared_ptr<const DependenciesLabelSequence> labels;
        const std::tr1::shared_ptr<const PackageDepSpec> spec;

        const std::string raw_name;
        const std::string human_name;

        Implementation(const Environment * const e, const std::tr1::shared_ptr<const PackageID> & v,
                const std::tr1::shared_ptr<const DependenciesLabelSequence> & l,
                bool exact, const std::string & h, const std::string & r) :
            env(e),
            value(new DependencySpecTree(make_shared_ptr(new AllDepSpec))),
            labels(l),
            spec(exact ?
                    make_shared_ptr(new PackageDepSpec(
                            make_package_dep_spec(PartiallyMadePackageDepSpecOptions())
                            .package(v->name())
                            .version_requirement(make_named_values<VersionRequirement>(
                                    value_for<n::version_operator>(vo_equal),
                                    value_for<n::version_spec>(v->version())))
                            .slot_requirement(make_shared_ptr(new UserSlotExactRequirement(
                                        v->slot_key() ? v->slot_key()->value() : SlotName("UNKNOWN"))))
                            .in_repository(v->repository()->name())))
                    :
                    make_shared_ptr(new PackageDepSpec(
                            make_package_dep_spec(PartiallyMadePackageDepSpecOptions())
                            .package(v->name())
                            ))
                ),
            raw_name(r),
            human_name(h)
        {
            value->root()->append(spec);
        }
    };
}

VirtualsDepKey::VirtualsDepKey(const Environment * const e, const std::string & r, const std::string & h,
        const std::tr1::shared_ptr<const PackageID> & v,
        const std::tr1::shared_ptr<const DependenciesLabelSequence> & l,
        const bool exact) :
    PrivateImplementationPattern<VirtualsDepKey>(new Implementation<VirtualsDepKey>(e, v, l, exact, r, h)),
    _imp(PrivateImplementationPattern<VirtualsDepKey>::_imp)
{
}

VirtualsDepKey::~VirtualsDepKey()
{
}

const std::tr1::shared_ptr<const DependencySpecTree>
VirtualsDepKey::value() const
{
    return _imp->value;
}

const std::string
VirtualsDepKey::raw_name() const
{
    return _imp->raw_name;
}

const std::string
VirtualsDepKey::human_name() const
{
    return _imp->human_name;
}

MetadataKeyType
VirtualsDepKey::type() const
{
    return mkt_dependencies;
}

std::string
VirtualsDepKey::pretty_print(const DependencySpecTree::ItemFormatter & f) const
{
    if (_imp->env)
    {
        if (! (*_imp->env)[selection::SomeArbitraryVersion(generator::Matches(*_imp->spec, MatchPackageOptions()) |
                    filter::InstalledAtRoot(_imp->env->root()))]->empty())
            return f.format(*_imp->spec, format::Installed());
        else if (! (*_imp->env)[selection::SomeArbitraryVersion(generator::Matches(*_imp->spec, MatchPackageOptions()) |
                    filter::SupportsAction<InstallAction>() | filter::NotMasked())]->empty())
            return f.format(*_imp->spec, format::Installable());
        else
            return f.format(*_imp->spec, format::Plain());
    }
    else
        return f.format(*_imp->spec, format::Plain());
}

std::string
VirtualsDepKey::pretty_print_flat(const DependencySpecTree::ItemFormatter & f) const
{
    return pretty_print(f);
}

const std::tr1::shared_ptr<const DependenciesLabelSequence>
VirtualsDepKey::initial_labels() const
{
    return _imp->labels;
}

namespace paludis
{
    template <>
    struct Implementation<VirtualsPackageID>
    {
        const Environment * const env;
        const std::tr1::shared_ptr<const Repository> repository;
        const QualifiedPackageName name;
        const VersionSpec version;
        std::tr1::shared_ptr<DependenciesLabelSequence> bdep_labels;
        std::tr1::shared_ptr<DependenciesLabelSequence> rdep_labels;
        const std::tr1::shared_ptr<const MetadataValueKey<std::tr1::shared_ptr<const PackageID> > > virtual_for;
        const std::tr1::shared_ptr<const MetadataSpecTreeKey<DependencySpecTree> > bdep;
        const std::tr1::shared_ptr<const MetadataSpecTreeKey<DependencySpecTree> > rdep;
        mutable bool has_masks;
        mutable Mutex mutex;

        Implementation(
                const Environment * const e,
                const std::tr1::shared_ptr<const Repository> & o,
                const QualifiedPackageName & n,
                const std::tr1::shared_ptr<const PackageID> & p,
                const bool b) :
            env(e),
            repository(o),
            name(n),
            version(p->version()),
            bdep_labels(new DependenciesLabelSequence),
            rdep_labels(new DependenciesLabelSequence),
            virtual_for(new LiteralMetadataValueKey<std::tr1::shared_ptr<const PackageID> > ("VIRTUAL_FOR", "Virtual for", mkt_normal, p)),
            bdep(new virtuals::VirtualsDepKey(e, "DEPEND", "Build dependencies", p, bdep_labels, b)),
            rdep(new virtuals::VirtualsDepKey(e, "RDEPEND", "Run dependencies", p, rdep_labels, b)),
            has_masks(false)
        {
            bdep_labels->push_back(make_shared_ptr(new DependenciesBuildLabel("DEPEND",
                            return_literal_function(true))));
            rdep_labels->push_back(make_shared_ptr(new DependenciesRunLabel("RDEPEND",
                            return_literal_function(true))));
        }
    };
}

VirtualsPackageID::VirtualsPackageID(
        const Environment * const e,
        const std::tr1::shared_ptr<const Repository> & owner,
        const QualifiedPackageName & virtual_name,
        const std::tr1::shared_ptr<const PackageID> & virtual_for,
        const bool exact) :
    PrivateImplementationPattern<VirtualsPackageID>(
            new Implementation<VirtualsPackageID>(e, owner, virtual_name, virtual_for, exact)),
    _imp(PrivateImplementationPattern<VirtualsPackageID>::_imp)
{
    add_metadata_key(_imp->virtual_for);
    add_metadata_key(_imp->bdep);
    add_metadata_key(_imp->rdep);
}

VirtualsPackageID::~VirtualsPackageID()
{
}

const std::string
VirtualsPackageID::canonical_form(const PackageIDCanonicalForm f) const
{
    switch (f)
    {
        case idcf_full:
            return stringify(_imp->name) + "-" + stringify(_imp->version) + "::" + stringify(_imp->repository->name()) +
                " (virtual for " + stringify(*_imp->virtual_for->value()) + ")";

        case idcf_no_version:
            return stringify(_imp->name) + "::" + stringify(_imp->repository->name()) +
                " (virtual for " + _imp->virtual_for->value()->canonical_form(idcf_no_version) + ")";

        case idcf_version:
            return stringify(_imp->version) + " (for " + stringify(_imp->virtual_for->value()->canonical_form(idcf_no_version)) + ")";

        case idcf_no_name:
            return stringify(_imp->version) + "::" + stringify(_imp->repository->name()) +
                " (virtual for " + stringify(*_imp->virtual_for->value()) + ")";

        case last_idcf:
            break;
    }

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

PackageDepSpec
VirtualsPackageID::uniquely_identifying_spec() const
{
    return parse_user_package_dep_spec("=" + stringify(name()) + "-" + stringify(version()) +
            (slot_key() ? ":" + stringify(slot_key()->value()) : "") + "::" + stringify(repository()->name()) +
            "[." + _imp->virtual_for->raw_name() + "=" + stringify(*_imp->virtual_for->value()) + "]",
            _imp->env, UserPackageDepSpecOptions());
}

const QualifiedPackageName
VirtualsPackageID::name() const
{
    return _imp->name;
}

const VersionSpec
VirtualsPackageID::version() const
{
    return _imp->version;
}

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

const std::tr1::shared_ptr<const MetadataValueKey<std::tr1::shared_ptr<const PackageID> > >
VirtualsPackageID::virtual_for_key() const
{
    return _imp->virtual_for;
}

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

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

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

const std::tr1::shared_ptr<const MetadataSpecTreeKey<DependencySpecTree> >
VirtualsPackageID::build_dependencies_key() const
{
    return _imp->bdep;
}

const std::tr1::shared_ptr<const MetadataSpecTreeKey<DependencySpecTree> >
VirtualsPackageID::run_dependencies_key() const
{
    return _imp->rdep;
}

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

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

const std::tr1::shared_ptr<const MetadataValueKey<std::string> >
VirtualsPackageID::short_description_key() const
{
    return std::tr1::shared_ptr<const MetadataValueKey<std::string> >();
}

const std::tr1::shared_ptr<const MetadataValueKey<std::string> >
VirtualsPackageID::long_description_key() const
{
    return std::tr1::shared_ptr<const MetadataValueKey<std::string> >();
}

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

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

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

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

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

bool
VirtualsPackageID::arbitrary_less_than_comparison(const PackageID & a) const
{
    if (a.virtual_for_key())
        return PackageIDSetComparator()(_imp->virtual_for->value(), a.virtual_for_key()->value());
    return false;
}

std::size_t
VirtualsPackageID::extra_hash_value() const
{
    return Hash<PackageID>()(*_imp->virtual_for->value());
}

void
VirtualsPackageID::need_keys_added() const
{
}

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
VirtualsPackageID::perform_action(Action & a) const
{
    PerformAction b(this);
    a.accept(b);
}

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

namespace
{
    class VirtualsAssociationMask :
        public AssociationMask
    {
        private:
            const std::tr1::shared_ptr<const PackageID> _id;

        public:
            VirtualsAssociationMask(const std::tr1::shared_ptr<const PackageID> & i) :
                _id(i)
            {
            }

            char key() const
            {
                return 'A';
            }

            const std::string description() const
            {
                return "by association";
            }

            const std::tr1::shared_ptr<const PackageID> associated_package() const
            {
                return _id;
            }
    };
}

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

    if (_imp->has_masks)
        return;

    if (_imp->virtual_for->value()->masked())
        add_mask(make_shared_ptr(new VirtualsAssociationMask(_imp->virtual_for->value())));

    _imp->has_masks = true;
}

void
VirtualsPackageID::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> >
VirtualsPackageID::breaks_portage() const
{
    return std::tr1::shared_ptr<const Set<std::string> >();
}

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

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

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

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

const std::tr1::shared_ptr<const MetadataValueKey<bool> >
VirtualsPackageID::transient_key() const
{
    return std::tr1::shared_ptr<const MetadataValueKey<bool> >();
}

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