xref: /aosp_15_r20/external/pytorch/tools/code_coverage/package/util/utils_init.py (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1import argparse
2import os
3from typing import Any
4
5from .setting import (
6    JSON_FOLDER_BASE_DIR,
7    LOG_DIR,
8    MERGED_FOLDER_BASE_DIR,
9    Option,
10    PROFILE_DIR,
11    SUMMARY_FOLDER_DIR,
12)
13from .utils import create_folder, get_raw_profiles_folder, remove_file
14
15
16def remove_files() -> None:
17    # remove log
18    remove_file(os.path.join(LOG_DIR, "log.txt"))
19
20
21def create_folders() -> None:
22    create_folder(
23        PROFILE_DIR,
24        MERGED_FOLDER_BASE_DIR,
25        JSON_FOLDER_BASE_DIR,
26        get_raw_profiles_folder(),
27        SUMMARY_FOLDER_DIR,
28        LOG_DIR,
29    )
30
31
32def add_arguments_utils(parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
33    parser.add_argument("--run", help="run the cpp test binaries", action="store_true")
34    parser.add_argument(
35        "--merge",
36        help="merge raw profiles (only apply to clang coverage)",
37        action="store_true",
38    )
39    parser.add_argument(
40        "--export", help="generate json report for each file", action="store_true"
41    )
42    parser.add_argument(
43        "--summary",
44        help="read json report and generate file/line-oriented summary",
45        action="store_true",
46    )
47    parser.add_argument(
48        "--interest-only",
49        help="Final report will be only about these folders and its sub-folders; for example: caff2/c10;",
50        nargs="+",
51        default=None,
52    )
53    parser.add_argument(
54        "--clean",
55        help="delete all files generated by coverage tool",
56        action="store_true",
57        default=False,
58    )
59
60    return parser
61
62
63def have_option(have_stage: bool, option: int) -> int:
64    if have_stage:
65        return option
66    else:
67        return 0
68
69
70def get_options(args: Any) -> Option:
71    option: Option = Option()
72    if args.__contains__("build"):
73        if args.build:
74            option.need_build = True
75
76    if args.__contains__("run"):
77        if args.run:
78            option.need_run = True
79
80    if args.__contains__("merge"):
81        if args.merge:
82            option.need_merge = True
83
84    if args.__contains__("export"):
85        if args.export:
86            option.need_export = True
87
88    if args.__contains__("summary"):
89        if args.summary:
90            option.need_summary = True
91
92    # user does not have specified stage like run
93    if not any(vars(option).values()):
94        option.need_build = True
95        option.need_run = True
96        option.need_merge = True
97        option.need_export = True
98        option.need_summary = True
99        option.need_pytest = True
100
101    return option
102