File: generate_client.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 (75 lines) | stat: -rw-r--r-- 2,583 bytes parent folder | download
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
import argparse
import logging
from pathlib import Path
from subprocess import run, PIPE
import tomli

_LOGGER = logging.getLogger(__name__)


def generate_autorest(folder: Path) -> None:

    readme_path = folder / "swagger" / "README.md"
    completed_process = run(f"autorest {readme_path} --python-sdks-folder=../../", cwd=folder, shell=True)

    if completed_process.returncode != 0:
        raise ValueError("Something happened with autorest: " + str(completed_process))
    _LOGGER.info("Autorest done")


def generate_typespec(folder: Path) -> None:
    tsp_location_path = folder / "tsp-location.yaml"

    if not tsp_location_path.exists():
        raise ValueError(
            "Didn't find a tsp_location.yaml in local directory. Please make sure a valid "
            "tsp-location.yaml file exists before running this command, for more information "
            "on how to create one, see: "
            "https://github.com/Azure/azure-sdk-tools/tree/main/tools/tsp-client/README.md"
        )

    completed_process = run("tsp-client update", cwd=folder, shell=True, stderr=PIPE)
    if completed_process.returncode != 0:
        if "'tsp-client' is not recognized" in completed_process.stderr.decode("utf-8"):
            raise ValueError(
                "tsp-client is not installed. Please run: npm install -g @azure-tools/typespec-client-generator-cli"
                )
        raise ValueError("Something happened with tsp-client update step: " + str(completed_process))

    _LOGGER.info("TypeSpec generate done")

def generate(folder: Path = Path(".")) -> None:
    if (folder / "swagger" / "README.md").exists():
        generate_autorest(folder)
    elif (folder / "tsp-location.yaml").exists():
        generate_typespec(folder)
    else:
        raise ValueError("Didn't find swagger/README.md nor tsp_location.yaml")

def generate_main() -> None:
    """Main method"""

    parser = argparse.ArgumentParser(
        description="Build SDK using Codegen.",
        formatter_class=argparse.RawTextHelpFormatter,
    )
    parser.add_argument(
        "-v",
        "--verbose",
        dest="verbose",
        action="store_true",
        help="Verbosity in INFO mode",
    )
    parser.add_argument("--debug", dest="debug", action="store_true", help="Verbosity in DEBUG mode")

    args = parser.parse_args()
    main_logger = logging.getLogger()
    if args.verbose or args.debug:
        logging.basicConfig()
        main_logger.setLevel(logging.DEBUG if args.debug else logging.INFO)

    generate()


if __name__ == "__main__":
    generate_main()