U
    '`fW!                     @   s*  d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl	Z	dddddd	d
dddddddddddddgZ
ejdkre
d e
d e
d edZG dd deZedddZd d! Zd3d#d$Zd4d%d&Zd'd( Zd)d* Zd+d, Zd-d. Zd5d/d0Zed1kr&eejd2d  dS )6a  Aids in creating a developer virtual environment for Certbot.

When this module is run as a script, it takes the arguments that should
be passed to pip to install the Certbot packages as command line
arguments. If no arguments are provided, all Certbot packages and their
development dependencies are installed. The virtual environment will be
created with the name "venv" in the current working directory. You can
change the name of the virtual environment by setting the environment
variable VENV_NAME.

    )print_functionNz-e acme[test]z-e certbot[all]z-e certbot-apachez-e certbot-dns-cloudflarez-e certbot-dns-digitaloceanz-e certbot-dns-dnsimplez-e certbot-dns-dnsmadeeasyz-e certbot-dns-gehirnz-e certbot-dns-googlez-e certbot-dns-linodez-e certbot-dns-luadnsz-e certbot-dns-nsonez-e certbot-dns-ovhz-e certbot-dns-rfc2136z-e certbot-dns-route53z-e certbot-dns-sakuracloudz-e certbot-nginxz-e certbot-compatibility-testz-e certbot-ciz-e letstestZwin32z-e windows-installerz^(\d+)\.(\d+).*$c                   @   s   e Zd ZdS )PythonExecutableNotFoundErrorN)__name__
__module____qualname__ r   r   &/home/beta/certs/certbot/tools/venv.pyr   8   s   r   )returnc               
   C   s  d} t dtjd tjd tjd r.tjS dD ]r}zRd|}tj|dgd	tjd
}t | 	 d rtj|ddgd	dW   S W q2 tj
tfk
r   Y q2X q2zHtjdddgd	tjd
}t | 	 d rtjdtddgd	dW S W n tj
tfk
r
   Y nX | stddS )ah  
    Find the relevant python executable that is of the given python major version.
    Will test, in decreasing priority order:

    * the current Python interpreter
    * 'pythonX' executable in PATH (with X the given major version) if available
    * 'python' executable in PATH if available
    * Windows Python launcher 'py' executable in PATH if available

    Incompatible python versions for Certbot will be evicted (e.g. Python 3
    versions less than 3.7).

    :rtype: str
    :return: the relevant python executable path
    :raise RuntimeError: if no relevant python executable path could be found
    Nz{0}.{1}.{2}r         )3 z	python{0}z	--versionT)universal_newlinesstderrz-cz-import sys; sys.stdout.write(sys.executable);)r   pyz-3zBError, no compatible Python executable for Certbot could be found.)_check_versionformatsysversion_info
executable
subprocesscheck_outputZSTDOUTstripsplitZCalledProcessErrorOSErrorZenv_argRuntimeError)Zpython_executable_pathZone_versionZ
one_pythonoutputZoutput_versionr   r   r   find_python_executable<   sJ      

  
r   c                 C   sL   t | }|sdS t|dt|df}|dkr:dS td|  dS )NFr
   r   )      Tz2Incompatible python version for Certbot found: {0})VERSION_PATTERNsearchintgroupprintr   )Zversion_strr!   versionr   r   r   r   p   s    
r   Fc                 C   sD   |d krt j}tt| tr*dt| n|  tj| ||d d S Nz+ {0})envshell)	osenvironr$   
isinstancelistr   r   list2cmdlineZ
check_callcmdr'   r(   r   r   r   subprocess_with_print   s    "r0   c                 C   s@   |d krt j}tt| tr*dt| n|  tj| ||dS r&   )	r)   r*   r$   r+   r,   r   r   r-   r   r.   r   r   r   subprocess_output_with_print   s    "r1   c                 C   s^   t j| d}t j|r&t j|S t j| d}t j|rLt j|S td| d S )Nz
bin/pythonzScripts\python.exezNError, could not find python executable in venv path {0}: is it a valid venv ?)r)   pathjoinisfileabspath
ValueErrorr   )	venv_pathZpython_linuxZpython_windowsr   r   r   get_venv_python_path   s    r8   c              	   C   s   t  dD ]&}tj|r&t| q
t| q
tjd}|rTt	d
| |} tj| r|t| d
| tt  | S )ay  Determines the venv path and prepares it for use.

    This function cleans up any Python eggs in the current working directory
    and ensures the venv path is available for use. The path used is the
    VENV_NAME environment variable if it is set and venv_name otherwise. If
    there is already a directory at the desired path, the existing directory is
    renamed by appending a timestamp to the directory name.

    :param str venv_name: The name or path at where the virtual
        environment should be created if VENV_NAME isn't set.

    :returns: path where the virtual environment should be created
    :rtype: str

    z
*.egg-infoZ	VENV_NAMEz.Creating venv at {0} as specified in VENV_NAMEz{0}.{1}.bak)globr)   r2   isdirshutilZrmtreeremover*   getr$   r   renamer"   time)	venv_namer2   Zenv_venv_namer   r   r   prepare_venv_path   s    rA   c                 C   s   t | }|tjdg}|| t| tjtj| drhtd td td	|  td nZtjtj| drtd td td		|  td
	|  td nt
d	| dS )a  Installs packages in the given venv.

    :param str venv_name: The name or path at where the virtual
        environment should be created.
    :param pip_args: Command line arguments that should be given to
        pip to install packages
    :type pip_args: `list` of `str`

    ztools/pip_install.pybinzC-------------------------------------------------------------------zCPlease run the following command to activate developer environment:zsource {0}/bin/activateZScriptszK---------------------------------------------------------------------------zKPlease run one of the following commands to activate developer environment:z${0}\Scripts\activate.bat (for Batch)z+.\{0}\Scripts\Activate.ps1 (for Powershell)z)Error, directory {0} is not a valid venv.N)r8   r)   r2   r5   extendr0   r:   r3   r$   r   r6   )r@   pip_argsZpy_venvcommandr   r   r   install_packages   s     


rF   c                 C   s   t  }|dd| g}t| dS )zxCreate a Python virtual environment at venv_path.

    :param str venv_path: path where the venv should be created

    z-mvenvN)r   r0   )r7   pythonrE   r   r   r   create_venv   s    rI   c                 C   s&   t d}t| | st} t||  d S )NrG   )rA   rI   REQUIREMENTSrF   )rD   r7   r   r   r   main   s
    rK   __main__r
   )NF)NF)N)__doc__Z
__future__r   r9   r)   rer;   r   r   r?   rJ   platformappendr<   compiler    	Exceptionr   strr   r   r0   r1   r8   rA   rF   rI   rK   r   argvr   r   r   r   <module>   s\   




4

"!


