aboutsummaryrefslogtreecommitdiff
path: root/paludis/repositories/cran/package_dep_spec.cc
blob: 6c8e5550e0982ec55f17f55c13c87023f5aea981 (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
/* 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/cran/package_dep_spec.hh>
#include <paludis/repositories/cran/normalise.hh>
#include <paludis/util/strip.hh>
#include <paludis/util/tokeniser.hh>
#include <paludis/util/sequence.hh>
#include <paludis/util/wrapped_forward_iterator.hh>
#include <paludis/util/make_named_values.hh>
#include <paludis/util/options.hh>
#include <paludis/util/make_null_shared_ptr.hh>
#include <paludis/dep_spec.hh>
#include <paludis/dep_spec_data.hh>
#include <paludis/version_operator.hh>
#include <paludis/version_spec.hh>
#include <paludis/version_requirements.hh>
#include <vector>

using namespace paludis;
using namespace paludis::cranrepository;

namespace
{
    struct CRANPackageDepSpecData :
        PackageDepSpecData
    {
        std::string unnormalised_package_name;
        std::shared_ptr<const QualifiedPackageName> package_v;
        std::shared_ptr<VersionRequirements> version_requirements_v;

        virtual std::string as_string() const
        {
            std::string result(unnormalised_package_name);

            if (version_requirements_v && ! version_requirements_v->empty())
            {
                result.append(" (");
                bool first(true);
                for (VersionRequirements::ConstIterator r(version_requirements_v->begin()),
                        r_end(version_requirements_v->end()) ; r != r_end ; ++r)
                {
                    if (! first)
                        result.append(", ");
                    first = false;
                    result.append(stringify(r->version_operator()));
                    result.append(" ");
                    result.append(stringify(r->version_spec()));
                }
                result.append(")");
            }

            return result;
        }

        virtual std::shared_ptr<const QualifiedPackageName> package_ptr() const
        {
            return package_v;
        }

        virtual std::shared_ptr<const PackageNamePart> package_name_part_ptr() const
        {
            return std::shared_ptr<const PackageNamePart>();
        }

        virtual std::shared_ptr<const CategoryNamePart> category_name_part_ptr() const
        {
            return std::shared_ptr<const CategoryNamePart>();
        }

        virtual std::shared_ptr<const VersionRequirements> version_requirements_ptr() const
        {
            return version_requirements_v;
        }

        virtual VersionRequirementsMode version_requirements_mode() const
        {
            return vr_and;
        }

        virtual std::shared_ptr<const SlotRequirement> slot_requirement_ptr() const
        {
            return std::shared_ptr<const SlotRequirement>();
        }

        virtual std::shared_ptr<const RepositoryName> from_repository_ptr() const
        {
            return std::shared_ptr<const RepositoryName>();
        }

        virtual std::shared_ptr<const RepositoryName> in_repository_ptr() const
        {
            return std::shared_ptr<const RepositoryName>();
        }

        virtual std::shared_ptr<const AdditionalPackageDepSpecRequirements> additional_requirements_ptr() const
        {
            return std::shared_ptr<const AdditionalPackageDepSpecRequirements>();
        }

        virtual std::shared_ptr<const MetadataSectionKey> annotations_key() const
        {
            return std::shared_ptr<const MetadataSectionKey>();
        }

        CRANPackageDepSpecData & version_requirement(const VersionRequirement & v)
        {
            if (! version_requirements_v)
                version_requirements_v = std::make_shared<VersionRequirements>();
            version_requirements_v->push_back(v);
            return *this;
        }

        CRANPackageDepSpecData & package(const std::string & s)
        {
            unnormalised_package_name = s;
            if ("R" == s)
                package_v = std::make_shared<QualifiedPackageName>("dev-lang/R");
            else
                package_v = std::make_shared<QualifiedPackageName>(CategoryNamePart("cran") + PackageNamePart(cran_name_to_internal(s)));

            return *this;
        }

        virtual std::shared_ptr<const InstallableToRepository> installable_to_repository_ptr() const
        {
            return make_null_shared_ptr();
        }

        virtual std::shared_ptr<const FSPath> installed_at_path_ptr() const
        {
            return make_null_shared_ptr();
        }

        virtual std::shared_ptr<const InstallableToPath> installable_to_path_ptr() const
        {
            return make_null_shared_ptr();
        }

        virtual const PartiallyMadePackageDepSpecOptions options_for_partially_made_package_dep_spec() const
        {
            return { };
        }
    };
}

PackageDepSpec
paludis::cranrepository::parse_cran_package_dep_spec(const std::string & ss)
{
    Context context("When parsing CRAN package dep spec '" + ss + "':");

    std::shared_ptr<CRANPackageDepSpecData> data(std::make_shared<CRANPackageDepSpecData>());
    std::string s(ss);

    std::string::size_type p(s.find('('));
    if (std::string::npos != p)
    {
        std::string restrictions(s.substr(p));
        s.erase(p);
        s = strip_trailing(s, " \r\n\t");

        if (restrictions.empty() || (')' != restrictions.at(restrictions.length() - 1)))
            throw PackageDepSpecError("Invalid () part in '" + ss + "'");
        restrictions = strip_leading(strip_trailing(restrictions.substr(1, restrictions.length() - 2), " \r\n\t"), " \r\n\t");

        std::vector<std::string> tokens;
        tokenise<delim_kind::AnyOfTag, delim_mode::DelimiterTag>(restrictions, ",", "", std::back_inserter(tokens));
        if (tokens.empty())
            throw PackageDepSpecError("Invalid empty () part in '" + ss + "'");

        for (std::vector<std::string>::const_iterator t(tokens.begin()), t_end(tokens.end()) ;
                t != t_end ; ++t)
        {
            std::vector<std::string> subtokens;
            tokenise_whitespace(*t, std::back_inserter(subtokens));
            if (subtokens.size() == 1)
            {
                std::string::size_type vp(subtokens[0].find_first_not_of("<>="));
                if (std::string::npos != vp)
                {
                    subtokens.push_back(subtokens[0].substr(vp));
                    subtokens[0].erase(vp);
                }
            }

            if (subtokens.size() != 2)
                throw PackageDepSpecError("Invalid () entry '" + *t + "' in '" + ss + "'");
            data->version_requirement(make_named_values<VersionRequirement>(
                        n::version_operator() = VersionOperator(subtokens[0]),
                        n::version_spec() = VersionSpec(cran_version_to_internal(subtokens[1]), { })));
        }
    }

    data->package(s);

    return PackageDepSpec(data);
}