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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
U
W±dfcã@sÚdZddlZddlZddlZddlZddlmZddl    m
Z
ddl m Z m Z ddlmZddlmZe e¡ZeƒaGdd    „d    eƒZd
d „Zee
fee
fedfedfd d „efeefdœZGdd„dƒZGdd„dƒZdS)z-
Code related to processing of import hooks.
éN)Úlog)Úformat_binaries_and_datas)Ú expand_pathÚimportlib_load_source)Ú PostGraphAPI)ÚImportErrorWhenRunningHookcs4eZdZdZdZ‡fdd„Zdd„Zdd„Z‡ZS)    ÚModuleHookCacheaÃ
    Cache of lazily loadable hook script objects.
 
    This cache is implemented as a `dict` subclass mapping from the fully-qualified names of all modules with at
    least one hook script to lists of `ModuleHook` instances encapsulating these scripts. As a `dict` subclass,
    all cached module names and hook scripts are accessible via standard dictionary operations.
 
    Attributes
    ----------
    module_graph : ModuleGraph
        Current module graph.
    _hook_module_name_prefix : str
        String prefixing the names of all in-memory modules lazily loaded from cached hook scripts. See also the
        `hook_module_name_prefix` parameter passed to the `ModuleHook.__init__()` method.
    rcs@tƒ ¡t |¡|_d tj¡|_tjd7_|     |¡dS)aé
        Cache all hook scripts in the passed directories.
 
        **Order of caching is significant** with respect to hooks for the same module, as the values of this
        dictionary are lists. Hooks for the same module will be run in the order in which they are cached. Previously
        cached hooks are always preserved rather than overridden.
 
        By default, official hooks are cached _before_ user-defined hooks. For modules with both official and
        user-defined hooks, this implies that the former take priority over and hence will be loaded _before_ the
        latter.
 
        Parameters
        ----------
        module_graph : ModuleGraph
            Current module graph.
        hook_dirs : list
            List of the absolute or relative paths of all directories containing **hook scripts** (i.e.,
            Python scripts with filenames matching `hook-{module_name}.py`, where `{module_name}` is the module
            hooked by that script) to be cached.
        z__PyInstaller_hooks_{}_éN)
ÚsuperÚ__init__ÚweakrefÚproxyÚ module_graphÚformatrÚ_cache_id_nextÚ_hook_module_name_prefixÚ_cache_hook_dirs)ÚselfrÚ    hook_dirs©Ú    __class__©úQd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\PyInstaller/depend/imphook.pyr ;s
 
 zModuleHookCache.__init__cCs”|D]Š}tj t|ƒ¡}tj |¡s2td |¡ƒ‚t tj |d¡¡}|D]B}tj     |¡dd…}t
|j |||j d}|  |g¡}| |¡qJqdS)zï
        Cache all hook scripts in the passed directories.
 
        Parameters
        ----------
        hook_dirs : list
            List of the absolute or relative paths of all directories containing hook scripts to be cached.
        zHook directory "{}" not found.z    hook-*.pyééýÿÿÿ)rÚ module_nameÚ hook_filenameÚhook_module_name_prefixN)ÚosÚpathÚabspathrÚisdirÚFileNotFoundErrorrÚglobÚjoinÚbasenameÚ
ModuleHookrrÚ
setdefaultÚappend)rrZhook_dirZhook_filenamesrrÚ module_hookÚ module_hooksrrrr_s
 ü z ModuleHookCache._cache_hook_dirscGs@|D]6}| |g¡}|D]}tj |jd¡q| |d¡qdS)zñ
        Remove the passed modules and all hook scripts cached for these modules from this cache.
 
        Parameters
        ----------
        module_names : list
            List of all fully-qualified module names to be removed.
        N)ÚgetÚsysÚmodulesÚpopÚhook_module_name)rZ module_namesrr*r)rrrÚremove_modules‚s
 
 zModuleHookCache.remove_modules)    Ú__name__Ú
__module__Ú __qualname__Ú__doc__rr rr0Ú __classcell__rrrrr!s
     $#rcCs2t|tƒr|St|tƒr d|iStd|›ƒ‚dS)Nz.Invalid module collection mode setting value: )Ú
