Left: | ||
Right: |
OLD | NEW |
---|---|
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 Loading... | |
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 isignored(self, relpath): | |
kzar
2017/07/17 09:37:40
Nit: Should it be is_ignored? (Kind of a question
tlucas
2017/07/17 12:29:48
for the sake of readability i would agree. Acknowl
| |
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.isignored(name)): | |
kzar
2017/07/17 09:37:40
The indentation looks a bit funky here, but I'm ru
tlucas
2017/07/17 12:29:48
flake8 didn't nag about this at all - and from wha
kzar
2017/07/17 12:32:52
Fair enough, let's see what he thinks.
Sebastian Noack
2017/07/17 14:02:31
The following two styles are acceptable according
tlucas
2017/07/17 16:34:12
As discussed, outsourcing the logic to a variable
| |
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 # Adhere to given ignorance |
kzar
2017/07/17 09:37:40
I'm not sure this comment adds much, maybe just re
tlucas
2017/07/17 12:29:48
Acknowledged.
| |
119 if '/' in target and not self.isIncluded(target): | 118 if '/' in target and self.isignored(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() |
OLD | NEW |