-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.py
executable file
·54 lines (48 loc) · 1.87 KB
/
parse.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
#!/usr/bin/env python
# Parses a file and types it to screen, if it encounters the word "include"
# followed by a filename at the beginning of a line that entire file printed
# instead of the include-line.
import sys
import os.path
import optparse
parser = optparse.OptionParser(
usage = "Usage: %prog [options] filename",
description = "Parse a source file and include other files.",
option_list = [
optparse.make_option("-o", "--optimize", dest = "optimize", action = "store_true", default = False, help = "Removes comments and indentation")
])
options, args = parser.parse_args()
if 1 != len(args):
parser.error("Only one filename is supported.")
print "options: " + str(options)
print "args: " + str(args)
if not os.path.exists(args[0]):
sys.exit("Error: Filename " + args[0] + " not found.")
sourceFile = file(args[0])
for line in sourceFile:
lineStripped = line.strip()
if lineStripped.startswith("include"):
lineInclude = lineStripped.split()
if not os.path.exists(lineInclude[1]):
sys.exit("Included file " + lineInclude[1] + " not found.")
includeFile = file(lineInclude[1])
if not os.path.exists(lineInclude[1]):
sys.exit("Included file " + lineInclude[1] + " not found.")
for incLine in includeFile:
incStripped = incLine.strip()
if incStripped.startswith("--") and options.optimize:
pass
else:
if options.optimize:
if not incStripped=="":
print incStripped
else:
print incLine,
elif lineStripped.startswith("--") and options.optimize:
pass
else:
if options.optimize:
if not lineStripped=="":
print lineStripped
else:
print line,