U
    ,ºcñ  ã                   @   sv   d Z ddlmZ ddlmZ ddlmZ ddlZddlmZ ddlm	Z	 ddl
Z
dd	„ Zd
d„ Zdd„ Zddd„ZdS )zCSome general file utilities used that can be used by the Cloud SDK.é    )Úabsolute_import)Údivision)Úunicode_literalsN)Úencoding)Ú	platformsc                   C   s   t  tjd¡S )z5Returns properly encoded system PATH variable string.ÚPATH)Úencoding_utilZGetEncodedValueÚosÚenviron© r   r   ú6/tmp/pip-unpacked-wheel-1xt1w7un/fire/console/files.pyÚ_GetSystemPath   s    r   c                 C   s„   t |tjƒrtd |¡ƒ‚|D ]`}| tj¡D ]N}| d¡}tj	 
tj	 || ¡| ¡}tj	 |¡r.t |tj¡r.|    S q.qdS )a}  Internal function to a find an executable.

  Args:
    executable: The name of the executable to find.
    path: A list of directories to search separated by 'os.pathsep'.
    pathext: An iterable of file name extensions to use.

  Returns:
    str, the path to a file on `path` with name `executable` + `p` for
      `p` in `pathext`.

  Raises:
    ValueError: invalid input.
  zr_FindExecutableOnPath(..., pathext='{0}') failed because pathext must be an iterable of strings, but got a string.ú"N)Ú
isinstanceÚsixÚstring_typesÚ
ValueErrorÚformatÚsplitr	   ÚpathsepÚstripÚpathÚnormpathÚjoinÚisfileÚaccessÚX_OK)Ú
executabler   ÚpathextÚextÚ	directoryÚfullr   r   r   Ú_FindExecutableOnPath#   s    þ
r"   c                 C   s   | t jjkrdS dS d S )N)z.exez.cmdz.batz.comz.ps1)Ú z.sh)r   ÚOperatingSystemÚWINDOWS)Úplatformr   r   r   Ú_PlatformExecutableExtensionsD   s    r'   Fc                 C   sv   |s"t j | ¡d r"td | ¡ƒ‚t j | ¡r<td | ¡ƒ‚|dkrLtƒ }n|}|dk	r\|nttj	 
¡ ƒ}t| ||ƒS )a$  Searches for `executable` in the directories listed in `path` or $PATH.

  Executable must not contain a directory or an extension.

  Args:
    executable: The name of the executable to find.
    path: A list of directories to search separated by 'os.pathsep'.  If None
      then the system PATH is used.
    pathext: An iterable of file name extensions to use.  If None then
      platform specific extensions are used.
    allow_extensions: A boolean flag indicating whether extensions in the
      executable are allowed.

  Returns:
    The path of 'executable' (possibly with a platform-specific extension) if
    found and executable, None if not found.

  Raises:
    ValueError: if executable has a path or an extension, and extensions are
      not allowed, or if there's an internal error.
  é   zWFindExecutableOnPath({0},...) failed because first argument must not have an extension.zQFindExecutableOnPath({0},...) failed because first argument must not have a path.N)r	   r   Úsplitextr   r   Údirnamer   r'   r   r$   ZCurrentr"   )r   r   r   Zallow_extensionsZeffective_pathZeffective_pathextr   r   r   ÚFindExecutableOnPathK   s&    ÿÿÿÿÿr+   )NNF)Ú__doc__Ú
__future__r   r   r   r	   Zfire.consoler   r   r   r   r   r"   r'   r+   r   r   r   r   Ú<module>   s   !  ÿ