1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3 4# Copyright (c) 2022 Huawei Device Co., Ltd. 5 6# Permission is hereby granted, free of charge, to any person obtaining a copy 7# of this software and associated documentation files (the "Software"), to deal 8# in the Software without restriction, including without limitation the rights 9# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10# copies of the Software, and to permit persons to whom the Software is 11# furnished to do so, subject to the following conditions: 12 13# The above copyright notice and this permission notice shall be included in 14# all copies or substantial portions of the Software. 15 16# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22# SOFTWARE. 23 24from ast import Delete 25import sys, os 26 27class Modify(): 28 def __init__(self, filePath) -> None: 29 self.filePath = filePath 30 self.lines = [] 31 32 fp = open(self.filePath, 'r') 33 for line in fp: 34 self.lines.append(line) 35 fp.close() 36 37 def saveAndClose(self): 38 s = ''.join(self.lines) 39 self.lines.clear() 40 fp = open(self.filePath, 'w') 41 fp.write(s) 42 fp.close() 43 44 def positionSearch(self, positionText, startLineNum = 0): 45 linesLen = len(self.lines) 46 for lineNum in range(startLineNum + 1, linesLen): 47 if(positionText in self.lines[lineNum]): 48 return lineNum 49 return -1 50 51 def addItem(self, newText, positionInedx, rowNume): 52 assert rowNume >= 0,"rowNum含义为“要将newText添加到positionSearch所定位的条目下的第几行”,可以等于0及添加到当前行,将当前行往下挤,但不可以为负数,如需要在positionSearch所定位的条目上方添加复数行,请按顺序调用addItem函数,且rowNum都填入0" 53 item = '' 54 if("is not set" in newText): 55 item = newText[2:-11] 56 else: 57 item = newText[:-2] 58 59 # 如果要添加的内容已经存在则直接返回 60 for line in self.lines: 61 if(item in line): 62 if ((item + "=y" in line) or (item + " is not set" in line)): 63 print("Already has this item: " + item) 64 return 65 66 assert positionInedx >= 0, "If want to add an item, first 'positionInedx' must be >= 0" 67 lineNum = positionInedx + rowNume 68 self.lines.insert(lineNum, newText+'\n') 69 if(rowNume <= 0): 70 positionInedx += 1 71 print("Add item: "+item) 72 73 def deleteLines(self, startLineIndex, endLineIndex): 74 for i in range(endLineIndex - startLineIndex): 75 self.lines.pop(startLineIndex) 76 77 78if __name__ == "__main__": 79 path = sys.argv[1] 80 modify = Modify(path) 81 print("Start to modifying file", sys.argv[1]) 82 83 test = [' compatible = "rockchip, rk3568-mali", "arm,mali-bifrost";', 84 ' reg = <0x0 0xfde60000 0x0 0x20000>;', 85 '', 86 ' interrupts = <GIC_SPI 40 IRQ_TYPE_LEVEL_HIGH>,', 87 ' <GIC_SPI 41 IRQ_TYPE_LEVEL_HIGH>,', 88 ' <GIC_SPI 39 IRQ_TYPE_LEVEL_HIGH>;', 89 ' interrupt-names = "job", "mmu", "gpu";', 90 ' clocks = <&scmi_clk 1>, <&cru CLK_GPU>;', 91 ' clock-names = "core", "bus";', 92 ' operating-points-v2 = <&gpu_opp_table>;', 93 '', 94 ' #cooling-cells = <2>;', 95 ' power-domains = <&power RK3568_PD_GPU>;', 96 ' status = "disabled";'] 97 98 if(modify.positionSearch("gpu_power_model: power-model",0) == -1): 99 print("rk3568.dtsi arealy modified") 100 exit() 101 102 positionInedx1 = modify.positionSearch("gpu: gpu@fde60000",0) 103 if(positionInedx1 >= 0): 104 positionInedx2 = modify.positionSearch("};",positionInedx1) 105 modify.deleteLines(positionInedx1 + 1, positionInedx2 + 1) 106 107 for i in range(len(test)): 108 modify.addItem(test[i], positionInedx1, i + 1) 109 110 modify.saveAndClose() 111 112 print("Succese") 113