-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepack.py
executable file
·171 lines (156 loc) · 5.47 KB
/
repack.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun May 14 21:41:16 2017
@author: xing
Version: 0.2
"""
import sys,re,os
def insertText(FileGuest, FileHost):
#FileGuest and FileHost are two FILE objects.
currLine=FileGuest.readline()
while currLine != "":
FileHost.write(currLine)
currLine=FileGuest.readline()
def expandFunc(currLine,parentFuncPath):
#Some debug info
#extract the name of the function:
nameFunc=re.sub(r'[\s(]*function\s*([^)(\s]*)\s*.*\n',r'\1',currLine)
#I still need to strip the comments here:
blockcomment=False
fileFunc=open(parentFuncPath+r'@'+nameFunc+'.js','r')
#The first line (func name) should go directly to "toReturn"
toReturn=fileFunc.readline()
#Start analyze the rest of the file:
currLine=fileFunc.readline()
rawCurrLine=currLine
while currLine != '':
#First deal with comments
#if block comments:
if re.match(r'.*\*/',currLine) == None and blockcomment == True:
toReturn=toReturn+currLine
currLine=fileFunc.readline()
rawCurrLine=currLine
continue
#whole inline comments:
elif re.match(r'\s*//',currLine) != None:
toReturn=toReturn+currLine
currLine=fileFunc.readline()
rawCurrLine=currLine
continue
#The last line of block comments:
elif re.match(r'.*\*/',currLine) != None and blockcomment == True:
toReturn=toReturn+currLine
currLine=fileFunc.readline()
rawCurrLine=currLine
blockcomment=False
continue
#The first line of block comments:
elif re.match(r'.*/\*',currLine) != None and blockcomment == False:
toReturn=toReturn+currLine
currLine=fileFunc.readline()
rawCurrLine=currLine
blockcomment=True
continue
#whole line comments are processed, strip comments for further processing
currLine=re.sub(r'(.*)//.*',r'\1',currLine)
currNameFunc=re.sub(r'[\s(]*function\s*([^)(\s]*)\s*.*\n',r'\1',currLine)
if re.match(reFuncStart,currLine):
if currNameFunc == '':
toReturn=toReturn+rawCurrLine
currLine=fileFunc.readline()
rawCurrLine=currLine
continue
toReturn=toReturn+expandFunc(currLine,parentFuncPath+r'@'+nameFunc+r'/')
#skip the ending "}"
currLine=fileFunc.readline()
currLine=fileFunc.readline()
rawCurrLine=currLine
continue
else:
#Simply return the current line
toReturn=toReturn+rawCurrLine
currLine=fileFunc.readline()
rawCurrLine=currLine
continue
#The last line of the while block. It should not be reached under normal circumstances.
print("Something wrong,-->{0:s}".format(fileFunc.readline()))
fileFunc.close()
return toReturn
nameJS=sys.argv[1]
print("Packing functions into {0} ...".format(nameJS))
fileJS=open(nameJS,'w')
#Append filePreface first.
filePreface=open(r'@preface.js','r')
insertText(filePreface,fileJS)
filePreface.close()
#Now let's deal with the main function file:
"""
Define some REs first
Note that re.match will only match string from the beginning and
no ^ is needed.
"""
reComment=re.compile(r'\s*//')
reFuncStart=re.compile(r'[\s(]*function[^)(]*\(\s*[^)(]*\s*\).*')
fileMain=open(r'@main.js','r')
#Skip the first line because it is often an anonymous function
fileJS.write(fileMain.readline())
currLine=fileMain.readline()
rawCurrLine=currLine
blockcomment=False
while currLine != "":
#First deal with comments
#if block comments:
if re.match(r'.*\*/',currLine) == None and blockcomment == True:
fileJS.write(currLine)
currLine=fileMain.readline()
rawCurrLine=currLine
continue
#whole inline comments:
elif re.match(r'\s*//',currLine) != None:
fileJS.write(currLine)
currLine=fileMain.readline()
rawCurrLine=currLine
continue
#The last line of block comments:
elif re.match(r'.*\*/',currLine) != None and blockcomment == True:
fileJS.write(currLine)
currLine=fileMain.readline()
rawCurrLine=currLine
blockcomment=False
continue
#The first line of block comments:
elif re.match(r'.*/\*',currLine) != None and blockcomment == False:
fileJS.write(currLine)
currLine=fileMain.readline()
rawCurrLine=currLine
blockcomment=True
continue
#whole line comments are processed, strip comments for further processing
currLine=re.sub(r'(.*)//.*',r'\1',currLine)
if re.match(reFuncStart,currLine):
#It seems that we need to define a recursive function...
nameFunc=re.sub(r'[\s(]*function\s*([^)(\s]*)\s*.*\n',r'\1',currLine)
if nameFunc == '':
fileJS.write(rawCurrLine)
currLine=fileMain.readline()
rawCurrLine=currLine
continue
fileJS.write(expandFunc(currLine,r'@main/'))
#Skip the ending "}"
currLine=fileMain.readline()
#The real "next" line
currLine=fileMain.readline()
rawCurrLine=currLine
continue
else:
fileJS.write(rawCurrLine)
currLine=fileMain.readline()
rawCurrLine=currLine
continue
#The last line of the while block. It should not be reached under normal circumstances.
currLine=fileMain.readline()
#close the fileMain and exit
fileMain.close()
fileJS.close()
print('All Done!')