~evarlast/ubuntu/utopic/mongodb/upstart-workaround-debian-bug-718702

« back to all changes in this revision

Viewing changes to src/mongo/db/auth/generate_action_types.py

  • Committer: Package Import Robot
  • Author(s): James Page, James Page, Robie Basak
  • Date: 2013-05-29 17:44:42 UTC
  • mfrom: (44.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20130529174442-z0a4qmoww4y0t458
Tags: 1:2.4.3-1ubuntu1
[ James Page ]
* Merge from Debian unstable, remaining changes:
  - Enable SSL support:
    + d/control: Add libssl-dev to BD's.
    + d/rules: Enabled --ssl option.
    + d/mongodb.conf: Add example SSL configuration options.
  - d/mongodb-server.mongodb.upstart: Add upstart configuration.
  - d/rules: Don't strip binaries during scons build for Ubuntu.
  - d/control: Add armhf to target archs.
  - d/p/SConscript.client.patch: fixup install of client libraries.
  - d/p/0010-install-libs-to-usr-lib-not-usr-lib64-Closes-588557.patch:
    Install libraries to lib not lib64.
* Dropped changes:
  - d/p/arm-support.patch: Included in Debian.
  - d/p/double-alignment.patch: Included in Debian.
  - d/rules,control: Debian also builds with avaliable system libraries
    now.
* Fix FTBFS due to gcc and boost upgrades in saucy:
  - d/p/0008-ignore-unused-local-typedefs.patch: Add -Wno-unused-typedefs
    to unbreak building with g++-4.8.
  - d/p/0009-boost-1.53.patch: Fixup signed/unsigned casting issue.

[ Robie Basak ]
* d/p/0011-Use-a-signed-char-to-store-BSONType-enumerations.patch: Fixup
  build failure on ARM due to missing signed'ness of char cast.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
#    Copyright 2012 10gen Inc.
 
4
#
 
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
 
8
#
 
9
#    http://www.apache.org/licenses/LICENSE-2.0
 
10
#
 
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.
 
16
#
 
17
 
 
18
"""Generate action_type.{h,cpp}
 
19
 
 
20
Usage:
 
21
    python generate_action_types.py <path to action_types.txt> <header file path> <source file path>
 
22
"""
 
23
 
 
24
import sys
 
25
 
 
26
 
 
27
headerFileTemplate = """// AUTO-GENERATED FILE DO NOT EDIT
 
28
// See src/mongo/db/auth/generate_action_types.py
 
29
/*    Copyright 2012 10gen Inc.
 
30
 *
 
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
 
34
 *
 
35
 *    http://www.apache.org/licenses/LICENSE-2.0
 
36
 *
 
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.
 
42
 */
 
43
 
 
44
#pragma once
 
45
 
 
46
#include <iosfwd>
 
47
#include <map>
 
48
#include <string>
 
49
 
 
50
#include "mongo/base/status.h"
 
51
#include "mongo/platform/cstdint.h"
 
52
 
 
53
namespace mongo {
 
54
 
 
55
    struct ActionType {
 
56
    public:
 
57
 
 
58
        explicit ActionType(uint32_t identifier) : _identifier(identifier) {};
 
59
        ActionType() {};
 
60
 
 
61
        uint32_t getIdentifier() const {
 
62
            return _identifier;
 
63
        }
 
64
 
 
65
        bool operator==(const ActionType& rhs) const;
 
66
 
 
67
        std::string toString() const;
 
68
 
 
69
        // Takes the string representation of a single action type and returns the corresponding
 
70
        // ActionType enum.
 
71
        static Status parseActionFromString(const std::string& actionString, ActionType* result);
 
72
 
 
73
        // Takes an ActionType and returns the string representation
 
74
        static std::string actionToString(const ActionType& action);
 
75
 
 
76
%(actionTypeConstants)s
 
77
        enum ActionTypeIdentifier {
 
78
%(actionTypeIdentifiers)s
 
79
            actionTypeEndValue, // Should always be last in this enum
 
80
        };
 
81
 
 
82
        static const int NUM_ACTION_TYPES = actionTypeEndValue;
 
83
 
 
84
    private:
 
85
 
 
86
        uint32_t _identifier; // unique identifier for this action.
 
87
    };
 
88
 
 
89
    // String stream operator for ActionType
 
90
    std::ostream& operator<<(std::ostream& os, const ActionType& at);
 
91
 
 
92
} // namespace mongo
 
93
"""
 
94
 
 
95
sourceFileTemplate = """// AUTO-GENERATED FILE DO NOT EDIT
 
96
// See src/mongo/db/auth/generate_action_types.py
 
97
/*    Copyright 2012 10gen Inc.
 
98
 *
 
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
 
102
 *
 
103
 *    http://www.apache.org/licenses/LICENSE-2.0
 
104
 *
 
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.
 
110
 */
 
111
 
 
112
#include "mongo/pch.h"
 
113
 
 
114
#include "mongo/db/auth/action_type.h"
 
115
 
 
116
#include <iostream>
 
117
#include <string>
 
118
 
 
119
#include "mongo/base/status.h"
 
120
#include "mongo/platform/cstdint.h"
 
121
#include "mongo/util/mongoutils/str.h"
 
122
 
 
123
namespace mongo {
 
124
 
 
125
%(actionTypeConstants)s
 
126
    bool ActionType::operator==(const ActionType& rhs) const {
 
127
        return _identifier == rhs._identifier;
 
128
    }
 
129
 
 
130
    std::ostream& operator<<(std::ostream& os, const ActionType& at) {
 
131
        os << ActionType::actionToString(at);
 
132
        return os;
 
133
    }
 
134
 
 
135
    std::string ActionType::toString() const {
 
136
        return actionToString(*this);
 
137
    }
 
138
 
 
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: "
 
143
                                                << action,
 
144
                      0);
 
145
    }
 
146
 
 
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:
 
151
            return "";
 
152
        }
 
153
    }
 
154
 
 
155
} // namespace mongo
 
156
"""
 
157
 
 
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;
 
168
            return Status::OK();
 
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)
 
176
 
 
177
    pass
 
178
 
 
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)
 
188
 
 
189
def hasDuplicateActionTypes(actionTypes):
 
190
    sortedActionTypes = sorted(actionTypes)
 
191
 
 
192
    didFail = False
 
193
    prevActionType = sortedActionTypes[0]
 
194
    for actionType in sortedActionTypes[1:]:
 
195
        if actionType == prevActionType:
 
196
            print 'Duplicate actionType %s\n' % actionType
 
197
            didFail = True
 
198
        prevActionType = actionType
 
199
 
 
200
    return didFail
 
201
 
 
202
def parseActionTypesFromFile(actionTypesFilename):
 
203
    actionTypesFile = open(actionTypesFilename, 'r')
 
204
    actionTypes = eval(actionTypesFile.read())
 
205
    return actionTypes
 
206
 
 
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>"
 
210
        sys.exit(-1)
 
211
 
 
212
    actionTypes = parseActionTypesFromFile(sys.argv[1])
 
213
    if hasDuplicateActionTypes(actionTypes):
 
214
        sys.exit(-1)
 
215
 
 
216
    headerOutputFile = open(sys.argv[2], 'w')
 
217
    sourceOutputFile = open(sys.argv[3], 'w')
 
218
 
 
219
    writeHeaderFile(actionTypes, headerOutputFile)
 
220
    writeSourceFile(actionTypes, sourceOutputFile)