示例#1
0
    def testBrotliLottie(self):
        test_data_root = util.PathFromRoot('grit/testdata')
        root = util.ParseGrdForUnittest('''
        <structures>
          <structure name="TEST_LOTTIE" file="test_json.json" type="lottie" compress="brotli" />
        </structures>''',
                                        base_dir=test_data_root)
        node, = root.GetChildrenOfType(structure.StructureNode)
        node.RunPreSubstitutionGatherer()
        # Using the mock brotli decompression executable.
        brotli_util.SetBrotliCommand([
            sys.executable,
            os.path.join(os.path.dirname(__file__), 'mock_brotli.py')
        ])
        data = node.GetDataPackValue(lang='en', encoding=util.BINARY)

        self.assertEqual('LOTTIE'.encode('utf-8'), data[0:6])
        self.assertEqual(constants.BROTLI_CONST, data[6:8])
        self.assertEqual(
            len(
                util.ReadFile(os.path.join(test_data_root, 'test_json.json'),
                              util.BINARY)),
            struct.unpack('<i', data[8:12])[0] +
            (struct.unpack('<h', data[12:14])[0] << 4 * 8))
        self.assertEqual(b'This has been mock compressed!', data[14:])
示例#2
0
    def testCompressBrotli(self):
        test_data_root = util.PathFromRoot('grit/testdata')
        root = util.ParseGrdForUnittest('''
        <structures>
          <structure name="TEST_TXT" file="test_text.txt"
                   compress="brotli" type="chrome_html" />
        </structures>''',
                                        base_dir=test_data_root)
        node, = root.GetChildrenOfType(structure.StructureNode)
        node.RunPreSubstitutionGatherer()

        # Using the mock brotli decompression executable.
        brotli_util.SetBrotliCommand([
            sys.executable,
            os.path.join(os.path.dirname(__file__), 'mock_brotli.py')
        ])
        compressed = node.GetDataPackValue(lang='en', encoding=util.BINARY)
        # Assert that the first two bytes in compressed format is BROTLI_CONST.
        self.assertEqual(constants.BROTLI_CONST, compressed[0:2])

        # Compare the actual size of the uncompressed test data with
        # the size appended during compression.
        actual_size = len(
            util.ReadFile(os.path.join(test_data_root, 'test_text.txt'),
                          util.BINARY))
        uncompress_size = struct.unpack('<i', compressed[2:6])[0]
        uncompress_size += struct.unpack('<h', compressed[6:8])[0] << 4 * 8
        self.assertEqual(actual_size, uncompress_size)

        self.assertEqual(b'This has been mock compressed!', compressed[8:])
示例#3
0
    def Run(self, opts, args):
        brotli_util.SetBrotliCommand(None)
        os.environ['cwd'] = os.getcwd()
        self.output_directory = '.'
        first_ids_file = None
        predetermined_ids_file = None
        whitelist_filenames = []
        assert_output_files = []
        target_platform = None
        depfile = None
        depdir = None
        whitelist_support = False
        write_only_new = False
        depend_on_stamp = False
        js_minifier = None
        replace_ellipsis = True
        (own_opts, args) = getopt.getopt(
            args, 'a:p:o:D:E:f:w:t:',
            ('depdir=', 'depfile=', 'assert-file-list=', 'help',
             'output-all-resource-defines', 'no-output-all-resource-defines',
             'no-replace-ellipsis', 'depend-on-stamp', 'js-minifier=',
             'write-only-new=', 'whitelist-support', 'brotli='))
        for (key, val) in own_opts:
            if key == '-a':
                assert_output_files.append(val)
            elif key == '--assert-file-list':
                with open(val) as f:
                    assert_output_files += f.read().splitlines()
            elif key == '-o':
                self.output_directory = val
            elif key == '-D':
                name, val = util.ParseDefine(val)
                self.defines[name] = val
            elif key == '-E':
                (env_name, env_value) = val.split('=', 1)
                os.environ[env_name] = env_value
            elif key == '-f':
                # TODO([email protected]): Remove this override once change
                # lands in WebKit.grd to specify the first_ids_file in the
                # .grd itself.
                first_ids_file = val
            elif key == '-w':
                whitelist_filenames.append(val)
            elif key == '--no-replace-ellipsis':
                replace_ellipsis = False
            elif key == '-p':
                predetermined_ids_file = val
            elif key == '-t':
                target_platform = val
            elif key == '--depdir':
                depdir = val
            elif key == '--depfile':
                depfile = val
            elif key == '--write-only-new':
                write_only_new = val != '0'
            elif key == '--depend-on-stamp':
                depend_on_stamp = True
            elif key == '--js-minifier':
                js_minifier = val
            elif key == '--whitelist-support':
                whitelist_support = True
            elif key == '--brotli':
                brotli_util.SetBrotliCommand([os.path.abspath(val)])
            elif key == '--help':
                self.ShowUsage()
                sys.exit(0)

        if len(args):
            print('This tool takes no tool-specific arguments.')
            return 2
        self.SetOptions(opts)
        self.VerboseOut(
            'Output directory: %s (absolute path: %s)\n' %
            (self.output_directory, os.path.abspath(self.output_directory)))

        if whitelist_filenames:
            self.whitelist_names = set()
            for whitelist_filename in whitelist_filenames:
                self.VerboseOut('Using whitelist: %s\n' % whitelist_filename)
                whitelist_contents = util.ReadFile(whitelist_filename,
                                                   util.RAW_TEXT)
                self.whitelist_names.update(
                    whitelist_contents.strip().split('\n'))

        if js_minifier:
            minifier.SetJsMinifier(js_minifier)

        self.write_only_new = write_only_new

        self.res = grd_reader.Parse(
            opts.input,
            debug=opts.extra_verbose,
            first_ids_file=first_ids_file,
            predetermined_ids_file=predetermined_ids_file,
            defines=self.defines,
            target_platform=target_platform)

        # Set an output context so that conditionals can use defines during the
        # gathering stage; we use a dummy language here since we are not outputting
        # a specific language.
        self.res.SetOutputLanguage('en')
        self.res.SetWhitelistSupportEnabled(whitelist_support)
        self.res.RunGatherers()

        # Replace ... with the single-character version. http://crbug.com/621772
        if replace_ellipsis:
            for node in self.res:
                if isinstance(node, message.MessageNode):
                    node.SetReplaceEllipsis(True)

        self.Process()

        if assert_output_files:
            if not self.CheckAssertedOutputFiles(assert_output_files):
                return 2

        if depfile and depdir:
            self.GenerateDepfile(depfile, depdir, first_ids_file,
                                 depend_on_stamp)

        return 0