forked from openshift/openshift-restclient-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocess_spec.py.stub
More file actions
171 lines (138 loc) · 6.17 KB
/
preprocess_spec.py.stub
File metadata and controls
171 lines (138 loc) · 6.17 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import re
import string_utils
VERSION_RX = re.compile(".*V\d((alpha|beta)\d)?")
DEFAULT_CODEGEN_IGNORE_LINES = {
".gitignore",
"git_push.sh",
"requirements.txt",
"test-requirements.txt",
"setup.py",
".travis.yml",
"tox.ini",
"client/api_client.py",
"client/configuration.py",
"client/rest.py",
"config/kube_config.py",
}
conflicted_modules = set()
def snake_case_model(model):
return '_'.join([string_utils.camel_case_to_snake(x) for x in model.split('.')])
def add_missing_openshift_tags_to_operation(op, _, path=None, **kwargs):
tags = op.get('tags', [])
if not tags and path:
path_parts = path.split('/')
if '.openshift.io' in path:
api_group = path_parts[2].replace('.', '_')
version = path_parts[3] if VERSION_RX.match(path_parts[3]) else None
elif 'oapi' in path:
api_group = path_parts[1]
version = None
else:
raise Exception('Do not know how to process missing tag for path: {}'.format(path))
tag = api_group
if version is not None:
tag += '_' + version
op['tags'] = [tag]
def update_codegen_ignore(spec, output_path):
import io
import os
import kubernetes
codegen_ignore_path = os.path.join(os.path.dirname(output_path), '.swagger-codegen-ignore')
ignore_lines = set()
k8s_apis = dir(kubernetes.client.apis)
k8s_models = dir(kubernetes.client.models)
api_modules = set()
model_modules = set()
for path in list(spec['paths'].values()):
for op_name in list(path.keys()):
if op_name != 'parameters':
op = path[op_name]
for tag in op.get('tags', []):
tag = '_'.join([string_utils.camel_case_to_snake(x) for x in tag.split('_')])
api_modules.add(tag + '_api')
for model in list(spec['definitions'].keys()):
model_modules.add(snake_case_model(model))
for api_module in api_modules:
suffix = api_module.split('_')[-1]
if suffix in ['0', 'pre012'] or api_module in k8s_apis:
ignore_lines.add('client/apis/{}.py'.format(api_module))
print("Skipping generation of client/apis/{}.py".format(api_module))
else:
print("Not skipping generation of client/apis/{}.py".format(api_module))
for model_module in model_modules:
if model_module in k8s_models and model_module not in conflicted_modules:
ignore_lines.add('client/models/{}.py'.format(model_module))
print("Skipping generation of client/models/{}.py".format(model_module))
else:
print("Not skipping generation of client/models/{}.py".format(model_module))
for module in list(ignore_lines):
module_name = module.split('/')[-1].split('.')[0]
test_module_name = 'test_{}'.format(module_name)
docs_module_name = "".join(map(lambda x: x.capitalize(), module_name.split('_')))
ignore_lines.add("test/{}.py".format(test_module_name))
ignore_lines.add('docs/{}.md'.format(docs_module_name))
print("Skipping generation of test/{}.py".format(test_module_name))
print("Skipping generation of docs/{}.md".format(docs_module_name))
ignore_lines = ignore_lines.union(DEFAULT_CODEGEN_IGNORE_LINES)
with open(codegen_ignore_path, 'w') as f:
f.write('\n'.join(sorted(ignore_lines)))
def process_openshift_swagger(spec, output_path):
remove_model_prefixes(spec, 'com.github.openshift')
apply_func_to_openshift_spec_operations(spec, add_missing_openshift_tags_to_operation)
# TODO: Kubernetes does not set a version for OpenAPI spec yet,
# remove this when that is fixed.
# spec['info']['version'] = SPEC_VERSION
return spec
def rename_model(spec, old_name, new_name):
find_rename_ref_recursive(spec,
"#/definitions/" + old_name,
"#/definitions/" + new_name)
if new_name not in spec['definitions']:
spec['definitions'][new_name] = spec['definitions'][old_name]
else:
# If we have a conflict here we should take the openshift version,
# which is preprocessed first
conflicted_modules.add(snake_case_model(new_name))
print("Deleting {old} instead of renaming to {new}, {new} already exists".format(old=old_name, new=new_name))
del spec['definitions'][old_name]
def apply_func_to_openshift_spec_operations(spec, func, **kwargs):
"""Apply func to each operation in the spec.
:param spec: The OpenAPI spec to apply func to.
:param func: the function to apply to the spec's operations. It should be
a func(operation, parent) where operation will be each
operation of the spec and parent would be the parent object of
the given operation.
If the return value of the func is True, then the operation
will be deleted from the spec.
"""
for k, v in spec['paths'].items():
for op in _ops:
if op not in v:
continue
if func(v[op], v, path=k, **kwargs):
del v[op]
def openshift_main():
import sys
import json
import codecs
import urllib3
from collections import OrderedDict
pool = urllib3.PoolManager()
reader = codecs.getreader('utf-8')
spec_url = 'https://raw.githubusercontent.com/openshift/origin/' \
'%s/api/swagger-spec/openshift-openapi-spec.json' % sys.argv[2]
output_path = sys.argv[3]
print("writing to {}".format(output_path))
with pool.request('GET', spec_url, preload_content=False) as response:
if response.status != 200:
print("Error downloading spec file. Reason: %s" % response.reason)
return 1
in_spec = json.load(reader(response), object_pairs_hook=OrderedDict)
out_spec = process_swagger(process_openshift_swagger(in_spec, output_path), sys.argv[1])
update_codegen_ignore(out_spec, output_path)
with open(output_path, 'w') as out:
json.dump(out_spec, out, sort_keys=True, indent=2,
separators=(',', ': '), ensure_ascii=True)
return 0
if __name__ == '__main__':
sys.exit(openshift_main())