File: run_apistubgen.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 (82 lines) | stat: -rw-r--r-- 2,946 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
76
77
78
79
80
81
82
#!/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.

from subprocess import check_call
import argparse
import os
import logging

from ci_tools.functions import find_whl
from ci_tools.parsing import ParsedSetup

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

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

def get_package_wheel_path(pkg_root, out_path):
    # parse setup.py to get package name and version
    pkg_details = ParsedSetup.from_path(pkg_root)

    # Check if wheel is already built and available for current package
    prebuilt_dir = os.getenv("PREBUILT_WHEEL_DIR")
    out_token_path = None
    if prebuilt_dir:
        pkg_path = os.path.join(prebuilt_dir, find_whl(prebuilt_dir, pkg_details.name, pkg_details.version))
        if not pkg_path:
            raise FileNotFoundError(
                "No prebuilt wheel found for package {} version {} in directory {}".format(
                    pkg_details.name, pkg_details.version, prebuilt_dir)
            )
        # If the package is a wheel and out_path is given, the token file output path should be the parent directory of the wheel
        if out_path:
            out_token_path = os.path.join(out_path, os.path.basename(os.path.dirname(pkg_path)))
        return pkg_path, out_token_path
    pkg_path = pkg_root
    # If the package is not a wheel and out_path is given, the token file output path should be the same as the target package path
    if out_path:
        out_token_path = os.path.join(out_path, os.path.basename(pkg_path))
    return  pkg_path, out_token_path

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="Run apistubgen against target folder. "
    )

    parser.add_argument(
        "-t",
        "--target",
        dest="target_package",
        help="The target package directory on disk.",
        required=True,
    )

    parser.add_argument(
        "-w",
        "--work-dir",
        dest="work_dir",
        help="Working directory to run apistubgen",
        required=True,
    )

    parser.add_argument(
        "-o",
        "--out-path",
        dest="out_path",
        help="Output directory to generate json token file"
    )

    args = parser.parse_args()

    # Check if a wheel is already built for current package and install from wheel when available
    # If wheel is not available then install package from source
    pkg_path, out_token_path = get_package_wheel_path(args.target_package, args.out_path)

    cmds = ["apistubgen", "--pkg-path", pkg_path]
    if out_token_path:
        cmds.extend(["--out-path", out_token_path])

    logging.info("Running apistubgen {}.".format(cmds))
    check_call(cmds, cwd=args.work_dir)