| 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, json, argparse | 4 import sys | 
|  | 5 import os | 
|  | 6 import codecs | 
|  | 7 import re | 
|  | 8 import json | 
|  | 9 import argparse | 
| 5 import xml.dom.minidom as minidom | 10 import xml.dom.minidom as minidom | 
|  | 11 | 
| 6 baseDir = os.path.abspath(os.path.dirname(__file__)) | 12 baseDir = os.path.abspath(os.path.dirname(__file__)) | 
| 7 sys.path.append(os.path.join(baseDir, 'adblockpluscore', 'buildtools', 'jshydra'
    )) | 13 sys.path.append(os.path.join(baseDir, 'adblockpluscore', 'buildtools', 'jshydra'
    )) | 
| 8 from abp_rewrite import doRewrite | 14 from abp_rewrite import doRewrite | 
| 9 | 15 | 
|  | 16 | 
| 10 class CStringArray: | 17 class CStringArray: | 
| 11   def __init__(self): | 18     def __init__(self): | 
| 12     self._buffer = [] | 19         self._buffer = [] | 
| 13     self._strings = [] | 20         self._strings = [] | 
| 14 | 21 | 
| 15   def add(self, string): | 22     def add(self, string): | 
| 16     string = string.encode('utf-8').replace('\r', '') | 23         string = string.encode('utf-8').replace('\r', '') | 
| 17     self._strings.append('std::string(buffer + %i, %i)' % (len(self._buffer), le
    n(string))) | 24         self._strings.append('std::string(buffer + %i, %i)' % (len(self._buffer)
    , len(string))) | 
| 18     self._buffer.extend(map(lambda c: str(ord(c)), string)) | 25         self._buffer.extend(map(lambda c: str(ord(c)), string)) | 
| 19 | 26 | 
| 20   def write(self, outHandle, arrayName): | 27     def write(self, outHandle, arrayName): | 
| 21     print >>outHandle, '#include <string>' | 28         print >>outHandle, '#include <string>' | 
| 22     print >>outHandle, 'namespace' | 29         print >>outHandle, 'namespace' | 
| 23     print >>outHandle, '{' | 30         print >>outHandle, '{' | 
| 24     print >>outHandle, '  const char buffer[] = {%s};' % ', '.join(self._buffer) | 31         print >>outHandle, '  const char buffer[] = {%s};' % ', '.join(self._buf
    fer) | 
| 25     print >>outHandle, '}' | 32         print >>outHandle, '}' | 
| 26     print >>outHandle, 'std::string %s[] = {%s, std::string()};' % (arrayName, '
    , '.join(self._strings)) | 33         print >>outHandle, 'std::string %s[] = {%s, std::string()};' % (arrayNam
    e, ', '.join(self._strings)) | 
