aboutsummaryrefslogtreecommitdiff
path: root/bdep/status.cxx
blob: d932e5848ac75b1ceef9c0765f27b99d13f63ec5 (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
// file      : bdep/status.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#include <bdep/status.hxx>

#include <iostream> // cout

#include <libbutl/json/serializer.hxx>

#include <bdep/project.hxx>
#include <bdep/database.hxx>
#include <bdep/diagnostics.hxx>

#include <bdep/fetch.hxx>

using namespace std;

namespace bdep
{
  // If the specified package list is not empty, then return only those
  // packages which are initialized in the specified configuration. Otherwise,
  // return all packages that have been initialized in this configuration.
  //
  static strings
  config_packages (const configuration& cfg, const package_locations& pkgs)
  {
    strings r;

    bool all (pkgs.empty ());
    for (const package_state& s: cfg.packages)
    {
      if (all ||
          find_if (pkgs.begin (),
                   pkgs.end (),
                   [&s] (const package_location& p)
                   {
                     return p.name == s.name;
                   }) != pkgs.end ())
        r.push_back (s.name.string ());
    }

    return r;
  }

  static process
  start_bpkg_status (const cmd_status_options& o,
                     int out,
                     const dir_path& prj,
                     const dir_path& cfg,
                     const strings& pkgs,
                     bool fetch,
                     const char* format)
  {
    // Shallow fetch the project to make sure we show latest iterations and
    // pick up any new repositories.
    //
    // We do it in a separate command for the same reason as in sync.
    //
    if (fetch)
      run_bpkg (3,
                o,
                "fetch",
                "-d", cfg,
                "--shallow",
                repository_name (prj));

    // Don't show the hold status since the only packages that will normally
    // be held are the project's. But do show dependency constraints.
    //
    return start_bpkg (2 /* verbosity */,
                       o,
                       out,
                       2 /* stderr */,
                       "status",
                       "-d", cfg,
                       "--no-hold",
                       "--constraint",
                       (o.old_available () ? "--old-available" : nullptr),
                       (o.immediate () ? "--immediate" :
                        o.recursive () ? "--recursive" : nullptr),
                       "--stdout-format", format,
                       pkgs);
  }

  static void
  cmd_status_lines (const cmd_status_options& o,
                    const project_packages& prj_pkgs,
                    const configurations& cfgs,
                    const strings& dep_pkgs)
  {
    tracer trace ("status_lines");

    // Print status in each configuration, skipping fetching repositories in
    // those where no package statuses needs to be printed.
    //
    bool first (true);
    for (const shared_ptr<configuration>& c: cfgs)
    {
      // Collect the packages to print, unless the dependency packages are
      // specified.
      //
      strings pkgs;

      if (dep_pkgs.empty ())
        pkgs = config_packages (*c, prj_pkgs.packages);

      // If we are printing multiple configurations, separate them with a
      // blank line and print the configuration name/directory.
      //
      if (verb && cfgs.size () > 1)
      {
        cout << (first ? "" : "\n")
             << "in configuration " << *c << ':' << endl;

        first = false;
      }

      if (!c->packages.empty () && (!pkgs.empty () || !dep_pkgs.empty ()))
      {
        const dir_path& prj (prj_pkgs.project);

        bool fetch (o.fetch () || o.fetch_full ());

        if (fetch)
          cmd_fetch (o, prj, c, o.fetch_full ());

        // Status for either packages or their dependencies must be printed,
        // but not for both.
        //
        assert (pkgs.empty () == !dep_pkgs.empty ());

        process pr (start_bpkg_status (o,
                                       1 /* stdout */,
                                       prj,
                                       c->path,
                                       !pkgs.empty () ? pkgs : dep_pkgs,
                                       !fetch,
                                       "lines"));

        finish_bpkg (o, pr);
      }
      else
      {
        if (verb)
        {
          diag_record dr (info);

          if (c->packages.empty ())
            dr << "no packages ";
          else
            dr << "none of specified packages ";

          dr << "initialized in configuration " << *c << ", skipping";
        }
      }
    }
  }

  static void
  cmd_status_json (const cmd_status_options& o,
                   const project_packages& prj_pkgs,
                   const configurations& cfgs,
                   const strings& dep_pkgs)
  {
    tracer trace ("status_json");

    butl::json::stream_serializer ss (cout);

    ss.begin_array ();

    // Print status in each configuration, skipping fetching repositories in
    // those where no package statuses need to be printed.
    //
    for (const shared_ptr<configuration>& c: cfgs)
    {
      // Collect the packages to print, unless the dependency packages are
      // specified.
      //
      strings pkgs;

      if (dep_pkgs.empty ())
        pkgs = config_packages (*c, prj_pkgs.packages);

      ss.begin_object ();
      ss.member_name ("configuration");
      ss.begin_object ();
      ss.member ("id", *c->id);
      ss.member ("path", c->path.string ());

      if (c->name)
        ss.member ("name", *c->name);

      ss.end_object ();

      if (!c->packages.empty () && (!pkgs.empty () || !dep_pkgs.empty ()))
      {
        const dir_path& prj (prj_pkgs.project);

        bool fetch (o.fetch () || o.fetch_full ());

        if (fetch)
          cmd_fetch (o, prj, c, o.fetch_full ());

        // Status for either packages or their dependencies must be printed,
        // but not for both.
        //
        assert (pkgs.empty () == !dep_pkgs.empty ());

        // Save the JSON representation of package statuses into a string from
        // bpkg-status' stdout and use it as a pre-serialized value for the
        // packages member of the configuration packages status object.
        //
        string ps;

        fdpipe pipe (open_pipe ()); // Text mode seems appropriate.

        process pr (start_bpkg_status (o,
                                       pipe.out.get (),
                                       prj,
                                       c->path,
                                       !pkgs.empty () ? pkgs : dep_pkgs,
                                       !fetch,
                                       "json"));

        // Shouldn't throw, unless something is severely damaged.
        //
        pipe.out.close ();

        bool io (false);
        try
        {
          ifdstream is (move (pipe.in));
          ps = is.read_text ();
          is.close ();
        }
        catch (const io_error&)
        {
          // Presumably the child process failed and issued diagnostics so let
          // finish_bpkg() try to deal with that first.
          //
          io = true;
        }

        finish_bpkg (o, pr, io);

        // Trim the trailing newline, which must be present. Let's however
        // check that it is, for good measure.
        //
        if (!ps.empty () && ps.back () == '\n')
          ps.pop_back ();

        ss.member_name ("packages");
        ss.value_json_text (ps);
      }
      else
      {
        // Not that unlike in the lines output we don't tell the user that
        // there are no packages.
      }

      ss.end_object ();
    }

    ss.end_array ();
    cout << endl;
  }

  int
  cmd_status (const cmd_status_options& o, cli::scanner& args)
  {
    tracer trace ("status");

    if (o.immediate () && o.recursive ())
      fail << "both --immediate|-i and --recursive|-r specified";

    // We have two pretty different modes: project package status and
    // dependency package status (have arguments).
    //
    strings dep_pkgs;
    for (; args.more (); dep_pkgs.push_back (args.next ())) ;

    // The same ignore/load story as in sync.
    //
    project_packages pp (
      find_project_packages (o,
                             !dep_pkgs.empty () /* ignore_packages */,
                             false              /* load_packages   */));

    const dir_path& prj (pp.project);

    database db (open (prj, trace));

    configurations cfgs;
    {
      transaction t (db.begin ());
      pair<configurations, bool> cs (find_configurations (o, prj, t));
      t.commit ();

      // If specified, verify packages are present in at least one
      // configuration.
      //
      if (!pp.packages.empty ())
        verify_project_packages (pp, cs);

      cfgs = move (cs.first);
    }

    switch (o.stdout_format ())
    {
    case stdout_format::lines:
      {
        cmd_status_lines (o, pp, cfgs, dep_pkgs);
        break;
      }
    case stdout_format::json:
      {
        cmd_status_json (o, pp, cfgs, dep_pkgs);
        break;
      }
    }

    return 0;
  }
}