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

Side by Side Diff: packager.py

Issue 29490563: Issue 3967 - Properly handle non-existing file-mappings (Closed)
Patch Set: Created July 17, 2017, 2:54 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 # This Source Code Form is subject to the terms of the Mozilla Public 1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this 2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
4 4
5 # Note: These are the base functions common to all packagers, the actual 5 # Note: These are the base functions common to all packagers, the actual
6 # packagers are implemented in packagerGecko and packagerChrome. 6 # packagers are implemented in packagerGecko and packagerChrome.
7 7
8 import sys 8 import sys
9 import os 9 import os
10 import re 10 import re
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 self.includedFiles = includedFiles 84 self.includedFiles = includedFiles
85 self.ignoredFiles = ignoredFiles 85 self.ignoredFiles = ignoredFiles
86 self.process = process 86 self.process = process
87 87
88 def __setitem__(self, key, value): 88 def __setitem__(self, key, value):
89 if self.process: 89 if self.process:
90 value = self.process(key, value) 90 value = self.process(key, value)
91 dict.__setitem__(self, key, value) 91 dict.__setitem__(self, key, value)
92 92
93 def isIncluded(self, relpath): 93 def isIncluded(self, relpath):
94 return relpath.split('/')[0] in self.includedFiles
95
96 def is_ignored(self, relpath):
94 parts = relpath.split('/') 97 parts = relpath.split('/')
95 if not parts[0] in self.includedFiles: 98 return any(part in self.ignoredFiles for part in parts)
96 return False
97 for part in parts:
98 if part in self.ignoredFiles:
99 return False
100 return True
101 99
102 def read(self, path, relpath='', skip=()): 100 def read(self, path, relpath='', skip=()):
103 if os.path.isdir(path): 101 if os.path.isdir(path):
104 for file in os.listdir(path): 102 for file in os.listdir(path):
105 name = relpath + ('/' if relpath != '' else '') + file 103 name = relpath + ('/' if relpath != '' else '') + file
106 if name not in skip and self.isIncluded(name): 104 if (name not in skip
105 and self.isIncluded(name) and not self.is_ignored(name)) :
107 self.read(os.path.join(path, file), name, skip) 106 self.read(os.path.join(path, file), name, skip)
108 else: 107 else:
109 with open(path, 'rb') as file: 108 with open(path, 'rb') as file:
110 if relpath in self: 109 if relpath in self:
111 print >>sys.stderr, 'Warning: File %s defined multiple times ' % relpath 110 print >>sys.stderr, 'Warning: File %s defined multiple times ' % relpath
112 self[relpath] = file.read() 111 self[relpath] = file.read()
113 112
114 def readMappedFiles(self, mappings): 113 def readMappedFiles(self, mappings):
115 for item in mappings: 114 for item in mappings:
116 target, source = item 115 target, source = item
117 116
118 # Make sure the file is inside an included directory 117 if '/' in target and self.is_ignored(target):
119 if '/' in target and not self.isIncluded(target):
120 continue 118 continue
119
121 parts = source.split('/') 120 parts = source.split('/')
122 path = os.path.join(os.path.dirname(item.source), *parts) 121 path = os.path.join(os.path.dirname(item.source), *parts)
123 if os.path.exists(path): 122 if os.path.exists(path):
124 self.read(path, target) 123 self.read(path, target)
125 else: 124 else:
126 print >>sys.stderr, "Warning: Mapped file %s doesn't exist" % so urce 125 print >>sys.stderr, "Warning: Mapped file %s doesn't exist" % so urce
127 126
128 def preprocess(self, filenames, params={}): 127 def preprocess(self, filenames, params={}):
129 import jinja2 128 import jinja2
130 env = jinja2.Environment() 129 env = jinja2.Environment()
131 130
132 for filename in filenames: 131 for filename in filenames:
133 env.autoescape = os.path.splitext(filename)[1].lower() in ('.html', '.xml') 132 env.autoescape = os.path.splitext(filename)[1].lower() in ('.html', '.xml')
134 template = env.from_string(self[filename].decode('utf-8')) 133 template = env.from_string(self[filename].decode('utf-8'))
135 self[filename] = template.render(params).encode('utf-8') 134 self[filename] = template.render(params).encode('utf-8')
136 135
137 def zip(self, outFile, sortKey=None, compression=zipfile.ZIP_DEFLATED): 136 def zip(self, outFile, sortKey=None, compression=zipfile.ZIP_DEFLATED):
138 with zipfile.ZipFile(outFile, 'w', compression) as zf: 137 with zipfile.ZipFile(outFile, 'w', compression) as zf:
139 for name in sorted(self, key=sortKey): 138 for name in sorted(self, key=sortKey):
140 zf.writestr(name, self[name]) 139 zf.writestr(name, self[name])
141 140
142 def zipToString(self, sortKey=None): 141 def zipToString(self, sortKey=None):
143 buffer = StringIO() 142 buffer = StringIO()
144 self.zip(buffer, sortKey=sortKey) 143 self.zip(buffer, sortKey=sortKey)
145 return buffer.getvalue() 144 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