
\\                 @   sM  d  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 m Z y d d l	 Z	 Wn e
 k
 r d d l Z Yn Xd d d d d d	 d
 d g Z d d   Z d d   Z d d   Z d d   Z d d   Z d d   Z d d   Z d d d d d d i Z d d  d d! d d" i Z d# d$   Z d% d&   Z d S)'z
Compatibility layer to run certbot both on Linux and Windows.

This module contains all required platform specific code,
allowing the rest of Certbot codebase to be platform agnostic.
    N)errorsZcertificatesZenhanceZrevokeZdeleteregisterZ
unregisterZconfig_changesZpluginsc             C   s[   t  t d  rW |  t k rW t t d  } | j j   d k rW t j d j |     d S)aj  
    On Windows, raise if current shell does not have the administrative rights.
    Do nothing on Linux.

    :param str subcommand: The subcommand (like 'certonly') passed to the certbot client.

    :raises .errors.Error: If the provided subcommand must be run on a shell with
        administrative rights, and current shell does not have these rights.

    windllr   zJError, "{0}" subcommand must be run on a shell with administrative rights.N)	hasattrctypes UNPRIVILEGED_SUBCOMMANDS_ALLOWEDgetattrZshell32ZIsUserAnAdminr   Errorformat)Z
subcommandr    r   0/usr/lib/python3/dist-packages/certbot/compat.py+raise_for_non_administrative_windows_rights   s    r   c               C   s+   y t  j   SWn t k
 r& d SYn Xd S)zT
    Get current user uid

    :returns: The current user uid.
    :rtype: int

    r   N)osgeteuidAttributeErrorr   r   r   r   
os_geteuid4   s    r   c             C   s   y t  j |  |  Wnk t k
 r } zK | j t j k r>   t t  d  sY t d   t t  d  |  |  WYd d } ~ Xn Xd S)z
    Rename a file to a destination path and handles situations where the destination exists.

    :param str src: The current file path.
    :param str dst: The new file path.
    replacez^Error: tried to run os_rename on Python < 3.3. Certbot supports only Python 3.4 >= on Windows.N)r   renameOSErrorerrnoZEEXISTr   RuntimeErrorr   )srcZdsterrr   r   r   	os_renameD   s    r   c             C   s}   yW t  j  t j g g  g  |   \ } } } | sH t j d j |    | d j   SWn t k
 rx t j j   SYn Xd S)a(  
    Read user input to return the first line entered, or raise after specified timeout.

    :param float timeout: The timeout in seconds given to the user.
    :param str prompt: The prompt message to display to the user.

    :returns: The first line entered by the user.
    :rtype: str

    z,Timed out waiting for answer to prompt '{0}'r   N)selectsysstdinr   r	   r
   readliner   )ZtimeoutpromptZrlist_r   r   r   readline_with_timeout\   s    'r    c             C   sF   d t  j k r, t j |  t j t j B n t j |  t j d  d S)z~
    Lock the file linked to the specified file descriptor.

    :param int fd: The file descriptor of the file to lock.

    fcntl   N)	r   modulesr!   lockfZLOCK_EXZLOCK_NBmsvcrtZlockingZLK_NBLCK)fdr   r   r   	lock_filey   s    r'   c             C   sj   zU y t  j |  Wn= t k
 rS } z | j t j k r> n   WYd d } ~ Xn XWd t  j |   Xd S)z
    Remove, close, and release a lock file specified by its file descriptor and its path.

    :param int fd: The file descriptor of the lock file.
    :param str path: The path of the lock file.

    N)r   remover   r   ZEACCESclose)r&   pathr   r   r   r   release_locked_file   s    r+   c             C   s   t  j d k r7 t t j |    t t j |   k St j |   t j @t j |  t j @k o t j |   t j @t j |  t j @k S)zJReturn true if the two modes can be considered as equals for this platformnt)r   nameoctstatS_IMODES_IREADS_IWRITE)Zmode1Zmode2r   r   r   compare_file_modes   s    (,r3   configz
C:\CertbotZworkzC:\Certbot\libZlogszC:\Certbot\logz/etc/letsencryptz/var/lib/letsencryptz/var/log/letsencryptc             C   s   t  j d k r t |  St |  S)z
    Return the relevant default folder for the current OS

    :param str folder_type: The type of folder to retrieve (config, work or logs)

    :returns: The relevant default folder.
    :rtype: str

    r,   )r   r-   LINUX_DEFAULT_FOLDERSWINDOWS_DEFAULT_FOLDERS)Zfolder_typer   r   r   get_default_folder   s    
r7   c             C   s?   t  j d k r |  St  j j |   \ } } | | j d d  S)z
    Replace unsupported characters in path for current OS by underscores.
    :param str path: the path to normalize
    :return: the normalized path
    :rtype: str
    r,   :r   )r   r-   r*   
splitdriver   )r*   Zdrivetailr   r   r   .underscores_for_unsupported_characters_in_path   s    r;   )__doc__r   r   r   r   r   r/   Zcertbotr   r!   ImportErrorr%   r   r   r   r   r    r'   r+   r3   r6   r5   r7   r;   r   r   r   r   <module>   s8   "