-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathswiftinclude.py
executable file
·79 lines (63 loc) · 2.18 KB
/
swiftinclude.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
#! /usr/bin/env python
#-----------------------------------------------------------------------------#
# Name: swiftinclude.py
# Desc: This script replaces 'include "file.swift"' statements with the actual
# file contents.
# Auth: Cezary Wojcik
# Opts: -i, --inputfile : specify input file (default "main.swift")
# -o, --outputfile : specify output file (default "app.swift")
# -d, --debug : show debug messages
#-----------------------------------------------------------------------------#
# ---- [ imports ] ------------------------------------------------------------
import getopt, sys, re
# ---- [ globals ] ------------------------------------------------------------
files_included = []
# ---- [ utility functions ] --------------------------------------------------
def handle_error(message):
print "Error: {0}".format(message)
sys.exit(2)
# ---- [ helper functions ] ---------------------------------------------------
def includify_file(inputfile):
global files_included
files_included.append(inputfile)
output = ""
pattern = re.compile("include \"(.*)\"")
with open(inputfile) as f:
arr = f.readlines()
for line in arr:
m = pattern.match(line)
if m:
filename = m.group(1)
if filename not in files_included:
output += includify_file(filename)
else:
output += line
return output
# ---- [ main ] ---------------------------------------------------------------
def main(argv):
inputfile = "main.swift"
outputfile = "app.swift"
try:
opts, args = getopt.getopt(sys.argv[1:], 'i:o:',
['inputfile=', 'outputfile='])
except getopt.GetoptError as err:
handle_error(str(err))
for o, a in opts:
if o in ['-i', '--inputfile']:
inputfile = a
elif o in ['-o', '--outputfile']:
outputfile = a
else:
handle_error("unhandled option '{0}' detected".format(o))
# create output file
try:
f = open(outputfile, "w+")
f.close()
except IOError:
handle_error("failed to write to file, '{0}'."
.format(benchmarkfile))
# parse file
f = open(outputfile, "w")
f.write(includify_file(inputfile))
if __name__ == "__main__":
main(sys.argv[1:])