isinstanceÚdictÚstrÚ
ValueError)ÚvaluerrrÚ!_module_collection_mode_sanitizer˜s
 
 
r;cCsdS)NTrrrrrÚ<lambda>¿ór<)ÚdatasÚbinariesZexcludedimportsÚ hiddenimportsÚwarn_on_missing_hiddenimportsÚmodule_collection_modecsVeZdZdZdd„Zdd„Z‡fdd„Zd‡fd    d
„    Zd d „Zd d„Z    dd„Z
‡Z S)r&a=
    Cached object encapsulating a lazy loadable hook script.
 
    This object exposes public attributes (e.g., `datas`) of the underlying hook script as attributes of the same
    name of this object. On the first access of any such attribute, this hook script is lazily loaded into an
    in-memory private module reused on subsequent accesses. These dynamic attributes are referred to as "magic." All
    other static attributes of this object (e.g., `hook_module_name`) are referred to as "non-magic."
 
    Attributes (Magic)
    ----------
    datas : set
        Set of `TOC`-style 2-tuples `(target_file, source_file)` for all external non-executable files required by
        the module being hooked, converted from the `datas` list of hook-style 2-tuples `(source_dir_or_glob,
        target_dir)` defined by this hook script.
    binaries : set
        Set of `TOC`-style 2-tuples `(target_file, source_file)` for all external executable files required by the
        module being hooked, converted from the `binaries` list of hook-style 2-tuples `(source_dir_or_glob,
        target_dir)` defined by this hook script.
    excludedimports : set
        Set of the fully-qualified names of all modules imported by the module being hooked to be ignored rather than
        imported from that module, converted from the `excludedimports` list defined by this hook script. These
        modules will only be "locally" rather than "globally" ignored. These modules will remain importable from all
        modules other than the module being hooked.
    hiddenimports : set
        Set of the fully-qualified names of all modules imported by the module being hooked that are _not_
        automatically detectable by PyInstaller (usually due to being dynamically imported in that module),
        converted from the `hiddenimports` list defined by this hook script.
    warn_on_missing_hiddenimports : bool
        Boolean flag indicating whether missing hidden imports from the hook should generate warnings or not. This
        behavior is enabled by default, but individual hooks can opt out of it.
    module_collection_mode : dict
        A dictionary of package/module names and their corresponding collection mode strings ('pyz', 'pyc', 'py',
        'pyz+py', 'py+pyz').
 
    Attributes (Non-magic)
    ----------
    module_graph : ModuleGraph
        Current module graph.
    module_name : str
        Name of the module hooked by this hook script.
    hook_filename : str
        Absolute or relative path of this hook script.
    hook_module_name : str
        Name of the in-memory module of this hook script's interpreted contents.
    _hook_module : module
        In-memory module of this hook script's interpreted contents, lazily loaded on the first call to the
        `_load_hook_module()` method _or_ `None` if this method has yet to be accessed.
    cCspt|tjƒst‚||_||_||_||j dd¡|_|jt    krHd|_
nd|_
t      |j¡d|_ d|_ d|_dS)aá
        Initialize this metadata.
 
        Parameters
        ----------
        module_graph : ModuleGraph
            Current module graph.
        module_name : str
            Name of the module hooked by this hook script.
        hook_filename : str
            Absolute or relative path of this hook script.
        hook_module_name_prefix : str
            String prefixing the name of the in-memory module for this hook script. To avoid namespace clashes with
            similar modules created by other `ModuleHook` objects in other `ModuleHookCache` containers, this string
            _must_ be unique to the `ModuleHookCache` container containing this `ModuleHook` object. If this string
            is non-unique, an existing in-memory module will be erroneously reused when lazily loading this hook
            script, thus erroneously resanitizing previously sanitized hook script attributes (e.g., `datas`) with
            the `format_binaries_and_datas()` helper.
 
        Ú.Ú_TFN)r6r Ú
ProxyTypesÚAssertionErrorrrrÚreplacer/ÚHOOKS_MODULE_NAMESÚ_shallowÚaddÚ_loadedÚ_has_hook_functionÚ _hook_module)rrrrrrrrr ús
 zModuleHook.__init__cCs,|tkr |js | ¡t||ƒSt|ƒ‚dS)a
        Get the magic attribute with the passed name (e.g., `datas`) from this lazily loaded hook script if any _or_
        raise `AttributeError` otherwise.
 
        This special method is called only for attributes _not_ already defined by this object. This includes
        undefined attributes and the first attempt to access magic attributes.
 
        This special method is _not_ called for subsequent attempts to access magic attributes. The first attempt to
        access magic attributes defines corresponding instance variables accessible via the `self.__dict__` instance
        dictionary (e.g., as `self.datas`) without calling this method. This approach also allows magic attributes to
        be deleted from this object _without_ defining the `__delattr__()` special method.
 
        See Also
        ----------
        Class docstring for supported magic attributes.
        N)Ú_MAGIC_MODULE_HOOK_ATTRSrKÚ_load_hook_moduleÚgetattrÚAttributeError)rÚ    attr_namerrrÚ __getattr__'s
zModuleHook.__getattr__cs|tkr| ¡tƒ ||¡S)a­
        Set the attribute with the passed name to the passed value.
 
        If this is a magic attribute, this hook script will be lazily loaded before setting this attribute. Unlike
        `__getattr__()`, this special method is called to set _any_ attribute -- including magic, non-magic,
        and undefined attributes.
 
        See Also
        ----------
        Class docstring for supported magic attributes.
        )rNrOr
Ú __setattr__)rrRÚ
attr_valuerrrrTBszModuleHook.__setattr__Fc
s‚ˆjrˆjdk    s|rˆjr~ˆjrzdˆ_dˆ_tjd tj ˆj¡ddd…ˆj    fžŽt
  ¡D]\}\}}t ƒ  ||ƒ¡q\dStj ˆj¡\}}t d||¡ztˆjˆjƒˆ_Wn0tk
