This repository has been archived by the owner on Mar 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
filein.py
executable file
·176 lines (155 loc) · 5.07 KB
/
filein.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
172
173
174
175
#!/usr/bin/env python
import os
import sys
import subprocess
try:
__file__
except NameError:
__file__ = ''
i = 0
while not __file__.endswith("filein.py"):
if len(sys.argv) > i:
__file__ = sys.argv[i]
else:
print('''You unusually execute me!
I expected my filename to be in the arguments passed to me..
would you do me this favor?''')
exit(1)
if subprocess.call('hash topaz', shell = True) != 0:
print('topaz must be callable from the commandline.')
exit(10)
ChangeTime = os.path.getctime
def setUpToDate():
f = open(__file__)
myContent = f.read()
f.close()
f = open(__file__, 'w')
f.write(myContent)
f.close()
update = False
reloadPrimitivesAtTheEnd = False
showOutput = len(sys.argv) >= 2 and 'o' in sys.argv[1]
showHelp = len(sys.argv) >= 2 and 'h' in sys.argv[1]
if showHelp:
print '''
h
for help
o
to show the output of topaz
'''
# find out the last time we run this script
lastChanged = ChangeTime(__file__)
gitDirectory = os.path.dirname(__file__ + "/..")
maglevHome = os.environ.get('MAGLEV_HOME', gitDirectory)
if not '.git' in os.listdir(maglevHome):
print('''I expected maglev home to be the git root.''')
exit(2)
relativePathForChangedGsFiles = 'src/smalltalk'
gsFileDirectory = os.path.join(maglevHome, relativePathForChangedGsFiles)
print('Changes in %s' % gsFileDirectory)
changedGsFiles = []
for dirPath, dirNames, fileNames in os.walk(gsFileDirectory):
for fileName in fileNames:
if fileName.endswith('.gs'):
# can be filed in
filePath = os.path.join(dirPath, fileName)
if lastChanged < ChangeTime(filePath) + 10:
print('\t%s' % fileName)
changedGsFiles.append(filePath)
if not changedGsFiles:
print('Nothing!')
else:
topazString = '''set gemstone maglev user SystemUser pass swordfish
login
'''
for filePath in changedGsFiles:
topazString += '''printit
'InPuT_FiLe: {filePath}'
%
input {filePath}
'''.format(filePath = filePath)
topazString += '''run
System commitTransaction
%
exit
'''
pipe = subprocess.Popen( ['topaz'], \
stdout=subprocess.PIPE, \
stderr = subprocess.PIPE, \
stdin = subprocess.PIPE, \
shell = True)
output, error = pipe.communicate(topazString)
if showOutput:
print('#' * 60)
print(' topaz output '.center(60, '#'))
print('#' * 60)
print(output)
print('#' * 60)
print(' topaz output end '.center(60, '#'))
print('#' * 60)
errors = ['unknown command:', '*****', '\nerror', '\nError', \
'GemStone Smalltalk Compiler Errors:', 'WARNING:']
errorStart = 0
while 1:
lastError = len(output)
errorFound = False
for error in errors:
indexOfError = output.find(error, errorStart)
if indexOfError < lastError and errorStart < indexOfError:
lastError = indexOfError
errorFound = True
if not errorFound:
break
errorStart = lastError
del lastError, indexOfError
## error output +- 3 lines
start = errorStart
for i in range(3): # lines
start = output.rfind('\n', 0, start)
stop = errorStart
for i in range(5): # lines
stop = output.find('\n', stop + 1)
## find the name of the bad file
fileNameIndex = output.rfind('InPuT_FiLe: ', 0, errorStart)
fileNameEndIndex = output.find('\n', fileNameIndex, errorStart)
errorFileName = output[fileNameIndex + len('InPuT_FiLe: ') :
fileNameEndIndex]
## print error results
print('-' * 50)
print('An error occurred in %s' % errorFileName)
print(output[start: stop])
## next error please!
errorStart += 1
if errorStart != 0:
exit(1)
update = True
print('Filein without known errors done!')
reloadPrimitivesAtTheEnd = True
## reload primitives
rbFileDirectory = os.path.join(maglevHome, 'src/kernel/bootstrap')
print('Changes in %s' % rbFileDirectory)
for dirPath, dirNames, fileNames in os.walk(rbFileDirectory):
for fileName in fileNames:
if fileName.endswith('.rb'):
# can be filed in
filePath = os.path.join(dirPath, fileName)
if lastChanged < ChangeTime(filePath) + 10:
print('\t%s' % fileName)
reloadPrimitivesAtTheEnd = True
if reloadPrimitivesAtTheEnd:
print('Reload primitives.')
pipe = subprocess.Popen(['rake maglev:reload_prims'],
stdout = subprocess.PIPE, \
shell = True)
stdout, _stderr = pipe.communicate()
if pipe.wait() != 0:
print('-' * 50)
print('rake maglev:reload_prims exited with status %i' % \
pipe.wait())
print stdout
exit(pipe.wait())
update = True
else:
print('Nothing!')
if update:
setUpToDate()