File: verify_autorest.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (107 lines) | stat: -rw-r--r-- 3,262 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
#!/usr/bin/env python

# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

import argparse
import os
import logging

from common_tasks import run_check_call

logging.getLogger().setLevel(logging.INFO)

root_dir = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "..", ".."))
sdk_dir = os.path.join(root_dir, "sdk")

SWAGGER_FOLDER = "swagger"


def run_autorest(service_dir):
    logging.info("Running autorest for {}".format(service_dir))

    service_dir = os.path.join(sdk_dir, service_dir)

    swagger_folders = find_swagger_folders(service_dir)

    for working_dir in swagger_folders:
        os.chdir(working_dir)
        f = os.path.abspath(os.path.join(working_dir, "README.md"))
        if os.path.exists(f):
            reset_command = ["autorest", "--reset"]
            run_check_call(reset_command, root_dir)

            command = ["autorest", "--python", f, "--verbose"]
            logging.info("Command: {}\nLocation: {}\n".format(command, working_dir))
            run_check_call(command, working_dir)
    return swagger_folders


def find_swagger_folders(directory):
    logging.info("Searching for swagger files in: {}".format(directory))

    ret = []
    for root, subdirs, files in os.walk(directory):
        for d in subdirs:
            if d == SWAGGER_FOLDER:
                if os.path.exists(os.path.join(root, d, "README.md")):
                    ret.append(os.path.join(root, d))

    logging.info("Found swagger files at: {}".format(ret))
    return ret


def check_diff(folder):
    # We don't care about changes to txt files (dev_requirements change)
    run_check_call(["git", "status"], sdk_dir, always_exit=False)

    command = [
        "git",
        "checkout",
        "--",
        "**/*.txt",
    ]
    result = run_check_call(command, sdk_dir, always_exit=False)

    # Remove the whl dirs
    command = [
        "rm",
        "-r",
        "**/.tmp_whl_dir/"
    ]
    result = run_check_call(command, sdk_dir, always_exit=False)

    # Next we need to move the autorest and _tox_logs directories and then replace them

    dir_changed = folder.split("/")[:-2]
    command = [
        "git",
        "diff",
        "--exit-code",
        "{}".format("/".join(dir_changed)),
    ]
    result = run_check_call(command, sdk_dir, always_exit=False)
    if result:
        command = ["git", "status"]
        run_check_call(command, root_dir)
        raise ValueError(
            "Found difference between re-generated code and current commit. Please re-generate with the latest autorest."
        )


if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="Run autorest to verify generated code."
    )
    parser.add_argument(
        "--service_directory", help="Directory of the package being tested"
    )

    args = parser.parse_args()
    folders = run_autorest(args.service_directory)

    if len(folders):
        for folder in folders:
            check_diff(folder)