râtjdddtˆjˆjƒ‚YnXdˆ_tˆjdƒˆ_t
  ¡D]L\}\}}tˆj|dƒ}    |    dkr,|ƒ}    n|dk    r>||    ƒ}    tˆ||    ƒqtˆd    ‡fd
d „tˆd    ƒ  ¡Dƒƒ|s~dˆ_dS) aÍ
        Lazily load this hook script into an in-memory private module.
 
        This method (and, indeed, this class) preserves all attributes and functions defined by this hook script as
        is, ensuring sane behaviour in hook functions _not_ expecting unplanned external modification. Instead,
        this method copies public attributes defined by this hook script (e.g., `binaries`) into private attributes
        of this object, which the special `__getattr__()` and `__setattr__()` methods safely expose to external
        callers. For public attributes _not_ defined by this hook script, the corresponding private attributes will
        be assigned sane defaults. For some public attributes defined by this hook script, the corresponding private
        attributes will be transformed into objects more readily and safely consumed elsewhere by external callers.
 
        See Also
        ----------
        Class docstring for supported attributes.
        NTúNSkipping module hook %r from %r because a hook for %s has already been loaded.éÿÿÿÿz!Loading module hook %r from %r...úHook failed with:©Úexc_infoÚhookrBcs$i|]\}}|dk    r|nˆj|“qS©N)r)Ú.0Úkeyr:©rrrÚ
<dictcomp>sÿz0ModuleHook._load_hook_module.<locals>.<dictcomp>)rV)rKrMrIÚloggerÚdebugrrÚsplitrrrNÚitemsr
rTÚinforr/Ú ImportErrorrÚhasattrrLrPÚsetattr)
rÚkeep_module_refrRZ    attr_typerDÚheadÚtailZ default_typeZsanitizer_funcrUrr_rrOZsLÿþ
 
 
 þÿ    zModuleHook._load_hook_modulecCs.|jr |jr"|jdd| |¡| ¡dS)aO
        Call the **post-graph hook** (i.e., `hook()` function) defined by this hook script, if any.
 
        Parameters
        ----------
        analysis: build_main.Analysis
            Analysis that calls the hook
 
        This method is intended to be called _after_ the module graph for this application is constructed.
        T)riN)rKrLrOÚ_process_hook_funcÚ_process_hidden_imports)rÚanalysisrrrÚ
post_graphªs  
zModuleHook.post_graphcCsÄt|jdƒsdSt|j|j|d}z|j |¡Wn0tk
rbtjdddt    |j
|j ƒ‚YnX|j   t|jƒ¡|j  t|jƒ¡|j |j¡|j  |j¡|jD]}|j |j|¡qªdS)z¶
        Call this hook's `hook()` function if defined.
 
        Parameters
        ----------
        analysis: build_main.Analysis
            Analysis that calls the hook
        r[N)rrrnrXTrY)rgrMrrrr[rfrarbrr/rr>ÚupdateÚsetZ _added_datasr?Z_added_binariesr@ÚextendZ_added_importsrBZ_module_collection_modeZ_deleted_importsZremoveReferenceÚnode)rrnZhook_apiZdeleted_module_namerrrrlÄs 
zModuleHook._process_hook_funcc    Cs\|jD]P}z$|jj|jdd}|j ||¡Wqtk
rT|jrPt d|¡YqXqdS)a1
        Add all imports listed in this hook script's `hiddenimports` attribute to the module graph as if directly
        imported by this hooked module.
 
        These imports are typically _not_ implicitly detectable by PyInstaller and hence must be explicitly defined
        by hook scripts.
        F)Z create_nspkgzHidden import "%s" not found!N)    r@rZ    find_noderZ import_hookrfrAraÚwarning)rZimport_module_nameZcallerrrrrmês
 
z"ModuleHook._process_hidden_imports)F) r1r2r3r4r rSrTrOrorlrmr5rrrrr&Æs3- P&r&c@s8eZdZdZdd„Zdd„Zdd„Zdd    „Zd
d „Zd S) ÚAdditionalFilesCachezq
    Cache for storing what binaries and datas were pushed by what modules when import hooks were processed.
    cCsi|_i|_dSr\©Ú    _binariesÚ_datasr_rrrr szAdditionalFilesCache.__init__cCsH|j |g¡|j| |pg¡|j |g¡|j| |p@g¡dSr\)rwr'rrrx)rÚmodnamer?r>rrrrJ
szAdditionalFilesCache.addcCs||jkp||jkSr\rv)rÚnamerrrÚ __contains__sz!AdditionalFilesCache.__contains__cCs
|j|S)z@
        Return list of binaries for given module name.
        )rw©rryrrrr?szAdditionalFilesCache.binariescCs
|j|S)z=
        Return list of datas for given module name.
        )rxr|rrrr>szAdditionalFilesCache.datasN)    r1r2r3r4r rJr{r?r>rrrrrus ru)r4r#Úos.pathrr,r Z PyInstallerrÚloggingZPyInstaller.building.utilsrZPyInstaller.compatrrZPyInstaller.depend.imphookapirZPyInstaller.exceptionsrÚ    getLoggerr1rarqrHr7rr;ÚlistÚboolrNr&rurrrrÚ<module> s0    
w
ë>