-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathincludeCode.py
More file actions
executable file
·111 lines (80 loc) · 3.18 KB
/
includeCode.py
File metadata and controls
executable file
·111 lines (80 loc) · 3.18 KB
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
#!/usr/bin/env python
"""
Updates all code files in the Wiki.
Run by: python includeCode.py <code-dir> <wiki-file1> <wiki-file2> ....
Note: You can use globbing to hand over multiple wiki files, e.g. by running the script like this:
python includeCode.py scripts/ wiki/*.wiki
"""
import os
import sys
import re
def getFiles(dirname, extension):
for fname in os.listdir(dirname):
if fname.endswith(extension):
yield fname, os.path.join(dirname, fname)
if len(sys.argv) < 3:
print globals()['__doc__'] % locals()
sys.exit(1)
codeDir = sys.argv[1]
wikiFiles = sys.argv[2:]
#############################################
# Read all code files
#############################################
programcodeStartLine = '#!/usr/bin/env jython'
replaceTabsWithSpace = 4
codes = {}
for fName, path in getFiles(codeDir, '.jy'):
fIn = open(path, 'r')
lines = fIn.readlines();
fIn.close()
#Do some sanity check
if lines[0].strip() != programcodeStartLine:
print "Error in file %s - File not started with: %s" % (fName, programcodeStartLine)
continue
if lines[1].replace(' ', '').startswith('#Filename:'):
fileNameInComment = lines[1][lines[1].find(':')+1:].strip()
if fileNameInComment != fName:
raise ValueError("Error in file %s - Filename in comment (%s) does not match actual filename" % (fName, fileNameInComment))
else:
lines.insert(1, '# Filename: %s\n' % fName)
content = "".join(lines)
if replaceTabsWithSpace > 0:
content = content.replace('\t', ' '*replaceTabsWithSpace)
codes[fName] = content
#############################################
# Read all wiki files
#############################################
for path in wikiFiles:
fName = os.path.basename(path)
fIn = open(path, 'r')
lines = fIn.readlines()
fIn.close
content = ""
codeBlockBegin = False
lineIdx = 0
while lineIdx < len(lines):
line = lines[lineIdx]
content += line
if line.startswith('{{{'):
nextLineIdx = lineIdx+1
if lines[nextLineIdx].strip() == programcodeStartLine: #Skip program code start line
nextLineIdx += 1
if lines[nextLineIdx].replace(' ', '').startswith('#Filename:'):
codeName = lines[nextLineIdx][lines[nextLineIdx].find(':')+1:].strip()
if codeName not in codes:
print "%s - Code %s not found!!!!!!!!!!!!!!" % (fName, codeName)
content += "#Filename: %s" % codeName
else:
content += codes[codeName]
print "%s - include script: %s" % (fName, codeName)
#Scan until ending }}}
while lines[nextLineIdx].strip() != '}}}':
nextLineIdx += 1
content += '\n}}}\n'
lineIdx = nextLineIdx
lineIdx += 1
fOut = open(path, 'w')
lines = fOut.write(content)
fOut.close
#print content
print "DONE"