-
Notifications
You must be signed in to change notification settings - Fork 0
/
script_changer.py
175 lines (140 loc) · 4.92 KB
/
script_changer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# ##########################################################
#
# Title : script_changer.py
# Author : O.C.
# Description : Applies a few changes to the tcl script
# generated by the "write_project_tcl"
# function
#
# ##########################################################
#Necessary for several functions used through this script
import os
#Declaring flags that will be used for every change
flag0 = 1
flag1 = 0
flag2 = 0
flag3 = 0
flag4 = 0
flag5 = 0
flag6 = 0
#Name of tcl script
filename = "build.tcl"
#Getting the current path of this file
current_path = os.path.dirname(os.path.abspath(os.path.basename(__file__)))
#Getting the names of every .bd file
bd_names = []
for subdir, dirs, files in os.walk(current_path):
for file in files:
#We take only the .bd files
if file.find(".bd")!= -1 :
bd_names.append(os.path.splitext(file)[0]) #removing extension
#Getting the number of .bd files
count_bd = len(bd_names)
#We open a temporary file to store the new information in it
with open("tmp.tcl","w+") as tmp :
#Opens up the tcl script
with open(filename, "r+") as f:
#starting from the beginning of the file
f.seek(0)
#Going through the file one line at the time
for line in f:
#Strips the line of the line changing function -> makes it easier to compare strings
line = line.rstrip("\n")
#Finding project name
if flag0 == 1 :
if line.find("Tcl script for re-creating project") != -1 :
first = line.find("'")
second = line.find("'", first+1)
project_name = line[first+1:second]
print("Project Name : "+line[first+1:second])
#First change happens -> changing path
if flag1 == 1 :
line = "set origin_dir [file dirname [info script]]"
flag1 = 0
#Setting up a flag -> changing next iteration
var1 = "# Set the reference directory for source file relative paths (by default the value is script directory path)"
if line == var1 :
flag1 = 1;
#Second change happens -> suppressing original project's path
if flag2 == 1 :
flag2 = 0
continue
#Setting up a flag -> changing next iteration
var2 = "# Set the directory path for the original project from where this script was exported"
if line == var2 :
flag2 = 1;
continue
#Third change happens -> creating new project and suppressing old one
if flag3 == 1 :
part = line[line.find("-part"):]
line = "create_project -force " + project_name + " $origin_dir/work_dir " + part
flag3 = 0
#Setting up a flag -> changing next iteration
var3 = "# Create project"
if line == var3 :
flag3 = 1;
#Fourth change happens -> avoiding importing .bd files and wrappers
if flag4 == 1 :
#Checking if it is a .bd file
if line.find(".bd\"]")!= -1 :
continue
#Checking if it is a wrapper file
if line.find("wrapper.v\"]")!= -1 :
continue
#Stop condition
var5 = "add_files -norecurse -fileset $obj $files"
if line == var5 :
flag4 = 0
#Setting up a flag -> changing next iteration
var4 = "set files [list \\"
if line == var4 :
flag4 = 1;
#Fifth change happens -> discarding the files that were not imported
#Stop condition
var8 = "# Set 'sources_1' fileset file properties for local files"
if line == var8 :
flag5 = 0
#Delete this portion
if flag5 == 1 :
continue
#Setting up a flag -> changing next iteration
var6 = "# Import local files from the original project"
var7 = "# Set 'sources_1' fileset file properties for remote files"
if line == var6 or line == var7:
flag5 = 1;
#Sixth change happens -> Removing extra information from Vivado 2018.1 tcl version
#Stop condition
var10 = "# Create 'synth_1' run (if not found)"
if line == var10 :
flag6 = 0
#Delete this portion
if flag6 == 1 :
continue
#Setting up a flag -> changing next iteration
var9 = "# Adding sources referenced in BDs, if not already added"
if line == var9 :
flag6 = 1;
#Prints the new lines in a temporary file
tmp.write(line+"\n")
#Sixth change happens -> adding code to generate the block designs from the tcl scripts
for i in range(0,count_bd) :
tmp.write("\n# Create block design \n")
tmp.write("source $origin_dir/src/bd/"+ bd_names[i] + ".tcl \n")
tmp.write("\n# Generate the wrapper\n")
#tmp.write("set design_name [get_bd_designs]\n")
tmp.write("make_wrapper -files [get_files $design_name.bd] -top -import\n")
tmp.write("set origin_dir [file dirname [info script]]\n")
tmp.write("\n")
# #Rebuilding the original file
#Starting at the beginning of the temp file
tmp.seek(0)
for line in tmp :
#Opening the original file to write in it
with open(filename,"w") as f:
#Starting at the beginning of the original file
f.seek(0)
for line in tmp :
#Writing each line
f.write(line)
#Removing the temporary file
os.remove("tmp.tcl")