cmCTestResourceSpec.cxx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmCTestResourceSpec.h"
  4. #include <map>
  5. #include <string>
  6. #include <utility>
  7. #include <vector>
  8. #include "cmsys/FStream.hxx"
  9. #include "cmsys/RegularExpression.hxx"
  10. #include "cm_jsoncpp_reader.h"
  11. #include "cm_jsoncpp_value.h"
  12. static const cmsys::RegularExpression IdentifierRegex{ "^[a-z_][a-z0-9_]*$" };
  13. static const cmsys::RegularExpression IdRegex{ "^[a-z0-9_]+$" };
  14. cmCTestResourceSpec::ReadFileResult cmCTestResourceSpec::ReadFromJSONFile(
  15. const std::string& filename)
  16. {
  17. cmsys::ifstream fin(filename.c_str());
  18. if (!fin) {
  19. return ReadFileResult::FILE_NOT_FOUND;
  20. }
  21. Json::Value root;
  22. Json::CharReaderBuilder builder;
  23. if (!Json::parseFromStream(builder, fin, &root, nullptr)) {
  24. return ReadFileResult::JSON_PARSE_ERROR;
  25. }
  26. if (!root.isObject()) {
  27. return ReadFileResult::INVALID_ROOT;
  28. }
  29. int majorVersion = 1;
  30. int minorVersion = 0;
  31. if (root.isMember("version")) {
  32. auto const& version = root["version"];
  33. if (version.isObject()) {
  34. if (!version.isMember("major") || !version.isMember("minor")) {
  35. return ReadFileResult::INVALID_VERSION;
  36. }
  37. auto const& major = version["major"];
  38. auto const& minor = version["minor"];
  39. if (!major.isInt() || !minor.isInt()) {
  40. return ReadFileResult::INVALID_VERSION;
  41. }
  42. majorVersion = major.asInt();
  43. minorVersion = minor.asInt();
  44. } else {
  45. return ReadFileResult::INVALID_VERSION;
  46. }
  47. } else {
  48. return ReadFileResult::NO_VERSION;
  49. }
  50. if (majorVersion != 1 || minorVersion != 0) {
  51. return ReadFileResult::UNSUPPORTED_VERSION;
  52. }
  53. auto const& local = root["local"];
  54. if (!local.isArray()) {
  55. return ReadFileResult::INVALID_SOCKET_SPEC;
  56. }
  57. if (local.size() > 1) {
  58. return ReadFileResult::INVALID_SOCKET_SPEC;
  59. }
  60. if (local.empty()) {
  61. this->LocalSocket.Resources.clear();
  62. return ReadFileResult::READ_OK;
  63. }
  64. auto const& localSocket = local[0];
  65. if (!localSocket.isObject()) {
  66. return ReadFileResult::INVALID_SOCKET_SPEC;
  67. }
  68. std::map<std::string, std::vector<cmCTestResourceSpec::Resource>> resources;
  69. cmsys::RegularExpressionMatch match;
  70. for (auto const& key : localSocket.getMemberNames()) {
  71. if (IdentifierRegex.find(key.c_str(), match)) {
  72. auto const& value = localSocket[key];
  73. auto& r = resources[key];
  74. if (value.isArray()) {
  75. for (auto const& item : value) {
  76. if (item.isObject()) {
  77. cmCTestResourceSpec::Resource resource;
  78. if (!item.isMember("id")) {
  79. return ReadFileResult::INVALID_RESOURCE;
  80. }
  81. auto const& id = item["id"];
  82. if (!id.isString()) {
  83. return ReadFileResult::INVALID_RESOURCE;
  84. }
  85. resource.Id = id.asString();
  86. if (!IdRegex.find(resource.Id.c_str(), match)) {
  87. return ReadFileResult::INVALID_RESOURCE;
  88. }
  89. if (item.isMember("slots")) {
  90. auto const& capacity = item["slots"];
  91. if (!capacity.isConvertibleTo(Json::uintValue)) {
  92. return ReadFileResult::INVALID_RESOURCE;
  93. }
  94. resource.Capacity = capacity.asUInt();
  95. } else {
  96. resource.Capacity = 1;
  97. }
  98. r.push_back(resource);
  99. } else {
  100. return ReadFileResult::INVALID_RESOURCE;
  101. }
  102. }
  103. } else {
  104. return ReadFileResult::INVALID_RESOURCE_TYPE;
  105. }
  106. }
  107. }
  108. this->LocalSocket.Resources = std::move(resources);
  109. return ReadFileResult::READ_OK;
  110. }
  111. const char* cmCTestResourceSpec::ResultToString(ReadFileResult result)
  112. {
  113. switch (result) {
  114. case ReadFileResult::READ_OK:
  115. return "OK";
  116. case ReadFileResult::FILE_NOT_FOUND:
  117. return "File not found";
  118. case ReadFileResult::JSON_PARSE_ERROR:
  119. return "JSON parse error";
  120. case ReadFileResult::INVALID_ROOT:
  121. return "Invalid root object";
  122. case ReadFileResult::NO_VERSION:
  123. return "No version specified";
  124. case ReadFileResult::INVALID_VERSION:
  125. return "Invalid version object";
  126. case ReadFileResult::UNSUPPORTED_VERSION:
  127. return "Unsupported version";
  128. case ReadFileResult::INVALID_SOCKET_SPEC:
  129. return "Invalid socket object";
  130. case ReadFileResult::INVALID_RESOURCE_TYPE:
  131. return "Invalid resource type object";
  132. case ReadFileResult::INVALID_RESOURCE:
  133. return "Invalid resource object";
  134. default:
  135. return "Unknown";
  136. }
  137. }
  138. bool cmCTestResourceSpec::operator==(const cmCTestResourceSpec& other) const
  139. {
  140. return this->LocalSocket == other.LocalSocket;
  141. }
  142. bool cmCTestResourceSpec::operator!=(const cmCTestResourceSpec& other) const
  143. {
  144. return !(*this == other);
  145. }
  146. bool cmCTestResourceSpec::Socket::operator==(
  147. const cmCTestResourceSpec::Socket& other) const
  148. {
  149. return this->Resources == other.Resources;
  150. }
  151. bool cmCTestResourceSpec::Socket::operator!=(
  152. const cmCTestResourceSpec::Socket& other) const
  153. {
  154. return !(*this == other);
  155. }
  156. bool cmCTestResourceSpec::Resource::operator==(
  157. const cmCTestResourceSpec::Resource& other) const
  158. {
  159. return this->Id == other.Id && this->Capacity == other.Capacity;
  160. }
  161. bool cmCTestResourceSpec::Resource::operator!=(
  162. const cmCTestResourceSpec::Resource& other) const
  163. {
  164. return !(*this == other);
  165. }