Linux dts list python tool

发布于:2024-03-20 ⋅ 阅读:(49) ⋅ 点赞:(0)

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

@ gv.py
out_file = ''
temp_file = ''
dt_dir_path = ''
nr_plats = 1
is_oem = False
is_ext = False
dts_name = ''

arg0_proc_name = ''
arg1_plat_name = ''

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

import sys,os
import shutil
import gv

def print_usage(toast):
    if toast != '':
        print('\n' + toast)
    print("\nUsage: python " + gv.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 " + gv.arg0_proc_name + " msm8909")
    print("    python " + gv.arg0_proc_name + " ./ msm8909")
    print("    python " + gv.arg0_proc_name + " -o msm8909-1gb-qrd-skuc.dts")
    print("    python " + gv.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 " + gv.arg0_proc_name + " msm8909")
    print("    python " + gv.arg0_proc_name + " E:\F\case\dts_test msm8909")
    print("    python " + gv.arg0_proc_name + " -o E:\F\case\dts_test\msm8909-1gb-qrd-skuc.dts")
    print("    python " + gv.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):
    for dts_file in os.listdir(dir_path):
        if contains_substring(dts_file, gv.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 = gv.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):
    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(gv.nr_plats) +
        ") platform*****************/\n")
        gv.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):
    h = open(gv.temp_file, 'r')
    for line in h:
        line = line.rstrip('\r').rstrip('\n')
        if line.endswith('.dts') == True:
            walk_dt(line, gv.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():
    create_file(gv.temp_file)

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

    remove_file(gv.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):
    if not os.path.exists(file_path):
        return

    is_found = False
    src_dir = gv.dt_dir_path
    dst_dir = gv.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():
    if len(sys.argv) == 2:
        gv.dt_dir_path = os.getcwd()
        gv.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'):
            gv.is_oem = True
            idx = sys.argv[2].rfind(os.path.sep)
            if idx > 0:
                idx += 1
                gv.dt_dir_path = sys.argv[2][0:idx]
                gv.dts_name = sys.argv[2][idx:]
            else:
                gv.dt_dir_path = os.getcwd()
                gv.dts_name = sys.argv[2]
        elif not cmp(sys.argv[1], '-e'):
            gv.is_ext = True
            idx = sys.argv[2].rfind(os.path.sep)
            if idx > 0:
                idx += 1
                gv.dt_dir_path = sys.argv[2][0:idx]
                gv.dts_name = sys.argv[2][idx:]
            else:
                gv.dt_dir_path = os.getcwd()
                gv.dts_name = sys.argv[2]
        else:
            gv.dt_dir_path = sys.argv[1]
            gv.arg1_plat_name = sys.argv[2]

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

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

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

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

    parse_args()

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

if __name__ == '__main__':
    main()


网站公告

今日签到

点亮在社区的每一天
去签到