Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 | 94 return relpath.split('/')[0] in self.includedFiles |
95 | 95 |
96 def isignored(self, relpath): | 96 def is_ignored(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
| |
97 parts = relpath.split('/') | 97 parts = relpath.split('/') |
98 return any([part in self.ignoredFiles for part in parts]) | 98 return any(part in self.ignoredFiles for part in parts) |
99 | 99 |
100 def read(self, path, relpath='', skip=()): | 100 def read(self, path, relpath='', skip=()): |
101 if os.path.isdir(path): | 101 if os.path.isdir(path): |
102 for file in os.listdir(path): | 102 for file in os.listdir(path): |
103 name = relpath + ('/' if relpath != '' else '') + file | 103 name = relpath + ('/' if relpath != '' else '') + file |
104 if (name not in skip | 104 |
105 and self.isIncluded(name) and not self.isignored(name)): | 105 included = self.isIncluded(name) and not self.is_ignored(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
| |
106 if name not in skip and included: | |
106 self.read(os.path.join(path, file), name, skip) | 107 self.read(os.path.join(path, file), name, skip) |
107 else: | 108 else: |
108 with open(path, 'rb') as file: | 109 with open(path, 'rb') as file: |
109 if relpath in self: | 110 if relpath in self: |
110 print >>sys.stderr, 'Warning: File %s defined multiple times ' % relpath | 111 print >>sys.stderr, 'Warning: File %s defined multiple times ' % relpath |
111 self[relpath] = file.read() | 112 self[relpath] = file.read() |
112 | 113 |
113 def readMappedFiles(self, mappings): | 114 def readMappedFiles(self, mappings): |
114 for item in mappings: | 115 for item in mappings: |
115 target, source = item | 116 target, source = item |
116 | 117 |
117 # Adhere to given ignorance | 118 if '/' in target and self.is_ignored(target): |
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.
| |
118 if '/' in target and self.isignored(target): | |
119 continue | 119 continue |
120 | 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() |
LEFT | RIGHT |