summaryrefslogtreecommitdiffstats
path: root/Source/CTest/cmCTestHardwareSpec.cxx
blob: 137398aa838de2ce3969a9802a37b4f267874ba2 (plain)
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
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
#include "cmCTestHardwareSpec.h"

#include <map>
#include <string>
#include <utility>
#include <vector>

#include "cmsys/FStream.hxx"
#include "cmsys/RegularExpression.hxx"

#include "cm_jsoncpp_reader.h"
#include "cm_jsoncpp_value.h"

static const cmsys::RegularExpression IdentifierRegex{ "^[a-z_][a-z0-9_]*$" };
static const cmsys::RegularExpression IdRegex{ "^[a-z0-9_]+$" };

bool cmCTestHardwareSpec::ReadFromJSONFile(const std::string& filename)
{
  cmsys::ifstream fin(filename.c_str());
  if (!fin) {
    return false;
  }

  Json::Value root;
  Json::CharReaderBuilder builder;
  if (!Json::parseFromStream(builder, fin, &root, nullptr)) {
    return false;
  }

  if (!root.isObject()) {
    return false;
  }

  auto const& local = root["local"];
  if (!local.isArray()) {
    return false;
  }
  if (local.size() > 1) {
    return false;
  }

  if (local.empty()) {
    this->LocalSocket.Resources.clear();
    return true;
  }

  auto const& localSocket = local[0];
  if (!localSocket.isObject()) {
    return false;
  }
  std::map<std::string, std::vector<cmCTestHardwareSpec::Resource>> resources;
  cmsys::RegularExpressionMatch match;
  for (auto const& key : localSocket.getMemberNames()) {
    if (IdentifierRegex.find(key.c_str(), match)) {
      auto const& value = localSocket[key];
      auto& r = resources[key];
      if (value.isArray()) {
        for (auto const& item : value) {
          if (item.isObject()) {
            cmCTestHardwareSpec::Resource resource;

            if (!item.isMember("id")) {
              return false;
            }
            auto const& id = item["id"];
            if (!id.isString()) {
              return false;
            }
            resource.Id = id.asString();
            if (!IdRegex.find(resource.Id.c_str(), match)) {
              return false;
            }

            if (item.isMember("slots")) {
              auto const& capacity = item["slots"];
              if (!capacity.isConvertibleTo(Json::uintValue)) {
                return false;
              }
              resource.Capacity = capacity.asUInt();
            } else {
              resource.Capacity = 1;
            }

            r.push_back(resource);
          } else {
            return false;
          }
        }
      } else {
        return false;
      }
    }
  }

  this->LocalSocket.Resources = std::move(resources);
  return true;
}

bool cmCTestHardwareSpec::operator==(const cmCTestHardwareSpec& other) const
{
  return this->LocalSocket == other.LocalSocket;
}

bool cmCTestHardwareSpec::operator!=(const cmCTestHardwareSpec& other) const
{
  return !(*this == other);
}

bool cmCTestHardwareSpec::Socket::operator==(
  const cmCTestHardwareSpec::Socket& other) const
{
  return this->Resources == other.Resources;
}

bool cmCTestHardwareSpec::Socket::operator!=(
  const cmCTestHardwareSpec::Socket& other) const
{
  return !(*this == other);
}

bool cmCTestHardwareSpec::Resource::operator==(
  const cmCTestHardwareSpec::Resource& other) const
{
  return this->Id == other.Id && this->Capacity == other.Capacity;
}

bool cmCTestHardwareSpec::Resource::operator!=(
  const cmCTestHardwareSpec::Resource& other) const
{
  return !(*this == other);
}