src/cli/MeasureUpdateCommand.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************************************************** | ||
| 2 | * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC. | ||
| 3 | * See also https://openstudio.net/license | ||
| 4 | ***********************************************************************************************************************/ | ||
| 5 | |||
| 6 | #include "MeasureUpdateCommand.hpp" | ||
| 7 | #include "MeasureManager.hpp" | ||
| 8 | |||
| 9 | #include "../utilities/core/Filesystem.hpp" | ||
| 10 | #include "../utilities/bcl/BCLMeasure.hpp" | ||
| 11 | #include "../scriptengine/ScriptEngine.hpp" | ||
| 12 | |||
| 13 | // CLI headers must come after ScriptEngine.hpp so that windows.h (and winnls.h) is | ||
| 14 | // pulled in before CLI11's windef.h, avoiding a missing LCTYPE in boost/regex's w32_regex_traits.hpp. | ||
| 15 | // https://github.com/NatLabRockies/OpenStudio/pull/5606#issuecomment-4391907202 | ||
| 16 | #include <CLI/App.hpp> | ||
| 17 | #include <CLI/ExtraValidators.hpp> | ||
| 18 | #include "../measure/OSMeasure.hpp" | ||
| 19 | #include "../measure/ModelMeasure.hpp" | ||
| 20 | #include "../measure/EnergyPlusMeasure.hpp" | ||
| 21 | #include "../measure/ReportingMeasure.hpp" | ||
| 22 | #include "../measure/OSArgument.hpp" | ||
| 23 | #include "../measure/OSOutput.hpp" | ||
| 24 | #include "../measure/OSRunner.hpp" | ||
| 25 | #include "../measure/OSMeasureInfoGetter.hpp" | ||
| 26 | #include "../utilities/idf/Workspace.hpp" | ||
| 27 | #include <utilities/idd/IddEnums.hxx> | ||
| 28 | |||
| 29 | #include <fmt/format.h> | ||
| 30 | #include <fmt/color.h> | ||
| 31 | #include <fmt/ranges.h> | ||
| 32 | |||
| 33 | #include <boost/optional.hpp> | ||
| 34 | |||
| 35 | #include <algorithm> // For std::transform | ||
| 36 | #include <cstdlib> // For std::exit | ||
| 37 | #include <memory> // make_shared | ||
| 38 | #include <map> // For std::map | ||
| 39 | #include <stdexcept> // For std::runtime_error | ||
| 40 | #include <string> | ||
| 41 | #include <string_view> | ||
| 42 | #include <vector> | ||
| 43 | |||
| 44 | namespace openstudio { | ||
| 45 | namespace cli { | ||
| 46 | |||
| 47 | 262 | void MeasureUpdateOptions::setupMeasureUpdateOptions(CLI::App* parentApp, ScriptEngineInstance& rubyEngine, ScriptEngineInstance& pythonEngine) { | |
| 48 | /// Set up a subcommand and capture a shared_ptr to a struct that holds all its options. | ||
| 49 | /// The variables of the struct are bound to the CLI options. | ||
| 50 | /// We use a shared ptr so that the addresses of the variables remain for binding, | ||
| 51 | /// You could return the shared pointer if you wanted to access the values in main. | ||
| 52 | |||
| 53 |
1/1✓ Branch 1 taken 262 times.
|
262 | auto opt = std::make_shared<MeasureUpdateOptions>(); |
| 54 | |||
| 55 |
3/3✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
✓ Branch 7 taken 262 times.
|
1048 | auto* measureCommand = parentApp->add_subcommand("measure", "Measure Commands"); |
| 56 | |||
| 57 | // Positional | ||
| 58 |
4/4✓ Branch 1 taken 262 times.
✓ Branch 5 taken 262 times.
✓ Branch 8 taken 262 times.
✓ Branch 11 taken 262 times.
|
1572 | auto* directoryPathOpt = measureCommand->add_option("DIRECTORY", opt->directoryPath, "Directory containing the measure(s)")->option_text(" "); |
| 59 | |||
| 60 | // TODO: I'm trying to preserve historical behavior, but --update, --update_all, --start_server should all be subcommands instead since they are | ||
| 61 | // exclusive! At least start_server should, so that I can make directory required... | ||
| 62 |
4/4✓ Branch 1 taken 262 times.
✓ Branch 5 taken 262 times.
✓ Branch 8 taken 262 times.
✓ Branch 11 taken 262 times.
|
1048 | auto* updateOpt = measureCommand->add_flag("-u,--update", opt->update, "Update the measure.xml")->needs(directoryPathOpt); |
| 63 | |||
| 64 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 5 taken 262 times.
|
524 | auto* updateAllOpt = measureCommand->add_flag("-t,--update_all", opt->update_all, "Update all measures in a directory") |
| 65 | ->needs(directoryPathOpt) | ||
| 66 |
3/3✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
✓ Branch 7 taken 262 times.
|
786 | ->excludes(updateOpt); |
| 67 | |||
| 68 | auto* computeArgsOpt = | ||
| 69 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 5 taken 262 times.
|
524 | measureCommand->add_option("-a,--compute_arguments", opt->compute_arguments_model, "Compute arguments for the given OSM or IDF") |
| 70 |
1/1✓ Branch 1 taken 262 times.
|
262 | ->option_text("MODEL") |
| 71 | ->needs(directoryPathOpt) | ||
| 72 |
3/3✓ Branch 1 taken 262 times.
✓ Branch 5 taken 262 times.
✓ Branch 8 taken 262 times.
|
1310 | ->excludes(updateOpt, updateAllOpt); |
| 73 | |||
| 74 | measureCommand | ||
| 75 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 5 taken 262 times.
|
524 | ->add_flag("-r,--run_tests", opt->run_tests, "Run all tests recursively found in a directory, additional arguments are passed to minitest") |
| 76 | ->needs(directoryPathOpt) | ||
| 77 |
3/3✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
✓ Branch 7 taken 262 times.
|
786 | ->excludes(updateOpt, updateAllOpt, computeArgsOpt); |
| 78 | |||
| 79 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 5 taken 262 times.
|
524 | [[maybe_unused]] auto* startServerOpt = measureCommand->add_option("-s,--start_server", opt->server_port, "Start a measure manager server") |
| 80 |
1/1✓ Branch 1 taken 262 times.
|
262 | ->option_text("PORT") |
| 81 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 5 taken 262 times.
|
1310 | ->excludes(directoryPathOpt); |
| 82 | |||
| 83 | { | ||
| 84 |
3/3✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
✓ Branch 7 taken 262 times.
|
1048 | auto* newMeasureSubCommand = measureCommand->add_subcommand("new", "Create a new measure"); |
| 85 | |||
| 86 | // TODO: this includes 'UtilityMeasure' which I don't think we want to include | ||
| 87 | 262 | std::vector<std::string> measureTypeNames; | |
| 88 | { | ||
| 89 |
1/1✓ Branch 1 taken 262 times.
|
262 | const auto& m = openstudio::MeasureType::getNames(); |
| 90 |
2/2✓ Branch 2 taken 262 times.
✓ Branch 7 taken 262 times.
|
1572 | std::transform(m.cbegin(), m.cend(), back_inserter(measureTypeNames), [](const auto& x) { return x.second; }); |
| 91 | // fmt::print("measureTypeNames={}\n", measureTypeNames); | ||
| 92 | [[maybe_unused]] auto* measureTypeOpt = | ||
| 93 | newMeasureSubCommand | ||
| 94 |
1/1✓ Branch 1 taken 262 times.
|
524 | ->add_option("-t,--type", opt->newMeasureOpts.measureType, |
| 95 |
1/1✓ Branch 1 taken 262 times.
|
786 | fmt::format("Type of Measure [Default: '{}']: {}", opt->newMeasureOpts.measureType.valueName(), measureTypeNames)) |
| 96 |
1/1✓ Branch 1 taken 262 times.
|
262 | ->option_text("measureType") |
| 97 |
4/4✓ Branch 2 taken 262 times.
✓ Branch 6 taken 262 times.
✓ Branch 9 taken 262 times.
✓ Branch 13 taken 262 times.
|
1834 | ->check(CLI::IsMember(measureTypeNames)); |
| 98 | } | ||
| 99 | |||
| 100 | 262 | std::vector<std::string> measureLanguageNames; | |
| 101 | { | ||
| 102 |
1/1✓ Branch 1 taken 262 times.
|
262 | const auto& m = openstudio::MeasureLanguage::getNames(); |
| 103 |
2/2✓ Branch 2 taken 262 times.
✓ Branch 7 taken 262 times.
|
786 | std::transform(m.cbegin(), m.cend(), back_inserter(measureLanguageNames), [](const auto& x) { return x.second; }); |
| 104 | // fmt::print("measureLanguageNames={}\n", measureLanguageNames); | ||
| 105 | [[maybe_unused]] auto* measureLanguageOpt = newMeasureSubCommand | ||
| 106 |
1/1✓ Branch 1 taken 262 times.
|
524 | ->add_option("-l,--language", opt->newMeasureOpts.measureLanguage, |
| 107 | 524 | fmt::format("Language of Measure [Default: '{}']: {}", | |
| 108 |
1/1✓ Branch 1 taken 262 times.
|
524 | opt->newMeasureOpts.measureLanguage.valueName(), measureLanguageNames)) |
| 109 |
1/1✓ Branch 1 taken 262 times.
|
262 | ->option_text("measureLanguage") |
| 110 |
4/4✓ Branch 2 taken 262 times.
✓ Branch 6 taken 262 times.
✓ Branch 9 taken 262 times.
✓ Branch 13 taken 262 times.
|
1834 | ->check(CLI::IsMember(measureLanguageNames)); |
| 111 | } | ||
| 112 | |||
| 113 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 5 taken 262 times.
|
524 | newMeasureSubCommand->add_option("-c,--class-name", opt->newMeasureOpts.className, "Measure Class Name [Required]") |
| 114 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
|
1572 | ->option_text("className") |
| 115 | 262 | ->required(true); | |
| 116 | |||
| 117 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 5 taken 262 times.
|
524 | newMeasureSubCommand->add_option("-n,--name", opt->newMeasureOpts.name, "Measure Human Readable Name [Default: className]") |
| 118 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
|
1572 | ->option_text("name") |
| 119 | 262 | ->required(false); | |
| 120 | |||
| 121 | 262 | std::vector<std::string> taxonomyTags; | |
| 122 |
1/1✓ Branch 1 taken 262 times.
|
262 | std::vector<std::string> firstLevelTaxonomyTerms = openstudio::BCLMeasure::suggestedFirstLevelTaxonomyTerms(); |
| 123 |
1/1✓ Branch 1 taken 262 times.
|
262 | taxonomyTags.reserve(35); // 2023-05-03: at this moment, this is the total number |
| 124 |
2/2✓ Branch 5 taken 2882 times.
✓ Branch 6 taken 262 times.
|
3144 | for (auto& firstLevelTaxonomyTerm : firstLevelTaxonomyTerms) { |
| 125 |
1/1✓ Branch 1 taken 2882 times.
|
2882 | auto secondLevelTaxonomyTerms = openstudio::BCLMeasure::suggestedSecondLevelTaxonomyTerms(firstLevelTaxonomyTerm); |
| 126 |
1/1✓ Branch 3 taken 2882 times.
|
2882 | taxonomyTags.reserve(taxonomyTags.size() + secondLevelTaxonomyTerms.size()); |
| 127 |
2/2✓ Branch 1 taken 2882 times.
✓ Branch 6 taken 2882 times.
|
2882 | std::transform(secondLevelTaxonomyTerms.begin(), secondLevelTaxonomyTerms.end(), std::back_inserter(taxonomyTags), |
| 128 | 9170 | [&firstLevelTaxonomyTerm](auto&& secondLevelTaxonomyTerm) { | |
| 129 | 18340 | return fmt::format("{}.{}", firstLevelTaxonomyTerm, secondLevelTaxonomyTerm); | |
| 130 | }); | ||
| 131 | 2882 | } | |
| 132 | |||
| 133 | newMeasureSubCommand | ||
| 134 |
1/1✓ Branch 3 taken 262 times.
|
786 | ->add_option("--taxonomy-tag", opt->newMeasureOpts.taxonomyTag, fmt::format("Taxonomy Tag [Default: '{}']", opt->newMeasureOpts.taxonomyTag)) |
| 135 |
2/2✓ Branch 2 taken 262 times.
✓ Branch 5 taken 262 times.
|
1310 | ->option_text("tag") |
| 136 | ->required(false) | ||
| 137 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
|
524 | ->check(CLI::IsMember(taxonomyTags)) |
| 138 |
2/2✓ Branch 3 taken 262 times.
✓ Branch 6 taken 262 times.
|
1048 | ->capture_default_str(); |
| 139 |
4/4✓ Branch 1 taken 262 times.
✓ Branch 5 taken 262 times.
✓ Branch 8 taken 262 times.
✓ Branch 11 taken 262 times.
|
1572 | newMeasureSubCommand->add_option("-d,--description", opt->newMeasureOpts.description, "Description")->option_text("desc")->required(false); |
| 140 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 5 taken 262 times.
|
524 | newMeasureSubCommand->add_option("-m,--modeler-description", opt->newMeasureOpts.modelerDescription, "Modeler Description") |
| 141 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
|
1572 | ->option_text("modeler_desc") |
| 142 | 262 | ->required(false); | |
| 143 | |||
| 144 | [[maybe_unused]] auto* directoryPathOpt = | ||
| 145 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 5 taken 262 times.
|
524 | newMeasureSubCommand->add_option("DIRECTORY", opt->newMeasureOpts.directoryPath, "Directory for the measure") |
| 146 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
|
1310 | ->option_text(" ") |
| 147 | ->required(true) | ||
| 148 |
3/3✓ Branch 2 taken 262 times.
✓ Branch 5 taken 262 times.
✓ Branch 8 taken 262 times.
|
786 | ->check(CLI::NonexistentPath); |
| 149 | |||
| 150 | { | ||
| 151 | static constexpr auto helpOptionsGroupName = "Help"; | ||
| 152 | static constexpr auto newMeasureExamples = R"(Examples: | ||
| 153 | |||
| 154 | 1. Create a Ruby ModelMeasure: | ||
| 155 | |||
| 156 | ``` | ||
| 157 | $ openstudio measure new --class-name MyExampleRubyModelMeasure | ||
| 158 | $ openstudio measure new --class-name MyExampleRubyModelMeasure --type ModelMeasure --language Ruby | ||
| 159 | ``` | ||
| 160 | |||
| 161 | 2. Pass all optional args to create a Python EnergyPlusMeasure: | ||
| 162 | |||
| 163 | ``` | ||
| 164 | $ openstudio measure new --class-name MyExamplePythonMeasure --type EnergyPlusMeasure --language Python --name "My Python Measure" --description "This is my measure" --modeler-description "This does complicated stuff" --taxonomy-tag "Envelope.Form" ./test_measure | ||
| 165 | |||
| 166 | $ openstudio measure new -c MyExamplePythonMeasure -t EnergyPlusMeasure -l Python -n "My Python Measure" -d "This is my measure" -m "This does complicated stuff" --taxonomy-tag "Envelope.Form" ./test_measure | ||
| 167 | ``` | ||
| 168 | |||
| 169 | 3. List taxonomy tags | ||
| 170 | ``` | ||
| 171 | $ openstudio measure new --list-taxonomy-tags | ||
| 172 | $ openstudio measure new --list-for-first-taxonomy-tag HVAC | ||
| 173 | ``` | ||
| 174 | )"; | ||
| 175 |
5/5✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
✓ Branch 7 taken 262 times.
✓ Branch 10 taken 262 times.
✓ Branch 13 taken 262 times.
|
1572 | newMeasureSubCommand->set_help_flag("-h,--help", "Print this help message and exit")->group(helpOptionsGroupName); |
| 176 | |||
| 177 | newMeasureSubCommand | ||
| 178 |
3/3✓ Branch 1 taken 262 times.
✓ Branch 5 taken 262 times.
✓ Branch 8 taken 262 times.
|
1048 | ->add_flag_callback( |
| 179 | "--examples", | ||
| 180 | 262 | []() { | |
| 181 | ✗ | fmt::print("{}\n", newMeasureExamples); | |
| 182 | ✗ | std::exit(0); // NOLINT(concurrency-mt-unsafe) | |
| 183 | }, | ||
| 184 | "Show Example usage") | ||
| 185 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
|
786 | ->group(helpOptionsGroupName); |
| 186 | |||
| 187 | [[maybe_unused]] auto* listTaxonomyFlag = newMeasureSubCommand | ||
| 188 |
4/4✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
✓ Branch 7 taken 262 times.
✓ Branch 10 taken 262 times.
|
1310 | ->add_flag_callback( |
| 189 | "--list-taxonomy-tags", | ||
| 190 |
1/1✓ Branch 1 taken 262 times.
|
524 | [taxonomyTags]() { |
| 191 | ✗ | fmt::print("{}\n", taxonomyTags); | |
| 192 | ✗ | std::exit(0); // NOLINT(concurrency-mt-unsafe) | |
| 193 | }, | ||
| 194 | "List all available Taxonomy tags") | ||
| 195 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
|
786 | ->group(helpOptionsGroupName); |
| 196 | |||
| 197 | newMeasureSubCommand | ||
| 198 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
|
524 | ->add_option_function<std::string>( |
| 199 | "--list-for-first-taxonomy-tag", | ||
| 200 |
1/1✓ Branch 1 taken 262 times.
|
524 | [taxonomyTags](const std::string& firstLevelTaxonomyTerm) { |
| 201 | ✗ | auto secondLevelTaxonomyTerms = openstudio::BCLMeasure::suggestedSecondLevelTaxonomyTerms(firstLevelTaxonomyTerm); | |
| 202 | ✗ | std::vector<std::string> taxonomyTags; | |
| 203 | ✗ | taxonomyTags.reserve(secondLevelTaxonomyTerms.size()); | |
| 204 | ✗ | std::transform(secondLevelTaxonomyTerms.begin(), secondLevelTaxonomyTerms.end(), std::back_inserter(taxonomyTags), | |
| 205 | ✗ | [&firstLevelTaxonomyTerm](auto&& secondLevelTaxonomyTerm) { | |
| 206 | ✗ | return fmt::format("{}.{}", firstLevelTaxonomyTerm, secondLevelTaxonomyTerm); | |
| 207 | }); | ||
| 208 | ✗ | fmt::print("{}\n", taxonomyTags); | |
| 209 | ✗ | std::exit(0); // NOLINT(concurrency-mt-unsafe) | |
| 210 | ✗ | }, | |
| 211 | "Limit taxonomy tags to this first level") | ||
| 212 |
1/1✓ Branch 1 taken 262 times.
|
262 | ->option_text("tag") |
| 213 | // ->excludes(listTaxonomyFlag) | ||
| 214 |
5/5✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
✓ Branch 8 taken 262 times.
✓ Branch 11 taken 262 times.
✓ Branch 15 taken 262 times.
|
2620 | ->check(CLI::IsMember(firstLevelTaxonomyTerms)) |
| 215 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
|
786 | ->group(helpOptionsGroupName); |
| 216 | |||
| 217 | // newMeasureSubCommand->footer(std::string{newMeasureExamples}); | ||
| 218 | } | ||
| 219 | |||
| 220 |
1/1✓ Branch 2 taken 262 times.
|
262 | newMeasureSubCommand->callback([opt] { |
| 221 | // opt->newMeasureOpts.debug_print(); | ||
| 222 | |||
| 223 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
|
6 | if (opt->newMeasureOpts.name.empty()) { |
| 224 | ✗ | opt->newMeasureOpts.name = opt->newMeasureOpts.className; | |
| 225 | } | ||
| 226 | |||
| 227 | 6 | BCLMeasure b(opt->newMeasureOpts.name, opt->newMeasureOpts.className, opt->newMeasureOpts.directoryPath, opt->newMeasureOpts.taxonomyTag, | |
| 228 | 6 | opt->newMeasureOpts.measureType, opt->newMeasureOpts.description, opt->newMeasureOpts.modelerDescription, | |
| 229 |
1/1✓ Branch 2 taken 6 times.
|
12 | opt->newMeasureOpts.measureLanguage); |
| 230 | |||
| 231 |
1/1✓ Branch 2 taken 6 times.
|
12 | fmt::print("Created a {} {} with class name '{}' in '{}'\n", opt->newMeasureOpts.measureLanguage.valueName(), |
| 232 |
1/1✓ Branch 3 taken 6 times.
|
12 | opt->newMeasureOpts.measureType.valueName(), opt->newMeasureOpts.className, |
| 233 |
4/4✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
✓ Branch 7 taken 6 times.
✓ Branch 10 taken 6 times.
|
12 | openstudio::toString(openstudio::filesystem::canonical(b.directory()))); |
| 234 | |||
| 235 | 6 | std::exit(0); // NOLINT(concurrency-mt-unsafe) | |
| 236 | ✗ | }); | |
| 237 | 262 | } | |
| 238 | |||
| 239 | { | ||
| 240 | |||
| 241 |
3/3✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
✓ Branch 7 taken 262 times.
|
1048 | auto* copyMeasureSubCommand = measureCommand->add_subcommand("copy", "Copy a measure"); |
| 242 | [[maybe_unused]] auto* existingDirectoryPathOpt = | ||
| 243 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 5 taken 262 times.
|
524 | copyMeasureSubCommand->add_option("EXISTING_DIRECTORY", opt->directoryPath, "Existing Directory for the measure") |
| 244 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
|
1310 | ->option_text(" ") |
| 245 | ->required(true) | ||
| 246 |
3/3✓ Branch 2 taken 262 times.
✓ Branch 5 taken 262 times.
✓ Branch 8 taken 262 times.
|
786 | ->check(CLI::ExistingDirectory); |
| 247 | [[maybe_unused]] auto* newDirectoryPathOpt = | ||
| 248 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 5 taken 262 times.
|
524 | copyMeasureSubCommand->add_option("NEW_DIRECTORY", opt->newMeasureOpts.directoryPath, "New Directory for the measure") |
| 249 |
2/2✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
|
1310 | ->option_text(" ") |
| 250 | ->required(true) | ||
| 251 |
3/3✓ Branch 2 taken 262 times.
✓ Branch 5 taken 262 times.
✓ Branch 8 taken 262 times.
|
786 | ->check(CLI::NonexistentPath); |
| 252 | |||
| 253 |
1/1✓ Branch 2 taken 262 times.
|
262 | copyMeasureSubCommand->callback([opt] { |
| 254 | ✗ | boost::optional<BCLMeasure> b_; | |
| 255 | try { | ||
| 256 | ✗ | b_ = BCLMeasure(opt->directoryPath); | |
| 257 | ✗ | } catch (...) { | |
| 258 | ✗ | fmt::print(stderr, "Could not find a valid measure at '{}'\n", openstudio::toString(opt->directoryPath)); | |
| 259 | ✗ | std::exit(1); | |
| 260 | ✗ | } | |
| 261 | ✗ | auto bClone_ = b_->clone(opt->newMeasureOpts.directoryPath); | |
| 262 | ✗ | if (bClone_) { | |
| 263 | // Force updating the UID | ||
| 264 | ✗ | bClone_->changeUID(); | |
| 265 | ✗ | bClone_->checkForUpdatesFiles(); | |
| 266 | ✗ | bClone_->checkForUpdatesXML(); | |
| 267 | ✗ | bClone_->save(); | |
| 268 | ✗ | fmt::print("Cloned the {} {} with class name '{}' from '{}' to '{}'\n", b_->measureLanguage().valueName(), b_->measureType().valueName(), | |
| 269 | ✗ | b_->className(), openstudio::toString(openstudio::filesystem::canonical(b_->directory())), | |
| 270 | ✗ | openstudio::toString(openstudio::filesystem::canonical(bClone_->directory()))); | |
| 271 | ✗ | std::exit(0); // NOLINT(concurrency-mt-unsafe) | |
| 272 | } else { | ||
| 273 | ✗ | fmt::print(stderr, "Something went wrong when cloning the measure"); | |
| 274 | ✗ | std::exit(1); // NOLINT(concurrency-mt-unsafe) | |
| 275 | } | ||
| 276 | ✗ | }); | |
| 277 | } | ||
| 278 | |||
| 279 |
1/3✓ Branch 2 taken 262 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
|
262 | measureCommand->callback([opt, &rubyEngine, &pythonEngine] { MeasureUpdateOptions::execute(*opt, rubyEngine, pythonEngine); }); |
| 280 | 262 | } | |
| 281 | |||
| 282 | ✗ | void MeasureUpdateOptions::execute(MeasureUpdateOptions const& opt, ScriptEngineInstance& rubyEngine, ScriptEngineInstance& pythonEngine) { | |
| 283 | // opt.debug_print(); | ||
| 284 | |||
| 285 | ✗ | if (opt.server_port > 0) { | |
| 286 | ✗ | MeasureManagerServer server(opt.server_port, rubyEngine, pythonEngine); | |
| 287 | ✗ | server.open(); // This starts the cpp-httplib http server in a background thread | |
| 288 | ✗ | server.do_tasks_forever(); // This starts the event loop on the **main** thread to process the requests there and NOT in a subthread | |
| 289 | |||
| 290 | ✗ | return; | |
| 291 | ✗ | } else if (opt.update) { | |
| 292 | ✗ | MeasureManager measureManager(rubyEngine, pythonEngine); | |
| 293 | ✗ | if (auto measure_ = measureManager.getMeasure(opt.directoryPath, true)) { | |
| 294 | // TODO: maybe I should write an OSMeasureInfo::toJSON() method, but that'd be duplicating the code in BCLMeasure (BCLXML to be exact). | ||
| 295 | // So since the only thing that's different is the OSArgument (OSMeasureInfo) versus BCLMeasureArgument (BCLMeasure), we just override | ||
| 296 | ✗ | auto result = measure_->toJSON(); | |
| 297 | |||
| 298 | ✗ | fmt::print("{}\n", result.toStyledString()); | |
| 299 | ✗ | } else { | |
| 300 | ✗ | fmt::print(stderr, "Cannot load measure from {}\n", opt.directoryPath.generic_string()); | |
| 301 | ✗ | } | |
| 302 | ✗ | return; | |
| 303 | ✗ | } else if (opt.update_all) { | |
| 304 | ✗ | MeasureManager measureManager(rubyEngine, pythonEngine); | |
| 305 | ✗ | std::vector<openstudio::path> subDirPaths; | |
| 306 | ✗ | for (auto const& dir_entry : boost::filesystem::directory_iterator{opt.directoryPath}) { | |
| 307 | ✗ | const auto& subDirPath = dir_entry.path(); | |
| 308 | |||
| 309 | ✗ | if (openstudio::filesystem::is_directory(subDirPath) && openstudio::filesystem::is_regular_file(subDirPath / "measure.xml")) { | |
| 310 | ✗ | subDirPaths.emplace_back(subDirPath); | |
| 311 | } | ||
| 312 | ✗ | } | |
| 313 | ✗ | fmt::print("Found {} measure directories to update\n", subDirPaths.size()); | |
| 314 | ✗ | for (const auto& subDirPath : subDirPaths) { | |
| 315 | ✗ | measureManager.getMeasure(subDirPath, true); | |
| 316 | } | ||
| 317 | ✗ | } else if (!opt.compute_arguments_model.empty()) { | |
| 318 | // NOTE: cannot move into MeasureManager to avoid repeating the code shared with MeasureManagerServer. This expects an OSM **or an IDF** in the | ||
| 319 | // case of an EnergyPlus Measure, whereas the server /compute_arguments endpoint always expects an OSM | ||
| 320 | ✗ | MeasureManager measureManager(rubyEngine, pythonEngine); | |
| 321 | ✗ | auto measure_ = measureManager.getMeasure(opt.directoryPath, true); | |
| 322 | ✗ | if (!measure_) { | |
| 323 | ✗ | auto msg = fmt::format("Cannot load measure at '{}'", opt.directoryPath.generic_string()); | |
| 324 | ✗ | fmt::print(stderr, "{}\n", msg); | |
| 325 | ✗ | return; | |
| 326 | ✗ | } | |
| 327 | |||
| 328 | ✗ | openstudio::measure::OSMeasureInfo info = measureManager.getMeasureInfo(opt.directoryPath, *measure_, opt.compute_arguments_model); | |
| 329 | ✗ | if (auto errorString_ = info.error()) { | |
| 330 | ✗ | fmt::print(stderr, "{}\n", *errorString_); | |
| 331 | ✗ | return; | |
| 332 | ✗ | } | |
| 333 | // TODO: maybe I should write an OSMeasureInfo::toJSON() method, but that'd be duplicating the code in BCLMeasure (BCLXML to be exact). | ||
| 334 | // So since the only thing that's different is the OSArgument (OSMeasureInfo) versus BCLMeasureArgument (BCLMeasure), we just override | ||
| 335 | ✗ | auto result = measure_->toJSON(); | |
| 336 | ✗ | auto& arguments = result["arguments"]; | |
| 337 | ✗ | arguments.clear(); | |
| 338 | ✗ | for (const measure::OSArgument& argument : info.arguments()) { | |
| 339 | ✗ | arguments.append(argument.toJSON()); | |
| 340 | ✗ | } | |
| 341 | ✗ | Json::StreamWriterBuilder builder; | |
| 342 | ✗ | builder["commentStyle"] = "None"; | |
| 343 | ✗ | builder["indentation"] = " "; | |
| 344 | ✗ | std::string resultStr = Json::writeString(builder, result); | |
| 345 | ✗ | fmt::print("{}\n", resultStr); | |
| 346 | ✗ | return; | |
| 347 | ✗ | } else if (opt.run_tests) { | |
| 348 | |||
| 349 | ✗ | auto canonicalTestDir = openstudio::filesystem::canonical(opt.directoryPath); | |
| 350 | |||
| 351 | // Pytest | ||
| 352 | ✗ | fmt::print(fmt::fg(fmt::color::yellow), | |
| 353 | "┌{0:─^{2}}┐\n" | ||
| 354 | "│{1: ^{2}}│\n" | ||
| 355 | "└{0:─^{2}}┘", | ||
| 356 | ✗ | "", "Starting Python Tests", 80); | |
| 357 | ✗ | fmt::print("\n"); | |
| 358 | ✗ | auto pytestOutDir = canonicalTestDir / "test_results" / "pytest"; | |
| 359 | |||
| 360 | // -o junit_familar=legacy is to mimic having a pytest.ini with: | ||
| 361 | // [pytest] | ||
| 362 | // junit_family=legacy | ||
| 363 | // This uses xunit1 format, and adds the line + file at which error occured, something we don't have with xunit2 (default) | ||
| 364 | |||
| 365 | auto runPytestCmd = fmt::format( | ||
| 366 | R"python( | ||
| 367 | import pytest | ||
| 368 | |||
| 369 | pytest.main([ | ||
| 370 | "--junit-xml={junit}", | ||
| 371 | "--cov={test_dir}", | ||
| 372 | "--cov-report=term-missing", | ||
| 373 | "--cov-report=json:{json}", | ||
| 374 | "--cov-report=lcov:{lcov}", | ||
| 375 | "--cov-report=html:{html}", | ||
| 376 | "--cov-report=xml:{xml}", | ||
| 377 | "-o", "junit_family=legacy", | ||
| 378 | "{test_dir}", | ||
| 379 | ]) | ||
| 380 | )python", | ||
| 381 | ✗ | fmt::arg("junit", (pytestOutDir / "junit.xml").generic_string()), // | |
| 382 | ✗ | fmt::arg("test_dir", canonicalTestDir.generic_string()), // | |
| 383 | ✗ | fmt::arg("json", (pytestOutDir / "python_coverage.json").generic_string()), | |
| 384 | ✗ | fmt::arg("lcov", (pytestOutDir / "python_coverage.lcov").generic_string()), | |
| 385 | ✗ | fmt::arg("html", (pytestOutDir / "python_coverage_html").generic_string()), | |
| 386 | ✗ | fmt::arg("xml", (pytestOutDir / "python_coverage.xml").generic_string())); | |
| 387 | |||
| 388 | ✗ | fmt::print("runPytestCmd={}\n", runPytestCmd); | |
| 389 | ✗ | pythonEngine->exec(runPytestCmd); | |
| 390 | |||
| 391 | ✗ | fmt::print(fmt::fg(fmt::color::red), | |
| 392 | "┌{0:─^{2}}┐\n" | ||
| 393 | "│{1: ^{2}}│\n" | ||
| 394 | "└{0:─^{2}}┘", | ||
| 395 | ✗ | "", "Starting ruby tests", 80); | |
| 396 | ✗ | fmt::print("\n"); | |
| 397 | |||
| 398 | auto runTestCmd = fmt::format( | ||
| 399 | R"ruby( | ||
| 400 | # load openstudio_measure_tester gem | ||
| 401 | #begin | ||
| 402 | require 'minitest' | ||
| 403 | require 'minitest/reporters' | ||
| 404 | |||
| 405 | # Minitest Reports use a plugin that is normally found by Minitest::load_plugins using Gem.find | ||
| 406 | # until Gem.find is overloaded to find embedded gems, we will manually load the plugin here | ||
| 407 | require 'minitest/minitest_reporter_plugin' | ||
| 408 | Minitest.extensions << 'minitest_reporter' | ||
| 409 | |||
| 410 | require 'openstudio_measure_tester' | ||
| 411 | #rescue LoadError | ||
| 412 | #puts "Cannot load 'openstudio_measure_tester'" | ||
| 413 | #return 1 | ||
| 414 | #end | ||
| 415 | |||
| 416 | runner = OpenStudioMeasureTester::Runner.new('{}') | ||
| 417 | result = runner.run_all(Dir.pwd) # <--------------- TODO: this is problematic? | ||
| 418 | |||
| 419 | if result != 0 | ||
| 420 | $logger.error("Measure tester returned errors") | ||
| 421 | return 1 | ||
| 422 | end | ||
| 423 | )ruby", | ||
| 424 | ✗ | canonicalTestDir.generic_string()); | |
| 425 | ✗ | rubyEngine->exec(runTestCmd); | |
| 426 | ✗ | } | |
| 427 | } | ||
| 428 | |||
| 429 | ✗ | void MeasureUpdateOptions::debug_print() const { | |
| 430 | ✗ | fmt::print("\nMeasureUpdateOptions:\n"); | |
| 431 | ✗ | fmt::print("directoryPath={}\n", this->directoryPath.string()); | |
| 432 | ✗ | fmt::print("update={}\n", this->update); | |
| 433 | ✗ | fmt::print("update_all={}\n", this->update_all); | |
| 434 | ✗ | fmt::print("compute_arguments_model={}\n", this->compute_arguments_model.string()); | |
| 435 | ✗ | fmt::print("run_tests={}\n", this->run_tests); | |
| 436 | ✗ | fmt::print("server_port={}\n", this->server_port); | |
| 437 | ✗ | fmt::print("\n\n"); | |
| 438 | ✗ | } | |
| 439 | |||
| 440 | ✗ | void MeasureNewOptions::debug_print() const { | |
| 441 | ✗ | fmt::print("\nMeasureNewOptions:\n"); | |
| 442 | ✗ | fmt::print("name={}\n", name); | |
| 443 | ✗ | fmt::print("className={}\n", className); | |
| 444 | ✗ | fmt::print("directoryPath={}\n", openstudio::toString(directoryPath)); | |
| 445 | ✗ | fmt::print("taxonomyTag={}\n", taxonomyTag); | |
| 446 | ✗ | fmt::print("measureType={}\n", measureType.valueName()); | |
| 447 | ✗ | fmt::print("description={}\n", description); | |
| 448 | ✗ | fmt::print("modelerDescription={}\n", modelerDescription); | |
| 449 | ✗ | fmt::print("measureLanguage={}\n", measureLanguage.valueName()); | |
| 450 | ✗ | }; | |
| 451 | |||
| 452 | } // namespace cli | ||
| 453 | } // namespace openstudio | ||
| 454 |