-
Notifications
You must be signed in to change notification settings - Fork 7
/
trav.cpp
374 lines (314 loc) · 11.6 KB
/
trav.cpp
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
// trav.cpp
/*===========================================================================
*
* PUBLIC DOMAIN NOTICE
* National Center for Biotechnology Information
*
* This software/database is a "United States Government Work" under the
* terms of the United States Copyright Act. It was written as part of
* the author's official duties as a United States Government employee and
* thus cannot be copyrighted. This software/database is freely available
* to the public for use. The National Library of Medicine and the U.S.
* Government have not placed any restriction on its use or reproduction.
*
* Although all reasonable efforts have been taken to ensure the accuracy
* and reliability of the software and data, the NLM and the U.S.
* Government do not and cannot warrant the performance or results that
* may be obtained by using this software or data. The NLM and the U.S.
* Government disclaim all warranties, express or implied, including
* warranties of performance, merchantability or fitness for any particular
* purpose.
*
* Please cite the author in any work or product based on this material.
*
* ===========================================================================
*
* Author: Vyacheslav Brover
*
* File Description:
* Traverse a list of objects and apply a command to each of the objects
*
*/
#undef NDEBUG
#include "common.hpp"
using namespace Common_sp;
#include "version.inc"
#include "common.inc"
namespace
{
Vector<int> goodStatuses;
unique_ptr<OFStream> errors;
std::mutex errorsMtx;
void executeCommand (const string &cmd,
const string &item/*,
uint blank_lines*/)
{
ASSERT (! cmd. empty ());
ASSERT (! item. empty ());
const int c = system (cmd. c_str ());
// "set -o pipefail && ..." does not work with dash
const int exitStatus = WEXITSTATUS (c);
if (exitStatus && ! goodStatuses. contains (exitStatus))
{
//cout << endl << exitStatus << endl;
if (errors)
{
//cout << c << endl; // always 256
const Lock lock (errorsMtx);
*errors << item << endl;
QC_ASSERT (c != -1);
}
else if (errorsMtx. try_lock ()) // To suppress messages from the other good threads which fail due to this thread
{
const string errMsg ("item: " + item + "\nstatus: " + to_string (exitStatus) + "\n" + cmd);
if (threads_max > 1)
errorExitStr (errMsg);
else
throw runtime_error (errMsg);
}
}
#if 0
if (isMainThread ())
FOR (uint, i, blank_lines)
cerr << " " << endl;
#endif
}
struct Command
{
string cmd;
string item;
};
void executeCommands (size_t from,
size_t to,
Notype /*&res*/,
const Vector<Command> &commands,
uint step/*,
uint blank_lines*/)
{
Progress prog (to - from, step);
FOR_START (size_t, i, from, to)
{
prog ();
const Command& command = commands [i];
executeCommand (command. cmd, command. item/*, blank_lines*/);
}
}
struct ThisApplication : Application
{
ThisApplication ()
: Application ("Apply <command> to all <items>", true, false, true)
{
version = VERSION;
addPositional ("items", "Items container: file with items (end-of-line separated), a directory (in this case items are files in this directory in raw order), or a natural number");
addPositional ("command", "Text with special symbols: \
\"%d\" = <items>, \
\"%f\" - item, \
\"%<n>\" - subitem #n (1 <= n <= 9), \
\"%{<n>}\" - subitem #n (n >= 10), \
\"%h\" - small item hash (0.." + to_string (small_hash_class_max - 1) + "), \
\"%H\" - large item hash (0.." + to_string (large_hash_class_max - 1) + "), \
\"%q\" - single quote, \
\"%Q\" - double quote, \
\"%D\" - $, \
\"%g\" - `, \
\"%b\" - backslash, \
\"%p\" - %");
addFlag ("large", "Directory <items> is large: it is subdivided into subdirectories \"0\" .. \"" + to_string (small_hash_class_max - 1) + "\" which are the hashes of file names");
addKey ("errors", "Ignore errors in running items and save error items into this file");
// Bug: ^C does not stop the program ??
//addKey ("blank_lines", "# Blank lines to be printed to stderr after each command", "0");
addKey ("step", "# Items processed to output the progress for", "100");
addKey ("start", "# Item to start with", "1");
addFlag ("zero", "Item numbers are 0-based, otherwise 1-based");
addFlag ("print", "Print command, not execute");
addFlag ("tsv", "<items> is a tsv-file");
addKey ("good_exit_codes", "Comma-delimited list of allowed exit codes", "0");
}
void body () const final
{
const string itemsName = getArg ("items");
const string cmd_orig = getArg ("command");
const bool large = getFlag ("large");
const string errorsFName = getArg ("errors");
//const uint blank_lines = str2<uint> (getArg ("blank_lines"));
const uint step = str2<uint> (getArg ("step"));
const uint start = str2<uint> (getArg ("start"));
const bool zero = getFlag ("zero");
const bool printP = getFlag ("print");
const bool tsv = getFlag ("tsv");
string goodStatusesS = getArg ("good_exit_codes");
if (itemsName. empty ())
throw runtime_error ("Empty items list name");
if (cmd_orig. empty ())
throw runtime_error ("Empty command");
if (! step)
throw runtime_error ("-step must be >= 1");
//if (blank_lines && step > 1)
//throw runtime_error ("-blank_lines requires -step to be 1");
if (printP && goodStatusesS != "0")
throw runtime_error ("-print ignores -good_exit_codes");
Vector<Command> commands; commands. reserve (100000); // PAR
{
unique_ptr<ItemGenerator> gen;
{
const uint stepItemGen = threads_max > 1 ? 1000 : step; // PAR
const bool isFile = fileExists (itemsName);
const bool isDir =
#ifdef _MSC_VER
false
#else
getFiletype (itemsName, true) == Filetype::dir
#endif
;
if (isDir && tsv)
throw runtime_error ("-tsv cannot be used if <items> is a directory");
if (isFile)
gen. reset (new FileItemGenerator (stepItemGen, itemsName, tsv));
else if (isDir)
gen. reset (new DirItemGenerator (stepItemGen, itemsName, large));
else
{
size_t n = 0;
if (str2<size_t> (itemsName, n))
{
if (tsv)
throw runtime_error ("-tsv cannot be used if <items> is a number");
gen. reset (new NumberItemGenerator (stepItemGen, n));
}
else
throw runtime_error ("File " + strQuote (itemsName) + " does not exist");
}
}
ASSERT (gen. get ());
ASSERT (! errors. get ());
if (! errorsFName. empty ())
errors. reset (new OFStream (errorsFName));
constexpr char delChar = '\177';
if (contains (cmd_orig, delChar))
throw runtime_error ("Command has an ASCII 127 (DEL) character");
if (contains (itemsName, delChar))
throw runtime_error ("Items container has an ASCII 127 (DEL) character");
string itemsName_cleaned (itemsName); // Can contain '%'
replace (itemsName_cleaned, '%', delChar);
string cmd (cmd_orig);
// Constant replacements of '%'
replaceStr (cmd, "%d", itemsName_cleaned);
replaceStr (cmd, "%q", "'");
replaceStr (cmd, "%Q", "\"");
replaceStr (cmd, "%D", "$");
replaceStr (cmd, "%g", "`");
replaceStr (cmd, "%b", "\\");
bool subitemsP = false;
FFOR (size_t, i, cmd. size () - 1)
if ( cmd [i] == '%'
&& ((isDigit (cmd [i + 1]) && cmd [i + 1] != '0') || cmd [i + 1] == '{')
)
{
subitemsP = true;
break;
}
commaize (goodStatusesS);
while (! goodStatusesS. empty ())
{
const string s (findSplit (goodStatusesS, ','));
goodStatuses << str2<int> (s);
}
#if 0
for (const int i : goodStatuses)
cout << i << endl;
#endif
string item;
Istringstream iss;
StringVector subitems;
while (gen->next (item))
{
if (! tsv)
trim (item);
if (item. empty ())
continue;
ASSERT (gen->prog. n);
const size_t n = gen->prog. n - (zero ? 1 : 0);
if (n < start)
continue;
const string item_orig (item);
if (contains (item, delChar))
throw runtime_error ("Item has an ASCII 127 (DEL) character");
replace (item, '%', delChar);
string thisCmd (cmd);
replaceStr (thisCmd, "%f", item);
replaceStr (thisCmd, "%h", to_string (str2hash_class (item_orig, false)));
replaceStr (thisCmd, "%H", to_string (str2hash_class (item_orig, true)));
replaceStr (thisCmd, "%n", to_string (n));
if (subitemsP)
{
subitems. clear ();
if (tsv)
{
#ifndef NDEBUG
const size_t colNum = strCountSet (item, "\t") + 1;
#endif
string s (item);
const bool lastEmpty = isRight (item, "\t");
while (! s. empty ())
subitems << findSplit (s, '\t');
if (lastEmpty)
subitems << noString;
ASSERT (subitems. size () == colNum);
}
else
{
iss. reset (item);
string s;
while (! iss. eof ())
{
ASSERT (s. empty ());
iss >> s;
ASSERT (! s. empty ());
subitems << std::move (s);
}
}
ASSERT (! subitems. empty ());
FFOR (size_t, i, subitems. size ())
{
const string numS (to_string (i + 1));
const bool longP = numS. size () > 1;
replaceStr (thisCmd, string ("%") + (longP ? "{" : "") + numS + (longP ? "}" : ""), subitems [i]);
}
}
// Last replacement of '%'
FFOR (size_t, i, thisCmd. size ())
if ( thisCmd [i] == '%'
&& ( i == thisCmd. size () - 1
|| thisCmd [i + 1] != 'p'
)
)
throw runtime_error ("Unprocessed " + strQuote (thisCmd. substr (i, 2)) + " in item: " + item_orig + "\n" + thisCmd);
replaceStr (thisCmd, "%p", "%");
// Restore original '%'
replace (thisCmd, delChar, '%');
if (verbose ())
cerr << thisCmd << endl;
if (printP)
cout << thisCmd << endl;
else
if (threads_max == 1)
executeCommand (thisCmd, item_orig/*, blank_lines*/);
else
commands << std::move (Command {std::move (thisCmd), std::move (item_orig)});
}
}
if (threads_max > 1)
{
commands. randomOrder ();
vector<Notype> notypes;
arrayThreads (false, executeCommands, commands. size (), notypes, cref (commands), step/*, blank_lines*/);
}
}
};
} // namespace
int main (int argc,
const char* argv[])
{
ThisApplication app;
return app. run (argc, argv);
}