def __init__(self, zip_file, data='', certinfo=None): self.zip_file = zip_file self.certinfo = certinfo if not data: data = zip_file.read('manifest.json') # Remove BOM if present. data = unicodehelper.decode(data) # Run through the JSON and remove all comments, then try to read # the manifest file. # Note that Firefox and the WebExtension spec only allow for # line comments (starting with `//`), not block comments (starting with # `/*`). We strip out both in AMO because the linter will flag the # block-level comments explicitly as an error (so the developer can # change them to line-level comments). # # But block level comments are not allowed. We just flag them elsewhere # (in the linter). json_string = '' lexer = JsLexer() for name, token in lexer.lex(data): if name not in ('blockcomment', 'linecomment'): json_string += token try: self.data = json.loads(json_string) except Exception: raise InvalidManifest( gettext('Could not parse the manifest file.'))
def _do_test(path): """Performs a test on a JS file""" path = os.path.join(os.path.abspath(os.path.dirname(__file__)), path) with open(path, 'rb') as fobj: text = fobj.read() utext = unicodehelper.decode(text) assert utext == COMPARISON
def _do_test(path): """Performs a test on a JS file""" path = os.path.join(os.path.abspath(os.path.dirname(__file__)), path) with open(path, 'rb') as fobj: text = fobj.read() utext = unicodehelper.decode(text) assert utext == COMPARISON
def get_content(self, obj): commit = self._get_commit(obj) blob_or_tree = commit.tree[self.get_selected_file()] if blob_or_tree.type == 'blob': # TODO: Test if this is actually needed, historically it was # because files inside a zip could have any encoding but I'm not # sure if git unifies this to some degree (cgrebs) return unicodehelper.decode( self.git_repo[blob_or_tree.oid].read_raw())
def get_content(self, obj): commit = self._get_commit(obj) tree = self.repo.get_root_tree(commit) blob_or_tree = tree[self.get_selected_file(obj)] if blob_or_tree.type == 'blob': # TODO: Test if this is actually needed, historically it was # because files inside a zip could have any encoding but I'm not # sure if git unifies this to some degree (cgrebs) return unicodehelper.decode( self.git_repo[blob_or_tree.oid].read_raw())
def get_content(self, obj): blob, name = self._get_blob_for_selected_file(obj) if blob is not None: mimetype, mime_category = get_mime_type_for_blob( tree_or_blob='blob', name=name, blob=blob) # Only return the raw data if we detect a file that contains text # data that actually can be rendered. if mime_category == 'text': # Remove any BOM data if preset. return unicodehelper.decode(blob.read_raw()) # By default return an empty string. # See https://github.com/mozilla/addons-server/issues/11782 for # more explanation. return ''
def get_content(self, obj): commit = self._get_commit(obj) tree = self.repo.get_root_tree(commit) selected_file = self.get_selected_file(obj) blob_or_tree = tree[selected_file] if blob_or_tree.type == pygit2.GIT_OBJ_BLOB: blob = self.git_repo[blob_or_tree.oid] mimetype, mime_category = get_mime_type_for_blob( tree_or_blob='blob', name=blob_or_tree.name, blob=blob) # Only return the raw data if we detect a file that contains text # data that actually can be rendered. if mime_category == 'text': # Remove any BOM data if preset. return unicodehelper.decode(blob.read_raw()) # By default return an empty string. # See https://github.com/mozilla/addons-server/issues/11782 for # more explanation. return ''
def decode_json(json_string): """Helper that transparently handles BOM encoding.""" return json.loads(unicodehelper.decode(json_string))
def decode_json(json_string): """Helper that transparently handles BOM encoding.""" return json.loads(unicodehelper.decode(json_string))