-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcpp.py
executable file
·136 lines (104 loc) · 4.05 KB
/
pcpp.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
#!/usr/bin/env python
"""
This is a preprocessor for cross-developing camera applications between
Mac OS and the Raspberry Pi.
"""
import argparse, time
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
# This declaration class holds the start and end line, as well as the platform target.
class Declaration:
def __init__(self, start, platform):
self.start = start
self.end = 0
self.platform = platform
class Handler(PatternMatchingEventHandler):
patterns = ["*.py"]
def process(self, event):
preprocess(event.src_path)
def on_modified(self, event):
self.process(event)
def on_created(self, event):
self.process(event)
def main():
# parse arguments
ap = argparse.ArgumentParser()
ap.add_argument("-s", "--source", required=True, help="source file / directory to preprocess")
args = vars(ap.parse_args())
# open source file (read-only)
input = args["source"]
observer = Observer()
observer.schedule(Handler(), input)
observer.start()
print "Watching " + input
print "To terminate, hit CTRL + C"
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
# Preprocesses an input file, and writes the result to an output file
def preprocess(inputPath):
platform = ""
index = 0
currentDeclaration = None
declarations = []
inputFile = open(inputPath)
# Identify preprocessor conditionals and defs
for line in inputFile:
if "#define mac" in line:
# we're compiling for mac.
platform = "mac"
print "Recognised target platform: Mac"
elif "#define pi" in line:
# we're compiling for the pi.
platform = "pi"
print "Recognised target platform: Pi"
elif "#ifdef mac" in line:
# mac declaration
currentDeclaration = Declaration(index, "mac")
elif "#ifdef pi" in line:
# pi declaration
currentDeclaration = Declaration(index, "pi")
elif "#endif" in line:
# end of declaration
currentDeclaration.end = index
declarations.insert(0,currentDeclaration)
print "Found conditional. Start line: " + `currentDeclaration.start` + " End line: " + `currentDeclaration.end`
index = index+1
if platform is "" and currentDeclaration is None:
print "No platform declaration or conditionals. Will not preprocess file."
elif platform is "" and currentDeclaration is not None:
print "No platform declaration. However, conditionals were found. Will not preprocess file."
elif platform is not "" and currentDeclaration is None:
print "File preprocessed."
else:
print "Preprocessing file..."
# create destination file
outputFile = open(inputPath[:-3] + "-" + platform + ".py", "w")
inputFile.seek(0, 0)
lines = inputFile.readlines()
for declaration in declarations:
if platform is "mac":
if declaration.platform is "mac":
# remove declarations, but keep code in between.
del lines[declaration.start]
del lines[declaration.end-1]
elif declaration.platform is "pi":
# throw away declarations and code in between.
del lines[declaration.start:declaration.end+1]
elif platform is "pi":
if declaration.platform is "pi":
# remove declarations, but keep code in between.
del lines[declaration.start]
del lines[declaration.end-1]
elif declaration.platform is "mac":
# throw away declarations and code in between.
del lines[declaration.start:declaration.end+1]
for line in lines:
outputFile.write(line)
outputFile.close()
inputFile.close()
if __name__ == '__main__':
main()