common.py 2.74 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
7
8
9
10
11
12
13
14
import torch

def clear_gpu_memory():
    '''
    clear GPU memory
    '''
    torch.cuda.empty_cache()
    print("GPU memory cleared.")

quyuan's avatar
quyuan committed
15
def check_shell(cmd):
yyy's avatar
yyy committed
16
    """shell successful."""
quyuan's avatar
quyuan committed
17
    res = os.system(cmd)
quyuan's avatar
quyuan committed
18
19
    assert res == 0

yyy's avatar
yyy committed
20
21
def update_config_file(file_path, key, value):
    """update config file."""
quyuan's avatar
quyuan committed
22
23
    with open(file_path, 'r', encoding="utf-8") as fr:
        config  = json.loads(fr.read())
yyy's avatar
yyy committed
24
    config[key] = value
quyuan's avatar
quyuan committed
25
26
27
        # 保存修改后的内容
    with open(file_path, 'w', encoding='utf-8') as fw:
        json.dump(config, fw, ensure_ascii=False, indent=4)
yyy's avatar
yyy committed
28
29
30
31
32
33
34
35
36
37
38

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
39
    if os.path.exists(file_path):
yyy's avatar
yyy committed
40
41
42
43
        file_count = os.path.getsize(file_path)
        assert file_count > 0
    else:
        exit(1)
quyuan's avatar
quyuan committed
44
45


quyuan's avatar
quyuan committed
46

yyy's avatar
yyy committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
            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