|  | 34 | 
| 27 | 35 | 
| 28 def addFilesVerbatim(array, files): | 36 def addFilesVerbatim(array, files): | 
| 29   for file in files: | 37     for file in files: | 
|  | 38         fileHandle = codecs.open(file, 'rb', encoding='utf-8') | 
|  | 39         array.add(os.path.basename(file)) | 
|  | 40         array.add(fileHandle.read()) | 
|  | 41         fileHandle.close() | 
|  | 42 | 
|  | 43 | 
|  | 44 def convertXMLFile(array, file): | 
| 30     fileHandle = codecs.open(file, 'rb', encoding='utf-8') | 45     fileHandle = codecs.open(file, 'rb', encoding='utf-8') | 
| 31     array.add(os.path.basename(file)) | 46     doc = minidom.parse(file) | 
| 32     array.add(fileHandle.read()) |  | 
| 33     fileHandle.close() | 47     fileHandle.close() | 
| 34 | 48 | 
| 35 def convertXMLFile(array, file): | 49     data = [] | 
| 36   fileHandle = codecs.open(file, 'rb', encoding='utf-8') | 50     for node in doc.documentElement.childNodes: | 
| 37   doc = minidom.parse(file) | 51         if node.nodeType != node.ELEMENT_NODE: | 
| 38   fileHandle.close() | 52             continue | 
|  | 53         result = {'type': node.tagName} | 
|  | 54         for name, value in node.attributes.items(): | 
|  | 55             result[name] = value | 
|  | 56         data.append(result) | 
|  | 57         fileName = os.path.basename(file) | 
|  | 58     array.add(fileName) | 
|  | 59     array.add('require.scopes["%s"] = %s;' % (fileName, json.dumps(data))) | 
|  | 60     fileHandle.close() | 
| 39 | 61 | 
| 40   data = [] |  | 
| 41   for node in doc.documentElement.childNodes: |  | 
| 42     if node.nodeType != node.ELEMENT_NODE: |  | 
| 43       continue |  | 
| 44     result = {'type': node.tagName} |  | 
| 45     for name, value in node.attributes.items(): |  | 
| 46       result[name] = value |  | 
| 47     data.append(result) |  | 
| 48     fileName = os.path.basename(file) |  | 
| 49   array.add(fileName) |  | 
| 50   array.add('require.scopes["%s"] = %s;' % (fileName, json.dumps(data))) |  | 
| 51   fileHandle.close() |  | 
| 52 | 62 | 
| 53 def convertJsFile(array, file): | 63 def convertJsFile(array, file): | 
| 54   converted = doRewrite([os.path.abspath(file)], ['module=true', 'source_repo=ht
    tps://hg.adblockplus.org/adblockpluscore/']) | 64     converted = doRewrite([os.path.abspath(file)], ['module=true', 'source_repo=
    https://hg.adblockplus.org/adblockpluscore/']) | 
| 55   array.add(os.path.basename(file)) | 65     array.add(os.path.basename(file)) | 
| 56   array.add(converted) | 66     array.add(converted) | 
|  | 67 | 
| 57 | 68 | 
| 58 def convert(verbatimBefore, convertFiles, verbatimAfter, outFile): | 69 def convert(verbatimBefore, convertFiles, verbatimAfter, outFile): | 
| 59   array = CStringArray() | 70     array = CStringArray() | 
| 60   addFilesVerbatim(array, verbatimBefore) | 71     addFilesVerbatim(array, verbatimBefore) | 
| 61 | 72 | 
| 62   for file in convertFiles: | 73     for file in convertFiles: | 
| 63     if file.endswith('.xml'): | 74         if file.endswith('.xml'): | 
| 64       convertXMLFile(array, file) | 75             convertXMLFile(array, file) | 
| 65     else: | 76         else: | 
| 66       convertJsFile(array, file) | 77             convertJsFile(array, file) | 
| 67 | 78 | 
| 68   addFilesVerbatim(array, verbatimAfter) | 79     addFilesVerbatim(array, verbatimAfter) | 
| 69 | 80 | 
| 70   outHandle = open(outFile, 'wb') | 81     outHandle = open(outFile, 'wb') | 
| 71   array.write(outHandle, 'jsSources') | 82     array.write(outHandle, 'jsSources') | 
| 72   outHandle.close() | 83     outHandle.close() | 
| 73 | 84 | 
| 74 if __name__ == '__main__': | 85 if __name__ == '__main__': | 
| 75   parser = argparse.ArgumentParser(description='Convert JavaScript files') | 86     parser = argparse.ArgumentParser(description='Convert JavaScript files') | 
| 76   parser.add_argument('--before', metavar='verbatim_file', nargs='+', | 87     parser.add_argument('--before', metavar='verbatim_file', nargs='+', | 
| 77       help='JavaScript file to include verbatim at the beginning') | 88                         help='JavaScript file to include verbatim at the beginni
    ng') | 
| 78   parser.add_argument('--convert', metavar='file_to_convert', nargs='+', | 89     parser.add_argument('--convert', metavar='file_to_convert', nargs='+', | 
| 79       help='JavaScript files to convert') | 90                         help='JavaScript files to convert') | 
| 80   parser.add_argument('--after', metavar='verbatim_file', nargs='+', | 91     parser.add_argument('--after', metavar='verbatim_file', nargs='+', | 
| 81       help='JavaScript file to include verbatim at the end') | 92                         help='JavaScript file to include verbatim at the end') | 
| 82   parser.add_argument('output_file', | 93     parser.add_argument('output_file', | 
| 83       help='output from the conversion') | 94                         help='output from the conversion') | 
| 84   args = parser.parse_args() | 95     args = parser.parse_args() | 
| 85   convert(args.before, args.convert, args.after, args.output_file) | 96     convert(args.before, args.convert, args.after, args.output_file) | 
| LEFT | RIGHT | 
|---|