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

Side by Side Diff: packager.py

Issue 9199007: Unified in-memory file processing between Gecko and Chrome (Closed)
Patch Set: Created Jan. 14, 2013, 11:14 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | packagerChrome.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details. 13 # GNU General Public License for more details.
14 # 14 #
15 # You should have received a copy of the GNU General Public License 15 # You should have received a copy of the GNU General Public License
16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
17 17
18 # Note: These are the base functions common to all packagers, the actual 18 # Note: These are the base functions common to all packagers, the actual
19 # packagers are implemented in packagerGecko and packagerChrome. 19 # packagers are implemented in packagerGecko and packagerChrome.
20 20
21 import os, re, codecs, subprocess, json, jinja2 21 import os, re, codecs, subprocess, json, zipfile, jinja2
22 from StringIO import StringIO
23 from ConfigParser import SafeConfigParser
24
22 import buildtools 25 import buildtools
23 from ConfigParser import SafeConfigParser
24 26
25 def getDefaultFileName(baseDir, metadata, version, ext): 27 def getDefaultFileName(baseDir, metadata, version, ext):
26 return os.path.join(baseDir, '%s-%s.%s' % (metadata.get('general', 'basename') , version, ext)) 28 return os.path.join(baseDir, '%s-%s.%s' % (metadata.get('general', 'basename') , version, ext))
27 29
28 def getMetadataPath(baseDir): 30 def getMetadataPath(baseDir):
29 return os.path.join(baseDir, 'metadata') 31 return os.path.join(baseDir, 'metadata')
30 32
31 def readMetadata(baseDir): 33 def readMetadata(baseDir):
32 metadata = SafeConfigParser() 34 metadata = SafeConfigParser()
33 metadata.optionxform = str 35 metadata.optionxform = str
(...skipping 24 matching lines...) Expand all
58 return version 60 return version
59 61
60 def getTemplate(template, autoEscape=False): 62 def getTemplate(template, autoEscape=False):
61 templatePath = buildtools.__path__[0] 63 templatePath = buildtools.__path__[0]
62 if autoEscape: 64 if autoEscape:
63 env = jinja2.Environment(loader=jinja2.FileSystemLoader(templatePath), autoe scape=True, extensions=['jinja2.ext.autoescape']) 65 env = jinja2.Environment(loader=jinja2.FileSystemLoader(templatePath), autoe scape=True, extensions=['jinja2.ext.autoescape'])
64 else: 66 else:
65 env = jinja2.Environment(loader=jinja2.FileSystemLoader(templatePath)) 67 env = jinja2.Environment(loader=jinja2.FileSystemLoader(templatePath))
66 env.filters.update({'json': json.dumps}) 68 env.filters.update({'json': json.dumps})
67 return env.get_template(template) 69 return env.get_template(template)
70
71 class Files(dict):
72 def __init__(self, includedFiles, ignoredFiles, process=None):
73 self.includedFiles = includedFiles
74 self.ignoredFiles = ignoredFiles
75 self.process = process
76
77 def isIncluded(self, relpath):
78 parts = relpath.split('/')
79 if not parts[0] in self.includedFiles:
80 return False
81 for part in parts:
82 if part in self.ignoredFiles:
83 return False
84 return True
85
86 def read(self, path, relpath='', skip=None):
87 if os.path.isdir(path):
88 for file in os.listdir(path):
89 name = relpath + ('/' if relpath != '' else '') + file
90 if (skip == None or file not in skip) and self.isIncluded(name):
91 self.read(os.path.join(path, file), name)
92 else:
93 file = open(path, 'rb')
94 self[relpath] = file.read()
95 file.close()
96
97 def zip(self, outFile, sortKey=None):
98 zip = zipfile.ZipFile(outFile, 'w', zipfile.ZIP_DEFLATED)
99 names = self.keys()
100 names.sort(key=sortKey)
101 for name in names:
102 data = self[name]
103 if self.process:
104 data = self.process(name, data)
105 zip.writestr(name, data)
106 zip.close()
107
108 def zipToString(self, sortKey=None):
109 buffer = StringIO()
110 self.zip(buffer, sortKey=sortKey)
111 return buffer.getvalue()
OLDNEW
« no previous file with comments | « no previous file | packagerChrome.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld