tlog_analysis.py

SZTS_TEST 邹涛, 2023-06-21 16:54

Download (3.34 KB)

 
1
#!/usr/bin/env python3
2
import os
3
import sys
4

    
5
def tlog_tar():
6
    print(os.system("date"))
7
    print("---------Start Running----------------")
8
    LOG_PATH = os.getcwd()
9
    print("---------Decompression File Start----------------")
10
    temp = os.listdir(LOG_PATH)
11
    for file in temp:
12
        if file.endswith(".tar.gz"):
13
            filename = os.path.basename(file)
14
            fname = os.path.splitext(filename)[0]
15
            fname = os.path.splitext(fname)[0]
16
            os.makedirs(os.path.join(LOG_PATH, fname), exist_ok=True)
17
            os.system(f"tar -zxvf {file} -C {os.path.join(LOG_PATH, fname)}")
18
    LOG_PATHS = os.path.join(LOG_PATH, fname) + "/"
19
    print(f"LOG_PATHS={LOG_PATHS}")
20
    temps = os.listdir(LOG_PATHS)
21
    for file in temps:
22
        if file.endswith(".tar.gz"):
23
            filename = os.path.basename(file)
24
            fname = os.path.splitext(filename)[0]
25
            os.makedirs(os.path.join(LOG_PATHS, fname), exist_ok=True)
26
            os.system(f"tar -zxvf {file} -C {os.path.join(LOG_PATHS, fname)}")
27
    print("---------Decompression End----------------")
28

    
29
def tlog_merge():
30
    output_dir = "out"
31
    if os.path.exists(output_dir):
32
        os.system(f"rm -rf {output_dir}/*")
33
    else:
34
        os.makedirs(output_dir, exist_ok=True)
35

    
36
    files = []
37
    for root, dirs, filenames in os.walk("."):
38
        for filename in filenames:
39
            if "persist" in filename and "log" in filename:
40
                files.append(os.path.join(root, filename))
41
    files.sort()
42
    for file in files:
43
        if file.endswith(".ing"):
44
            boot_number = file[-42:-36]
45
        else:
46
            boot_number = file[-38:-32]
47
        output_file = f"{boot_number}_merged_logcat_file.log"
48
        if os.path.exists(output_file):
49
            print(output_file)
50
        else:
51
            open(output_file, "w").close()
52
        with open(file, "r") as f:
53
            with open(output_file, "a") as out:
54
                out.write(f.read())
55

    
56
    files = []
57
    for root, dirs, filenames in os.walk("."):
58
        for filename in filenames:
59
            if "kernel" in filename and "log" in filename:
60
                files.append(os.path.join(root, filename))
61
    files.sort()
62
    for file in files:
63
        if file.endswith(".ing"):
64
            boot_number = file[-41:-35]
65
        else:
66
            boot_number = file[-37:-31]
67
        output_file = f"{boot_number}_merged_kernel_file.log"
68
        if os.path.exists(output_file):
69
            print(output_file)
70
        else:
71
            open(output_file, "w").close()
72
        with open(file, "r") as f:
73
            with open(output_file, "a") as out:
74
                out.write(f.read())
75

    
76
    merged_files = [f for f in os.listdir() if f.endswith("_merged_logcat_file.log") or f.endswith("_merged_kernel_file.log")]
77
    for fullname in merged_files:
78
        if os.path.getsize(fullname) > 104857600:
79
            with open(fullname, "rb") as f:
80
                data = f.read()
81
                i = 1
82
                while data:
83
                    chunk = data[:100*1024*1024]
84
                    data = data[100*1024*1024:]
85
                    with open(f"{fullname}({i}).{fullname.split('.')[-1]}", "wb") as out:
86
                        out.write(chunk)
87
                    i += 1
88
    os.system(f"mv *.log {output_dir}/")
89
    print("End merge")
90

    
91
tlog_tar()
92
tlog_merge()
93
print("The merged files are located in the out directory")
94
sys.exit(0)