-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickPre.py
More file actions
executable file
·218 lines (180 loc) · 5 KB
/
QuickPre.py
File metadata and controls
executable file
·218 lines (180 loc) · 5 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env python
#
# The quick pre-processor. Only toggles single #ifdefs.
#
# Useful for hacking our large swaths of code for a modified
# source code release.
#
# Usage:
# QuickPre #define filename (or names)
#
# Output is put into the QuickPreDir directory.
# For NOT defining (so it kicks off ifndefs) use "not name".
# Note: Somehow I still need to get the value input. Probably needs a flag or something.
import sys
import os
import glob
import re
#
# QP does all the work.
#
def negate(string):
if string[0] == "!":
return string[1:]
else:
return "!"+string
#end negate
def QP(define, value, filename):
print "===== def ", define, " filename " , filename, " ====="
DirName = "./QuickPreDir"
#debug output level.
dbg_lev = 0
# check for file existance
# read it (or complain if it doesn't exist)
# foreach line:
# (future development)
# (pull out comments, quotes)
# (if NOT ifdef, ifndef, )
# ( Examine line for the word, probably wants a regexp.)
# ( if found, replace existance with value.)
# If line begins with #ifdef, push argument onto the ifdef stack
# If line begins with #ifndef, push !argument onto the ifdef stack
# If line begins with #endif, pop ifdef stack.
# if line begins with #else, pop ifdef, push !argument
# If the ifdef stack isn't empty:
# If (any) define matches anything in the stack, copy line to output array
# If (any) !define matches anything in the stack, don't copy line.
# write crap.
# error check.
if glob.glob(filename) == []:
print "Missing ", filename
return
if filename == DirName:
return
OutName = DirName + "/"+filename
file = open(filename, "r")
lines = file.readlines()
stack = []
output = []
# figure out what the negation is: inside Ndefine, lines aren't written.
Ndefine = negate(define)
for line in lines:
if dbg_lev > 0:
print line[0:-1]
if dbg_lev > 1:
print "------",stack
match = ""
caught_case = 0
# if re.findall(r"^#\s*ifdef\s*",line) != []:
if line[0:6] == "#ifdef":
match = re.findall(r"#ifdef\s*(.*)", line)[0]
stack.append(match)
caught_case = 1
if dbg_lev > 1:
print "------ match ^#ifdef"
if re.findall(r"#if\s*defined\s*(.*)", line) != []:
match = re.match(r"(#if\s*defined\s*)\((.*)\)", line).group(2)
stack.append(match)
caught_case = 1
if dbg_lev > 1:
print "------ match if defined"
if line[0:7] == "#ifndef":
match = negate( re.findall(r"#ifndef\s*(.*)", line)[0])
stack.append(match)
caught_case = 1
if dbg_lev > 1:
print "------ match ifndef"
if line[0:6] == "#endif":
match = stack.pop()
caught_case = 1
if dbg_lev > 1:
print "------ match endif"
if line[0:5] == "#else":
match = stack.pop()
stack.append( negate(match) )
caught_case = 1
if dbg_lev > 1:
print "------ match else"
#some 'other' if statement, probably worth skiping.
if re.findall(r"#if\s*(.*)", line) != [] and caught_case == 0:
match = line[3:]
stack.append(match)
caught_case = 1
if dbg_lev > 1:
print "------ match something other:", line[0:-1]
#
# Cases that don't affect the stack, but we watch for.
#
if re.findall(r"#\s*define\b",line) != []:
caught_case = 1
if re.findall(r"#\s*include",line) != []:
caught_case = 1
if re.findall(r"#\s*undef",line) != []:
caught_case = 1
if line[0:5] == "#elif":
print "#elif encountered: ignoring. Output code might not compile..."
caught_case = 1
if line[0] == "#" and caught_case == 0:
print "You missed a cpp case:"
print "--- ", line[0:-1]
# match2 = stack.pop()
# match = re.findall(r"#elif\s*defined\s*\((.*)\)",line)
# stack.append(match)
#print "---",line[0:-1], "(",match, ")"
#if this line has anything to do with my directive,
#i don't want it
if match == define:
continue
if match == "!" +define:
continue
if match == Ndefine:
continue
#the only lines I don't write are the ones I've been told not to.
if stack.count(Ndefine):
continue
output.append(line)
#
# Now write the file!
#
file = open(OutName, "w")
for line in output:
file.write(line)
file.close()
#end QP
#
# Command Line crap.
#
if len(sys.argv) == 1:
print """
#
# The quick pre-processor. Only toggles single #ifdefs.
# Useful for hacking our large swaths of code for a modified
# source code release.
#
# Usage:
# %> QuickPre.py define filename (or names)
# i.e.
# %> QuickPre.py MHD *
# will look through every file in the directory, and define MHD.
#
# %> QuickPre.py not MHD *
# will parse every file with MHD undefined.
# Each file so parsed is placed in a directory called QuickPreDir
# Only one define can be called at a time.
"""
else:
# Make the output directory
DirName = "./QuickPreDir"
if glob.glob(DirName) == []:
os.mkdir(DirName)
filenamestart = 2
if sys.argv[1] == "not":
define = "!" + sys.argv[2]
filenamestart = 3
else:
define = sys.argv[1]
value = 0 #not used currently.
# for each argv, call 'split'
for name in sys.argv[filenamestart:]:
QP(define, value,name)
#end