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 |
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 convert(convertFiles, verbatimFiles, outFile): | 17 def printFilesVerbatim(outHandle, files): |
18 outHandle = open(outFile, 'wb') | 18 for file in files: |
19 print >>outHandle, 'const char* jsSources[] = {' | |
20 | |
21 for file in verbatimFiles: | |
22 fileHandle = codecs.open(file, 'rb', encoding='utf-8') | 19 fileHandle = codecs.open(file, 'rb', encoding='utf-8') |
23 print >>outHandle, toCString(os.path.basename(file)) + ',' | 20 print >>outHandle, toCString(os.path.basename(file)) + ',' |
24 print >>outHandle, toCString(fileHandle.read()) + ',' | 21 print >>outHandle, toCString(fileHandle.read()) + ',' |
25 fileHandle.close() | 22 fileHandle.close() |
26 | 23 |
| 24 def convert(verbatimBefore, convertFiles, verbatimAfter, outFile): |
| 25 outHandle = open(outFile, 'wb') |
| 26 print >>outHandle, 'const char* jsSources[] = {' |
| 27 |
| 28 printFilesVerbatim(outHandle, verbatimBefore) |
| 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 |
| 35 printFilesVerbatim(outHandle, verbatimAfter) |
| 36 |
32 print >>outHandle, '0, 0' | 37 print >>outHandle, '0, 0' |
33 print >>outHandle, '};' | 38 print >>outHandle, '};' |
34 | 39 |
35 if __name__ == '__main__': | 40 if __name__ == '__main__': |
36 args = sys.argv[1:] | 41 args = sys.argv[1:] |
37 outFile = args.pop() | 42 outFile = args.pop() |
38 | 43 |
39 verbatimFiles = [] | 44 verbatimBefore = [] |
40 while len(args): | 45 verbatimAfter = [] |
41 file = args.pop() | 46 convertFiles = [] |
42 if file == '--': | 47 for fileName in args: |
43 break | 48 if fileName.startswith('before='): |
| 49 verbatimBefore.append(fileName.replace('before=', '')) |
| 50 elif fileName.startswith('after='): |
| 51 verbatimAfter.append(fileName.replace('after=', '')) |
44 else: | 52 else: |
45 verbatimFiles.insert(0, file) | 53 convertFiles.append(fileName) |
46 | 54 |
47 convertFiles = args | 55 convert(verbatimBefore, convertFiles, verbatimAfter, outFile) |
48 convert(convertFiles, verbatimFiles, outFile) | |
OLD | NEW |