Linux dts list python tool

这篇文章介绍了如何使用Python脚本在WindowsNotepad中操作DT文件,包括列出、替换和复制DT文件,以及在指定目录下递归遍历DT树结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Windows Notepad - Save as - Encoding (ANSI)
Windows Notepad - Edit - Replace... - replaces ? with blank

@ dts_list.py
################################################
# Show dts tree for msm platform
# 2015-2016, all rights reserved.
################################################

import shutil
import sys,os

arg0_proc_name = ''
arg1_plat_name = ''
dt_dir_path = ''
dts_name = ''
is_oem = False
is_ext = False
nr_plats = 1
out_file = ''
temp_file = ''

def print_usage(toast):
    global arg0_proc_name

    if toast != '':
        print('\n' + toast)
    print("\nUsage: python " + arg0_proc_name + " [OPTION...]\n")
    print("    If you want to print all the DT of one platform, you should")
    print("    assign the directory as the first argument, otherwise it ")
    print("    will use the current work directory.")
    print("    -o    list all the .dtsi files which belong to a .dts to")
    print("        a file")
    print("    -e    list all the .dtsi files which belong to a .dts to")
    print("        a file and copy the assciated .dtsi files to")
    print("        ." + os.path.sep + 'EXT\n')
    print("For example:")
    print("    Linux:")
    print("    python " + arg0_proc_name + " msm8909")
    print("    python " + arg0_proc_name + " ./ msm8909")
    print("    python " + arg0_proc_name + " -o msm8909-1gb-qrd-skuc.dts")
    print("    python " + arg0_proc_name + " -e msm8909-1gb-qrd-skuc.dts\n")
    print("    vi KERNEL-msm8909.list")
    print("    vi OEM-msm8909-1gb-qrd-skuc.dts.list")
    print("    vi ./EXT/OEM-msm8909-1gb-qrd-skuc.dts.list\n")

    print("    <------------------------------------------------>\n")
    print("    Windows:")
    print("    python " + arg0_proc_name + " msm8909")
    print("    python " + arg0_proc_name + " E:\F\case\dts_test msm8909")
    print("    python " + arg0_proc_name + " -o E:\F\case\dts_test\msm8909-1gb-qrd-skuc.dts")
    print("    python " + arg0_proc_name + " -e E:\F\case\dts_test\msm8909-1gb-qrd-skuc.dts\n")
    print("    Open E:\F\case\dts_test\KERNEL-msm8909.list with text editor")
    print("    Open E:\F\case\dts_test\OEM-msm8909-1gb-qrd-skuc.dts.list ")
    print("        with text editor")
    print("    Open E:\F\case\dts_test\EXT\OEM-msm8909-1gb-qrd-skuc.dts.list ")
    print("        with text editor\n")

def write_file(f_name, line):
    h = open(f_name, 'a+')
    h.writelines(line)
    h.close()

def contains_substring(src, sub):
    if sub in src:
        return True
    elif src.startswith(sub):
        return True
    else:
        return False

def get_dt_list(dir_path, temp_file):
    global arg1_plat_name

    for dts_file in os.listdir(dir_path):
        if contains_substring(dts_file, arg1_plat_name) > 0:
            write_file(temp_file, dir_path + os.path.sep + dts_file + '\n')

def list_one_tree(dt_file_path, h, out_file):
    fp = open(dt_file_path, 'r')
    for line in fp:
        p = line.lstrip(' ')
        if p.startswith('/*') or p.startswith('//'):
            continue

        if line.find('include') > 0 and line.find(".dts") > 0:
            if line.find("/include/") > 0:
                idx = line.find('/include/')
                idx += 9
                p = line[idx:len(line)]
            elif line.find("#include") > 0:
                idx = line.find('#include')
                idx += 8
                p = line[idx:len(line)]
            else:
                idx = line.find('include')
                idx += 7
                p = line[idx:len(line)]

            p = p.lstrip('/').lstrip(' ').lstrip('\"').rstrip('\"')
            p = p.rstrip('\r').rstrip('\n').rstrip('\"')
            write_file(out_file, h + p + '\n')

            # Get the next
            p = p.rstrip('\r').rstrip('\n').lstrip('\"').rstrip('\"')
            p = dt_dir_path + os.path.sep + p
            if os.path.exists(p):
                list_one_tree(p, h + '-', out_file)
    fp.close()

def walk_dt(dt_file_path, out_file, is_oem):
    global nr_plats

    p = dt_file_path

    idx = dt_file_path.find(os.path.sep)
    if idx > 0:
        idx += 1
        p = dt_file_path[idx:len(dt_file_path)]

    if p != '':
        write_file(out_file,
                "/**************(" + str(nr_plats) +
        ") platform*****************/\n")
        nr_plats += 1

        p = p.rstrip('\r').rstrip('\n')
        idx = p.rfind(os.path.sep)
        if idx > 0:
            idx += 1
            _dts = p[idx:]
        else:
            _dts = p
        write_file(out_file, _dts + '\n')

        dt_file_path = dt_file_path.rstrip('\r').rstrip('\n')
        list_one_tree(dt_file_path, "-", out_file)
        write_file(out_file, "\n")

def _show_dt(dir_path):
    global out_file
    global temp_file

    h = open(temp_file, 'r')
    for line in h:
        line = line.rstrip('\r').rstrip('\n')
        if line.endswith('.dts') == True:
            walk_dt(line, out_file, False)
    h.close();

