-
Notifications
You must be signed in to change notification settings - Fork 0
/
combiner.py
117 lines (95 loc) · 3 KB
/
combiner.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
import os
folderPath = 'src/hypersonic'
# mainFile is the file containing the main class.
mainFile = 'src/Player.java'
# exclude these files from the project.
excluded = ['State.java', 'Frame.java', 'Label.java']
def printSafe(s):
s = str(s).encode('ascii', 'ignore')
s = s.decode('ascii')
print(s)
def getName(file):
return file[file.rfind('/')+1:]
def isMain(file):
global mainFile
return getName(file) == mainFile
def notExcluded(file):
global excluded
return (file not in excluded)
def getFirstClassInterface(s):
firstClass = s.find('class')
firstInterface = s.find('interface')
if firstClass == -1:
index = firstInterface
elif firstInterface == -1:
index = firstClass
else:
index = min(firstClass, firstInterface)
return index
def getImports(file):
f = open(file)
s = f.read()
f.close()
s = s[:getFirstClassInterface(s)]
lines = s.split('\n')
impts = []
for line in lines:
if line.find('import java') != -1:
impts.append(line)
return impts
def processFile(file, isPublic):
f = open(file)
s = f.read()
f.close()
startIndex = getFirstClassInterface(s)
classCommentIndex = s.find('/*')
commentEndIndex = s.find('*/', classCommentIndex)
if classCommentIndex < startIndex < commentEndIndex:
startIndex = commentEndIndex + getFirstClassInterface(s[commentEndIndex:])
publicString = ''
if isPublic:
publicString = 'public '
if classCommentIndex > startIndex or True:
# print('Missing class comment for ' + file)
return publicString + s[startIndex:]
else:
return s[classCommentIndex:commentEndIndex] + '*/\n' + publicString + s[startIndex:]
if __name__ == '__main__':
# ds = os.listdir(folderPath)
# allFiles = []
# for d in ds:
# files = os.listdir(folderPath + d)
# files = filter(lambda s : s[-5:] == '.java', files)
# files = filter(notExcluded, files)
# files = map(lambda s : folderPath + d + '/' + s, files)
# allFiles += files
allFiles = []
files = os.listdir(folderPath)
files = filter(lambda s : s[-5:] == '.java', files)
files = filter(notExcluded, files)
files = map(lambda s : folderPath + '/' + s, files)
allFiles += files
jImports = set()
for file in allFiles:
jImports = jImports.union(getImports(file))
s = []
for imp in jImports:
s.append(imp + '\n')
s.append('\n')
# for file in allFiles:
# if isMain(file):
# s.append(processFile(file, True))
s.append('class Player { public static void main(String[] args) { new Engine().start();}}')
s.append('\n')
for file in allFiles:
if not isMain(file):
pro = processFile(file, False)
s.append(pro)
print('- combine ' + getName(file))
s.append('\n')
s = ''.join(s)
f = open(mainFile, 'w+')
f.write(s)
f.close()
#printSafe(s)
print ('Combination complete')