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, 4:24 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
105 included = self.isIncluded(name) and not self.is_ignored(name)
106 if name not in skip and included:
107 self.read(os.path.join(path, file), name, skip) 107 self.read(os.path.join(path, file), name, skip)
108 else: 108 else:
109 with open(path, 'rb') as file: 109 with open(path, 'rb') as file:
110 if relpath in self: 110 if relpath in self:
111 print >>sys.stderr, 'Warning: File %s defined multiple times ' % relpath 111 print >>sys.stderr, 'Warning: File %s defined multiple times ' % relpath
112 self[relpath] = file.read() 112 self[relpath] = file.read()
113 113
114 def readMappedFiles(self, mappings): 114 def readMappedFiles(self, mappings):
115 for item in mappings: 115 for item in mappings:
116 target, source = item 116 target, source = item
117 117
118 # Make sure the file is inside an included directory 118 if '/' in target and self.is_ignored(target):
119 if '/' in target and not self.isIncluded(target):
120 continue 119 continue
120
121 parts = source.split('/') 121 parts = source.split('/')
122 path = os.path.join(os.path.dirname(item.source), *parts) 122 path = os.path.join(os.path.dirname(item.source), *parts)
123 if os.path.exists(path): 123 if os.path.exists(path):
124 self.read(path, target) 124 self.read(path, target)
125 else: 125 else:
126 print >>sys.stderr, "Warning: Mapped file %s doesn't exist" % so urce 126 print >>sys.stderr, "Warning: Mapped file %s doesn't exist" % so urce
127 127
128 def preprocess(self, filenames, params={}): 128 def preprocess(self, filenames, params={}):
129 import jinja2 129 import jinja2
130 env = jinja2.Environment() 130 env = jinja2.Environment()
131 131
132 for filename in filenames: 132 for filename in filenames:
133 env.autoescape = os.path.splitext(filename)[1].lower() in ('.html', '.xml') 133 env.autoescape = os.path.splitext(filename)[1].lower() in ('.html', '.xml')
134 template = env.from_string(self[filename].decode('utf-8')) 134 template = env.from_string(self[filename].decode('utf-8'))
135 self[filename] = template.render(params).encode('utf-8') 135 self[filename] = template.render(params).encode('utf-8')
136 136
137 def zip(self, outFile, sortKey=None, compression=zipfile.ZIP_DEFLATED): 137 def zip(self, outFile, sortKey=None, compression=zipfile.ZIP_DEFLATED):
138 with zipfile.ZipFile(outFile, 'w', compression) as zf: 138 with zipfile.ZipFile(outFile, 'w', compression) as zf:
139 for name in sorted(self, key=sortKey): 139 for name in sorted(self, key=sortKey):
140 zf.writestr(name, self[name]) 140 zf.writestr(name, self[name])
141 141
142 def zipToString(self, sortKey=None): 142 def zipToString(self, sortKey=None):
143 buffer = StringIO() 143 buffer = StringIO()
144 self.zip(buffer, sortKey=sortKey) 144 self.zip(buffer, sortKey=sortKey)
145 return buffer.getvalue() 145 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