LEFT | RIGHT |
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 |
5 baseDir = os.path.abspath(os.path.dirname(__file__)) | 5 baseDir = os.path.abspath(os.path.dirname(__file__)) |
6 sys.path.append(os.path.join(baseDir, 'adblockplus', 'buildtools', 'jshydra')) | 6 sys.path.append(os.path.join(baseDir, 'adblockplus', 'buildtools', 'jshydra')) |
7 from abp_rewrite import doRewrite | 7 from abp_rewrite import doRewrite |
8 | 8 |
9 def toCString(string): | 9 def toCString(string): |
10 string = string.replace('\\', '\\\\').replace('"', '\\"') | 10 string = string.replace('\\', '\\\\').replace('"', '\\"') |
11 string = string.replace('\r', '').replace('\n', '\\n') | 11 string = string.replace('\r', '').replace('\n', '\\n') |
12 # Work around MSVC line length limitation | 12 # Work around MSVC line length limitation |
13 #(see http://msdn.microsoft.com/en-us/library/dddywwsc(v=vs.80).aspx) | 13 #(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) | 14 string = re.sub(r'(.{16000})(.)', r'\1"\n"\2', string, re.S) |
15 return '"%s"' % string.encode('utf-8') | 15 return '"%s"' % string.encode('utf-8') |
16 | 16 |
| 17 def printFilesVerbatim(outHandle, files): |
| 18 for file in files: |
| 19 fileHandle = codecs.open(file, 'rb', encoding='utf-8') |
| 20 print >>outHandle, toCString(os.path.basename(file)) + ',' |
| 21 print >>outHandle, toCString(fileHandle.read()) + ',' |
| 22 fileHandle.close() |
| 23 |
17 def convert(verbatimBefore, convertFiles, verbatimAfter, outFile): | 24 def convert(verbatimBefore, convertFiles, verbatimAfter, outFile): |
18 outHandle = open(outFile, 'wb') | 25 outHandle = open(outFile, 'wb') |
19 print >>outHandle, 'const char* jsSources[] = {' | 26 print >>outHandle, 'const char* jsSources[] = {' |
20 | 27 |
21 for file in verbatimBefore: | 28 printFilesVerbatim(outHandle, verbatimBefore) |
22 fileHandle = codecs.open(file, 'rb', encoding='utf-8') | |
23 print >>outHandle, toCString(os.path.basename(file)) + ',' | |
24 print >>outHandle, toCString(fileHandle.read()) + ',' | |
25 fileHandle.close() | |
26 | 29 |
27 convertFiles = map(lambda f: f if os.path.isabs(f) else os.path.join(baseDir,
f), convertFiles) | 30 convertFiles = map(lambda f: f if os.path.isabs(f) else os.path.join(baseDir,
f), convertFiles) |
28 converted = doRewrite(convertFiles, ['module=true', 'source_repo=https://hg.ad
blockplus.org/adblockplus/']) | 31 converted = doRewrite(convertFiles, ['module=true', 'source_repo=https://hg.ad
blockplus.org/adblockplus/']) |
29 print >>outHandle, toCString('adblockplus.js') + ',' | 32 print >>outHandle, toCString('adblockplus.js') + ',' |
30 print >>outHandle, toCString(converted) + ',' | 33 print >>outHandle, toCString(converted) + ',' |
31 | 34 |
32 for file in verbatimAfter: | 35 printFilesVerbatim(outHandle, verbatimAfter) |
33 fileHandle = codecs.open(file, 'rb', encoding='utf-8') | |
34 print >>outHandle, toCString(os.path.basename(file)) + ',' | |
35 print >>outHandle, toCString(fileHandle.read()) + ',' | |
36 fileHandle.close() | |
37 | 36 |
38 print >>outHandle, '0, 0' | 37 print >>outHandle, '0, 0' |
39 print >>outHandle, '};' | 38 print >>outHandle, '};' |
40 | 39 |
41 if __name__ == '__main__': | 40 if __name__ == '__main__': |
42 args = sys.argv[1:] | 41 args = sys.argv[1:] |
43 outFile = args.pop() | 42 outFile = args.pop() |
44 | 43 |
45 verbatimBefore = [] | 44 verbatimBefore = [] |
46 verbatimAfter = [] | 45 verbatimAfter = [] |
47 convertFiles = [] | 46 convertFiles = [] |
48 for fileName in args: | 47 for fileName in args: |
49 if fileName.startswith('before='): | 48 if fileName.startswith('before='): |
50 verbatimBefore.append(fileName.replace('before=', '')) | 49 verbatimBefore.append(fileName.replace('before=', '')) |
51 elif fileName.startswith('after='): | 50 elif fileName.startswith('after='): |
52 verbatimAfter.append(fileName.replace('after=', '')) | 51 verbatimAfter.append(fileName.replace('after=', '')) |
53 else: | 52 else: |
54 convertFiles.append(fileName) | 53 convertFiles.append(fileName) |
55 | 54 |
56 convert(verbatimBefore, convertFiles, verbatimAfter, outFile) | 55 convert(verbatimBefore, convertFiles, verbatimAfter, outFile) |
LEFT | RIGHT |