3 \H@sddlZddlZddlZddlZddlZddlZejeZGdddZ d ddZ dddZ ed krd Z ye dZ Wn4e k rZzed eejd WYddZ[XnXeje dS)Nc@seZdZdddZddZddZd d Zd d Zej d krDddZ dddZ ddZ ddZ ddZddZddZddZdS) EnvBuilderFNcCs(||_||_||_||_||_||_dS)N)system_site_packagesclearsymlinksupgradewith_pipprompt)selfrrrrrrr %/usr/lib64/python3.6/venv/__init__.py__init__*s zEnvBuilder.__init__cCsxtjj|}|j|}|j}d|_|j||j||jrF|j||j s`|j ||j ||rtd|_|j|dS)NFT) ospathabspathensure_directoriesrcreate_configuration setup_pythonr _setup_pipr setup_scripts post_setup)r env_dircontextZtrue_system_site_packagesr r r create3s       zEnvBuilder.createcCs`xZtj|D]L}tjj||}tjj|s6tjj|rBtj|q tjj|r tj |q WdS)N) r listdirrjoinislinkisfileremoveisdirshutilZrmtree)r rfnr r r clear_directoryMs   zEnvBuilder.clear_directorycCsdd}tjj|r$|jr$|j|tj}||_tjj|d|_ |j dk rT|j n|j }d||_ ||tj }t j dkrd|krtj d}nt j}tjjtjj|\}}||_||_||_t j dkrd} d } tjj|d d } n(d } d } tjj|ddt jddd } tjj|| |_} || || t jdkr|tjdkr|t j dkr|tjj|d} tjj| s|tjd| tjj|| |_}| |_tjj|||_|||S)NcSs@tjj|stj|n$tjj|s0tjj|r.create_if_neededz(%s) darwin__PYVENV_LAUNCHER__Zwin32ZScriptsZIncludeLibz site-packagesbinincludelibz python%d.%d posixlib64l)r rr"rr!typesSimpleNamespacersplitenv_namerenvironsysplatform executabler python_dirZ python_exer version_infoZinc_pathmaxsizenamesymlinkbin_pathbin_nameenv_exe)r rr&rrenvr9dirnameZexenameZbinnameZincpathZlibpathrZ link_pathbinpathr r r rUsN       zEnvBuilder.ensure_directoriesc Csztjj|jd|_}t|dddL}|jd|j|jrBd}nd}|jd||jd t j dd WdQRXdS) Nz pyvenv.cfgwzutf-8)encodingz home = %s trueZfalsez"include-system-site-packages = %s zversion = %d.%d.%d ) r rrrZcfg_pathopenwriter:rr7r;)r rrfZinclr r r rs zEnvBuilder.create_configurationntcCs(|jdrd}n|jdo"|jd}|S)N.pyd.dllTpythonz.exe)rMrN)endswith startswith)r rKresultr r r include_binarys zEnvBuilder.include_binaryc Cs~|j }|sjy6tjj|s@|r4tjtjj||n tj||Wn&tk rhtjd||d}YnX|rzt j ||dS)NzUnable to symlink %r to %rT) rr rrr>basename Exceptionloggerwarningrcopyfile)r srcdstrelative_symlinks_okZ force_copyr r r symlink_or_copys  zEnvBuilder.symlink_or_copycs|j}|j}|j}||j||j}tjdkrtjj|sFtj |dxNd D]F}tjj ||}tjj |sL||j|ddtjj|sLtj |dqLWnRd}|j fdd tj |D}x<|D]4} tjj || } tjj || } | |jkr|| | qWtjj ||}tjj|rdfd d tj |D}x4|D],} tjj || } tjj || } || | q4Wxtj|jD]v\} } }d |krrtjj| }tjj |jd |}tjj |stj|tjj | d } tjj |d } tj| | PqrWdS)NrLirOpython3T)r[ZDLLscsg|]}|r|qSr r ).0rK)r,r r sz+EnvBuilder.setup_python..csg|]}|r|qSr r )r^rK)r,r r r_szinit.tclr*)rOr])r?rAr\r9r:r r=rrchmodrr"rSrrwalkrTrr#rrX)r rrDrZcopierrCsuffixZsubdirfilesrKrYrZrootdirsZtcldirr )r,r rsN              zEnvBuilder.setup_pythoncCs$|jddddg}tj|tjddS)Nz-ImZ ensurepipz --upgradez --default-pip)stderr)rA subprocessZ check_outputZSTDOUT)r rcmdr r r rs zEnvBuilder._setup_pipcCs2tjjtjjt}tjj|d}|j||dS)Nscripts)r rrrC__file__rinstall_scripts)r rrr r r rs zEnvBuilder.setup_scriptscCsdS)Nr )r rr r r rszEnvBuilder.post_setupcCsJ|jd|j}|jd|j}|jd|j}|jd|j}|jd|j}|S)NZ __VENV_DIR__Z __VENV_NAME__Z__VENV_PROMPT__Z__VENV_BIN_NAME__Z__VENV_PYTHON__)replacerr5rr@rA)r textrr r r replace_variabless zEnvBuilder.replace_variablesc!Cs|j}t|}xtj|D]x\}}}||kr`x,|ddD]}|dtjfkr>|j|q>Wqx2|D](} tjj|| } ||djtj dd} | s|} ntjj|f| } tjj | stj | tjj| | } t | d} | j }WdQRX| jds^y$|jd}|j||}|jd}Wn6tk r\}zd}tjd| |WYdd}~XnX|dk rht | d} | j|WdQRXtj| | qhWqWdS)Ncommonr.rbz.exezutf-8z+unable to copy script %r, may be binary: %swb)r?lenr rar=rrrr4sepr"r#rIreadrPdecodernencode UnicodeErrorrVrWrJrZcopymode)r rrrDZplenrdrercr%rKZsrcfilerbZdstdirZdstfiledataer r r rk"s@        zEnvBuilder.install_scripts)FFFFFN)F)__name__ __module__ __qualname__r rr!rrr r=rSr\rrrrrnrkr r r r rs 8  3  rFcCs t|||||d}|j|dS)N)rrrrr)rr)rrrrrrbuilderr r r rRsrc Csbd}tjd*krd}nttds"d}|s2tdn,ddl}|jtddd }|jd d d d d|jddddddtj dkrd}nd}|j }|jd|dddd|jd| dddd|jdddddd|jddddd d|jd!d"ddd#d$|jd%d&d'|j |}|j r"|j r"td(t|j|j |j|j |j|jd)}x|jD]}|j|qJWdS)+NTrHF base_prefixz.This script is only for use with Python >= 3.3rzFCreates virtual Python environments in one or more target directories.z|Once an environment has been created, you may wish to activate it, e.g. by sourcing an activate script in its bin directory.)progZ descriptionZepilogreZENV_DIR+z)A directory to create the environment in.)metavarnargshelpz--system-site-packages store_true system_sitezDGive the virtual environment access to the system site-packages dir.)defaultactiondestrrLz --symlinksrz[Try to use symlinks rather than copies, when symlinks are not the default for the platform.z--copiesZ store_falsez\Try to use copies rather than symlinks, even when symlinks are the default for the platform.z--clearrzcDelete the contents of the environment directory if it already exists, before environment creation.z --upgraderzlUpgrade the environment directory to use this version of Python, assuming Python has been upgraded in-place.z --without-piprz]Skips installing or upgrading pip in the virtual environment (pip is bootstrapped by default))rrrrz--promptz;Provides an alternative prompt prefix for this environment.)rz1you cannot supply --upgrade and --clear together.)rrrrrr)rHrH)r7r;hasattrr$argparseArgumentParserrz add_argumentr r=Zadd_mutually_exclusive_group parse_argsrrrrrrrrer) argsZ compatiblerparserZ use_symlinksgroupZoptionsr}r%r r r mainZs\             r__main__r'z Error: %s)file)FFFFN)N)Zloggingr rrgr7r2Z getLoggerrzrVrrrZrcrUryprintrfexitr r r r s( C  H$