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()
Linux dts list python tool
于 2024-03-20 15:30:10 首次发布