3
# Copyright 2012 10gen Inc.
5
# Licensed under the Apache License, Version 2.0 (the "License");
6
# you may not use this file except in compliance with the License.
7
# You may obtain a copy of the License at
9
# http://www.apache.org/licenses/LICENSE-2.0
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS,
13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
# See the License for the specific language governing permissions and
15
# limitations under the License.
18
"""Generate action_type.{h,cpp}
21
python generate_action_types.py <path to action_types.txt> <header file path> <source file path>
27
headerFileTemplate = """// AUTO-GENERATED FILE DO NOT EDIT
28
// See src/mongo/db/auth/generate_action_types.py
29
/* Copyright 2012 10gen Inc.
31
* Licensed under the Apache License, Version 2.0 (the "License");
32
* you may not use this file except in compliance with the License.
33
* You may obtain a copy of the License at
35
* http://www.apache.org/licenses/LICENSE-2.0
37
* Unless required by applicable law or agreed to in writing, software
38
* distributed under the License is distributed on an "AS IS" BASIS,
39
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
40
* See the License for the specific language governing permissions and
41
* limitations under the License.
50
#include "mongo/base/status.h"
51
#include "mongo/platform/cstdint.h"
58
explicit ActionType(uint32_t identifier) : _identifier(identifier) {};
61
uint32_t getIdentifier() const {
65
bool operator==(const ActionType& rhs) const;
67
std::string toString() const;
69
// Takes the string representation of a single action type and returns the corresponding
71
static Status parseActionFromString(const std::string& actionString, ActionType* result);
73
// Takes an ActionType and returns the string representation
74
static std::string actionToString(const ActionType& action);
76
%(actionTypeConstants)s
77
enum ActionTypeIdentifier {
78
%(actionTypeIdentifiers)s
79
actionTypeEndValue, // Should always be last in this enum
82
static const int NUM_ACTION_TYPES = actionTypeEndValue;
86
uint32_t _identifier; // unique identifier for this action.
89
// String stream operator for ActionType
90
std::ostream& operator<<(std::ostream& os, const ActionType& at);
95
sourceFileTemplate = """// AUTO-GENERATED FILE DO NOT EDIT
96
// See src/mongo/db/auth/generate_action_types.py
97
/* Copyright 2012 10gen Inc.
99
* Licensed under the Apache License, Version 2.0 (the "License");
100
* you may not use this file except in compliance with the License.
101
* You may obtain a copy of the License at
103
* http://www.apache.org/licenses/LICENSE-2.0
105
* Unless required by applicable law or agreed to in writing, software
106
* distributed under the License is distributed on an "AS IS" BASIS,
107
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
108
* See the License for the specific language governing permissions and
109
* limitations under the License.
112
#include "mongo/pch.h"
114
#include "mongo/db/auth/action_type.h"
119
#include "mongo/base/status.h"
120
#include "mongo/platform/cstdint.h"
121
#include "mongo/util/mongoutils/str.h"
125
%(actionTypeConstants)s
126
bool ActionType::operator==(const ActionType& rhs) const {
127
return _identifier == rhs._identifier;
130
std::ostream& operator<<(std::ostream& os, const ActionType& at) {
131
os << ActionType::actionToString(at);
135
std::string ActionType::toString() const {
136
return actionToString(*this);
139
Status ActionType::parseActionFromString(const std::string& action, ActionType* result) {
140
%(fromStringIfStatements)s
141
return Status(ErrorCodes::FailedToParse,
142
mongoutils::str::stream() << "Unrecognized action privilege string: "
147
// Takes an ActionType and returns the string representation
148
std::string ActionType::actionToString(const ActionType& action) {
149
switch (action.getIdentifier()) {
150
%(toStringCaseStatements)s default:
158
def writeSourceFile(actionTypes, sourceOutputFile):
159
actionTypeConstants = ""
160
fromStringIfStatements = ""
161
toStringCaseStatements = ""
162
for actionType in actionTypes:
163
actionTypeConstants += (" const ActionType ActionType::%(actionType)s"
164
"(%(actionType)sValue);\n" %
165
dict(actionType=actionType))
166
fromStringIfStatements += """ if (action == "%(actionType)s") {
167
*result = %(actionType)s;
169
}\n""" % dict(actionType=actionType)
170
toStringCaseStatements += """ case %(actionType)sValue:
171
return "%(actionType)s";\n""" % dict(actionType=actionType)
172
formattedSourceFile = sourceFileTemplate % dict(actionTypeConstants=actionTypeConstants,
173
fromStringIfStatements=fromStringIfStatements,
174
toStringCaseStatements=toStringCaseStatements)
175
sourceOutputFile.write(formattedSourceFile)
179
def writeHeaderFile(actionTypes, headerOutputFile):
180
actionTypeConstants = ""
181
actionTypeIdentifiers = ""
182
for actionType in actionTypes:
183
actionTypeConstants += " static const ActionType %s;\n" % (actionType)
184
actionTypeIdentifiers += " %sValue,\n" % (actionType)
185
formattedHeaderFile = headerFileTemplate % dict(actionTypeConstants=actionTypeConstants,
186
actionTypeIdentifiers=actionTypeIdentifiers)
187
headerOutputFile.write(formattedHeaderFile)
189
def hasDuplicateActionTypes(actionTypes):
190
sortedActionTypes = sorted(actionTypes)
193
prevActionType = sortedActionTypes[0]
194
for actionType in sortedActionTypes[1:]:
195
if actionType == prevActionType:
196
print 'Duplicate actionType %s\n' % actionType
198
prevActionType = actionType
202
def parseActionTypesFromFile(actionTypesFilename):
203
actionTypesFile = open(actionTypesFilename, 'r')
204
actionTypes = eval(actionTypesFile.read())
207
if __name__ == "__main__":
208
if len(sys.argv) != 4:
209
print "Usage: generate_action_types.py <path to action_types.txt> <header file path> <source file path>"
212
actionTypes = parseActionTypesFromFile(sys.argv[1])
213
if hasDuplicateActionTypes(actionTypes):
216
headerOutputFile = open(sys.argv[2], 'w')
217
sourceOutputFile = open(sys.argv[3], 'w')
219
writeHeaderFile(actionTypes, headerOutputFile)
220
writeSourceFile(actionTypes, sourceOutputFile)