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

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

#include <map>
#include <iostream>

#include <bpkg/types.hxx>
#include <bpkg/utility.hxx>

#undef NDEBUG
#include <cassert>

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

using namespace std;

namespace bpkg
{
  using package_status = system_package_status_debian;
  using package_policy = package_status::package_policy;

  using butl::manifest_parser;
  using butl::manifest_parsing;

  // Usage: args[0] <command> ...
  //
  // Where <command> is one of:
  //
  //   apt-cache-policy <pkg>...          result comes from stdin
  //
  //   apt-cache-show <pkg> <ver>         result comes from stdin
  //
  //   parse-name-value <pkg>             debian-name value from stdin
  //
  //   main-from-dev <dev-pkg> <dev-ver>  depends comes from stdin
  //
  //   build <query-pkg>... [--install [--no-fetch] <install-pkg>...]
  //
  // The stdin of the build command is used to read the simulation description
  // which consists of lines in the following forms (blanks are ignored):
  //
  // manifest: <query-pkg> <file>
  //
  //   Available package manifest for one of <query-pkg>. If none is
  //   specified, then a stub is automatically added.
  //
  // apt-cache-policy[-{fetched,installed}]: <sys-pkg>... <file>
  //
  //   Values for simulation::apt_cache_policy_*. If <file> is the special `!`
  //   value, then make the entry empty.
  //
  // apt-cache-show[-fetched]: <sys-pkg> <sys-ver> <file>
  //
  //   Values for simulation::apt_cache_show_*. If <file> is the special `!`
  //   value, then make the entry empty.
  //
  // apt-get-update-fail: true
  // apt-get-install-fail: true
  //
  //   Values for simulation::apt_get_{update,install}_fail_.
  //
  int
  main (int argc, char* argv[])
  try
  {
    assert (argc >= 2); // <command>

    string cmd (argv[1]);

    // @@ TODO: add option to customize? Maybe option before command?
    //
    os_release osr {"debian", {}, "10", "", "Debian", "", ""};

    if (cmd == "apt-cache-policy")
    {
      assert (argc >= 3); // <pkg>...

      strings key;
      vector<package_policy> pps;
      for (int i (2); i != argc; ++i)
      {
        key.push_back (argv[i]);
        pps.push_back (package_policy (argv[i]));
      }

      system_package_manager_debian::simulation s;
      s.apt_cache_policy_.emplace (move (key), path ("-"));

      system_package_manager_debian m (move (osr),
                                       host_triplet,
                                       ""      /* arch */,
                                       nullopt /* progress */,
                                       nullopt /* fetch_timeout */,
                                       false   /* install */,
                                       false   /* fetch */,
                                       false   /* yes */,
                                       "sudo");
      m.simulate_ = &s;

      m.apt_cache_policy (pps);

      for (const package_policy& pp: pps)
      {
        cout << pp.name << " '"
             << pp.installed_version << "' '"
             << pp.candidate_version << "'\n";
      }
    }
    else if (cmd == "apt-cache-show")
    {
      assert (argc == 4); // <pkg> <ver>

      pair<string, string> key (argv[2], argv[3]);

      system_package_manager_debian::simulation s;
      s.apt_cache_show_.emplace (key, path ("-"));

      system_package_manager_debian m (move (osr),
                                       host_triplet,
                                       ""      /* arch */,
                                       nullopt /* progress */,
                                       nullopt /* fetch_timeout */,
                                       false   /* install */,
                                       false   /* fetch */,
                                       false   /* yes */,
                                       "sudo");
      m.simulate_ = &s;

      cout << m.apt_cache_show (key.first, key.second) << '\n';
    }
    else if (cmd == "parse-name-value")
    {
      assert (argc == 3); // <pkg>

      package_name pn (argv[2]);

      string v;
      getline (cin, v);

      package_status s (
        system_package_manager_debian::parse_name_value (pn, v, false, false));

      if (!s.main.empty ())   cout << "main: "   << s.main   << '\n';
      if (!s.dev.empty ())    cout << "dev: "    << s.dev    << '\n';
      if (!s.doc.empty ())    cout << "doc: "    << s.doc    << '\n';
      if (!s.dbg.empty ())    cout << "dbg: "    << s.dbg    << '\n';
      if (!s.common.empty ()) cout << "common: " << s.common << '\n';
      if (!s.extras.empty ())
      {
        cout << "extras:";
        for (const string& e: s.extras)
          cout << ' ' << e;
        cout << '\n';
      }
    }
    else if (cmd == "main-from-dev")
    {
      assert (argc == 4); // <dev-pkg> <dev-ver>

      string n (argv[2]);
      string v (argv[3]);
      string d;
      getline (cin, d);

      cout << system_package_manager_debian::main_from_dev (n, v, d) << '\n';
    }
    else if (cmd == "build")
    {
      assert (argc >= 3); // <query-pkg>...

      strings qps;
      map<string, available_packages> aps;

      // Parse <query-pkg>...
      //
      int argi (2);
      for (; argi != argc; ++argi)
      {
        string a (argv[argi]);

        if (a.compare (0, 2, "--") == 0)
          break;

        aps.emplace (a, available_packages {});
        qps.push_back (move (a));
      }

      // Parse --install [--no-fetch]
      //
      bool install (false);
      bool fetch (true);

      for (; argi != argc; ++argi)
      {
        string a (argv[argi]);

        if (a == "--install") install = true;
        else if (a == "--no-fetch") fetch = false;
        else break;
      }

      // Parse the description.
      //
      system_package_manager_debian::simulation s;

      for (string l; !eof (getline (cin, l)); )
      {
        if (l.empty ())
          continue;

        size_t p (l.find (':')); assert (p != string::npos);
        string k (l, 0, p);

        if (k == "manifest")
        {
          size_t q (l.rfind (' ')); assert (q != string::npos);
          string n (l, p + 2, q - p - 2); trim (n);
          string f (l, q + 1); trim (f);

          auto i (aps.find (n));
          if (i == aps.end ())
            fail << "unknown package " << n << " in '" << l << "'";

          i->second.push_back (make_available_from_manifest (n, f));
        }
        else if (
          map<strings, path>* policy =
          k == "apt-cache-policy"           ? &s.apt_cache_policy_ :
          k == "apt-cache-policy-fetched"   ? &s.apt_cache_policy_fetched_ :
          k == "apt-cache-policy-installed" ? &s.apt_cache_policy_installed_ :
          nullptr)
        {
          size_t q (l.rfind (' ')); assert (q != string::npos);
          string n (l, p + 2, q - p - 2); trim (n);
          string f (l, q + 1); trim (f);

          strings ns;
          for (size_t b (0), e (0); next_word (n, b, e); )
            ns.push_back (string (n, b, e - b));

          if (f == "!")
            f.clear ();

          policy->emplace (move (ns), path (move (f)));
        }
        else if (map<pair<string, string>, path>* show =
                 k == "apt-cache-show"         ? &s.apt_cache_show_ :
                 k == "apt-cache-show-fetched" ? &s.apt_cache_show_fetched_ :
                 nullptr)
        {
          size_t q (l.rfind (' ')); assert (q != string::npos);
          string n (l, p + 2, q - p - 2); trim (n);
          string f (l, q + 1); trim (f);

          q = n.find (' '); assert (q != string::npos);
          pair<string, string> nv (string (n, 0, q), string (n, q + 1));
          trim (nv.second);

          if (f == "!")
            f.clear ();

          show->emplace (move (nv), path (move (f)));
        }
        else if (k == "apt-get-update-fail")
        {
          s.apt_get_update_fail_ = true;
        }
        else if (k == "apt-get-install-fail")
        {
          s.apt_get_install_fail_ = true;
        }
        else
          fail << "unknown keyword '" << k << "' in simulation description";
      }

      // Fallback to stubs and sort in the version descending order.
      //
      for (pair<const string, available_packages>& p: aps)
      {
        if (p.second.empty ())
          p.second.push_back (make_available_stub (p.first));

        sort_available (p.second);
      }

      system_package_manager_debian m (move (osr),
                                       host_triplet,
                                       ""      /* arch */,
                                       nullopt /* progress */,
                                       nullopt /* fetch_timeout */,
                                       install,
                                       fetch,
                                       false   /* yes */,
                                       "sudo");
      m.simulate_ = &s;

      // Query each package.
      //
      for (const string& n: qps)
      {
        package_name pn (n);

        const system_package_status* s (*m.pkg_status (pn, &aps[n]));

        assert (*m.pkg_status (pn, nullptr) == s); // Test caching.

        if (s == nullptr)
          fail << "no installed " << (install ? "or available " : "")
               << "system package for " << pn;

        cout << pn << ' ' << s->version
             << " (" << s->system_name << ' ' << s->system_version << ") ";

        switch (s->status)
        {
        case package_status::installed:           cout << "installed"; break;
        case package_status::partially_installed: cout << "part installed"; break;
        case package_status::not_installed:       cout << "not installed"; break;
        }

        cout << '\n';
      }

      // Install if requested.
      //
      if (install)
      {
        assert (argi != argc); // <install-pkg>...

        vector<package_name> ips;
        for (; argi != argc; ++argi)
          ips.push_back (package_name (argv[argi]));

        m.pkg_install (ips);
      }
    }
    else
      fail << "unknown command '" << cmd << "'";

    return 0;
  }
  catch (const failed&)
  {
    return 1;
  }
}

int
main (int argc, char* argv[])
{
  return bpkg::main (argc, argv);
}