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 base_dir = os.path.abspath(os.path.dirname(__file__)) | 5 baseDir = os.path.abspath(os.path.dirname(__file__)) |
6 sys.path.append(os.path.join(base_dir, '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 return '"%s"' % string.encode('utf-8') | 12 return '"%s"' % string.encode('utf-8') |
13 | 13 |
14 def convert(convertFiles, verbatimFiles, outFile): | 14 def convert(convertFiles, verbatimFiles, outFile): |
15 outHandle = open(outFile, 'wb') | 15 outHandle = open(outFile, 'wb') |
16 print >>outHandle, 'const char* jsSources[] = {' | 16 print >>outHandle, 'const char* jsSources[] = {' |
17 | 17 |
18 for file in verbatimFiles: | 18 for file in verbatimFiles: |
19 fileHandle = codecs.open(file, 'rb', encoding='utf-8') | 19 fileHandle = codecs.open(file, 'rb', encoding='utf-8') |
20 print >>outHandle, toCString(os.path.basename(file)) + ',' | 20 print >>outHandle, toCString(os.path.basename(file)) + ',' |
21 print >>outHandle, toCString(fileHandle.read()) + ',' | 21 print >>outHandle, toCString(fileHandle.read()) + ',' |
22 fileHandle.close() | 22 fileHandle.close() |
23 | 23 |
24 convertFiles = map(lambda f: f if os.path.isabs(f) else os.path.join(base_dir,
f), convertFiles) | 24 convertFiles = map(lambda f: f if os.path.isabs(f) else os.path.join(baseDir,
f), convertFiles) |
25 converted = doRewrite(convertFiles, ['module=true', 'source_repo=https://hg.ad
blockplus.org/adblockplus/']) | 25 converted = doRewrite(convertFiles, ['module=true', 'source_repo=https://hg.ad
blockplus.org/adblockplus/']) |
26 print >>outHandle, toCString('adblockplus.js') + ',' | 26 print >>outHandle, toCString('adblockplus.js') + ',' |
27 print >>outHandle, toCString(converted) + ',' | 27 print >>outHandle, toCString(converted) + ',' |
28 | 28 |
29 print >>outHandle, '0, 0' | 29 print >>outHandle, '0, 0' |
30 print >>outHandle, '};' | 30 print >>outHandle, '};' |
31 | 31 |
32 if __name__ == '__main__': | 32 if __name__ == '__main__': |
33 args = sys.argv[1:] | 33 args = sys.argv[1:] |
34 outFile = args.pop() | 34 outFile = args.pop() |
35 | 35 |
36 verbatimFiles = [] | 36 verbatimFiles = [] |
37 while len(args): | 37 while len(args): |
38 file = args.pop() | 38 file = args.pop() |
39 if file == '--': | 39 if file == '--': |
40 break | 40 break |
41 else: | 41 else: |
42 verbatimFiles.insert(0, file) | 42 verbatimFiles.insert(0, file) |
43 | 43 |
44 convertFiles = args | 44 convertFiles = args |
45 convert(convertFiles, verbatimFiles, outFile) | 45 convert(convertFiles, verbatimFiles, outFile) |
LEFT | RIGHT |