File: package_initializer.py

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (131 lines) | stat: -rw-r--r-- 5,094 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# Copyright 2020 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import os.path
import web_idl

from . import style_format
from .codegen_tracing import CodeGenTracing
from .path_manager import PathManager
from .union_name_mapper import UnionNameMapper

def init(**kwargs):
    """
    Initializes this package.  See PackageInitializer.__init__ for details
    about the arguments.
    """
    the_instance = PackageInitializer(**kwargs)
    the_instance.init()
    assert the_instance is PackageInitializer.the_instance()


def package_initializer():
    """
    Returns the instance of PackageInitializer that actually initialized this
    package.
    """
    the_instance = PackageInitializer.the_instance()
    assert the_instance
    return the_instance


class PackageInitializer(object):
    """
    PackageInitializer is designed to support 'multiprocessing' package so that
    users can initialize this package in another process with the same
    settings.

    When the 'start method' of 'multiprocessing' package is 'spawn', the global
    environment (e.g. module variables, class variables, etc.) will not be
    inherited.  See also https://docs.python.org/3/library/multiprocessing.html

    PackageInitializer helps reproduce the same runtime environment of this
    process in other processes.  PackageInitializer.init() initializes this
    package in the same way as it was originally initialized iff the current
    process' runtime environment has not yet been initialized.  In other words,
    PackageInitializer.init() works with any start method of multiprocessing
    package.
    """

    # The instance of PackageInitializer that actually initialized this
    # package.
    _the_instance = None

    # The instance of web_idl.Database.
    _the_web_idl_database = None

    @classmethod
    def the_instance(cls):
        return cls._the_instance

    def __init__(self, web_idl_database_path, root_src_dir, root_gen_dir,
                 component_reldirs, enable_style_format,
                 enable_code_generation_tracing):
        """
        Args:
            web_idl_database_path: File path to the web_idl.Database.
            root_src_dir: Project's root directory, which corresponds to "//"
                in GN.
            root_gen_dir: Root directory of generated files, which corresponds
                to "//out/Default/gen" in GN.
            component_reldirs: Pairs of component and output directory.
            enable_style_format: Enable style formatting of the generated
                files.
            enable_code_generation_tracing: Enable tracing of code generation
                to see which Python code generates which line of generated
                code.
        """

        self._web_idl_database_path = web_idl_database_path
        self._root_src_dir = root_src_dir
        self._root_gen_dir = root_gen_dir
        self._component_reldirs = component_reldirs
        self._enable_style_format = enable_style_format
        self._enable_code_generation_tracing = enable_code_generation_tracing

    def init(self):
        if PackageInitializer._the_instance:
            return
        PackageInitializer._the_instance = self

        self._init()

    def _init(self):
        # Load the web_idl.Database as a global object so that every worker or
        # every function running in a worker of 'multiprocessing' does not need
        # to load it.
        PackageInitializer._the_web_idl_database = (
            web_idl.Database.read_from_file(self._web_idl_database_path))

        union_name_config = os.path.abspath(
            os.path.join(self._root_src_dir, "third_party", "blink",
                         "renderer", "bindings", "union_name_map.conf"))
        union_name_mapper = UnionNameMapper.init(
            union_name_config, PackageInitializer._the_web_idl_database)
        PathManager.init(root_src_dir=self._root_src_dir,
                         root_gen_dir=self._root_gen_dir,
                         component_reldirs=self._component_reldirs,
                         union_name_mapper=union_name_mapper)

        style_format.init(root_src_dir=self._root_src_dir,
                          enable_style_format=self._enable_style_format)

        if self._enable_code_generation_tracing:
            CodeGenTracing.enable_code_generation_tracing()
            # The following Python modules are generally not interesting, so
            # skip the functions in the modules.
            from . import code_node
            from . import code_node_cxx
            from . import codegen_utils
            CodeGenTracing.add_modules_to_be_ignored([
                code_node,
                code_node_cxx,
                codegen_utils,
            ])

    def web_idl_database(self):
        """Returns the global instance of web_idl.Database."""
        assert isinstance(PackageInitializer._the_web_idl_database,
                          web_idl.Database)
        return PackageInitializer._the_web_idl_database