-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinclude.py
executable file
·106 lines (70 loc) · 2.67 KB
/
include.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
#! /usr/bin/env python
# Thanks fam
# https://github.com/cezarywojcik/Swift-Script-Include
#-----------------------------------------------------------------------------#
# Name: include.py
# Desc: 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 ] ------------------------------------------------------------
includedFileNames = []
# ---- [ utility functions ] --------------------------------------------------
def absoluteFilePath(relativePath):
path = sys.path[0]
return path + "/src/" + relativePath
def handleError(message):
print "Error: {0}".format(message)
sys.exit(2)
# ---- [ helper functions ] ---------------------------------------------------
def includify(file):
global includedFileNames
includedFileNames.append(file)
output = ""
pattern = re.compile("include \"(.*)\"")
with open(file) as f:
arr = f.readlines()
for line in arr:
match = pattern.match(line)
if match:
name = match.group(1) # => "source.swift"
path = absoluteFilePath(name) # => "../src/source.swift" (based on this scripts path)
# print(name)
# print(path)
if name not in includedFileNames:
output += includify(path)
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:
handleError(str(err))
for o, a in opts:
if o in ['-i', '--inputfile']:
inputfile = a
elif o in ['-o', '--outputfile']:
outputfile = a
else:
handleError("unhandled option '{0}' detected".format(o))
# Create output file
try:
file = open(outputfile, "w+")
file.close()
except IOError:
handleError("failed to open output file, '{0}'."
.format(benchmarkfile))
# Parse input file
file = open(outputfile, "w")
file.write(includify(inputfile))
if __name__ == "__main__":
main(sys.argv[1:])