dZddlZddlZddlZddlZddlZddlZejjr ej rdZ ndZ dZ da ej ZdadZgZejejZeeejeZejeejekreedZejdkred rd Zejejed Zeeejejed Z e ekree d Z!Gd dZ"dZ#GddZ$GddZ%GddZ&dZ'dZ(dZ)dS)z8 PEP-302 and PEP-451 importers for frozen applications. Nctj||ztjddS)N )sysstderrwrite)msgas pyimod02_importers.pytracer s8 q!!! cdS)N)rr s r r r $s r cddlm}tj|j}||}tjdd}|||dS)a Decode bytes representing source code and return the string. Universal newline support is used in the decoding. Based on CPython's implementation of the same functionality: https://github.com/python/cpython/blob/3.9/Lib/importlib/_bootstrap_external.py#L679-L688 r)detect_encodingNT)decoder translate)tokenizerioBytesIOreadlineIncrementalNewlineDecoderdecode) source_bytesrsource_bytes_readlineencodingnewline_decoders r _decode_sourcer(st)(((((J|44=455H244PPPO  ! !,"5"5hqk"B"B C CCr ct5tttatcdddS#1swxYwYdS)N)_pyz_tree_lock _pyz_tree_build_pyz_prefix_tree pyz_archiverr r get_pyz_toc_treer#@s   .{;;Is "7;;FdarwinzContents/FrameworksT Resourcescft}|jD]\}}|d}|d}|}|tjtjhvr|D]}||i}Z|ddD]}||i}d||d<|S)N.r)dicttocitemssplitpyimod01_archive PYZ_ITEM_PKGPYZ_ITEM_NSPKG setdefault)r"tree entry_name entry_dataname_componentstypecodecurrentname_components r r!r!ks 66D"-/"7"7"9"9 . . J$**3//a= (57G7VW W W"1 A A!,,^R@@ A#2#2#"6 A A!,,^R@@+-GOB' ( ( Kr ceZdZdZdZedZdZdZe dZ dZ dZ dd Z ejd d d krd ZdZd Sd S)PyiFrozenFinderaG PyInstaller's frozen path entry finder for specific search path. Per-path instances allow us to properly translate the given module name ("fullname") into full PYZ entry name. For example, with search path being `sys._MEIPASS`, the module "mypackage.mod" would translate to "mypackage.mod" in the PYZ archive. However, if search path was `sys._MEIPASS/myotherpackage/_vendored` (for example, if `myotherpacakge` added this path to `sys.path`), then "mypackage.mod" would need to translate to "myotherpackage._vendored.mypackage.mod" in the PYZ archive. c0|jjd|jdS)N()) __class____name___path)selfs r __repr__zPyiFrozenFinder.__repr__s .)99DJ9999r ctd| ||}td|S#t$r}td|d}~wwxYw)Nz0PyInstaller: running path finder hook for path: zPyInstaller: hook succeededzPyInstaller: hook failed: )r Exception)clspathfinderes r path_hookzPyiFrozenFinder.path_hooksx IIIJJJ SYYF / 0 0 0M    2q22 3 3 3  s0 AA  Ac||_t|_tD]K} tj||}n#t$rY0wxYw|drJntdtj |rtd|dkr d|_ dSd | tjj|_ dS)Nz..zIFailed to determine relative path w.r.t. top-level application directory.zonly directories are supportedr'r))r@r" _pyz_archive_TOP_LEVEL_DIRECTORY_PATHSosrFrelpath ValueError startswith ImportErrorisfile_pyz_entry_prefixjoinr-sep)rArFtop_level_path relative_paths r __init__zPyiFrozenFinder.__init__s '9 k kN  "n E E     ''--  Eijj j 7>>$   @>?? ? C  %'D " " "%(XXm.A.A"'+.N.N%O%OD " " "s ? A  A cd|dd}|jr |jdz|zS|S)zz Convert module fullname into PYZ entry name, subject to the prefix implied by this finder's search path. r') rpartitionrS)rAfullname tail_modules r _compute_pyz_entry_namez'PyiFrozenFinder._compute_pyz_entry_names@))#..q1  ! )C/+= = r ct|dr|jSd}d|_ttjD]=\}}||jkrd}|s ||j|_n#t$rY:wxYw|jS)aB Opportunistically create a *fallback finder* using `sys.path_hooks` entries that are located *after* our hook. The main goal of this exercise is to obtain an instance of python's FileFinder, but in theory any other hook that comes after ours is eligible to be a fallback. Having this fallback allows our finder to "cooperate" with python's FileFinder, as if the two were a single finder, which allows us to work around the python's PathFinder permitting only one finder instance per path without subclassing FileFinder. _fallback_finderFNT)hasattrr` enumerater path_hooksrIr@rQ)rAour_hook_foundidxhooks r fallback_finderzPyiFrozenFinder.fallback_finders 4+ , , )( ( $"3>22  ICt~%%!%!  (,TZ(8(8%    $$sA(( A54A5ct|ds||_|jdS|j||S)a@ Attempt to find the spec using fallback finder, which is opportunistically created here. Typically, this would be python's FileFinder, which can discover specs for on-filesystem modules, such as extension modules and modules that are collected only as source .py files. Having this fallback allows our finder to "cooperate" with python's FileFinder, as if the two were a single finder, which allows us to work around the python's PathFinder permitting only one finder instance per path without subclassing FileFinder. r`N)ra_get_fallback_finderr` find_spec)rAr\targets r _find_fallback_specz#PyiFrozenFinder._find_fallback_specsSt/00 @$($=$=$?$?D !  (4$..x@@@r c|t|dd}|&t|dr|dSdSdS)a< A method which, when called, should invalidate any internal cache used by the finder. Used by importlib.invalidate_caches() when invalidating the caches of all finders on sys.meta_path. https://docs.python.org/3/library/importlib.html#importlib.abc.MetaPathFinder.invalidate_caches r`Ninvalidate_caches)getattrrarn)rArgs r rnz!PyiFrozenFinder.invalidate_cachessY"$(:DAA  &(;<< 41133333 ' & 4 4r Nc t|d|d|||}|jj|}||t|d|d|jLt|d|jd|j||}t|d|d|St|d dS|d }t|d |d |d ||tjkrjtj |d}tj tj|dtj jg|_|S|tjk}t)||j||} | j } tj || || }d|_|r%tj | g|_|S)a A method for finding a spec for the specified module. The finder will search for the module only within the path entry to which it is assigned. If a spec cannot be found, None is returned. When passed in, target is a module object that the finder may use to make a more educated guess about what spec to return. https://docs.python.org/3/library/importlib.html#importlib.abc.PathEntryFinder.find_spec z": find_spec: called with fullname=z , target=Nz : find_spec: z not found in PYZ...z6: find_spec: attempting resolve using fallback finder r'z,: find_spec: fallback finder returned spec: z.: find_spec: fallback finder is not available.rz: find_spec: found z in PYZ as z , typecode=)namer"pyz_entry_name is_package)rsoriginT)r r^rKr+getrgrjr.r0_frozen_importlib ModuleSpecrMrFrTr_MEIPASSreplacerUsubmodule_search_locationsr/PyiFrozenLoader has_locationdirname) rAr\rkrrr4 fallback_specr6specrsloaderrts r rjzPyiFrozenFinder.find_specs/ ZZZZhZZ[[[55h??&*..~>>   THHHHH I I I#/nnUYUinnnooo $ 4 > >x P P ]]=]]]^^^$$MMMNNN4a= hh(hhhh^fhhiii '6 6 6%/$??D S\>+A+A#rw{+S+STT/D + K!1!>> !))!    !+  !    !  H/1wv/F/F.GD + r rZ) cX||}|dgfS|j|jpgfS)a A legacy method for finding a loader for the specified module. Returns a 2-tuple of (loader, portion) where portion is a sequence of file system locations contributing to part of a namespace package. The loader may be None while specifying portion to signify the contribution of the file system locations to a namespace package. An empty list can be used for portion to signify the loader is not part of a namespace package. If loader is None and portion is the empty list then no loader or location for a namespace package were found (i.e. failure to find anything for the module). Deprecated since python 3.4, removed in 3.12. N)rjrrz)rAr\rs r find_loaderzPyiFrozenFinder.find_loaderZs8>>(++D|Rx; ? E2E Er c6||\}}|S)z A concrete implementation of Finder.find_module() which is equivalent to self.find_loader(fullname)[0]. Deprecated since python 3.4, removed in 3.12. )r)rAr\rportionss r find_modulezPyiFrozenFinder.find_modulels! $//99 FHMr )N)r? __module__ __qualname____doc__rB classmethodrIrXr^propertyrgrlrnrjr version_inforrrr r r:r:}s:::[PPPB   %%X%BAAA( 4 4 4NNNNd g%% F F F$     )&%r r:cfd}|S)Ncl|j|krtd|jd||||g|Ri|S)Nz loader for z cannot handle rq)rqrQ)rArqargskwargsmethods r _check_name_wrapperz(_check_name.._check_name_wrapper|sY 9  LDILLdLLSWXXX XvdD242226222r r)rrs` r _check_namer{s$33333 r ceZdZdZdZdZdZ edZedZ edZ ed Z ed Z d Z ed Zd S)r{a PyInstaller's frozen loader for modules in the PYZ archive, which are discovered by PyiFrozenFinder. Since this loader is instantiated only from PyiFrozenFinder and since each loader instance is tied to a specific module, the fact that the loader was instantiated serves as the proof that the module exists in the PYZ archive. Hence, we can avoid any additional validation in the implementation of the loader's methods. c||_||_||_|rOtjt j|dtjj d}nPtjt j|dtjj dz}||_ ||_dS)Nr' __init__.py.py) rK_pyz_entry_name _is_packagerMrFrTrrxryrUrq)rArqr"rrrs module_files r rXzPyiFrozenLoader.__init__s(-%  g',,s|^5K5KCQSQXQ\5]5]_lmmKK',,s|^5K5KCQSQXQ\5]5]`e5effK   r cdS)a  A method that returns the module object to use when importing a module. This method may return None, indicating that default module creation semantics should take place. https://docs.python.org/3/library/importlib.html#importlib.abc.Loader.create_module Nr)rArs r create_modulezPyiFrozenLoader.create_modules tr c|j}||j}|td|jdt |dsJ|j |j|_t||jdS)a[ A method that executes the module in its own namespace when a module is imported or reloaded. The module should already be initialized when exec_module() is called. When this method exists, create_module() must be defined. https://docs.python.org/3/library/importlib.html#importlib.abc.Loader.exec_module Nz Failed to retrieve bytecode for !__file__) __spec__get_coderq RuntimeErrorrarz__path__exec__dict__)rAmodulerbytecodes r exec_modulezPyiFrozenLoader.exec_modules==++  P$)PPPQQ Qvz*****  * 6"=FO Xv'''''r Tc:ddlm}|||S)aK A legacy method for loading a module. If the module cannot be loaded, ImportError is raised, otherwise the loaded module is returned. Deprecated since python 3.4, slated for removal in 3.12 (but still present in python's own FileLoader in both v3.12.4 and v3.13.0rc1). rN)importlib._bootstrap _bootstrap_load_module_shim)rAr\rs r load_modulezPyiFrozenLoader.load_modules, 6 5 5 5 5 5//h?? ?r c|jS)a A method that is to return the value of __file__ for the specified module. If no path is available, ImportError is raised. If source code is available, then the method should return the path to the source file, regardless of whether a bytecode was used to load the module. https://docs.python.org/3/library/importlib.html#importlib.abc.ExecutionLoader.get_filename rF)rAr\s r get_filenamezPyiFrozenLoader.get_filenames yr c@|j|jS)aL Return the code object for a module, or None if the module does not have a code object (as would be the case, for example, for a built-in module). Raise an ImportError if loader cannot find the requested module. https://docs.python.org/3/library/importlib.html#importlib.abc.InspectLoader.get_code )rKextractr)rAr\s r rzPyiFrozenLoader.get_codes (()=>>>r c|j} t|d5}|}dddn #1swxYwYt|S#t$rYnwxYwdS)a A method to return the source of a module. It is returned as a text string using universal newlines, translating all recognized line separators into ' ' characters. Returns None if no source is available (e.g. a built-in module). Raises ImportError if the loader cannot find the module specified. https://docs.python.org/3/library/importlib.html#importlib.abc.InspectLoader.get_source rbN)rFopenreadrFileNotFoundError)rAr\filenamefprs r get_sourcezPyiFrozenLoader.get_sources9 h%% )!wwyy  ) ) ) ) ) ) ) ) ) ) ) ) ) ) )!,// /     D ts.A: A>A>A A! A!c|jS)a  A method to return a true value if the module is a package, a false value otherwise. ImportError is raised if the loader cannot find the module. https://docs.python.org/3/library/importlib.html#importlib.abc.InspectLoader.is_package )r)rAr\s r rszPyiFrozenLoader.is_packages r c~t|d5}|cdddS#1swxYwYdS)u  A method to return the bytes for the data located at path. Loaders that have a file-like storage back-end that allows storing arbitrary data can implement this abstract method to give direct access to the data stored. OSError is to be raised if the path cannot be found. The path is expected to be constructed using a module’s __file__ attribute or an item from a package’s __path__. https://docs.python.org/3/library/importlib.html#importlib.abc.ResourceLoader.get_data rN)rr)rArFrs r get_datazPyiFrozenLoader.get_datas$   7799                  s 266c t|S)zO Return resource reader compatible with `importlib.resources`. )PyiFrozenResourceReader)rAr\s r get_resource_readerz#PyiFrozenLoader.get_resource_reader,s 't,,,r N)r?rrrrXrrrrrrrrsrrrr r r{r{s   @(((2@  @ @  @  [ ??[?[.  [    --[---r r{c6eZdZdZdZdZdZdZdZdZ dS) ra Resource reader for importlib.resources / importlib_resources support. Supports only on-disk resources, which should cover the typical use cases, i.e., the access to data files; PyInstaller collects data files onto filesystem, and as of v6.0.0, the embedded PYZ archive is guaranteed to contain only .pyc modules. When listing resources, source .py files will not be listed as they are not collected by default. Similarly, sub-directories that contained only .py files are not reconstructed on filesystem, so they will not be listed, either. If access to .py files is required for whatever reason, they need to be explicitly collected as data files anyway, which will place them on filesystem and make them appear as resources. For on-disk resources, we *must* return path compatible with pathlib.Path() in order to avoid copy to a temporary file, which might break under some circumstances, e.g., metpy with importlib_resources back-port, due to: https://github.com/Unidata/MetPy/blob/a3424de66a44bf3a92b0dcacf4dff82ad7b86712/src/metpy/plots/wx_symbols.py#L24-L25 (importlib_resources tries to use 'fonts/wx_symbols.ttf' as a temporary filename suffix, which fails as it contains a separator). Furthermore, some packages expect files() to return either pathlib.Path or zipfile.Path, e.g., https://github.com/tensorflow/datasets/blob/master/tensorflow_datasets/core/utils/resource_utils.py#L81-L97 This makes implementation of mixed support for on-disk and embedded resources using importlib.abc.Traversable protocol rather difficult. So in order to maximize compatibility with unfrozen behavior, the below implementation is basically equivalent of importlib.readers.FileReader from python 3.10: https://github.com/python/cpython/blob/839d7893943782ee803536a47f1d4de160314f85/Lib/importlib/readers.py#L11 and its underlying classes, importlib.abc.TraversableResources and importlib.abc.ResourceReader: https://github.com/python/cpython/blob/839d7893943782ee803536a47f1d4de160314f85/Lib/importlib/abc.py#L422 https://github.com/python/cpython/blob/839d7893943782ee803536a47f1d4de160314f85/Lib/importlib/abc.py#L312 cVddl}||jj|_dS)Nr)pathlibPathrFparent)rArrs r rXz PyiFrozenResourceReader.__init__Ss)LL--4 r cv||dS)Nr)filesjoinpathr)rAresources r open_resourcez%PyiFrozenResourceReader.open_resourceZs,zz||$$X..33D999r cPt|j|S)N)strrFr)rArs r resource_pathz%PyiFrozenResourceReader.resource_path]s 49%%h//000r ct||S)N)rris_file)rArFs r is_resourcez#PyiFrozenResourceReader.is_resource`s*zz||$$T**22444r cbd|DS)Nc3$K|] }|jV dS)Nr).0items r z3PyiFrozenResourceReader.contents..ds$==d ======r )riterdir)rAs r contentsz PyiFrozenResourceReader.contentscs*==djjll&:&:&<&<====r c|jS)Nr)rAs r rzPyiFrozenResourceReader.filesfs yr N) r?rrrrXrrrrrrr r rr4sx<555:::111555>>>r rceZdZdZdZdZdS)PyiFrozenEntryPointLoaderz] A special loader that enables retrieval of the code-object for the __main__ module. c|jjS)N)r>r?)rAs r rBz"PyiFrozenEntryPointLoader.__repr__ns ~&&r cd|dkrtjdjSt|d|)N__main__z cannot handle module )rmodules _pyi_main_corQ)rAr\s r rz"PyiFrozenEntryPointLoader.get_codeqs; z ! !;z*7 7TEEEEFFFr N)r?rrrrBrrr r rrjsA'''GGGGGr rcttdstd tjtjdan"#t$r}td|d}~wwxYwttdtj D]8}t|dddkr!tj |n9ttj D]_\}}t|ddd krEtd |d zd tj |d zt jn:`td tj dt jt%tjtjd t-tjd_n#t$rYnwxYwtjdkrt5dSdS)z` Install PyInstaller's frozen finders/loaders/importers into python's import machinery. _pyinstaller_pyzz,Bootloader did not set sys._pyinstaller_pyz!T) check_pymagicz#Failed to setup PYZ archive reader!Nr?WindowsRegistryFinder zipimporterz0PyInstaller: inserting our finder hook at index z in sys.path_hooks.zbPyInstaller: zipimporter hook not found in sys.path_hooks! Prepending our finder hook to the list.rr)r )rarrr.ZlibArchiveReaderrr"rDdelattr meta_pathroremoverbrcr insertr:rI_patch_zipimporter_get_sourcepath_importer_cachepoprxrr __loader__r_fixup_frozen_stdlib)rHentryres r installrzs 3* + +KIJJJI&89M]abbb III@AAqHI C#$$$  5*d + +/F F F M  ' ' ' E G  //<< U 5*d + +} < < aS1Waaa b b b N ! !#'?+D E E E E = rsss a!:;;;"###  d333 -F-H-H J**       7""#"s) A A&A!!A&.#G GGc0ddl}tjs( tjt_n#t$rYnwxYwtjD]\}}||s||}|j j }|j }|r|dz }tj jtjg|dRdz}t!|ds ||_n#t$rYnwxYw|j |dkr||_dS)Nrz .__init__r'z.pycrzimportlib._bootstrap)_impr _stdlib_dirrxAttributeErrorrr, is_frozenis_frozen_packager loader_stateorignamerMrFrTr-rarr)r module_nameris_pkgr orig_namers r rrsTKKK ? !lCOO    D  #{0022-- V~~k**  '' 443  )  %  $I7< Dys/C/CDDDvMvz**  "*!      (Y:P-P-P$,L !7--s) 66)C11 C>=C>cJddl}|jjfd}||j_dS)NrcR||}||Stj|jdkrdS||r,tjjg|ddR}n)tjj|ddz}tjt|} t|d5}| }dddn #1swxYwYt|S#t$rYnwxYwdS)Nzbase_library.zipr'rrr) rMrFbasenamearchiversrTr-_RESOLVED_TOP_LEVEL_DIRECTORYrrrr)rAr\sourcerrr_orig_get_sources r _get_sourcez2_patch_zipimporter_get_source.._get_sourcesf "!$11  M 7  DL ) )-? ? ?4 ??8 $ $ Bw|HX^^C%8%8H-HHHHHw|X^^C%8%89EAH7<< =xHH h%% )!wwyy  ) ) ) ) ) ) ) ) ) ) ) ) ) ) )!,// /     D ts6 DC=1 D=DDDD D$#D$) zipimportrr)rrrs @r rrsG ,7>(3I$$$r )*rrrMrrv_threadr.flagsverboserr rr"RLockrr r#rLrFnormpathrx_TOP_LEVEL_DIRECTORYappendrealpathrnormcase_is_macos_app_bundleplatformendswithrTr} _ALTERNATIVE_TOP_LEVEL_DIRECTORY)_RESOLVED_ALTERNATIVE_TOP_LEVEL_DIRECTORYr!r:rr{rrrrrrr r rs& 9       D D D    w'' 55!!"6777!# 0 01E F F7122bg6F6FG[6\6\\\%%&CDDD<8 4 = =>S T T')w|| ,--(($%%&FGGG02  56611-14TTT"))*STTT$zzzzzzzz|m-m-m-m-m-m-m-m-`33333333l G G G G G G G G 999@(-(-(-^$3$3$3$3$3r