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