File: patch_models_v2.py

package info (click to toggle)
azure-cli 2.82.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,359,416 kB
  • sloc: python: 1,910,381; sh: 1,343; makefile: 406; cs: 145; javascript: 74; sql: 37; xml: 21
file content (282 lines) | stat: -rw-r--r-- 10,725 bytes parent folder | download | duplicates (2)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from enum import Enum
import importlib
import inspect
import logging
from pathlib import Path
import pkgutil
import shutil
import sys
import tempfile

from msrest.serialization import Model
from msrest.exceptions import HttpOperationError
from msrest.paging import Paged

_LOGGER = logging.getLogger(__name__)


copyright_header = b"""# coding=utf-8
# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
#
# Code generated by Microsoft (R) AutoRest Code Generator.
# Changes may cause incorrect behavior and will be lost if the code is
# regenerated.
# --------------------------------------------------------------------------

"""

header = copyright_header+b"""from msrest.serialization import Model
from msrest.exceptions import HttpOperationError
"""

paging_header = copyright_header+b"""from msrest.paging import Paged
"""

init_file = """
try:
    from .{} import *
except (SyntaxError, ImportError):
    from .{} import *
from .{} import *
"""

MODEL_PY2_NAME = "_models"
MODEL_PY3_NAME = "_models_py3"
PAGED_NAME = "_paged_models"

def as_file_name(name):
    return name + ".py"

def parse_input(input_parameter):
    """From a syntax like package_name#submodule, build a package name
    and complete module name.
    """
    split_package_name = input_parameter.split('#')
    package_name = split_package_name[0]
    module_name = package_name.replace("-", ".")
    if len(split_package_name) >= 2:
        module_name = ".".join([module_name, split_package_name[1]])
    return package_name, module_name

def solve_mro(models):
    for models_module in models:
        models_path = models_module.__path__[0]
        _LOGGER.info(f"Working on {models_path}")
        if Path(models_path, as_file_name(MODEL_PY3_NAME)).exists():
            _LOGGER.info("Skipping since already patched")
            return

        # Build the new files in a temp folder
        with tempfile.TemporaryDirectory() as temp_folder:
            final_models_path = Path(temp_folder, "models")
            final_models_path.mkdir()
            solve_one_model(models_module, final_models_path)

            # Swith the files
            shutil.rmtree(models_path)
            shutil.move(final_models_path, models_path)

def solve_one_model(models_module, output_folder):
    """Will build the compacted models in the output_folder"""

    models_classes = [
        (len(model_class.__mro__), model_name, inspect.getfile(model_class), model_class) for model_name, model_class in vars(models_module).items()
        if model_name[0].isupper() and Model in model_class.__mro__
    ]
    # Sort on MRO size first, and then alphabetically
    models_classes.sort(key=lambda x: (x[0], x[1]))

    # Just need the name of exceptions
    exceptions_classes = [
        model_name for model_name, model_class in vars(models_module).items()
        if model_name[0].isupper() and HttpOperationError in model_class.__mro__
    ]

    py2_models_classes = [
        (len_mro, model_name, path.replace("_py3.py", ".py"), None)
        for len_mro, model_name, path, _ in models_classes
    ]

    paged_models_classes = [
        (model_name, inspect.getfile(model_class), model_class) for model_name, model_class in vars(models_module).items()
        if model_name[0].isupper() and Paged in model_class.__mro__
    ]

    enum_models_classes = [
        (model_name, inspect.getfile(model_class), model_class) for model_name, model_class in vars(models_module).items()
        if model_name[0].isupper() and Enum in model_class.__mro__
    ]
    if enum_models_classes:
        # Can't be more than one enum file
        enum_file = Path(enum_models_classes[0][1])
        enum_file_module_name = "_"+enum_file.with_suffix('').name
        shutil.copyfile(enum_file, Path(output_folder, as_file_name(enum_file_module_name)))
    else:
        enum_file_module_name = None

    write_model_file(Path(output_folder, as_file_name(MODEL_PY3_NAME)), models_classes)
    write_model_file(Path(output_folder, as_file_name(MODEL_PY2_NAME)), py2_models_classes)
    write_paging_file(Path(output_folder, as_file_name(PAGED_NAME)), paged_models_classes)
    write_complete_init(
        Path(output_folder, "__init__.py"),
        models_classes,
        exceptions_classes,
        paged_models_classes,
        enum_models_classes,
        enum_file_module_name
    )

def write_model_file(output_file_path, classes_to_write):
    with open(output_file_path, "bw") as write_fd:
        write_fd.write(header)

        for model in classes_to_write:
            _, _, model_file_path, _ = model

            with open(model_file_path, "rb") as read_fd:
                lines = read_fd.readlines()
                # Skip until it's "class XXXX"
                while lines:
                    if lines[0].startswith(b"class "):
                        break
                    lines.pop(0)
                else:
                    raise ValueError("Never found any class definition!")
                # Now I keep everything
                write_fd.write(b'\n')
                write_fd.write(b'\n')
                write_fd.writelines(lines)

