python/mozbuild/mozpack/packager/formats.py

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

michael@0 1 # This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 # License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
michael@0 4
michael@0 5 from mozpack.chrome.manifest import (
michael@0 6 Manifest,
michael@0 7 ManifestInterfaces,
michael@0 8 ManifestChrome,
michael@0 9 ManifestBinaryComponent,
michael@0 10 ManifestResource,
michael@0 11 )
michael@0 12 from urlparse import urlparse
michael@0 13 import mozpack.path
michael@0 14 from mozpack.files import (
michael@0 15 ManifestFile,
michael@0 16 XPTFile,
michael@0 17 )
michael@0 18 from mozpack.copier import (
michael@0 19 FileRegistry,
michael@0 20 Jarrer,
michael@0 21 )
michael@0 22
michael@0 23 STARTUP_CACHE_PATHS = [
michael@0 24 'jsloader',
michael@0 25 'jssubloader',
michael@0 26 ]
michael@0 27
michael@0 28 '''
michael@0 29 Formatters are classes receiving packaging instructions and creating the
michael@0 30 appropriate package layout.
michael@0 31
michael@0 32 There are three distinct formatters, each handling one of the different chrome
michael@0 33 formats:
michael@0 34 - flat: essentially, copies files from the source with the same file system
michael@0 35 layout. Manifests entries are grouped in a single manifest per directory,
michael@0 36 as well as XPT interfaces.
michael@0 37 - jar: chrome content is packaged in jar files.
michael@0 38 - omni: chrome content, modules, non-binary components, and many other
michael@0 39 elements are packaged in an omnijar file for each base directory.
michael@0 40
michael@0 41 The base interface provides the following methods:
michael@0 42 - add_base(path)
michael@0 43 Register a base directory for an application or GRE. Base directories
michael@0 44 usually contain a root manifest (manifests not included in any other
michael@0 45 manifest) named chrome.manifest.
michael@0 46 - add(path, content)
michael@0 47 Add the given content (BaseFile instance) at the given virtual path
michael@0 48 - add_interfaces(path, content)
michael@0 49 Add the given content (BaseFile instance) and link it to other
michael@0 50 interfaces in the parent directory of the given virtual path.
michael@0 51 - add_manifest(entry)
michael@0 52 Add a ManifestEntry.
michael@0 53 - contains(path)
michael@0 54 Returns whether the given virtual path is known of the formatter.
michael@0 55
michael@0 56 The virtual paths mentioned above are paths as they would be with a flat
michael@0 57 chrome.
michael@0 58
michael@0 59 Formatters all take a FileCopier instance they will fill with the packaged
michael@0 60 data.
michael@0 61 '''
michael@0 62
michael@0 63
michael@0 64 class FlatFormatter(object):
michael@0 65 '''
michael@0 66 Formatter for the flat package format.
michael@0 67 '''
michael@0 68 def __init__(self, copier):
michael@0 69 assert isinstance(copier, FileRegistry)
michael@0 70 self.copier = copier
michael@0 71 self._bases = ['']
michael@0 72 self._frozen_bases = False
michael@0 73
michael@0 74 def add_base(self, base):
michael@0 75 # Only allow to add a base directory before calls to _get_base()
michael@0 76 assert not self._frozen_bases
michael@0 77 if not base in self._bases:
michael@0 78 self._bases.append(base)
michael@0 79
michael@0 80 def _get_base(self, path):
michael@0 81 '''
michael@0 82 Return the deepest base directory containing the given path.
michael@0 83 '''
michael@0 84 self._frozen_bases = True
michael@0 85 return mozpack.path.basedir(path, self._bases)
michael@0 86
michael@0 87 def add(self, path, content):
michael@0 88 self.copier.add(path, content)
michael@0 89
michael@0 90 def add_manifest(self, entry):
michael@0 91 # Store manifest entries in a single manifest per directory, named
michael@0 92 # after their parent directory, except for root manifests, all named
michael@0 93 # chrome.manifest.
michael@0 94 base = self._get_base(entry.base)
michael@0 95 if entry.base == base:
michael@0 96 name = 'chrome'
michael@0 97 else:
michael@0 98 name = mozpack.path.basename(entry.base)
michael@0 99 path = mozpack.path.normpath(mozpack.path.join(entry.base,
michael@0 100 '%s.manifest' % name))
michael@0 101 if not self.copier.contains(path):
michael@0 102 assert mozpack.path.basedir(entry.base, [base]) == base
michael@0 103 # Add a reference to the manifest file in the parent manifest, if
michael@0 104 # the manifest file is not a root manifest.
michael@0 105 if len(entry.base) > len(base):
michael@0 106 parent = mozpack.path.dirname(entry.base)
michael@0 107 relbase = mozpack.path.basename(entry.base)
michael@0 108 relpath = mozpack.path.join(relbase,
michael@0 109 mozpack.path.basename(path))
michael@0 110 FlatFormatter.add_manifest(self, Manifest(parent, relpath))
michael@0 111 self.copier.add(path, ManifestFile(entry.base))
michael@0 112 self.copier[path].add(entry)
michael@0 113
michael@0 114 def add_interfaces(self, path, content):
michael@0 115 # Interfaces in the same directory are all linked together in an
michael@0 116 # interfaces.xpt file.
michael@0 117 interfaces_path = mozpack.path.join(mozpack.path.dirname(path),
michael@0 118 'interfaces.xpt')
michael@0 119 if not self.copier.contains(interfaces_path):
michael@0 120 FlatFormatter.add_manifest(self, ManifestInterfaces(
michael@0 121 mozpack.path.dirname(path), 'interfaces.xpt'))
michael@0 122 self.copier.add(interfaces_path, XPTFile())
michael@0 123 self.copier[interfaces_path].add(content)
michael@0 124
michael@0 125 def contains(self, path):
michael@0 126 assert '*' not in path
michael@0 127 return self.copier.contains(path)
michael@0 128
michael@0 129
michael@0 130 class JarFormatter(FlatFormatter):
michael@0 131 '''
michael@0 132 Formatter for the jar package format. Assumes manifest entries related to
michael@0 133 chrome are registered before the chrome data files are added. Also assumes
michael@0 134 manifest entries for resources are registered after chrome manifest
michael@0 135 entries.
michael@0 136 '''
michael@0 137 def __init__(self, copier, compress=True, optimize=True):
michael@0 138 FlatFormatter.__init__(self, copier)
michael@0 139 self._chrome = set()
michael@0 140 self._frozen_chrome = False
michael@0 141 self._compress = compress
michael@0 142 self._optimize = optimize
michael@0 143
michael@0 144 def _chromepath(self, path):
michael@0 145 '''
michael@0 146 Return the chrome base directory under which the given path is. Used to
michael@0 147 detect under which .jar (if any) the path should go.
michael@0 148 '''
michael@0 149 self._frozen_chrome = True
michael@0 150 return mozpack.path.basedir(path, self._chrome)
michael@0 151
michael@0 152 def add(self, path, content):
michael@0 153 chrome = self._chromepath(path)
michael@0 154 if chrome:
michael@0 155 jar = chrome + '.jar'
michael@0 156 if not self.copier.contains(jar):
michael@0 157 self.copier.add(jar, Jarrer(self._compress, self._optimize))
michael@0 158 if not self.copier[jar].contains(mozpack.path.relpath(path,
michael@0 159 chrome)):
michael@0 160 self.copier[jar].add(mozpack.path.relpath(path, chrome),
michael@0 161 content)
michael@0 162 else:
michael@0 163 FlatFormatter.add(self, path, content)
michael@0 164
michael@0 165 def _jarize(self, entry, relpath):
michael@0 166 '''
michael@0 167 Transform a manifest entry in one pointing to chrome data in a jar.
michael@0 168 Return the corresponding chrome path and the new entry.
michael@0 169 '''
michael@0 170 base = entry.base
michael@0 171 basepath = mozpack.path.split(relpath)[0]
michael@0 172 chromepath = mozpack.path.join(base, basepath)
michael@0 173 entry = entry.rebase(chromepath) \
michael@0 174 .move(mozpack.path.join(base, 'jar:%s.jar!' % basepath)) \
michael@0 175 .rebase(base)
michael@0 176 return chromepath, entry
michael@0 177
michael@0 178 def add_manifest(self, entry):
michael@0 179 if isinstance(entry, ManifestChrome) and \
michael@0 180 not urlparse(entry.relpath).scheme:
michael@0 181 chromepath, entry = self._jarize(entry, entry.relpath)
michael@0 182 assert not self._frozen_chrome
michael@0 183 self._chrome.add(chromepath)
michael@0 184 elif isinstance(entry, ManifestResource) and \
michael@0 185 not urlparse(entry.target).scheme:
michael@0 186 chromepath, new_entry = self._jarize(entry, entry.target)
michael@0 187 if chromepath in self._chrome:
michael@0 188 entry = new_entry
michael@0 189 FlatFormatter.add_manifest(self, entry)
michael@0 190
michael@0 191 def contains(self, path):
michael@0 192 assert '*' not in path
michael@0 193 chrome = self._chromepath(path)
michael@0 194 if not chrome:
michael@0 195 return self.copier.contains(path)
michael@0 196 if not self.copier.contains(chrome + '.jar'):
michael@0 197 return False
michael@0 198 return self.copier[chrome + '.jar']. \
michael@0 199 contains(mozpack.path.relpath(path, chrome))
michael@0 200
michael@0 201
michael@0 202 class OmniJarFormatter(FlatFormatter):
michael@0 203 '''
michael@0 204 Formatter for the omnijar package format.
michael@0 205 '''
michael@0 206 def __init__(self, copier, omnijar_name, compress=True, optimize=True,
michael@0 207 non_resources=[]):
michael@0 208 FlatFormatter.__init__(self, copier)
michael@0 209 self.omnijars = {}
michael@0 210 self._omnijar_name = omnijar_name
michael@0 211 self._compress = compress
michael@0 212 self._optimize = optimize
michael@0 213 self._non_resources = non_resources
michael@0 214
michael@0 215 def _get_omnijar(self, path, create=True):
michael@0 216 '''
michael@0 217 Return the omnijar corresponding to the given path, its base directory
michael@0 218 and the path translated to be under the omnijar..
michael@0 219 '''
michael@0 220 base = self._get_base(path)
michael@0 221 if not base in self.omnijars:
michael@0 222 if not create:
michael@0 223 return None, '', path
michael@0 224 omnijar = Jarrer(self._compress, self._optimize)
michael@0 225 self.omnijars[base] = FlatFormatter(omnijar)
michael@0 226 self.copier.add(mozpack.path.join(base, self._omnijar_name),
michael@0 227 omnijar)
michael@0 228 return self.omnijars[base], base, mozpack.path.relpath(path, base)
michael@0 229
michael@0 230 def add(self, path, content):
michael@0 231 if self.is_resource(path):
michael@0 232 formatter, base, path = self._get_omnijar(path)
michael@0 233 else:
michael@0 234 formatter = self
michael@0 235 FlatFormatter.add(formatter, path, content)
michael@0 236
michael@0 237 def add_manifest(self, entry):
michael@0 238 if isinstance(entry, ManifestBinaryComponent):
michael@0 239 formatter, base = self, ''
michael@0 240 else:
michael@0 241 formatter, base, path = self._get_omnijar(entry.base)
michael@0 242 entry = entry.move(mozpack.path.relpath(entry.base, base))
michael@0 243 FlatFormatter.add_manifest(formatter, entry)
michael@0 244
michael@0 245 def add_interfaces(self, path, content):
michael@0 246 formatter, base, path = self._get_omnijar(path)
michael@0 247 FlatFormatter.add_interfaces(formatter, path, content)
michael@0 248
michael@0 249 def contains(self, path):
michael@0 250 assert '*' not in path
michael@0 251 if self.copier.contains(path):
michael@0 252 return True
michael@0 253 for base, copier in self.omnijars.iteritems():
michael@0 254 if copier.contains(mozpack.path.relpath(path, base)):
michael@0 255 return True
michael@0 256 return False
michael@0 257
michael@0 258 def is_resource(self, path):
michael@0 259 '''
michael@0 260 Return whether the given path corresponds to a resource to be put in an
michael@0 261 omnijar archive.
michael@0 262 '''
michael@0 263 base = self._get_base(path)
michael@0 264 path = mozpack.path.relpath(path, base)
michael@0 265 if any(mozpack.path.match(path, p.replace('*', '**'))
michael@0 266 for p in self._non_resources):
michael@0 267 return False
michael@0 268 path = mozpack.path.split(path)
michael@0 269 if path[0] == 'chrome':
michael@0 270 return len(path) == 1 or path[1] != 'icons'
michael@0 271 if path[0] == 'components':
michael@0 272 return path[-1].endswith('.js')
michael@0 273 if path[0] == 'res':
michael@0 274 return len(path) == 1 or \
michael@0 275 (path[1] != 'cursors' and path[1] != 'MainMenu.nib')
michael@0 276 if path[0] == 'defaults':
michael@0 277 return len(path) != 3 or \
michael@0 278 not (path[2] == 'channel-prefs.js' and
michael@0 279 path[1] in ['pref', 'preferences'])
michael@0 280 return path[0] in [
michael@0 281 'modules',
michael@0 282 'greprefs.js',
michael@0 283 'hyphenation',
michael@0 284 'update.locale',
michael@0 285 ] or path[0] in STARTUP_CACHE_PATHS

mercurial