OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # coding: utf-8 | 2 # coding: utf-8 |
3 | 3 |
| 4 import io |
4 import sys | 5 import sys |
5 import os | 6 import os |
6 import codecs | 7 import codecs |
7 import re | 8 import re |
8 import json | 9 import json |
9 import argparse | 10 import argparse |
10 import xml.dom.minidom as minidom | 11 import xml.dom.minidom as minidom |
11 | 12 |
12 baseDir = os.path.abspath(os.path.dirname(__file__)) | 13 jsTemplate = """require.scopes["%s"] = (function() { |
13 sys.path.append(os.path.join(baseDir, 'adblockpluscore', 'buildtools', 'jshydra'
)) | 14 let exports = {}; |
14 from abp_rewrite import doRewrite | 15 %s |
15 | 16 return exports; |
| 17 })();""" |
16 | 18 |
17 class CStringArray: | 19 class CStringArray: |
18 def __init__(self): | 20 def __init__(self): |
19 self._buffer = [] | 21 self._buffer = [] |
20 self._strings = [] | 22 self._strings = [] |
21 | 23 |
22 def add(self, string): | 24 def add(self, string): |
23 string = string.encode('utf-8').replace('\r', '') | 25 string = string.encode('utf-8').replace('\r', '') |
24 self._strings.append('std::string(buffer + %i, %i)' % (len(self._buffer)
, len(string))) | 26 self._strings.append('std::string(buffer + %i, %i)' % (len(self._buffer)
, len(string))) |
25 self._buffer.extend(map(lambda c: str(ord(c)), string)) | 27 self._buffer.extend(map(lambda c: str(ord(c)), string)) |
(...skipping 28 matching lines...) Expand all Loading... |
54 for name, value in node.attributes.items(): | 56 for name, value in node.attributes.items(): |
55 result[name] = value | 57 result[name] = value |
56 data.append(result) | 58 data.append(result) |
57 fileName = os.path.basename(file) | 59 fileName = os.path.basename(file) |
58 array.add(fileName) | 60 array.add(fileName) |
59 array.add('require.scopes["%s"] = %s;' % (fileName, json.dumps(data))) | 61 array.add('require.scopes["%s"] = %s;' % (fileName, json.dumps(data))) |
60 fileHandle.close() | 62 fileHandle.close() |
61 | 63 |
62 | 64 |
63 def convertJsFile(array, file): | 65 def convertJsFile(array, file): |
64 converted = doRewrite([os.path.abspath(file)], ['module=true', 'source_repo=
https://hg.adblockplus.org/adblockpluscore/']) | 66 with io.open(file, encoding="utf-8") as jsFile: |
65 array.add(os.path.basename(file)) | 67 jsFileContent = jsFile.read() |
66 array.add(converted) | 68 referenceFileName = os.path.basename(file) |
| 69 array.add(referenceFileName) |
| 70 array.add(jsTemplate % (re.sub("\\.jsm?$", "", referenceFileName), jsFileCon
tent)) |
67 | 71 |
68 | 72 |
69 def convert(verbatimBefore, convertFiles, verbatimAfter, outFile): | 73 def convert(verbatimBefore, convertFiles, verbatimAfter, outFile): |
70 array = CStringArray() | 74 array = CStringArray() |
71 addFilesVerbatim(array, verbatimBefore) | 75 addFilesVerbatim(array, verbatimBefore) |
72 | 76 |
73 for file in convertFiles: | 77 for file in convertFiles: |
74 if file.endswith('.xml'): | 78 if file.endswith('.xml'): |
75 convertXMLFile(array, file) | 79 convertXMLFile(array, file) |
76 else: | 80 else: |
(...skipping 10 matching lines...) Expand all Loading... |
87 parser.add_argument('--before', metavar='verbatim_file', nargs='+', | 91 parser.add_argument('--before', metavar='verbatim_file', nargs='+', |
88 help='JavaScript file to include verbatim at the beginni
ng') | 92 help='JavaScript file to include verbatim at the beginni
ng') |
89 parser.add_argument('--convert', metavar='file_to_convert', nargs='+', | 93 parser.add_argument('--convert', metavar='file_to_convert', nargs='+', |
90 help='JavaScript files to convert') | 94 help='JavaScript files to convert') |
91 parser.add_argument('--after', metavar='verbatim_file', nargs='+', | 95 parser.add_argument('--after', metavar='verbatim_file', nargs='+', |
92 help='JavaScript file to include verbatim at the end') | 96 help='JavaScript file to include verbatim at the end') |
93 parser.add_argument('output_file', | 97 parser.add_argument('output_file', |
94 help='output from the conversion') | 98 help='output from the conversion') |
95 args = parser.parse_args() | 99 args = parser.parse_args() |
96 convert(args.before, args.convert, args.after, args.output_file) | 100 convert(args.before, args.convert, args.after, args.output_file) |
OLD | NEW |