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

Delta Between Two Patch Sets: packagerChrome.py

Issue 9051052: Changes to Chrome build process (Closed)
Left Patch Set: Fixed jshydra failures Created Dec. 20, 2012, 2:21 p.m.
Right Patch Set: Moved JSHydra dependency to build tools and added automatic devenv reloading Created Dec. 29, 2012, 8:14 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 | « manifest.json.tmpl ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 # coding: utf-8 1 # coding: utf-8
2 2
3 # This file is part of the Adblock Plus build tools, 3 # This file is part of the Adblock Plus build tools,
4 # Copyright (C) 2006-2012 Eyeo GmbH 4 # Copyright (C) 2006-2012 Eyeo GmbH
5 # 5 #
6 # Adblock Plus is free software: you can redistribute it and/or modify 6 # Adblock Plus is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License version 3 as 7 # it under the terms of the GNU General Public License version 3 as
8 # published by the Free Software Foundation. 8 # published by the Free Software Foundation.
9 # 9 #
10 # Adblock Plus is distributed in the hope that it will be useful, 10 # Adblock Plus is distributed in the hope that it will be useful,
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 if metadata.has_option('general', 'icons'): 72 if metadata.has_option('general', 'icons'):
73 icons = {} 73 icons = {}
74 iconsDir = baseDir 74 iconsDir = baseDir
75 for dir in metadata.get('general', 'icons').split('/')[0:-1]: 75 for dir in metadata.get('general', 'icons').split('/')[0:-1]:
76 iconsDir = os.path.join(iconsDir, dir) 76 iconsDir = os.path.join(iconsDir, dir)
77 77
78 prefix, suffix = metadata.get('general', 'icons').split('/')[-1].split('?', 1) 78 prefix, suffix = metadata.get('general', 'icons').split('/')[-1].split('?', 1)
79 for file in os.listdir(iconsDir): 79 for file in os.listdir(iconsDir):
80 path = os.path.join(iconsDir, file) 80 path = os.path.join(iconsDir, file)
81 if os.path.isfile(path) and file.startswith(prefix) and file.endswith(suff ix): 81 if os.path.isfile(path) and file.startswith(prefix) and file.endswith(suff ix):
82 size = file[len(prefix):-len(suffix)] 82 size = file[len(prefix):-len(suffix)]
Felix Dahlke 2013/01/11 12:24:16 How about using a regex here? if os.path.isfile(p
Wladimir Palant 2013/01/11 14:45:39 As soon as we add the required escaping (prefix an
Felix Dahlke 2013/01/11 14:47:55 Right, forgot about the escaping. I suppose it's b
83 if not re.search(r'\D', size): 83 if not re.search(r'\D', size):
84 icons[size] = os.path.relpath(path, baseDir).replace('\\', '/') 84 icons[size] = os.path.relpath(path, baseDir).replace('\\', '/')
85 85
86 templateData['icons'] = icons 86 templateData['icons'] = icons
87 87
88 if metadata.has_option('general', 'permissions'): 88 if metadata.has_option('general', 'permissions'):
89 templateData['permissions'] = re.split(r'\s+', metadata.get('general', 'perm issions')) 89 templateData['permissions'] = re.split(r'\s+', metadata.get('general', 'perm issions'))
90 if params['experimentalAPI']: 90 if params['experimentalAPI']:
91 templateData['permissions'].append('experimental') 91 templateData['permissions'].append('experimental')
92 92
93 if metadata.has_option('general', 'backgroundScripts'): 93 if metadata.has_option('general', 'backgroundScripts'):
94 templateData['backgroundScripts'] = re.split(r'\s+', metadata.get('general', 'backgroundScripts')) 94 templateData['backgroundScripts'] = re.split(r'\s+', metadata.get('general', 'backgroundScripts'))
95 if params['devenv']:
96 templateData['backgroundScripts'].append('devenvPoller__.js')
95 97
96 if metadata.has_option('general', 'webAccessible'): 98 if metadata.has_option('general', 'webAccessible'):
97 templateData['webAccessible'] = re.split(r'\s+', metadata.get('general', 'we bAccessible')) 99 templateData['webAccessible'] = re.split(r'\s+', metadata.get('general', 'we bAccessible'))
98 100
99 if metadata.has_section('contentScripts'): 101 if metadata.has_section('contentScripts'):
100 contentScripts = [] 102 contentScripts = []
101 for run_at, scripts in metadata.items('contentScripts'): 103 for run_at, scripts in metadata.items('contentScripts'):
102 contentScripts.append({ 104 contentScripts.append({
103 'matches': ['http://*/*', 'https://*/*'], 105 'matches': ['http://*/*', 'https://*/*'],
104 'js': re.split(r'\s+', scripts), 106 'js': re.split(r'\s+', scripts),
105 'run_at': run_at, 107 'run_at': run_at,
106 'all_frames': True, 108 'all_frames': True,
107 }) 109 })
108 templateData['contentScripts'] = contentScripts 110 templateData['contentScripts'] = contentScripts
109 111
110 manifest = template.render(templateData) 112 manifest = template.render(templateData)
111 113
112 # Normalize JSON structure 114 # Normalize JSON structure
113 licenseComment = re.compile(r'/\*.*?\*/', re.S) 115 licenseComment = re.compile(r'/\*.*?\*/', re.S)
114 data = json.loads(re.sub(licenseComment, '', manifest, 1)) 116 data = json.loads(re.sub(licenseComment, '', manifest, 1))
115 if '_dummy' in data: 117 if '_dummy' in data:
116 del data['_dummy'] 118 del data['_dummy']
117 manifest = json.dumps(data, sort_keys=True, indent=2) 119 manifest = json.dumps(data, sort_keys=True, indent=2)
118 120
119 return manifest.encode('utf-8') 121 return manifest.encode('utf-8')
122
123 def createPoller(params):
124 env = jinja2.Environment(loader=jinja2.FileSystemLoader(buildtools.__path__[0] ))
125 env.filters.update({'json': json.dumps})
126 template = env.get_template('chromeDevenvPoller__.js.tmpl')
127 return template.render(params).encode('utf-8');
120 128
121 def readFile(params, files, path): 129 def readFile(params, files, path):
122 ignoredFiles = getIgnoredFiles(params) 130 ignoredFiles = getIgnoredFiles(params)
123 if os.path.isdir(path): 131 if os.path.isdir(path):
124 for file in os.listdir(path): 132 for file in os.listdir(path):
125 if file in ignoredFiles: 133 if file in ignoredFiles:
126 continue 134 continue
127 readFile(params, files, os.path.join(path, file)) 135 readFile(params, files, os.path.join(path, file))
128 else: 136 else:
129 file = open(path, 'rb') 137 file = open(path, 'rb')
130 data = file.read() 138 data = file.read()
131 file.close() 139 file.close()
132 140
133 name = os.path.relpath(path, params['baseDir']).replace('\\', '/') 141 name = os.path.relpath(path, params['baseDir']).replace('\\', '/')
134 files[name] = data 142 files[name] = data
135 143
136 def convertJS(params, files): 144 def convertJS(params, files):
145 from jshydra.abp_rewrite import doRewrite
137 baseDir = params['baseDir'] 146 baseDir = params['baseDir']
138 hydraDir = os.path.join(baseDir, 'jshydra') 147
139 sys.path.append(hydraDir) 148 for file, sources in params['metadata'].items('convert_js'):
140 try: 149 dirsep = file.find('/')
141 if 'abp_rewrite' in sys.modules: 150 if dirsep >= 0:
142 import abp_rewrite 151 # Not a top-level file, make sure it is inside an included directory
143 reload(abp_rewrite.utils) 152 dirname = file[0:dirsep]
144 reload(abp_rewrite) 153 if os.path.join(baseDir, dirname) not in getPackageFiles(params):
145 else: 154 continue
146 import abp_rewrite 155
147 156 sourceFiles = re.split(r'\s+', sources)
148 for file, sources in params['metadata'].items('convert_js'): 157 args = []
149 dirsep = file.find('/') 158 try:
150 if dirsep >= 0: 159 argsStart = sourceFiles.index('--arg')
151 # Not a top-level file, make sure it is inside an included director 160 args = sourceFiles[argsStart + 1:]
152 dirname = file[0:dirsep] 161 sourceFiles = sourceFiles[0:argsStart]
153 if os.path.join(baseDir, dirname) not in getPackageFiles(params): 162 except ValueError:
154 continue 163 pass
155 164
156 sourceFiles = re.split(r'\s+', sources) 165 sourceFiles = map(lambda f: os.path.abspath(os.path.join(baseDir, f)), sourc eFiles)
157 args = [] 166 files[file] = doRewrite(sourceFiles, args)
158 try:
159 argsStart = sourceFiles.index('--arg')
160 args = sourceFiles[argsStart + 1:]
161 sourceFiles = sourceFiles[0:argsStart]
162 except ValueError:
163 pass
164
165 sourceFiles = map(lambda f: os.path.abspath(os.path.join(baseDir, f)), sou rceFiles)
166 files[file] = abp_rewrite.doRewrite(sourceFiles, args)
167 finally:
168 sys.path.remove(hydraDir)
169 167
170 def packFiles(files): 168 def packFiles(files):
171 buffer = StringIO() 169 buffer = StringIO()
172 zip = ZipFile(buffer, 'w', ZIP_DEFLATED) 170 zip = ZipFile(buffer, 'w', ZIP_DEFLATED)
173 for file, data in files.iteritems(): 171 for file, data in files.iteritems():
174 zip.writestr(file, data) 172 zip.writestr(file, data)
175 zip.close() 173 zip.close()
176 return buffer.getvalue() 174 return buffer.getvalue()
177 175
178 def signBinary(zipdata, keyFile): 176 def signBinary(zipdata, keyFile):
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 'devenv': devenv, 219 'devenv': devenv,
222 'metadata': metadata, 220 'metadata': metadata,
223 } 221 }
224 222
225 files = {} 223 files = {}
226 files['manifest.json'] = createManifest(params) 224 files['manifest.json'] = createManifest(params)
227 for path in getPackageFiles(params): 225 for path in getPackageFiles(params):
228 if os.path.exists(path): 226 if os.path.exists(path):
229 readFile(params, files, path) 227 readFile(params, files, path)
230 228
231 if metadata.has_section('convert_js') and os.path.isdir(os.path.join(baseDir, 'jshydra')): 229 if metadata.has_section('convert_js'):
232 convertJS(params, files) 230 convertJS(params, files)
231
232 if devenv:
233 files['devenvPoller__.js'] = createPoller(params)
233 234
234 zipdata = packFiles(files) 235 zipdata = packFiles(files)
235 signature = None 236 signature = None
236 pubkey = None 237 pubkey = None
237 if keyFile != None: 238 if keyFile != None:
238 signature = signBinary(zipdata, keyFile) 239 signature = signBinary(zipdata, keyFile)
239 pubkey = getPublicKey(keyFile) 240 pubkey = getPublicKey(keyFile)
240 writePackage(outFile, pubkey, signature, zipdata) 241 writePackage(outFile, pubkey, signature, zipdata)
241 242
242 def createDevEnv(baseDir): 243 def createDevEnv(baseDir):
243 fileBuffer = StringIO() 244 fileBuffer = StringIO()
244 createBuild(baseDir, outFile=fileBuffer, devenv=True, releaseBuild=True) 245 createBuild(baseDir, outFile=fileBuffer, devenv=True, releaseBuild=True)
245 zip = ZipFile(StringIO(fileBuffer.getvalue()), 'r') 246 zip = ZipFile(StringIO(fileBuffer.getvalue()), 'r')
246 zip.extractall(os.path.join(baseDir, 'devenv')) 247 zip.extractall(os.path.join(baseDir, 'devenv'))
247 zip.close() 248 zip.close()
249
250 print 'Development environment created, waiting for connections from active ex tensions...'
251 metadata = readMetadata(baseDir)
252 connections = [0]
253
254 import SocketServer, time, thread
255
256 class ConnectionHandler(SocketServer.BaseRequestHandler):
257 def handle(self):
258 connections[0] += 1
259 self.request.sendall('HTTP/1.0 OK\nConnection: close\n\n%s' % metadata.get ('general', 'basename'))
260
261 server = SocketServer.TCPServer(('localhost', 43816), ConnectionHandler)
262
263 def shutdown_server(server):
264 time.sleep(10)
265 server.shutdown()
266 thread.start_new_thread(shutdown_server, (server,))
267 server.serve_forever()
268
269 if connections[0] == 0:
270 print 'Warning: No incoming connections, extension probably not active in th e browser yet'
271 else:
272 print 'Handled %i connection(s)' % connections[0]
LEFTRIGHT

Powered by Google App Engine
This is Rietveld