common.py 2.54 KB
Newer Older
yyy's avatar
yyy committed
1
"""common definitions."""
quyuan's avatar
quyuan committed
2
import os
yyy's avatar
yyy committed
3
import shutil
yyy's avatar
yyy committed
4
5
import re
import json
quyuan's avatar
quyuan committed
6
def check_shell(cmd):
yyy's avatar
yyy committed
7
    """shell successful."""
quyuan's avatar
quyuan committed
8
    res = os.system(cmd)
quyuan's avatar
quyuan committed
9
10
    assert res == 0

yyy's avatar
yyy committed
11
12
13
14
15
16
17
def update_config_file(file_path, key, value):
    """update config file."""
    with open(file_path, 'r', encoding="utf-8") as f:
        config  = json.loads(f.read())
    config[key] = value
    with open(file_path, 'w', encoding="utf-8") as f:
        f.write(json.dumps(config))
yyy's avatar
yyy committed
18
19
20
21
22
23
24
25
26
27
28

def cli_count_folders_and_check_contents(file_path):
    """" count cli files."""
    if os.path.exists(file_path):
        for files in os.listdir(file_path):
            folder_count = os.path.getsize(os.path.join(file_path, files))
            assert folder_count > 0
    assert len(os.listdir(file_path)) > 5

def sdk_count_folders_and_check_contents(file_path):
    """count folders."""
quyuan's avatar
add ci  
quyuan committed
29
    if os.path.exists(file_path):
yyy's avatar
yyy committed
30
31
32
33
        file_count = os.path.getsize(file_path)
        assert file_count > 0
    else:
        exit(1)
quyuan's avatar
quyuan committed
34
35


yyy's avatar
yyy committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def delete_file(path):
    """delete file."""
    if not os.path.exists(path):
        if os.path.isfile(path):
            try:
                os.remove(path)
                print(f"File '{path}' deleted.")
            except TypeError as e:
                print(f"Error deleting file '{path}': {e}")
    elif os.path.isdir(path):
        try:
            shutil.rmtree(path)
            print(f"Directory '{path}' and its contents deleted.")
        except TypeError as e:
yyy's avatar
yyy committed
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
            print(f"Error deleting directory '{path}': {e}")

def check_latex_table_exists(file_path):
    """check latex table exists."""
    pattern = r'\\begin\{tabular\}.*?\\end\{tabular\}'
    with open(file_path, 'r', encoding='utf-8') as file:
        content = file.read()
    matches = re.findall(pattern, content, re.DOTALL)
    return len(matches) > 0

def check_html_table_exists(file_path):
    """check html table exists."""
    pattern = r'<table.*?>.*?</table>'
    with open(file_path, 'r', encoding='utf-8') as file:
        content = file.read()
    matches = re.findall(pattern, content, re.DOTALL)
    return len(matches) > 0

def check_close_tables(file_path):
    """delete no tables."""
    latex_pattern = r'\\begin\{tabular\}.*?\\end\{tabular\}'
    html_pattern = r'<table.*?>.*?</table>'
    with open(file_path, 'r', encoding='utf-8') as file:
        content = file.read()
    latex_matches = re.findall(latex_pattern, content, re.DOTALL)
    html_matches = re.findall(html_pattern, content, re.DOTALL)
    if len(latex_matches) == 0 and len(html_matches) == 0:
        return True
    else:
        return False