| Left: | ||
| Right: | 
| LEFT | RIGHT | 
|---|---|
| 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 Loading... | |
| 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 import abp_rewrite | 150 if dirsep >= 0: | 
| 142 for file, sources in params['metadata'].items('convert_js'): | 151 # Not a top-level file, make sure it is inside an included directory | 
| 143 dirsep = file.find('/') | 152 dirname = file[0:dirsep] | 
| 144 if dirsep >= 0: | 153 if os.path.join(baseDir, dirname) not in getPackageFiles(params): | 
| 145 # Not a top-level file, make sure it is inside an included director | 154 continue | 
| 146 dirname = file[0:dirsep] | 155 | 
| 147 if os.path.join(baseDir, dirname) not in getPackageFiles(params): | 156 sourceFiles = re.split(r'\s+', sources) | 
| 148 continue | 157 args = [] | 
| 149 | 158 try: | 
| 150 sourceFiles = re.split(r'\s+', sources) | 159 argsStart = sourceFiles.index('--arg') | 
| 151 args = [] | 160 args = sourceFiles[argsStart + 1:] | 
| 152 try: | 161 sourceFiles = sourceFiles[0:argsStart] | 
| 153 argsStart = sourceFiles.index('--arg') | 162 except ValueError: | 
| 154 args = sourceFiles[argsStart + 1:] | 163 pass | 
| 155 sourceFiles = sourceFiles[0:argsStart] | 164 | 
| 156 except ValueError: | 165 sourceFiles = map(lambda f: os.path.abspath(os.path.join(baseDir, f)), sourc eFiles) | 
| 157 pass | 166 files[file] = doRewrite(sourceFiles, args) | 
| 158 | |
| 159 sourceFiles = map(lambda f: os.path.abspath(os.path.join(baseDir, f)), sou rceFiles) | |
| 160 files[file] = abp_rewrite.doRewrite(sourceFiles, args) | |
| 161 finally: | |
| 162 sys.path.remove(hydraDir) | |
| 163 | 167 | 
| 164 def packFiles(files): | 168 def packFiles(files): | 
| 165 buffer = StringIO() | 169 buffer = StringIO() | 
| 166 zip = ZipFile(buffer, 'w', ZIP_DEFLATED) | 170 zip = ZipFile(buffer, 'w', ZIP_DEFLATED) | 
| 167 for file, data in files.iteritems(): | 171 for file, data in files.iteritems(): | 
| 168 zip.writestr(file, data) | 172 zip.writestr(file, data) | 
| 169 zip.close() | 173 zip.close() | 
| 170 return buffer.getvalue() | 174 return buffer.getvalue() | 
| 171 | 175 | 
| 172 def signBinary(zipdata, keyFile): | 176 def signBinary(zipdata, keyFile): | 
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 215 'devenv': devenv, | 219 'devenv': devenv, | 
| 216 'metadata': metadata, | 220 'metadata': metadata, | 
| 217 } | 221 } | 
| 218 | 222 | 
| 219 files = {} | 223 files = {} | 
| 220 files['manifest.json'] = createManifest(params) | 224 files['manifest.json'] = createManifest(params) | 
| 221 for path in getPackageFiles(params): | 225 for path in getPackageFiles(params): | 
| 222 if os.path.exists(path): | 226 if os.path.exists(path): | 
| 223 readFile(params, files, path) | 227 readFile(params, files, path) | 
| 224 | 228 | 
| 225 if metadata.has_section('convert_js') and os.path.isdir(os.path.join(baseDir, 'jshydra')): | 229 if metadata.has_section('convert_js'): | 
| 226 convertJS(params, files) | 230 convertJS(params, files) | 
| 231 | |
| 232 if devenv: | |
| 233 files['devenvPoller__.js'] = createPoller(params) | |
| 227 | 234 | 
| 228 zipdata = packFiles(files) | 235 zipdata = packFiles(files) | 
| 229 signature = None | 236 signature = None | 
| 230 pubkey = None | 237 pubkey = None | 
| 231 if keyFile != None: | 238 if keyFile != None: | 
| 232 signature = signBinary(zipdata, keyFile) | 239 signature = signBinary(zipdata, keyFile) | 
| 233 pubkey = getPublicKey(keyFile) | 240 pubkey = getPublicKey(keyFile) | 
| 234 writePackage(outFile, pubkey, signature, zipdata) | 241 writePackage(outFile, pubkey, signature, zipdata) | 
| 235 | 242 | 
| 236 def createDevEnv(baseDir): | 243 def createDevEnv(baseDir): | 
| 237 fileBuffer = StringIO() | 244 fileBuffer = StringIO() | 
| 238 createBuild(baseDir, outFile=fileBuffer, devenv=True, releaseBuild=True) | 245 createBuild(baseDir, outFile=fileBuffer, devenv=True, releaseBuild=True) | 
| 239 zip = ZipFile(StringIO(fileBuffer.getvalue()), 'r') | 246 zip = ZipFile(StringIO(fileBuffer.getvalue()), 'r') | 
| 240 zip.extractall(os.path.join(baseDir, 'devenv')) | 247 zip.extractall(os.path.join(baseDir, 'devenv')) | 
| 241 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] | |
| LEFT | RIGHT |