aboutsummaryrefslogtreecommitdiff
path: root/bpkg/system-package-manager-fedora.test.cxx
blob: 4e59da14f5ef936fdc3ffbde09543ab08b19d6ab (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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
// file      : bpkg/system-package-manager-fedora.test.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#include <bpkg/system-package-manager-fedora.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_fedora;
  using package_info_ = package_status::package_info;
  using package = system_package_manager_fedora::simulation::package;

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

  // Usage: args[0] <command> ...
  //
  // Where <command> is one of:
  //
  //   dnf-list <pkg>...                          result comes from stdin
  //
  //   dnf-repoquery-requires <pkg> <ver> <arc>   result comes from stdin
  //
  //   parse-name-value <pkg>                     fedora-name value from stdin
  //
  //   main-from-devel <dev-pkg> <dev-ver>        depends comes from stdin in
  //                                              the `<dep-pkg> <dep-ver>`
  //                                              per line form
  //
  //   map-package                                manifest 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.
  //
  // dnf-list[-{fetched,installed}]: <sys-pkg>... <file>
  //
  //   Values for simulation::dnf_list_*. If <file> is the special `!` value,
  //   then make the entry empty.
  //
  // dnf-repoquery-requires[-fetched]: <sys-pkg> <sys-ver> <sys-arch> <file>
  //
  //   Values for simulation::dnf_repoquery_requires_*. If <file> is the
  //   special `!` value, then make the entry empty.
  //
  // dnf_makecache-fail: true
  // dnf-install-fail: true
  // dnf-mark-install-fail: true
  //
  //   Values for simulation::dnf_{makecache,install,mark_install}_fail_.
  //
  // While creating the system package manager always pretend to be the x86_64
  // Fedora host (x86_64-redhat-linux-gnu), regardless of the actual host
  // platform.
  //
  int
  main (int argc, char* argv[])
  try
  {
    assert (argc >= 2); // <command>

    target_triplet host_triplet ("x86_64-redhat-linux-gnu");

    string cmd (argv[1]);

    // @@ TODO: add option to customize? Maybe option before command?
    //
    os_release osr {"fedora", {}, "35", "", "Fedora Linux", "", ""};

    auto to_bool = [] (const string& s)
    {
      assert (s == "true" || s == "false");
      return s == "true";
    };

    if (cmd == "dnf-list")
    {
      assert (argc >= 3); // <pkg>...

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

      system_package_manager_fedora::simulation s;
      s.dnf_list_.emplace (move (key), path ("-"));

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

      m.dnf_list (pis);

      for (const package_info_& pi: pis)
      {
        cout << pi.name << " '"
             << pi.installed_version << "' '"
             << pi.installed_arch    << "' '"
             << pi.candidate_version << "' '"
             << pi.candidate_arch    << "'\n";
      }
    }
    else if (cmd == "dnf-repoquery-requires")
    {
      assert (argc == 6); // <pkg> <ver> <arch> <installed>

      package key {argv[2], argv[3], argv[4], to_bool (argv[5])};

      system_package_manager_fedora::simulation s;
      s.dnf_repoquery_requires_.emplace (key, path ("-"));

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

      for (const pair<string, string>& d:
             m.dnf_repoquery_requires (key.name,
                                       key.version,
                                       key.arch,
                                       key.installed))
      {
        cout << d.first << ' ' << d.second << '\n';
      }
    }
    else if (cmd == "parse-name-value")
    {
      assert (argc == 3); // <pkg>

      package_name pn (argv[2]);
      string pt (package_manifest::effective_type (nullopt, pn));

      string v;
      getline (cin, v);

      package_status s (
        system_package_manager_fedora::parse_name_value (
          pt, v, false, false, false));

      if (!s.main.empty ())        cout << "main: "        << s.main        << '\n';
      if (!s.devel.empty ())       cout << "devel: "       << s.devel       << '\n';
      if (!s.static_.empty ())     cout << "static: "      << s.static_     << '\n';
      if (!s.doc.empty ())         cout << "doc: "         << s.doc         << '\n';
      if (!s.debuginfo.empty ())   cout << "debuginfo: "   << s.debuginfo   << '\n';
      if (!s.debugsource.empty ()) cout << "debugsource: " << s.debugsource << '\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-devel")
    {
      assert (argc == 4); // <dev-pkg> <dev-ver>

      string n (argv[2]);
      string v (argv[3]);
      vector<pair<string, string>> ds;

      for (string l; !eof (getline (cin, l)); )
      {
        size_t p (l.find (' '));
        assert (p != string::npos);

        ds.emplace_back (string (l, 0, p), string (l, p + 1));
      }

      cout << system_package_manager_fedora::main_from_devel (n, v, ds) << '\n';
    }
    else if (cmd == "map-package")
    {
      assert (argc == 2);

      available_packages aps;
      aps.push_back (make_available_from_manifest ("", "-"));

      const package_name& n (aps.front ().first->id.name);
      const version& v (aps.front ().first->version);

      system_package_manager_fedora m (move (osr),
                                       host_triplet,
                                       ""      /* arch */,
                                       nullopt /* progress */,
                                       nullptr /* options */);

      package_status s (m.map_package (n, v, aps));

      cout <<                              "version: "     << s.system_version << '\n'
           <<                              "main: "        << s.main           << '\n';
      if (!s.devel.empty ())       cout << "devel: "       << s.devel          << '\n';
      if (!s.static_.empty ())     cout << "static: "      << s.static_        << '\n';
      if (!s.doc.empty ())         cout << "doc: "         << s.doc            << '\n';
      if (!s.debuginfo.empty ())   cout << "debuginfo: "   << s.debuginfo      << '\n';
      if (!s.debugsource.empty ()) cout << "debugsource: " << s.debugsource    << '\n';
      if (!s.common.empty ())      cout << "common: "      << s.common         << '\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_fedora::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>* infos =
          k == "dnf-list"           ? &s.dnf_list_ :
          k == "dnf-list-fetched"   ? &s.dnf_list_fetched_   :
          k == "dnf-list-installed" ? &s.dnf_list_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 ();

          infos->emplace (move (ns), path (move (f)));
        }
        else if (map<package, path>* req =
                 k == "dnf-repoquery-requires"         ? &s.dnf_repoquery_requires_         :
                 k == "dnf-repoquery-requires-fetched" ? &s.dnf_repoquery_requires_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.rfind (' '); assert (q != string::npos);
          bool i (to_bool (string (n, q + 1)));
          n.resize (q);

          q = n.rfind (' '); assert (q != string::npos);
          string a (n, q + 1);
          n.resize (q);

          q = n.find (' '); assert (q != string::npos);

          package pkg {string (n, 0, q), string (n, q + 1), move (a), i};

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

          req->emplace (move (pkg), path (move (f)));
        }
        else if (k == "dnf-makecache-fail")
        {
          s.dnf_makecache_fail_ = true;
        }
        else if (k == "dnf-install-fail")
        {
          s.dnf_install_fail_ = true;
        }
        else if (k == "dnf-mark-install-fail")
        {
          s.dnf_mark_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_fedora 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.status (pn, &aps[n]));

        assert (*m.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.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);
}