aboutsummaryrefslogtreecommitdiff
path: root/bpkg/system-package-manager.cxx
blob: 215ea4751f9e56b27504e94ea6012dd0dabdac57 (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
// file      : bpkg/system-package-manager.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#include <bpkg/system-package-manager.hxx>

#include <libbutl/semantic-version.hxx>

#include <bpkg/package.hxx>
#include <bpkg/package-odb.hxx>
#include <bpkg/database.hxx>
#include <bpkg/diagnostics.hxx>

#include <bpkg/system-package-manager-debian.hxx>

using namespace std;
using namespace butl;

namespace bpkg
{
  system_package_manager::
  ~system_package_manager ()
  {
    // vtable
  }

  unique_ptr<system_package_manager>
  make_system_package_manager (const target_triplet& host,
                               const string& name)
  {
    unique_ptr<system_package_manager> r;

    if (optional<os_release> osr = host_os_release (host))
    {
      if (host.class_ == "linux")
      {
        if (osr->name_id == "debian" ||
            osr->name_id == "ubuntu" ||
            find_if (osr->like_ids.begin (), osr->like_ids.end (),
                     [] (const string& n)
                     {
                       return n == "debian" || n == "ubuntu";
                     }) != osr->like_ids.end ())
        {
          // @@ TODO: verify name if specified.

          // @@ TMP
          //
          //r.reset (new system_package_manager_debian (move (*osr)));
        }
      }
    }

    if (r == nullptr)
    {
      if (!name.empty ())
        fail << "unsupported package manager '" << name << "' for host "
             << host;
    }

    return r;
  }

  strings system_package_manager::
  system_package_names (const available_packages& aps,
                        const string& name_id,
                        const string& version_id,
                        const string& like_id)
  {
    assert (!aps.empty ());

    // Parse the version id if it is not empty and leave it "0" otherwise.
    //
    semantic_version vid;

    if (!version_id.empty ())
    try
    {
      vid = semantic_version (version_id, semantic_version::allow_omit_minor);
    }
    catch (const invalid_argument& e)
    {
      fail << "invalid version '" << version_id << "' for " << name_id
           << " operating system: " << e;
    }

    // Return those <name>[_<version>]-name distribution values of the
    // specified available packages whose <name> component matches the
    // specified distribution name and the <version> component (assumed as "0"
    // if not present) is less or equal the specified distribution version.
    // Suppress duplicate entries with the same name (so that distribution
    // values of the later available package versions are preferred) or value.
    //
    using values = vector<reference_wrapper<const distribution_name_value>>;

    auto name_values = [&aps] (const string& n, const semantic_version& v)
    {
      values r;
      for (const auto& a: aps)
      {
        const shared_ptr<available_package>& ap (a.first);

        for (const distribution_name_value& nv: ap->distribution_values)
        {
          if (optional<string> d = nv.distribution ("-name"))
          {
            string dn (move (*d));     // <name>[_<version>]
            size_t p (dn.rfind ('_')); // Version-separating underscore.

            // If '_' separator is present, then make sure that the right-hand
            // part looks like a version (not empty and only contains digits
            // and dots).
            //
            if (p != string::npos)
            {
              if (p != dn.size () - 1)
              {
                for (size_t i (p); i != dn.size (); ++i)
                {
                  if (!digit (dn[i]) && dn[i] != '.')
                  {
                    p = string::npos;
                    break;
                  }
                }
              }
              else
                p = string::npos;
            }

            // Parse the distribution version if present and leave it "0"
            // otherwise.
            //
            semantic_version dv;
            if (p != string::npos)
            try
            {
              dv = semantic_version (dn,
                                     p + 1,
                                     semantic_version::allow_omit_minor);
            }
            catch (const invalid_argument& e)
            {
              fail << "invalid distribution version in value " << nv.name
                   << " for package " << ap->id.name << ' ' << ap->version
                   << a.second.database () << " in repository "
                   << a.second.load ()->location << ": " << e;
            }

            dn.resize (p);

            if (dn == n &&
                dv <= v &&
                find_if (r.begin (), r.end (),
                         [&nv] (const distribution_name_value& v)
                         {return v.name == nv.name || v.value == nv.value;}) ==
                r.end ())
            {
              r.push_back (nv);
            }
          }
        }
      }

      return r;
    };

    // Collect the <distribution>-name values that match the name id and refer
    // to the version which is less or equal than the version id.
    //
    values vs (name_values (name_id, vid));

    // If the resulting list is empty and the like id is specified, then
    // re-collect but now using the like id and "0" version id instead.
    //
    if (vs.empty () && !like_id.empty ())
      vs = name_values (like_id, semantic_version (0, 0, 0));

    // Return the values of the collected name/values list.
    //
    strings r;
    r.reserve (vs.size ());

    transform (vs.begin (), vs.end (),
               back_inserter (r),
               [] (const distribution_name_value& v) {return v.value;});

    return r;
  }
}