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

Side by Side Diff: packager.py

Issue 29340721: Issue 3967 - Create directories for mapped files (Closed)
Patch Set: Created April 21, 2016, 3:37 p.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 | no next file » | 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 Source Code Form is subject to the terms of the Mozilla Public 3 # This Source Code Form is subject to the terms of the Mozilla Public
4 # License, v. 2.0. If a copy of the MPL was not distributed with this 4 # License, v. 2.0. If a copy of the MPL was not distributed with this
5 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 6
7 # Note: These are the base functions common to all packagers, the actual 7 # Note: These are the base functions common to all packagers, the actual
8 # packagers are implemented in packagerGecko and packagerChrome. 8 # packagers are implemented in packagerGecko and packagerChrome.
9 9
10 import sys 10 import sys
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 self.includedFiles = includedFiles 87 self.includedFiles = includedFiles
88 self.ignoredFiles = ignoredFiles 88 self.ignoredFiles = ignoredFiles
89 self.process = process 89 self.process = process
90 90
91 def __setitem__(self, key, value): 91 def __setitem__(self, key, value):
92 if self.process: 92 if self.process:
93 value = self.process(key, value) 93 value = self.process(key, value)
94 dict.__setitem__(self, key, value) 94 dict.__setitem__(self, key, value)
95 95
96 def isIncluded(self, relpath): 96 def isIncluded(self, relpath):
97 return relpath.split('/')[0] in self.includedFiles
98
99 def isIgnored(self, relpath):
Sebastian Noack 2016/05/12 12:00:24 Nit: New names should be lowercase.
97 parts = relpath.split('/') 100 parts = relpath.split('/')
98 if not parts[0] in self.includedFiles:
99 return False
100 for part in parts: 101 for part in parts:
101 if part in self.ignoredFiles: 102 if part in self.ignoredFiles:
102 return False 103 return True
103 return True 104 return False
104 105
105 def read(self, path, relpath='', skip=()): 106 def read(self, path, relpath='', skip=()):
106 if os.path.isdir(path): 107 if os.path.isdir(path):
107 for file in os.listdir(path): 108 for file in os.listdir(path):
108 name = relpath + ('/' if relpath != '' else '') + file 109 name = relpath + ('/' if relpath != '' else '') + file
109 if name not in skip and self.isIncluded(name): 110 if (name not in skip and
111 self.isIncluded(name) and not self.isIgnored(name)):
Sebastian Noack 2016/05/12 12:00:24 Nit: The indentation here isn't compliant with PEP
110 self.read(os.path.join(path, file), name, skip) 112 self.read(os.path.join(path, file), name, skip)
111 else: 113 else:
112 with open(path, 'rb') as file: 114 with open(path, 'rb') as file:
113 if relpath in self: 115 if relpath in self:
114 print >>sys.stderr, 'Warning: File %s defined multiple times ' % relpath 116 print >>sys.stderr, 'Warning: File %s defined multiple times ' % relpath
115 self[relpath] = file.read() 117 self[relpath] = file.read()
116 118
117 def readMappedFiles(self, mappings): 119 def readMappedFiles(self, mappings):
118 for item in mappings: 120 for item in mappings:
119 target, source = item 121 target, source = item
120 122
121 # Make sure the file is inside an included directory 123 if '/' in target:
122 if '/' in target and not self.isIncluded(target): 124 # Make sure the file isn't ignored
123 continue 125 if self.isIgnored(target):
126 continue
127 # Make sure the directory is included
128 if not self.isIncluded(target):
129 self.includedFiles.add(target.split('/')[0])
Sebastian Noack 2016/05/12 12:00:24 Why is it necessary to add it add the directory to
130
124 parts = source.split('/') 131 parts = source.split('/')
125 path = os.path.join(os.path.dirname(item.source), *parts) 132 path = os.path.join(os.path.dirname(item.source), *parts)
126 if os.path.exists(path): 133 if os.path.exists(path):
127 self.read(path, target) 134 self.read(path, target)
128 else: 135 else:
129 print >>sys.stderr, 'Warning: Mapped file %s doesn\'t exist' % s ource 136 print >>sys.stderr, 'Warning: Mapped file %s doesn\'t exist' % s ource
130 137
131 def preprocess(self, filenames, params={}): 138 def preprocess(self, filenames, params={}):
132 import jinja2 139 import jinja2
133 env = jinja2.Environment() 140 env = jinja2.Environment()
134 141
135 for filename in filenames: 142 for filename in filenames:
136 env.autoescape = os.path.splitext(filename)[1].lower() in ('.html', '.xml') 143 env.autoescape = os.path.splitext(filename)[1].lower() in ('.html', '.xml')
137 template = env.from_string(self[filename].decode('utf-8')) 144 template = env.from_string(self[filename].decode('utf-8'))
138 self[filename] = template.render(params).encode('utf-8') 145 self[filename] = template.render(params).encode('utf-8')
139 146
140 def zip(self, outFile, sortKey=None): 147 def zip(self, outFile, sortKey=None):
141 zip = zipfile.ZipFile(outFile, 'w', zipfile.ZIP_DEFLATED) 148 zip = zipfile.ZipFile(outFile, 'w', zipfile.ZIP_DEFLATED)
142 names = self.keys() 149 names = self.keys()
143 names.sort(key=sortKey) 150 names.sort(key=sortKey)
144 for name in names: 151 for name in names:
145 zip.writestr(name, self[name]) 152 zip.writestr(name, self[name])
146 zip.close() 153 zip.close()
147 154
148 def zipToString(self, sortKey=None): 155 def zipToString(self, sortKey=None):
149 buffer = StringIO() 156 buffer = StringIO()
150 self.zip(buffer, sortKey=sortKey) 157 self.zip(buffer, sortKey=sortKey)
151 return buffer.getvalue() 158 return buffer.getvalue()
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld