import os
import re
from urllib.parse import urlparse
import mozpack.path as mozpath
from mozpack.chrome.flags import Flags
from mozpack.errors import errors
class ManifestEntry:
localized = False
type = None
allowed_flags = [
"application",
"platformversion",
"os",
"osversion",
"abi",
"xpcnativewrappers",
"tablet",
"process",
"contentaccessible",
"backgroundtask",
]
def __init__(self, base, *flags):
self.base = base
self.flags = Flags(*flags)
if not all(f in self.allowed_flags for f in self.flags):
errors.fatal(
"%s unsupported for %s manifest entries"
% (
",".join(f for f in self.flags if f not in self.allowed_flags),
self.type,
)
)
def serialize(self, *args):
entry = [self.type] + list(args)
flags = str(self.flags)
if flags:
entry.append(flags)
return " ".join(entry)
def __eq__(self, other):
return self.base == other.base and str(self) == str(other)
def __ne__(self, other):
return not self.__eq__(other)
def __repr__(self):
return "<%s@%s>" % (str(self), self.base)
def move(self, base):
return parse_manifest_line(base, str(self))
def rebase(self, base):
return self.move(base)
class ManifestEntryWithRelPath(ManifestEntry):
def __init__(self, base, relpath, *flags):
ManifestEntry.__init__(self, base, *flags)
self.relpath = relpath
def __str__(self):
return self.serialize(self.relpath)
def rebase(self, base):
clone = ManifestEntry.rebase(self, base)
clone.relpath = mozpath.rebase(self.base, base, self.relpath)
return clone
@property
def path(self):
return mozpath.normpath(mozpath.join(self.base, self.relpath))
class Manifest(ManifestEntryWithRelPath):
type = "manifest"
class ManifestChrome(ManifestEntryWithRelPath):
def __init__(self, base, name, relpath, *flags):
ManifestEntryWithRelPath.__init__(self, base, relpath, *flags)
self.name = name
@property
def location(self):
return mozpath.join(self.base, self.relpath)
class ManifestContent(ManifestChrome):
type = "content"
allowed_flags = ManifestChrome.allowed_flags + [
"contentaccessible",
"platform",
]
def __str__(self):
return self.serialize(self.name, self.relpath)
class ManifestMultiContent(ManifestChrome):
type = None
def __init__(self, base, name, id, relpath, *flags):
ManifestChrome.__init__(self, base, name, relpath, *flags)
self.id = id
def __str__(self):
return self.serialize(self.name, self.id, self.relpath)
class ManifestLocale(ManifestMultiContent):
localized = True
type = "locale"
class ManifestSkin(ManifestMultiContent):
type = "skin"
class ManifestOverload(ManifestEntry):
type = None
def __init__(self, base, overloaded, overload, *flags):
ManifestEntry.__init__(self, base, *flags)
self.overloaded = overloaded
self.overload = overload
def __str__(self):
return self.serialize(self.overloaded, self.overload)
class ManifestOverlay(ManifestOverload):
type = "overlay"
class ManifestStyle(ManifestOverload):
type = "style"
class ManifestOverride(ManifestOverload):
type = "override"
class ManifestResource(ManifestEntry):
type = "resource"
def __init__(self, base, name, target, *flags):
ManifestEntry.__init__(self, base, *flags)
self.name = name
self.target = target
def __str__(self):
return self.serialize(self.name, self.target)
def rebase(self, base):
u = urlparse(self.target)
if u.scheme and u.scheme != "jar":
return ManifestEntry.rebase(self, base)
clone = ManifestEntry.rebase(self, base)
clone.target = mozpath.rebase(self.base, base, self.target)
return clone
class ManifestBinaryComponent(ManifestEntryWithRelPath):
type = "binary-component"
class ManifestComponent(ManifestEntryWithRelPath):
type = "component"
def __init__(self, base, cid, file, *flags):
ManifestEntryWithRelPath.__init__(self, base, file, *flags)
self.cid = cid
def __str__(self):
return self.serialize(self.cid, self.relpath)
class ManifestInterfaces(ManifestEntryWithRelPath):
type = "interfaces"
class ManifestCategory(ManifestEntry):
type = "category"
def __init__(self, base, category, name, value, *flags):
ManifestEntry.__init__(self, base, *flags)
self.category = category
self.name = name
self.value = value
def __str__(self):
return self.serialize(self.category, self.name, self.value)
class ManifestContract(ManifestEntry):
type = "contract"
def __init__(self, base, contractID, cid, *flags):
ManifestEntry.__init__(self, base, *flags)
self.contractID = contractID
self.cid = cid
def __str__(self):
return self.serialize(self.contractID, self.cid)
MANIFESTS_TYPES = dict(
[
(c.type, c)
for c in globals().values()
if type(c) is type
and issubclass(c, ManifestEntry)
and hasattr(c, "type")
and c.type
]
)
MANIFEST_RE = re.compile(r"^#.*$")
def parse_manifest_line(base, line):
cmd = MANIFEST_RE.sub("", line).strip().split()
if not cmd:
return None
if not cmd[0] in MANIFESTS_TYPES:
return errors.fatal("Unknown manifest directive: %s" % cmd[0])
return MANIFESTS_TYPES[cmd[0]](base, *cmd[1:])
def parse_manifest(root, path, fileobj=None):
base = mozpath.dirname(path)
if root:
path = os.path.normpath(os.path.abspath(os.path.join(root, path)))
if not fileobj:
fileobj = open(path)
linenum = 0
for line in fileobj:
if isinstance(line, bytes):
line = line.decode()
linenum += 1
with errors.context(path, linenum):
e = parse_manifest_line(base, line)
if e:
yield e
def is_manifest(path):
return (
path.endswith(".manifest")
and not path.endswith(".CRT.manifest")
and not path.endswith(".exe.manifest")
and os.path.basename(path) != "cose.manifest"
)