Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: convert_js.py

Issue 9846017: Make JavaScript sources compile into the library; convert JavaScript files on the fly (Closed)
Left Patch Set: Created March 14, 2013, 10:02 p.m.
Right Patch Set: Review comments addressed Created March 15, 2013, 3:59 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « .hgsubstate ('k') | include/AdblockPlus/JsEngine.h » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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__))
Felix Dahlke 2013/03/15 15:18:36 Everything else is named using camel case, why not
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)
LEFTRIGHT

Powered by Google App Engine
This is Rietveld