def create_file(file):
    if os.path.exists(file):
        os.remove(file)

    h = open(file, 'w')
    h.write('Author: George Tso\n')
    h.write('If you have any good advice, you can contact me directly\n\n')
    h.close()

def remove_file(file):
    if os.path.exists(file):
        os.remove(file)

def show_dt():
    global dt_dir_path
    global temp_file

    create_file(temp_file)

    if len(dt_dir_path) > 1:
        get_dt_list(dt_dir_path, temp_file)
        _show_dt(dt_dir_path)

    remove_file(temp_file)

def remove_dir(dir_path):
    if os.path.isdir(dir_path):
        for file in os.listdir(dir_path):
            if (os.path.isdir(dir_path + os.path.sep + file)):
                remove_dir(dir_path + os.path.sep + file)
            else:
                os.remove(dir_path + os.path.sep + file)
        os.removedirs(dir_path)

def extract_one_DT_to_EXT(file_path):
    global dt_dir_path

    if not os.path.exists(file_path):
        return

    is_found = False
    src_dir = dt_dir_path
    dst_dir = dt_dir_path

    h = open(file_path, 'r')
    for line in h:
        if line.find('*') > 0:
            is_found = True
            continue
        if (is_found == False):
            continue
        line = line.lstrip('-').lstrip('/').lstrip('\\')
        line = line.rstrip('\r').rstrip('\n').rstrip('\"')
        src_path = src_dir + os.path.sep + line
        dst_path = dst_dir + os.path.sep + 'EXT' + os.path.sep + line
        if os.path.exists(src_path) and not os.path.isdir(src_path):
            idx = dst_path.rfind(os.path.sep)
            tmp_dst_dir = dst_path[0:idx]
            if (os.path.exists(tmp_dst_dir) == False):
                os.makedirs(tmp_dst_dir)
            shutil.copy(src_path, dst_path)

def parse_args():
    global arg0_proc_name
    global arg1_plat_name
    global dt_dir_path
    global dts_name
    global is_ext
    global is_oem
    global nr_plats
    global out_file
    global temp_file

    if len(sys.argv) == 2:
        dt_dir_path = os.getcwd()
        arg1_plat_name = sys.argv[1]
    elif len(sys.argv) == 3:
        if not os.path.isdir(sys.argv[1]) and cmp(sys.argv[1], '-o') and cmp(sys.argv[1], '-e'):
            print_usage()
            sys.exit(0)
        elif not cmp(sys.argv[1], '-o'):
            is_oem = True
            idx = sys.argv[2].rfind(os.path.sep)
            if idx > 0:
                idx += 1
                dt_dir_path = sys.argv[2][0:idx]
                dts_name = sys.argv[2][idx:]
            else:
                dt_dir_path = os.getcwd()
                dts_name = sys.argv[2]
        elif not cmp(sys.argv[1], '-e'):
            is_ext = True
            idx = sys.argv[2].rfind(os.path.sep)
            if idx > 0:
                idx += 1
                dt_dir_path = sys.argv[2][0:idx]
                dts_name = sys.argv[2][idx:]
            else:
                dt_dir_path = os.getcwd()
                dts_name = sys.argv[2]
        else:
            dt_dir_path = sys.argv[1]
            arg1_plat_name = sys.argv[2]

    dt_dir_path = dt_dir_path.rstrip('\r').rstrip('\n')
    temp_file = dt_dir_path + os.path.sep + '.tmp.log'
    if is_oem == True:
        out_file = dt_dir_path + os.path.sep + 'OEM-' + dts_name + '.list'
    elif is_ext == True:
        remove_dir(dt_dir_path + os.path.sep + 'EXT')
        os.makedirs(dt_dir_path + os.path.sep + 'EXT')
        out_file = dt_dir_path + os.path.sep + 'EXT' + os.path.sep + 'OEM-' + dts_name + '.list'
    else:
        out_file = dt_dir_path + os.path.sep + 'KERNEL-' + arg1_plat_name + '.list'

    print('\nDT directory: ' + dt_dir_path)
    if is_ext:
        print('\nEXT directory: ' + dt_dir_path + os.path.sep + 'EXT' + os.path.sep)
    print('\nout file: ' + out_file + '\n')
    create_file(out_file)

def main():
    global arg0_proc_name
    global dt_dir_path
    global dts_name
    global is_ext
    global is_oem
    global out_file

    arg0_proc_name = sys.argv[0]
    if sys.argv[0].rfind(os.path.sep) > 0 :
        index = sys.argv[0].rfind(os.path.sep)
        arg0_proc_name = sys.argv[0][index+1:]

    if len(sys.argv) < 2:
        print_usage('')
        sys.exit(0)

    parse_args()

    if is_ext == True:
        walk_dt(dt_dir_path + os.path.sep + dts_name,
        out_file, True)
        extract_one_DT_to_EXT(out_file)
    elif is_oem == True:
            walk_dt(dt_dir_path + os.path.sep + dts_name,
        out_file, True)
    else:
        if arg1_plat_name.find('.dts') > 0 or arg1_plat_name.find('dtsi') > 0:
            print_usage('Invalid arguments')
            sys.exit(0)
        show_dt()

if __name__ == '__main__':
    main()

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值