cmCTestResourceSpec.cxx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. bool cmCTestResourceSpec::ReadFromJSONFile(const std::string& filename)
  15. {
  16. cmsys::ifstream fin(filename.c_str());
  17. if (!fin) {
  18. return false;
  19. }
  20. Json::Value root;
  21. Json::CharReaderBuilder builder;
  22. if (!Json::parseFromStream(builder, fin, &root, nullptr)) {
  23. return false;
  24. }
  25. if (!root.isObject()) {
  26. return false;
  27. }
  28. int majorVersion = 1;
  29. int minorVersion = 0;
  30. if (root.isMember("version")) {
  31. auto const& version = root["version"];
  32. if (version.isObject()) {
  33. if (!version.isMember("major") || !version.isMember("minor")) {
  34. return false;
  35. }
  36. auto const& major = version["major"];
  37. auto const& minor = version["minor"];
  38. if (!major.isInt() || !minor.isInt()) {
  39. return false;
  40. }
  41. majorVersion = major.asInt();
  42. minorVersion = minor.asInt();
  43. } else {
  44. return false;
  45. }
  46. } else {
  47. return false;
  48. }
  49. if (majorVersion != 1 || minorVersion != 0) {
  50. return false;
  51. }
  52. auto const& local = root["local"];
  53. if (!local.isArray()) {
  54. return false;
  55. }
  56. if (local.size() > 1) {
  57. return false;
  58. }
  59. if (local.empty()) {
  60. this->LocalSocket.Resources.clear();
  61. return true;
  62. }
  63. auto const& localSocket = local[0];
  64. if (!localSocket.isObject()) {
  65. return false;
  66. }
  67. std::map<std::string, std::vector<cmCTestResourceSpec::Resource>> resources;
  68. cmsys::RegularExpressionMatch match;
  69. for (auto const& key : localSocket.getMemberNames()) {
  70. if (IdentifierRegex.find(key.c_str(), match)) {
  71. auto const& value = localSocket[key];
  72. auto& r = resources[key];
  73. if (value.isArray()) {
  74. for (auto const& item : value) {
  75. if (item.isObject()) {
  76. cmCTestResourceSpec::Resource resource;
  77. if (!item.isMember("id")) {
  78. return false;
  79. }
  80. auto const& id = item["id"];
  81. if (!id.isString()) {
  82. return false;
  83. }
  84. resource.Id = id.asString();
  85. if (!IdRegex.find(resource.Id.c_str(), match)) {
  86. return false;
  87. }
  88. if (item.isMember("slots")) {
  89. auto const& capacity = item["slots"];
  90. if (!capacity.isConvertibleTo(Json::uintValue)) {
  91. return false;
  92. }
  93. resource.Capacity = capacity.asUInt();
  94. } else {
  95. resource.Capacity = 1;
  96. }
  97. r.push_back(resource);
  98. } else {
  99. return false;
  100. }
  101. }
  102. } else {
  103. return false;
  104. }
  105. }
  106. }
  107. this->LocalSocket.Resources = std::move(resources);
  108. return true;
  109. }
  110. bool cmCTestResourceSpec::operator==(const cmCTestResourceSpec& other) const
  111. {
  112. return this->LocalSocket == other.LocalSocket;
  113. }
  114. bool cmCTestResourceSpec::operator!=(const cmCTestResourceSpec& other) const
  115. {
  116. return !(*this == other);
  117. }
  118. bool cmCTestResourceSpec::Socket::operator==(
  119. const cmCTestResourceSpec::Socket& other) const
  120. {
  121. return this->Resources == other.Resources;
  122. }
  123. bool cmCTestResourceSpec::Socket::operator!=(
  124. const cmCTestResourceSpec::Socket& other) const
  125. {
  126. return !(*this == other);
  127. }
  128. bool cmCTestResourceSpec::Resource::operator==(
  129. const cmCTestResourceSpec::Resource& other) const
  130. {
  131. return this->Id == other.Id && this->Capacity == other.Capacity;
  132. }
  133. bool cmCTestResourceSpec::Resource::operator!=(
  134. const cmCTestResourceSpec::Resource& other) const
  135. {
  136. return !(*this == other);
  137. }