aboutsummaryrefslogtreecommitdiff
path: root/paludis/repositories/e/exheres_profile.cc
blob: ca8338402e53e96d4a29c3c739a769357967c098 (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
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2005, 2006, 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/e/exheres_profile.hh>
#include <paludis/repositories/e/e_repository_mask_file.hh>
#include <paludis/repositories/e/e_repository_exceptions.hh>
#include <paludis/repositories/e/e_repository.hh>
#include <paludis/repositories/e/profile_file.hh>
#include <paludis/repositories/e/eapi.hh>

#include <paludis/util/log.hh>
#include <paludis/util/tokeniser.hh>
#include <paludis/util/private_implementation_pattern-impl.hh>
#include <paludis/util/wrapped_forward_iterator.hh>
#include <paludis/util/wrapped_output_iterator.hh>
#include <paludis/util/config_file.hh>
#include <paludis/util/make_shared_ptr.hh>
#include <paludis/util/map.hh>
#include <paludis/util/mutex.hh>
#include <paludis/util/set.hh>
#include <paludis/util/make_named_values.hh>
#include <paludis/util/hashes.hh>
#include <paludis/util/system.hh>
#include <paludis/choice.hh>
#include <paludis/dep_tag.hh>
#include <paludis/environment.hh>
#include <paludis/match_package.hh>
#include <paludis/distribution.hh>
#include <paludis/package_id.hh>
#include <paludis/metadata_key.hh>
#include <paludis/paludislike_options_conf.hh>
#include <tr1/unordered_map>
#include <list>

using namespace paludis;
using namespace paludis::erepository;

namespace
{
    const std::tr1::shared_ptr<const LineConfigFile> make_config_file(
            const FSEntry & f,
            const LineConfigFileOptions & o)
    {
        return make_shared_ptr(new LineConfigFile(f, o));
    }

    typedef std::tr1::unordered_map<std::string, std::string, Hash<std::string> > EnvironmentVariablesMap;
    typedef std::tr1::unordered_map<QualifiedPackageName,
            std::list<std::pair<std::tr1::shared_ptr<const PackageDepSpec>, std::tr1::shared_ptr<const RepositoryMaskInfo> > >,
            Hash<QualifiedPackageName> > PackageMaskMap;
}

namespace paludis
{
    template<>
    struct Implementation<ExheresProfile>
    {
        const Environment * const env;
        const ERepository * const repository;

        std::tr1::shared_ptr<FSEntrySequence> profiles_with_parents;

        PaludisLikeOptionsConf options_conf;
        EnvironmentVariablesMap environment_variables;
        PackageMaskMap package_mask;

        ProfileFile<MaskFile> package_mask_file;
        ProfileFile<LineConfigFile> packages_file;

        const std::tr1::shared_ptr<Set<std::string> > use_expand;
        const std::tr1::shared_ptr<Set<std::string> > use_expand_hidden;
        const std::tr1::shared_ptr<Set<std::string> > use_expand_unprefixed;
        const std::tr1::shared_ptr<Set<std::string> > use_expand_implicit;
        const std::tr1::shared_ptr<Set<std::string> > iuse_implicit;
        const std::tr1::shared_ptr<Set<std::string> > use_expand_values;

        const std::tr1::shared_ptr<SetSpecTree> system_packages;
        const std::tr1::shared_ptr<GeneralSetDepTag> system_tag;

        Implementation(const Environment * const e, const ERepository * const p,
                const RepositoryName & name, const FSEntrySequence &,
                const std::string &, const bool) :
            env(e),
            repository(p),
            profiles_with_parents(new FSEntrySequence),
            options_conf(make_named_values<PaludisLikeOptionsConfParams>(
                        value_for<n::allow_locking>(true),
                        value_for<n::environment>(e),
                        value_for<n::make_config_file>(&make_config_file)
                        )),
            package_mask_file(p),
            packages_file(p),
            use_expand(new Set<std::string>),
            use_expand_hidden(new Set<std::string>),
            use_expand_unprefixed(new Set<std::string>),
            use_expand_implicit(new Set<std::string>),
            iuse_implicit(new Set<std::string>),
            use_expand_values(new Set<std::string>),
            system_packages(new SetSpecTree(make_shared_ptr(new AllDepSpec))),
            system_tag(new GeneralSetDepTag(SetName("system"), stringify(name)))
        {
            environment_variables["CONFIG_PROTECT"] = getenv_with_default("CONFIG_PROTECT", "/etc");
            environment_variables["CONFIG_PROTECT_MASK"] = getenv_with_default("CONFIG_PROTECT_MASK", "");
        }
    };
}

ExheresProfile::ExheresProfile(
        const Environment * const env, const ERepository * const p, const RepositoryName & name,
        const FSEntrySequence & location,
        const std::string & arch_var_if_special, const bool x) :
    PrivateImplementationPattern<ExheresProfile>(
            new Implementation<ExheresProfile>(env, p, name, location, arch_var_if_special, x))
{
    for (FSEntrySequence::ConstIterator l(location.begin()), l_end(location.end()) ;
            l != l_end ; ++l)
        _load_dir(*l);

    const std::tr1::shared_ptr<const Set<UnprefixedChoiceName> > s(_imp->options_conf.known_choice_value_names(
                make_null_shared_ptr(), ChoicePrefixName("suboptions")));
    for (Set<UnprefixedChoiceName>::ConstIterator f(s->begin()), f_end(s->end()) ;
            f != f_end ; ++f)
        if (_imp->options_conf.want_choice_enabled_locked(make_null_shared_ptr(),
                    ChoicePrefixName("suboptions"), *f).first.is_true())
            _imp->use_expand->insert(stringify(*f));

    const std::tr1::shared_ptr<const Set<UnprefixedChoiceName> > sh(_imp->options_conf.known_choice_value_names(
                make_null_shared_ptr(), ChoicePrefixName("hidden_suboptions")));
    for (Set<UnprefixedChoiceName>::ConstIterator f(sh->begin()), f_end(sh->end()) ;
            f != f_end ; ++f)
        if (_imp->options_conf.want_choice_enabled_locked(make_null_shared_ptr(),
                    ChoicePrefixName("hidden_suboptions"), *f).first.is_true())
            _imp->use_expand_hidden->insert(stringify(*f));

    if (! _imp->repository->params().master_repositories())
        for (ProfileFile<LineConfigFile>::ConstIterator i(_imp->packages_file.begin()),
                i_end(_imp->packages_file.end()) ; i != i_end ; ++i)
        {
            if (0 != i->second.compare(0, 1, "*", 0, 1))
                continue;

            Context context_spec("When parsing '" + i->second + "':");
            std::tr1::shared_ptr<PackageDepSpec> spec(new PackageDepSpec(
                        parse_elike_package_dep_spec(i->second.substr(1),
                            i->first->supported()->package_dep_spec_parse_options(),
                            i->first->supported()->version_spec_options(),
                            std::tr1::shared_ptr<const PackageID>())));

            spec->set_tag(_imp->system_tag);
            _imp->system_packages->root()->append(spec);
        }

    for (ProfileFile<MaskFile>::ConstIterator line(_imp->package_mask_file.begin()), line_end(_imp->package_mask_file.end()) ;
            line != line_end ; ++line)
    {
        if (line->second.first.empty())
            continue;

        try
        {
            std::tr1::shared_ptr<const PackageDepSpec> a(new PackageDepSpec(
                        parse_elike_package_dep_spec(line->second.first,
                            line->first->supported()->package_dep_spec_parse_options(),
                            line->first->supported()->version_spec_options(),
                            std::tr1::shared_ptr<const PackageID>())));

            if (a->package_ptr())
                _imp->package_mask[*a->package_ptr()].push_back(std::make_pair(a, line->second.second));
            else
                Log::get_instance()->message("e.profile.package_mask.bad_spec", ll_warning, lc_context)
                    << "Loading package.mask spec '" << line->second.first << "' failed because specification does not restrict to a "
                    "unique package";
        }
        catch (const InternalError &)
        {
            throw;
        }
        catch (const Exception & e)
        {
            Log::get_instance()->message("e.profile.package_mask.bad_spec", ll_warning, lc_context)
                << "Loading package.mask spec '" << line->second.first << "' failed due to exception '" << e.message() << "' ("
                << e.what() << ")";
        }
    }
}

ExheresProfile::~ExheresProfile()
{
}

void
ExheresProfile::_load_dir(const FSEntry & f)
{
    if (! f.is_directory_or_symlink_to_directory())
    {
        Log::get_instance()->message("e.exheres_profile.not_a_directory", ll_warning, lc_context) <<
            "Profile component '" << f << "' is not a directory";
        return;
    }

    if ((f / "parents.conf").exists())
    {
        LineConfigFile file(f / "parents.conf", LineConfigFileOptions());
        for (LineConfigFile::ConstIterator line(file.begin()), line_end(file.end()) ;
                line != line_end ; ++line)
            _load_dir((f / *line).realpath());
    }

    if ((f / "options.conf").exists())
        _imp->options_conf.add_file(f / "options.conf");

    if ((f / "package_mask.conf").exists())
        _imp->package_mask_file.add_file(f / "package_mask.conf");

    if ((f / "packages").exists())
        _imp->packages_file.add_file(f / "packages");

    if ((f / "make.defaults").exists())
    {
        const std::tr1::shared_ptr<const EAPI> eapi(EAPIData::get_instance()->eapi_from_string(
                    _imp->repository->eapi_for_file(f / "make.defaults")));
        if (! eapi->supported())
            throw ERepositoryConfigurationError("Can't use profile directory '" + stringify(f) +
                    "' because it uses an unsupported EAPI");

        KeyValueConfigFile file(f / "make.defaults", KeyValueConfigFileOptions() +
                kvcfo_disallow_source + kvcfo_disallow_space_inside_unquoted_values +
                kvcfo_allow_inline_comments + kvcfo_allow_multiple_assigns_per_line,
                &KeyValueConfigFile::no_defaults, &KeyValueConfigFile::no_transformation);

        for (KeyValueConfigFile::ConstIterator k(file.begin()), k_end(file.end()) ;
                k != k_end ; ++k)
            _imp->environment_variables[k->first] = k->second;
    }

    _imp->profiles_with_parents->push_back(f);
}

std::tr1::shared_ptr<const FSEntrySequence>
ExheresProfile::profiles_with_parents() const
{
    return _imp->profiles_with_parents;
}

bool
ExheresProfile::use_masked(
        const std::tr1::shared_ptr<const PackageID> & id,
        const std::tr1::shared_ptr<const Choice> & choice,
        const UnprefixedChoiceName & value_unprefixed,
        const ChoiceNameWithPrefix &
        ) const
{
    std::pair<Tribool, bool> enabled_locked(_imp->options_conf.want_choice_enabled_locked(
                id, choice->prefix(), value_unprefixed));
    return enabled_locked.first.is_false() && enabled_locked.second;
}

bool
ExheresProfile::use_forced(
        const std::tr1::shared_ptr<const PackageID> & id,
        const std::tr1::shared_ptr<const Choice> & choice,
        const UnprefixedChoiceName & value_unprefixed,
        const ChoiceNameWithPrefix &
        ) const
{
    std::pair<Tribool, bool> enabled_locked(_imp->options_conf.want_choice_enabled_locked(
                id, choice->prefix(), value_unprefixed));
    return enabled_locked.first.is_true() && enabled_locked.second;
}

Tribool
ExheresProfile::use_state_ignoring_masks(
        const std::tr1::shared_ptr<const PackageID> & id,
        const std::tr1::shared_ptr<const Choice> & choice,
        const UnprefixedChoiceName & value_unprefixed,
        const ChoiceNameWithPrefix &
        ) const
{
    std::pair<Tribool, bool> enabled_locked(_imp->options_conf.want_choice_enabled_locked(
                id, choice->prefix(), value_unprefixed));
    return enabled_locked.first;
}

const std::tr1::shared_ptr<const Set<UnprefixedChoiceName> >
ExheresProfile::known_choice_value_names(
        const std::tr1::shared_ptr<const erepository::ERepositoryID> & id,
        const std::tr1::shared_ptr<const Choice> & choice
        ) const
{
    return _imp->options_conf.known_choice_value_names(id, choice->prefix());
}

const std::tr1::shared_ptr<const Set<std::string> >
ExheresProfile::use_expand() const
{
    return _imp->use_expand;
}

const std::tr1::shared_ptr<const Set<std::string> >
ExheresProfile::use_expand_hidden() const
{
    return _imp->use_expand_hidden;
}

const std::tr1::shared_ptr<const Set<std::string> >
ExheresProfile::use_expand_unprefixed() const
{
    return _imp->use_expand_unprefixed;
}

const std::tr1::shared_ptr<const Set<std::string> >
ExheresProfile::use_expand_implicit() const
{
    return _imp->use_expand_implicit;
}

const std::tr1::shared_ptr<const Set<std::string> >
ExheresProfile::iuse_implicit() const
{
    return _imp->iuse_implicit;
}

const std::tr1::shared_ptr<const Set<std::string> >
ExheresProfile::use_expand_values(const std::string &) const
{
    return _imp->use_expand_values;
}

const std::string
ExheresProfile::environment_variable(const std::string & s) const
{
    EnvironmentVariablesMap::const_iterator i(_imp->environment_variables.find(s));
    if (_imp->environment_variables.end() == i)
    {
        Log::get_instance()->message("e.exheres_profile.unknown", ll_warning, lc_context) <<
            "Something is asking for environment variable '" << s << "' from profiles, but that isn't in "
            "our list of special vars. This is probably a bug.";
        return "";
    }
    else
        return i->second;
}

const std::tr1::shared_ptr<const RepositoryMaskInfo>
ExheresProfile::profile_masked(const PackageID & id) const
{
    PackageMaskMap::const_iterator rr(_imp->package_mask.find(id.name()));
    if (_imp->package_mask.end() == rr)
        return std::tr1::shared_ptr<const RepositoryMaskInfo>();
    else
    {
        for (std::list<std::pair<std::tr1::shared_ptr<const PackageDepSpec>, std::tr1::shared_ptr<const RepositoryMaskInfo> > >::const_iterator k(rr->second.begin()),
                k_end(rr->second.end()) ; k != k_end ; ++k)
            if (match_package(*_imp->env, *k->first, id, MatchPackageOptions()))
                return k->second;
    }

    return std::tr1::shared_ptr<const RepositoryMaskInfo>();
}

const std::tr1::shared_ptr<const SetSpecTree>
ExheresProfile::system_packages() const
{
    return _imp->system_packages;
}

const std::tr1::shared_ptr<const Map<QualifiedPackageName, PackageDepSpec> >
ExheresProfile::virtuals() const
{
    return make_shared_ptr(new Map<QualifiedPackageName, PackageDepSpec>);
}