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, argparse |
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): | 17 def printFilesVerbatim(outHandle, files): |
18 for file in files: | 18 for file in files: |
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 def convert(verbatimBefore, convertFiles, verbatimAfter, outFile): | 24 def convert(verbatimBefore, convertFiles, verbatimAfter, outFile): |
25 outHandle = open(outFile, 'wb') | 25 outHandle = open(outFile, 'wb') |
26 print >>outHandle, 'const char* jsSources[] = {' | 26 print >>outHandle, 'const char* jsSources[] = {' |
27 | 27 |
28 printFilesVerbatim(outHandle, verbatimBefore) | 28 printFilesVerbatim(outHandle, verbatimBefore) |
29 | 29 |
30 convertFiles = map(lambda f: f if os.path.isabs(f) else os.path.join(baseDir,
f), convertFiles) | 30 # We should trust that gyp is giving us the correct path names. In case we can
't, however, the |
| 31 # following is commented out rather than removed. We can add an optional argum
ent for it if needed. |
| 32 #convertFiles = map(lambda f: f if os.path.isabs(f) else os.path.join(baseDir,
f), convertFiles) |
31 converted = doRewrite(convertFiles, ['module=true', 'source_repo=https://hg.ad
blockplus.org/adblockplus/']) | 33 converted = doRewrite(convertFiles, ['module=true', 'source_repo=https://hg.ad
blockplus.org/adblockplus/']) |
32 print >>outHandle, toCString('adblockplus.js') + ',' | 34 print >>outHandle, toCString('adblockplus.js') + ',' |
33 print >>outHandle, toCString(converted) + ',' | 35 print >>outHandle, toCString(converted) + ',' |
34 | 36 |
35 printFilesVerbatim(outHandle, verbatimAfter) | 37 printFilesVerbatim(outHandle, verbatimAfter) |
36 | 38 |
37 print >>outHandle, '0, 0' | 39 print >>outHandle, '0, 0' |
38 print >>outHandle, '};' | 40 print >>outHandle, '};' |
39 | 41 |
40 if __name__ == '__main__': | 42 if __name__ == '__main__': |
41 args = sys.argv[1:] | 43 parser = argparse.ArgumentParser(description='Convert JavaScript files') |
42 outFile = args.pop() | 44 parser.add_argument('--before', metavar='verbatim_file', nargs='+', |
43 | 45 help='JavaScript file to include verbatim at the beginning
') |
44 verbatimBefore = [] | 46 parser.add_argument('--convert', metavar='file_to_convert', nargs='+', |
45 verbatimAfter = [] | 47 help='JavaScript files to convert') |
46 convertFiles = [] | 48 parser.add_argument('--after', metavar='verbatim_file', nargs='+', |
47 for fileName in args: | 49 help='JavaScript file to include verbatim at the end') |
48 if fileName.startswith('before='): | 50 parser.add_argument('output_file', |
49 verbatimBefore.append(fileName.replace('before=', '')) | 51 help='output from the conversion') |
50 elif fileName.startswith('after='): | 52 args = parser.parse_args() |
51 verbatimAfter.append(fileName.replace('after=', '')) | 53 convert(args.before, args.convert, args.after, args.output_file) |
52 else: | |
53 convertFiles.append(fileName) | |
54 | |
55 convert(verbatimBefore, convertFiles, verbatimAfter, outFile) | |
OLD | NEW |