Skip to content

Commit b3fed15

Browse files
committed
DM-52370: Refactored the partitionng tools to allow using them as a library
The new API allows embedding the partitioning tools into other applications. Migrated error handling to throw exceptions instead of calling std::exit(). Migrated the library code not to print anything directly onto std::cout.
1 parent d401da1 commit b3fed15

File tree

9 files changed

+970
-680
lines changed

9 files changed

+970
-680
lines changed

src/partition/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ target_sources(partition PRIVATE
1313
InputLines.cc
1414
ObjectIndex.cc
1515
ParquetInterface.cc
16+
PartitionTool.cc
17+
PartitionMatchesTool.cc
1618
)
1719

1820
target_link_libraries(partition PUBLIC

src/partition/CmdLineUtils.cc

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,14 @@
2626
#include <algorithm>
2727
#include <iostream>
2828
#include <set>
29+
#include <sstream>
30+
#include <stdexcept>
2931
#include <vector>
3032
#include "boost/algorithm/string/predicate.hpp"
3133

3234
#include "partition/ConfigStore.h"
3335
#include "partition/Constants.h"
36+
#include "partition/Exceptions.h"
3437
#include "partition/FileUtils.h"
3538

3639
namespace fs = boost::filesystem;
@@ -75,8 +78,9 @@ ConfigStore parseCommandLine(po::options_description const& options, int argc, c
7578
po::store(po::parse_command_line(argc, const_cast<char**>(argv), all), vm);
7679
po::notify(vm);
7780
if ((vm.count("help") != 0) && vm["help"].as<bool>()) {
78-
std::cout << argv[0] << " [options]\n\n" << help << "\n" << all << std::endl;
79-
std::exit(EXIT_SUCCESS);
81+
std::ostringstream oss;
82+
oss << argv[0] << " [options]\n\n" << help << "\n" << all << std::endl;
83+
throw ExitOnHelp(oss.str());
8084
}
8185
// Parse configuration files, if any.
8286
if (vm.count("config-file") != 0) {
@@ -218,8 +222,7 @@ void makeOutputDirectory(ConfigStore& config, bool mayExist) {
218222
outDir = config.get<std::string>("out.dir");
219223
}
220224
if (outDir.empty()) {
221-
std::cerr << "No output directory specified (use --out.dir)." << std::endl;
222-
std::exit(EXIT_FAILURE);
225+
throw std::invalid_argument("No output directory specified (use --out.dir).");
223226
}
224227
outDir = fs::system_complete(outDir);
225228
if (outDir.filename() == ".") {
@@ -231,9 +234,8 @@ void makeOutputDirectory(ConfigStore& config, bool mayExist) {
231234
}
232235
config.set("out.dir", outDir.string());
233236
if (fs::create_directories(outDir) == false && !mayExist) {
234-
std::cerr << "The output directory --out.dir=" << outDir.string()
235-
<< " already exists - please choose another." << std::endl;
236-
std::exit(EXIT_FAILURE);
237+
throw std::runtime_error("The output directory --out.dir=" + outDir.string() +
238+
" already exists - please choose another.");
237239
}
238240
}
239241

@@ -244,8 +246,7 @@ void ensureOutputFieldExists(ConfigStore& config, std::string const& opt) {
244246
std::vector<std::string> columns;
245247
if (!config.has("out.csv.field")) {
246248
if (!config.has("in.csv.field")) {
247-
std::cerr << "Input CSV column names not specified." << std::endl;
248-
std::exit(EXIT_FAILURE);
249+
throw std::invalid_argument("Input CSV column names not specified.");
249250
}
250251
columns = config.get<std::vector<std::string>>("in.csv.field");
251252
} else {

src/partition/Exceptions.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* LSST Data Management System
3+
*
4+
* This product includes software developed by the
5+
* LSST Project (http://www.lsst.org/).
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the LSST License Statement and
18+
* the GNU General Public License along with this program. If not,
19+
* see <http://www.lsstcorp.org/LegalNotices/>.
20+
*/
21+
#ifndef LSST_PARTITION_EXCEPTIONS_H
22+
#define LSST_PARTITION_EXCEPTIONS_H
23+
24+
// System headers
25+
#include <stdexcept>
26+
27+
// This header declarations
28+
namespace lsst::partition {
29+
30+
/**
31+
* An exception type used to indicate that help information was requested.
32+
* The message associated with the exception contains the help text.
33+
*/
34+
class ExitOnHelp : public std::runtime_error {
35+
public:
36+
using std::runtime_error::runtime_error;
37+
};
38+
39+
} // namespace lsst::partition
40+
41+
#endif // LSST_PARTITION_EXCEPTIONS_H

0 commit comments

Comments
 (0)