a a`C@sddlmZddlZddlZddlZddlZddlZddlmZm Z m Z ddl m Z ddl mZmZmZmZmZeeZdZedZd Zd d ZeZGd d d eZdS))BytesION) sysconfigdetect_encodingZipFile)finder) FileOperatorget_export_entry convert_pathget_executablein_venva s^#!.*pythonw?[0-9.]*([ ].*)?$z# -*- coding: utf-8 -*- import re import sys from %(module)s import %(import_name)s if __name__ == '__main__': sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) sys.exit(%(func)s()) cCsXd|vrT|drB|dd\}}d|vrT|dsTd||f}n|dsTd|}|S)N z /usr/bin/env r"z%s "%s"z"%s") startswithsplit) executableenvZ _executabler3/usr/lib/python3.9/site-packages/distlib/scripts.pyenquote_executable3s  rc@seZdZdZeZdZd*ddZddZe j d rBd d Z d d Z ddZd+ddZddZeZddZddZdZddZd,ddZddZed d!Zejd"d!Zejd#ksejd krejd#krd$d%Zd-d&d'Z d.d(d)Z!dS)/ ScriptMakerz_ A class to copy or create scripts from source scripts or callable specifications. NTFcCs||_||_||_d|_d|_tjdkp:tjdko:tjdk|_t d|_ |pRt ||_ tjdkprtjdkortjdk|_ tj|_dS)NFposixjava)X.Ynt) source_dir target_dir add_launchersforceclobberosname_nameset_modesetvariantsr_fileop_is_ntsys version_info)selfrrrdry_runZfileoprrr__init__Ns  zScriptMaker.__init__cCs@|ddr<|jrrlangZSystemZ getPropertylowerendswith)r+rrrrr_fix_jython_executabless z"ScriptMaker._fix_jython_executablecCstjdkrd}n8t|t|d}tjdkr4d}nd}d|voF||k}|r^d||d }n d }|d ||d 7}|d 7}|S)a Build a shebang line. In the simple case (on Windows, or a shebang line which is not too long or contains spaces) use a simple formulation for the shebang. Otherwise, use /bin/sh as the executable, with a contrived shebang which allows the script to run either under Python or sh, using suitable quoting. Thanks to Harald Nordgren for his input. See also: http://www.in-ulm.de/~mascheck/various/shebang/#length https://hg.mozilla.org/mozilla-central/file/tip/mach rTdarwini s#! s #!/bin/sh s '''exec' s "$0" "$@" s' ''')r!r"lenr)platform)r+r post_interpZsimple_shebangZshebang_lengthZmax_shebang_lengthresultrrr_build_shebangs  zScriptMaker._build_shebangcCsPd}|jr|j}d}n^ts&t}nNtrLtjtddt d}n(tjt ddt dt df}|r| ||}t j d r||}|rt|}|d }t j d krd |vrd |vr|d7}|||}z|d Wn ty td|Yn0|d krLz||Wn$tyJtd||fYn0|S)NTFZscriptszpython%sZEXEZBINDIRz python%s%sZVERSIONrutf-8Zcliz -X:Framesz -X:FullFramess -X:Framesz,The shebang (%r) is not decodable from utf-8z?The shebang (%r) is not decodable from the script encoding (%r))rrZis_python_buildr r r!r0r2Zget_pathZget_config_varr5r)rIrrBrencoderLdecodeUnicodeDecodeError ValueError)r+encodingrJr3Zenquotershebangrrr _get_shebangsX          zScriptMaker._get_shebangcCs$|jt|j|jdd|jdS)N.r)moduleZ import_namefunc)script_templatedictprefixsuffixr)r+entryrrr_get_script_texts zScriptMaker._get_script_textcCstj|}|j|SN)r!r0basenamemanifest)r+Zexenamebaserrr get_manifests zScriptMaker.get_manifestc Cs|jo |j}tjd}||s*||7}|s8||}nn|dkrL|d}n |d}t} t| d} | d|Wdn1s0Y| } ||| }|D]R} tj |j | } |rtj | \}}|dr|} d| } z|j| |Wntytdd | }tj |r8t|t| ||j| |td zt|Wnty~Yn0Yn0nl|jr| d |sd | |f} tj | r|jstd | q|j| ||jr|j| g|| qdS)NrNpytwz __main__.pyz.pyz%s.exez:Failed to write executable - trying to use .deleteme logicz %s.deletemez0Able to replace executable using .deleteme logicrVz%s.%szSkipping existing file %s)rr(r!lineseprOrA _get_launcherrrZwritestrgetvaluer0r2rsplitextrr'Zwrite_binary_file Exceptionr;r<existsremoverenamedebugr r$set_executable_modeappend)r+namesrTZ script_bytes filenamesextZ use_launcherrgZlauncherstreamzfZzip_datar"outnameneZdfnamerrr _write_scriptsX       *         zScriptMaker._write_script-cCsnt}d|jvr||d|jvr<|d||jdfd|jvrj|d||j|jd|jdf|S)NrXz%s%srrz %s%s%s.%sr)r%r&addr*variant_separator)r+r"rKrrrget_script_filenamess     z ScriptMaker.get_script_filenamesc Csd}|r0|dg}|r0dd|}|d}|jd||d}||d}||j}|rr|ddrrd } nd } |||||| dS) NrMZinterpreter_argsz %sr rNr3r.Fpywrd)r/r2rOrUr^rr"rz) r+r]rsr3rJargsrTscriptZ scriptnamesrtrrr _make_script*s   zScriptMaker._make_scriptcCsd}tj|jt|}tj|jtj|}|jsT|j ||sTt d|dSzt |d}Wnt y|jsxd}YnF0|}|st d|dSt|dd}|rd}|dpd }|s|r||j|||jr|j|g||nt d ||j|jjs|t|j\} } |d || |} d |vrTd } nd} tj|} || g| ||| |r|dS)NFznot copying %s (up-to-date)rbz%s is an empty file (skipping)s rGTrrMzcopying and adjusting %s -> %srspythonwrrd) r!r0r2rr rr`rr'Znewerr;ror7r:r,readliner< FIRST_LINE_REmatchr1groupcloseZ copy_filer$rprqinforseekrUrzr8)r+rrsZadjustrwfZ first_linerrJrSlinesrTrtrxrrr _copy_script:sR          zScriptMaker._copy_scriptcCs|jjSr_r'r,)r+rrrr,mszScriptMaker.dry_runcCs ||j_dSr_r)r+valuerrrr,qsrcCs`tddkrd}nd}d||f}tddd}t||}|sZd ||f}t||jS) NPZ64Z32z%s%s.exerVrrz(Unable to find resource %s in package %s)structcalcsize__name__rsplitrfindrRbytes)r+kindbitsr"Zdistlib_packageresourcemsgrrrrhys zScriptMaker._get_launchercCs6g}t|}|dur"|||n|j|||d|S)a Make a script. :param specification: The specification, which is either a valid export entry specification (to make a script from a callable) or a filename (to make a script by copying from a source location). :param options: A dictionary of options controlling script generation. :return: A list of all absolute pathnames written to. Nr)r rr)r+ specificationr3rsr]rrrmakes zScriptMaker.makecCs$g}|D]}||||q|S)z Take a list of specifications and make scripts from them, :param specifications: A list of specifications. :return: A list of all absolute pathnames written to, )extendr)r+Zspecificationsr3rsrrrr make_multipleszScriptMaker.make_multiple)TFN)rMN)N)N)N)"r __module__ __qualname____doc__SCRIPT_TEMPLATErYrr-r5r)rIrr>rBrLrUr^_DEFAULT_MANIFESTrarcrzr~rrrpropertyr,setterr!r"r#rhrrrrrrrEs8     @4 3   r)iorZloggingr!rerr)compatrrrZ resourcesrutilrr r r r Z getLoggerrr;striprcompilerrrZ_enquote_executableobjectrrrrrs