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
|
/* Copyright © 2005-2013 Roger Leigh <rleigh@debian.org>
*
* schroot is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* schroot 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, see
* <http://www.gnu.org/licenses/>.
*
*********************************************************************/
#include <config.h>
#include <sbuild/util.h>
#include <schroot-common/options.h>
#include <cstdlib>
#include <iostream>
#include <stdexcept>
#include <boost/format.hpp>
#include <boost/program_options.hpp>
using std::endl;
using boost::format;
using sbuild::_;
namespace opt = boost::program_options;
namespace schroot_common
{
const options::action_type options::ACTION_SESSION_AUTO ("session_auto");
const options::action_type options::ACTION_SESSION_BEGIN ("session_begin");
const options::action_type options::ACTION_SESSION_RECOVER ("session_recover");
const options::action_type options::ACTION_SESSION_RUN ("session_run");
const options::action_type options::ACTION_SESSION_END ("session_end");
const options::action_type options::ACTION_LIST ("list");
const options::action_type options::ACTION_INFO ("info");
const options::action_type options::ACTION_LOCATION ("location");
const options::action_type options::ACTION_CONFIG ("config");
options::options ():
bin_common::options (),
chroots(),
command(),
directory(),
shell(),
user(),
preserve(false),
all(false),
all_chroots(false),
all_sessions(false),
all_source_chroots(false),
exclude_aliases(false),
session_name(),
session_force(false),
useroptions(),
useroptions_map(),
chroot(_("Chroot selection")),
chrootenv(_("Chroot environment")),
session_actions(_("Session actions")),
session_options(_("Session options"))
{
}
options::~options ()
{
}
void
options::add_options ()
{
// Chain up to add basic options.
bin_common::options::add_options();
action.add(ACTION_SESSION_AUTO);
action.set_default(ACTION_SESSION_AUTO);
action.add(ACTION_SESSION_BEGIN);
action.add(ACTION_SESSION_RECOVER);
action.add(ACTION_SESSION_RUN);
action.add(ACTION_SESSION_END);
action.add(ACTION_LIST);
action.add(ACTION_INFO);
action.add(ACTION_LOCATION);
action.add(ACTION_CONFIG);
actions.add_options()
("list,l",
_("List available chroots"))
("info,i",
_("Show information about selected chroots"))
("config",
_("Dump configuration of selected chroots"));
chroot.add_options()
("chroot,c", opt::value<sbuild::string_list>(&this->chroots),
_("Use specified chroot"));
hidden.add_options()
("command", opt::value<sbuild::string_list>(&this->command),
_("Command to run"));
positional.add("command", -1);
}
void
options::add_option_groups ()
{
// Chain up to add basic option groups.
bin_common::options::add_option_groups();
#ifndef BOOST_PROGRAM_OPTIONS_DESCRIPTION_OLD
if (!chroot.options().empty())
#else
if (!chroot.primary_keys().empty())
#endif
{
visible.add(chroot);
global.add(chroot);
}
#ifndef BOOST_PROGRAM_OPTIONS_DESCRIPTION_OLD
if (!chrootenv.options().empty())
#else
if (!chrootenv.primary_keys().empty())
#endif
{
visible.add(chrootenv);
global.add(chrootenv);
}
#ifndef BOOST_PROGRAM_OPTIONS_DESCRIPTION_OLD
if (!session_actions.options().empty())
#else
if (!session_actions.primary_keys().empty())
#endif
{
visible.add(session_actions);
global.add(session_actions);
}
#ifndef BOOST_PROGRAM_OPTIONS_DESCRIPTION_OLD
if (!session_options.options().empty())
#else
if (!session_options.primary_keys().empty())
#endif
{
visible.add(session_options);
global.add(session_options);
}
}
void
options::check_options ()
{
// Chain up to check basic options.
bin_common::options::check_options();
if (vm.count("list"))
this->action = ACTION_LIST;
if (vm.count("info"))
this->action = ACTION_INFO;
if (vm.count("config"))
this->action = ACTION_CONFIG;
}
void
options::check_actions ()
{
// Chain up to check basic actions.
bin_common::options::check_actions();
if (this->quiet && this->verbose)
{
sbuild::log_warning()
<< _("--quiet and --verbose may not be used at the same time")
<< endl;
sbuild::log_info() << _("Using verbose output") << endl;
}
if (!this->chroots.empty() && all_used())
{
sbuild::log_warning()
<< _("--chroot and --all may not be used at the same time")
<< endl;
sbuild::log_info() << _("Using --chroots only") << endl;
this->all = this->all_chroots = this->all_source_chroots = this->all_sessions = false;
}
/* Determine which chroots to load and use. */
if (this->action == ACTION_SESSION_AUTO)
{
// Only allow normal chroots
this->load_chroots = true;
this->load_sessions = false;
this->all = this->all_sessions = false;
// If no chroot was specified, fall back to the "default" chroot.
if (this->chroots.empty() && all_used() == false)
this->chroots.push_back("default");
}
else if (this->action == ACTION_SESSION_BEGIN)
{
// Only allow one session chroot
this->load_chroots = true;
this->load_sessions = false;
if (this->chroots.size() != 1 || all_used())
throw error
(_("Exactly one chroot must be specified when beginning a session"));
this->all = this->all_chroots = this->all_source_chroots = this->all_sessions = false;
}
else if (this->action == ACTION_SESSION_RECOVER ||
this->action == ACTION_SESSION_RUN ||
this->action == ACTION_SESSION_END)
{
// Session operations work on all chroots.
this->load_chroots = this->load_sessions = true;
if (!this->session_name.empty())
throw error
(_("--session-name is not permitted for the specified action; did you mean to use --chroot?"));
}
else if (this->action == ACTION_HELP ||
this->action == ACTION_VERSION)
{
// Chroots don't make sense here.
this->load_chroots = this->load_sessions = false;
this->all = this->all_chroots = this->all_source_chroots = this->all_sessions = false;
}
else if (this->action == ACTION_LIST ||
this->action == ACTION_INFO ||
this->action == ACTION_LOCATION ||
this->action == ACTION_CONFIG)
{
// If not specified otherwise, load normal chroots, but allow
// --all options.
if (!this->chroots.empty()) // chroot specified
this->load_chroots = this->load_sessions = true;
else if (!all_used()) // no chroots specified
{
this->all_chroots = true;
if (this->action == ACTION_LIST || this->action == ACTION_INFO)
this->all_source_chroots = true;
this->load_chroots = true;
}
if (this->all_chroots || this->all_source_chroots)
this->load_chroots = true;
if (this->all_sessions)
this->load_chroots = this->load_sessions = true;
}
else
{
// Something went wrong
this->load_chroots = this->load_sessions = false;
this->all = this->all_chroots = this->all_source_chroots = this->all_sessions = false;
throw error(_("Unknown action specified"));
}
if (!this->session_name.empty() && this->action != ACTION_SESSION_BEGIN)
throw error
(_("--session-name is not permitted for the specified action"));
if (!this->session_name.empty() &&
!sbuild::is_valid_sessionname(this->session_name))
throw error(_("Invalid session name"));
}
}
|