OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # coding: utf-8 | 2 # coding: utf-8 |
3 | 3 |
4 import sys, os, codecs, re | 4 import sys, os, codecs, re, json |
| 5 import xml.dom.minidom as minidom |
5 baseDir = os.path.abspath(os.path.dirname(__file__)) | 6 baseDir = os.path.abspath(os.path.dirname(__file__)) |
6 sys.path.append(os.path.join(baseDir, 'adblockplus', 'buildtools', 'jshydra')) | 7 sys.path.append(os.path.join(baseDir, 'adblockplus', 'buildtools', 'jshydra')) |
7 from abp_rewrite import doRewrite | 8 from abp_rewrite import doRewrite |
8 | 9 |
9 def toCString(string): | 10 def toCString(string): |
10 string = string.replace('\\', '\\\\').replace('"', '\\"') | 11 string = string.replace('\\', '\\\\').replace('"', '\\"') |
11 string = string.replace('\r', '').replace('\n', '\\n') | 12 string = string.replace('\r', '').replace('\n', '\\n') |
12 # Work around MSVC line length limitation | 13 # Work around MSVC line length limitation |
13 #(see http://msdn.microsoft.com/en-us/library/dddywwsc(v=vs.80).aspx) | 14 #(see http://msdn.microsoft.com/en-us/library/dddywwsc(v=vs.80).aspx) |
14 string = re.sub(r'((?:[^\\]|\\.){16000})(.)', r'\1"\n"\2', string, re.S) | 15 string = re.sub(r'((?:[^\\]|\\.){16000})(.)', r'\1"\n"\2', string, re.S) |
15 return '"%s"' % string.encode('utf-8') | 16 return '"%s"' % string.encode('utf-8') |
16 | 17 |
17 def printFilesVerbatim(outHandle, files): | 18 def printFilesVerbatim(outHandle, files): |
18 for file in files: | 19 for file in files: |
19 fileHandle = codecs.open(file, 'rb', encoding='utf-8') | 20 fileHandle = codecs.open(file, 'rb', encoding='utf-8') |
20 print >>outHandle, toCString(os.path.basename(file)) + ',' | 21 print >>outHandle, toCString(os.path.basename(file)) + ',' |
21 print >>outHandle, toCString(fileHandle.read()) + ',' | 22 print >>outHandle, toCString(fileHandle.read()) + ',' |
22 fileHandle.close() | 23 fileHandle.close() |
23 | 24 |
| 25 def convertXMLFile(outHandle, file): |
| 26 fileHandle = codecs.open(file, 'rb', encoding='utf-8') |
| 27 doc = minidom.parse(file) |
| 28 fileHandle.close() |
| 29 |
| 30 data = [] |
| 31 for node in doc.documentElement.childNodes: |
| 32 if node.nodeType != node.ELEMENT_NODE: |
| 33 continue |
| 34 result = {'type': node.tagName} |
| 35 for name, value in node.attributes.items(): |
| 36 result[name] = value |
| 37 data.append(result) |
| 38 fileNameString = toCString(os.path.basename(file)) |
| 39 print >>outHandle, fileNameString + ',' |
| 40 print >>outHandle, toCString('require.scopes[%s] = %s;' % (fileNameString, j
son.dumps(data))) + ',' |
| 41 fileHandle.close() |
| 42 |
| 43 def convertJsFile(outHandle, file): |
| 44 if not os.path.isabs(file): |
| 45 file = os.path.join(baseDir, file) |
| 46 converted = doRewrite([file], ['module=true', 'source_repo=https://hg.adblockp
lus.org/adblockplus/']) |
| 47 print >>outHandle, toCString(os.path.basename(file)) + ',' |
| 48 print >>outHandle, toCString(converted) + ',' |
| 49 |
24 def convert(verbatimBefore, convertFiles, verbatimAfter, outFile): | 50 def convert(verbatimBefore, convertFiles, verbatimAfter, outFile): |
25 outHandle = open(outFile, 'wb') | 51 outHandle = open(outFile, 'wb') |
26 print >>outHandle, 'const char* jsSources[] = {' | 52 print >>outHandle, 'const char* jsSources[] = {' |
27 | 53 |
28 printFilesVerbatim(outHandle, verbatimBefore) | 54 printFilesVerbatim(outHandle, verbatimBefore) |
29 | 55 |
30 for file in convertFiles: | 56 for file in convertFiles: |
31 if not os.path.isabs(file): | 57 if file.endswith('.xml'): |
32 file = os.path.join(baseDir, file) | 58 convertXMLFile(outHandle, file) |
33 converted = doRewrite([file], ['module=true', 'source_repo=https://hg.adbloc
kplus.org/adblockplus/']) | 59 else: |
34 print >>outHandle, toCString(os.path.basename(file)) + ',' | 60 convertJsFile(outHandle, file) |
35 print >>outHandle, toCString(converted) + ',' | |
36 | 61 |
37 printFilesVerbatim(outHandle, verbatimAfter) | 62 printFilesVerbatim(outHandle, verbatimAfter) |
38 | 63 |
39 print >>outHandle, '0, 0' | 64 print >>outHandle, '0, 0' |
40 print >>outHandle, '};' | 65 print >>outHandle, '};' |
41 | 66 |
42 if __name__ == '__main__': | 67 if __name__ == '__main__': |
43 args = sys.argv[1:] | 68 args = sys.argv[1:] |
44 outFile = args.pop() | 69 outFile = args.pop() |
45 | 70 |
46 verbatimBefore = [] | 71 verbatimBefore = [] |
47 verbatimAfter = [] | 72 verbatimAfter = [] |
48 convertFiles = [] | 73 convertFiles = [] |
49 for fileName in args: | 74 for fileName in args: |
50 if fileName.startswith('before='): | 75 if fileName.startswith('before='): |
51 verbatimBefore.append(fileName.replace('before=', '')) | 76 verbatimBefore.append(fileName.replace('before=', '')) |
52 elif fileName.startswith('after='): | 77 elif fileName.startswith('after='): |
53 verbatimAfter.append(fileName.replace('after=', '')) | 78 verbatimAfter.append(fileName.replace('after=', '')) |
54 else: | 79 else: |
55 convertFiles.append(fileName) | 80 convertFiles.append(fileName) |
56 | 81 |
57 convert(verbatimBefore, convertFiles, verbatimAfter, outFile) | 82 convert(verbatimBefore, convertFiles, verbatimAfter, outFile) |
OLD | NEW |