TIOVX User Guide
code_modify.py
1 #
2 # Copyright (c) 2017 Texas Instruments Incorporated
3 #
4 # All rights reserved not granted herein.
5 #
6 # Limited License.
7 #
8 # Texas Instruments Incorporated grants a world-wide, royalty-free, non-exclusive
9 # license under copyrights and patents it now or hereafter owns or controls to make,
10 # have made, use, import, offer to sell and sell ("Utilize") this software subject to the
11 # terms herein. With respect to the foregoing patent license, such license is granted
12 # solely to the extent that any such patent is necessary to Utilize the software alone.
13 # The patent license shall not apply to any combinations which include this software,
14 # other than combinations with devices manufactured by or for TI ("TI Devices").
15 # No hardware patent is licensed hereunder.
16 #
17 # Redistributions must preserve existing copyright notices and reproduce this license
18 # (including the above copyright notice and the disclaimer and (if applicable) source
19 # code license limitations below) in the documentation and/or other materials provided
20 # with the distribution
21 #
22 # Redistribution and use in binary form, without modification, are permitted provided
23 # that the following conditions are met:
24 #
25 # No reverse engineering, decompilation, or disassembly of this software is
26 # permitted with respect to any software provided in binary form.
27 #
28 # any redistribution and use are licensed by TI for use only with TI Devices.
29 #
30 # Nothing shall obligate TI to provide you with source code for the software
31 # licensed and provided to you in object code.
32 #
33 # If software source code is provided to you, modification and redistribution of the
34 # source code are permitted provided that the following conditions are met:
35 #
36 # any redistribution and use of the source code, including any resulting derivative
37 # works, are licensed by TI for use only with TI Devices.
38 #
39 # any redistribution and use of any object code compiled from the source code
40 # and any resulting derivative works, are licensed by TI for use only with TI Devices.
41 #
42 # Neither the name of Texas Instruments Incorporated nor the names of its suppliers
43 #
44 # may be used to endorse or promote products derived from this software without
45 # specific prior written permission.
46 #
47 # DISCLAIMER.
48 #
49 # THIS SOFTWARE IS PROVIDED BY TI AND TI'S LICENSORS "AS IS" AND ANY EXPRESS
50 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
52 # IN NO EVENT SHALL TI AND TI'S LICENSORS BE LIABLE FOR ANY DIRECT, INDIRECT,
53 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
54 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
55 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
56 # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
57 # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
58 # OF THE POSSIBILITY OF SUCH DAMAGE.
59 #
60 #
61 import os, re
62 
63 from . import *
64 
65 class CodeModify :
66 
67  def file_append(self, filename, insert) :
68  self.file = open(filename,'a')
69  self.file.write(insert)
70  self.file.close()
71 
72  def file_search(self, in_filename, search) :
73  if os.path.exists(in_filename):
74  self.status = False
75  with open(in_filename) as rd_file:
76  for line in rd_file:
77  if search in line :
78  return True
79  return False
80 
81  def block_search(self, in_filename, start, end, search) :
82  if os.path.exists(in_filename):
83  self.status = False
84  with open(in_filename) as rd_file:
85  for line in rd_file:
86  if start in line :
87  self.multiline = line
88  self.status = True
89  elif self.status :
90  self.multiline = self.multiline + line
91  if end in line :
92  self.status = False
93  searchObj = re.findall(search, self.multiline)
94  return (searchObj[-1])
95 
96  def block_insert(self, in_filename, start, end, find, search, searchNoEsc, insert, overrideFind=False) :
97  if os.path.exists(in_filename):
98  self.status = False
99  self.found = False
100  self.include_customer_kernels_code = CodeGenerate(in_filename + ".tmp", header=False)
101  self.multiline = ""
102  with open(in_filename) as rd_file:
103  for line in rd_file:
104  if self.status==False and start in line :
105  self.include_customer_kernels_code.write_line(self.multiline, new_line=False)
106  self.multiline = line
107  self.status = True
108  self.found = True
109  elif self.status :
110  self.multiline = self.multiline + line
111  if end in line:
112  self.status = False
113  if not find in self.multiline or overrideFind:
114  self.multiline = re.sub(search,insert + searchNoEsc, self.multiline)
115  self.include_customer_kernels_code.write_line(self.multiline, new_line=False)
116  else :
117  self.include_customer_kernels_code.write_line(line, new_line=False)
118  if self.status==True and self.found==True:
119  if not find in self.multiline :
120  self.multiline = re.sub(search,insert + searchNoEsc, self.multiline)
121  self.include_customer_kernels_code.write_line(self.multiline, new_line=False)
122 
123  self.include_customer_kernels_code.close(new_line=False)
124  os.remove(in_filename)
125  os.rename(in_filename + ".tmp", in_filename)
126 
127