aboutsummaryrefslogtreecommitdiff
path: root/paludis/environment/no_config/no_config_environment.cc
blob: 7ab925e4125da095ecaef357c3023c3cf5b30de8 (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
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2006 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 "no_config_environment.hh"
#include <paludis/util/collection_concrete.hh>
#include <paludis/util/tokeniser.hh>
#include <paludis/util/log.hh>
#include <paludis/repositories/portage/portage_repository.hh>
#include <paludis/config_file.hh>
#include <set>

using namespace paludis;

#include <paludis/environment/no_config/no_config_environment-sr.cc>

namespace paludis
{
    template<>
    struct Implementation<NoConfigEnvironment> :
        InternalCounted<Implementation<NoConfigEnvironment> >
    {
        const FSEntry top_level_dir;
        bool accept_unstable;
        std::set<KeywordName> accepted_keywords;
        std::list<NoConfigEnvironmentProfilesDescLine> profiles;

        Implementation(Environment * const env, const NoConfigEnvironmentParams & params) :
            top_level_dir(params.repository_dir),
            accept_unstable(params.accept_unstable)
        {
            Context context("When initialising NoConfigEnvironment at '" + stringify(params.repository_dir) + "':");

            try
            {
                LineConfigFile profiles_desc(params.repository_dir / "profiles" / "profiles.desc");
                for (LineConfigFile::Iterator line(profiles_desc.begin()), line_end(profiles_desc.end()) ;
                        line != line_end ; ++line)
                {
                    std::vector<std::string> tokens;
                    WhitespaceTokeniser::get_instance()->tokenise(*line, std::back_inserter(tokens));

                    if (tokens.size() != 3)
                    {
                        Log::get_instance()->message(ll_warning, lc_context, "Skipping invalid line '"
                                + *line + "'");
                        continue;
                    }

                    profiles.push_back(NoConfigEnvironmentProfilesDescLine::create()
                            .path(params.repository_dir / "profiles" / tokens.at(1))
                            .status(tokens.at(2))
                            .arch(tokens.at(0))
                            .db(PackageDatabase::Pointer(new PackageDatabase(env))));
                }
            }
            catch (const ConfigFileError & e)
            {
                Log::get_instance()->message(ll_warning, lc_context, "Could not load profiles.desc due to exception '"
                        + e.message() + "' (" + e.what() + ")");
            }

            if (profiles.empty())
                profiles.push_back(NoConfigEnvironmentProfilesDescLine::create()
                        .path(params.repository_dir / "profiles" / "base")
                        .status("default")
                        .arch("default")
                        .db(PackageDatabase::Pointer(new PackageDatabase(env))));

            for (std::list<NoConfigEnvironmentProfilesDescLine>::iterator
                    p(profiles.begin()), p_end(profiles.end()) ; p != p_end ; ++p)
            {
                AssociativeCollection<std::string, std::string>::Pointer keys(
                        new AssociativeCollection<std::string, std::string>::Concrete);

                keys->insert("format", "portage");
                keys->insert("location", stringify(params.repository_dir));
                keys->insert("profiles", stringify(p->path));

                p->db->add_repository(RepositoryMaker::get_instance()->find_maker("portage")(env,
                            p->db.raw_pointer(), keys));
                p->db->add_repository(RepositoryMaker::get_instance()->find_maker("virtuals")(env,
                            p->db.raw_pointer(), AssociativeCollection<std::string, std::string>::Pointer(0)));
            }
        }
    };
}

NoConfigEnvironment::NoConfigEnvironment(const NoConfigEnvironmentParams & params) :
    PrivateImplementationPattern<NoConfigEnvironment>(
            new Implementation<NoConfigEnvironment>(this, params)),
    Environment(_imp->profiles.begin()->db)
{
    /* do this to load accepted_keywords etc */
    set_profile(ProfilesIterator(_imp->profiles.begin()));
}

NoConfigEnvironment::~NoConfigEnvironment()
{
}

std::string
NoConfigEnvironment::paludis_command() const
{
    return "false";
}

FSEntry
NoConfigEnvironment::main_repository_dir() const
{
    return _imp->top_level_dir;
}

void
NoConfigEnvironment::set_profile(const NoConfigEnvironment::ProfilesIterator & i)
{
    change_package_database(i->db);

    _imp->accepted_keywords.clear();
    _imp->accepted_keywords.insert(KeywordName("*"));
    _imp->accepted_keywords.insert(KeywordName(i->arch));
    if (_imp->accept_unstable)
        _imp->accepted_keywords.insert(KeywordName("~" + i->arch));
}

void
NoConfigEnvironment::set_profile(const FSEntry & location)
{
    Context context("When setting NoConfigEnvironment profile to '" + stringify(location) + "':");

    for (ProfilesIterator i(begin_profiles()), i_end(end_profiles()) ; i != i_end ; ++i)
        if (i->path == location)
        {
            set_profile(i);
            return;
        }

    Log::get_instance()->message(ll_warning, lc_context, "No profiles.desc entry found, faking it");

    PackageDatabase::Pointer db(new PackageDatabase(this));
    AssociativeCollection<std::string, std::string>::Pointer keys(
            new AssociativeCollection<std::string, std::string>::Concrete);

    keys->insert("format", "portage");
    keys->insert("location", stringify(_imp->top_level_dir));
    keys->insert("profiles", stringify(location));

    PortageRepository::Pointer p(RepositoryMaker::get_instance()->find_maker("portage")(this,
                db.raw_pointer(), keys));
    db->add_repository(RepositoryMaker::get_instance()->find_maker("virtuals")(this,
                db.raw_pointer(), AssociativeCollection<std::string, std::string>::Pointer(0)));
    db->add_repository(p);

    _imp->accepted_keywords.clear();
    _imp->accepted_keywords.insert(KeywordName("*"));
    _imp->accepted_keywords.insert(KeywordName(p->profile_variable("ARCH")));
    if (_imp->accept_unstable)
        _imp->accepted_keywords.insert(KeywordName("~" + p->profile_variable("ARCH")));

    change_package_database(db);
}

bool
NoConfigEnvironment::accept_keyword(const KeywordName & k, const PackageDatabaseEntry * const) const
{
    return _imp->accepted_keywords.end() != _imp->accepted_keywords.find(k);
}

NoConfigEnvironment::ProfilesIterator
NoConfigEnvironment::begin_profiles() const
{
    return ProfilesIterator(_imp->profiles.begin());
}

NoConfigEnvironment::ProfilesIterator
NoConfigEnvironment::end_profiles() const
{
    return ProfilesIterator(_imp->profiles.end());
}