def write_paging_file(output_file_path, classes_to_write):
    with open(output_file_path, "bw") as write_fd:
        write_fd.write(paging_header)

        for model in classes_to_write:
            _, model_file_path, _ = model

            with open(model_file_path, "rb") as read_fd:
                # Skip the first 15 lines (based on Autorest deterministic behavior)
                # If we want this less random, look for the first line starts with "class"
                lines = read_fd.readlines()[14:]
                write_fd.write(b'\n')
                write_fd.write(b'\n')
                write_fd.writelines(lines)

def write_init(output_file_path, model_file_name, model_file_name_py2, paging_file_name, enum_file_name):
    with open(output_file_path, "bw") as write_fd:
        write_fd.write(copyright_header)

        write_fd.write(init_file.format(
            model_file_name,
            model_file_name_py2,
            paging_file_name,
        ).encode('utf8'))
        if enum_file_name:
            write_fd.write(
                "from .{} import *".format(enum_file_name).encode('utf8')
            )

def write_complete_init(output_file_path, models, exceptions_classes, paging_models, enum_models, enum_file_module_name):
    with open(output_file_path, "bw") as write_fd:
        write_fd.write(copyright_header)

        write_fd.write(b"\ntry:\n")
        # Write py3 import
        for _, model_name, _, _ in models:
            write_fd.write(f"    from .{MODEL_PY3_NAME} import {model_name}\n".encode("utf-8"))
        # Py 3 exceptions
        for model_name in exceptions_classes:
            write_fd.write(f"    from .{MODEL_PY3_NAME} import {model_name}\n".encode("utf-8"))

        write_fd.write(b"except (SyntaxError, ImportError):\n")

        # Write py2 import
        for _, model_name, _, _ in models:
            write_fd.write(f"    from .{MODEL_PY2_NAME} import {model_name}\n".encode("utf-8"))
        # Py 2 exceptions
        for model_name in exceptions_classes:
            write_fd.write(f"    from .{MODEL_PY2_NAME} import {model_name}\n".encode("utf-8"))

        # Write paged import
        for model_name, _, _ in paging_models:
            write_fd.write(f"from .{PAGED_NAME} import {model_name}\n".encode("utf-8"))

        if enum_models:
            # Write enum model import
            for model_name, _, _ in enum_models:
                write_fd.write(f"from .{enum_file_module_name} import {model_name}\n".encode("utf-8"))

        write_fd.write(b"\n\n__all__=[\n")
        # Write all classes name
        for _, model_name, _, _ in models:
            write_fd.write(f"    '{model_name}',\n".encode("utf-8"))
        for model_name in exceptions_classes:
            write_fd.write(f"    '{model_name}',\n".encode("utf-8"))
        for model_name, _, _ in paging_models:
            write_fd.write(f"    '{model_name}',\n".encode("utf-8"))
        if enum_models:
            for model_name, _, _ in enum_models:
                write_fd.write(f"    '{model_name}',\n".encode("utf-8"))

        write_fd.write(b"]\n")


def find_models_to_change(module_name):
    """Will figure out if the package is a multi-api one,
    and understand what to generate.
    """
    main_module = importlib.import_module(module_name)
    try:
        models_module = main_module.models
        models_module.__path__
        # It didn't fail, that's a single API package
        return [models_module]
    except AttributeError:
        # This means I loaded the fake module "models"
        # and it's multi-api, load all models
        return [
            importlib.import_module('.'+label+'.models', main_module.__name__)
            for (_, label, ispkg) in pkgutil.iter_modules(main_module.__path__)
            if ispkg and label != 'aio'
        ]


def find_autorest_generated_folder(module_prefix="azure.mgmt"):
    """Find all Autorest generated code in that module prefix.

    This actually looks for a "models" package only. We could be smarter if necessary.
    """
    _LOGGER.info(f"Looking for Autorest generated package in {module_prefix}")
    result = []
    try:
        _LOGGER.debug(f"Try {module_prefix}")
        importlib.import_module(".models", module_prefix)
        # If not exception, we found it
        _LOGGER.info(f"Found {module_prefix}")
        result.append(module_prefix)
    except ModuleNotFoundError:
        # No model, might dig deeper
        prefix_module = importlib.import_module(module_prefix)
        for _, sub_package, ispkg in pkgutil.iter_modules(prefix_module.__path__, module_prefix+"."):
            if ispkg:
                result += find_autorest_generated_folder(sub_package)
    return result


def main(prefix="azure.mgmt"):
    packages = find_autorest_generated_folder(prefix)
    for autorest_package in packages:
        models_module = find_models_to_change(autorest_package)
        solve_mro(models_module)

if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO)
    main(sys.argv[1] if len(sys.argv) >= 2 else "azure.mgmt")