promptflow

Форк
0
/
extract_steps_from_readme.py 
60 строк · 2.1 Кб
1
import argparse
2
from pathlib import Path
3
from jinja2 import Environment, FileSystemLoader
4

5
from ghactions_driver.readme_parse import readme_parser
6
from ghactions_driver.readme_step import ReadmeStepsManage
7

8

9
def comment_pytest(full_text: str):
10
    # separate full_text into lines, group them by start with pytest or not
11
    full_text_lines = full_text.split("\n")
12
    full_text_pytest = []
13
    full_text_no_pytest = []
14
    for line in full_text_lines:
15
        if line.strip().startswith("python -m unittest"):
16
            full_text_pytest.append(line)
17
        else:
18
            full_text_no_pytest.append(line)
19
    return "\n".join(full_text_no_pytest), "\n".join(full_text_pytest)
20

21

22
def write_readme_shell(readme_path: str, output_folder: str):
23
    full_text = readme_parser(readme_path)
24
    full_text, full_text_pytest = comment_pytest(full_text)
25
    Path(ReadmeStepsManage.git_base_dir())
26
    bash_script_path = (
27
        Path(ReadmeStepsManage.git_base_dir()) / output_folder / "bash_script.sh"
28
    )
29
    bash_script_backup = (
30
        Path(ReadmeStepsManage.git_base_dir()) / output_folder / "bash_script_pytest.sh"
31
    )
32
    template_env = Environment(
33
        loader=FileSystemLoader(
34
            Path(ReadmeStepsManage.git_base_dir())
35
            / "scripts/readme/ghactions_driver/bash_script"
36
        )
37
    )
38
    bash_script_template = template_env.get_template("bash_script.sh.jinja2")
39
    with open(bash_script_path, "w") as f:
40
        f.write(bash_script_template.render({"command": full_text}))
41
    if full_text_pytest != "":
42
        with open(bash_script_backup, "w") as f:
43
            f.write(bash_script_template.render({"command": full_text_pytest}))
44

45

46
if __name__ == "__main__":
47
    # setup argparse
48
    parser = argparse.ArgumentParser()
49
    parser.add_argument(
50
        "-f",
51
        "--readme-file",
52
        help="Input README.md example 'examples/flows/standard/basic/README.md'",
53
    )
54
    parser.add_argument(
55
        "-o",
56
        "--output-folder",
57
        help="Output folder for bash_script.sh example 'examples/flows/standard/basic/'",
58
    )
59
    args = parser.parse_args()
60
    write_readme_shell(args.readme_file, args.output_folder)
61

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.