U
    ee                     @   s  d dl Z d dlmZ d dlZd dlmZmZ d dlmZmZm	Z	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mZmZmZ ed	Zd
je je jd Ze jdd Ze jdd Z dd Z!dd Z"G dd de#Z$dd Z%d!ddZ&dd Z'dd Z(dd  Z)dS )"    N)fnmatch)existsjoin)getcwdchdirmakedirswalk)Path)uname)mkdtemp)loggerErr_Foreerrorinfozp4a.utilz{system}-{machine})systemmachinec                 c   sX   t  }tdtjd| tjf t|  d V  tdtjd|tjf t| d S )N z-> directory context z<- directory context )r   r   r   r   r   CYANRESETr   )Znew_dirZcur_dir r   9/tmp/pip-unpacked-wheel-h4dze4ss/pythonforandroid/util.pycurrent_directory   s    r   c                  c   sZ   t  } z&tdtjd| tjf | V  W 5 t|  tdtjd| tjf X d S )Nr   z - temp directory deleted z + temp directory used )	r   shutilrmtreer   debugr   r   r   r   )temp_dirr   r   r   temp_directory#   s     

 r   c           
      c   sv   t | D ]h\}}}ttt|D ]}|| }||kr"|| q"|D ](}|D ]}	t||	rN qFqNt||V  qFqdS )a  Recursively walks all the files and directories in ``dirn``,
    ignoring directories that match any pattern in ``invalid_dirns``
    and files that patch any pattern in ``invalid_filens``.

    ``invalid_dirns`` and ``invalid_filens`` should both be lists of
    strings to match. ``invalid_dir_patterns`` expects a list of
    invalid directory names, while ``invalid_file_patterns`` expects a
    list of glob patterns compared against the full filepath.

    File and directory paths are evaluated as full paths relative to ``dirn``.

    N)r   reversedrangelenpopr   r   )
base_dirZinvalid_dir_namesZinvalid_file_patternsZdirnsubdirsZfilensisubdirZfilenpatternr   r   r   walk_valid_filens0   s    
r&   c                 C   s\   dd l }t|jdr>|j| |}|j|}|j| |S ddlm} || |	 S d S )Nr   module_from_spec)SourceFileLoader)
importlib.utilhasattrutilspec_from_file_locationr'   loaderexec_moduleZimportlib.machineryr(   load_module)modulefilename	importlibspecmodr(   r   r   r   load_sourceN   s    r5   c                       s   e Zd Zd fdd	Z  ZS )BuildInterruptingExceptionNc                    s   t  || || _|| _d S N)super__init__messageinstructions)selfr:   r;   	__class__r   r   r9   ]   s    z#BuildInterruptingException.__init__)N)__name__
__module____qualname__r9   __classcell__r   r   r=   r   r6   \   s   r6   c                 C   s6   t d| j | jdk	r*td| j td dS )z
    Handles a raised BuildInterruptingException by printing its error
    message and associated instructions, if any, then exiting.
    zBuild failed: {}NzInstructions: {}   )r   formatr:   r;   r   exit)	exceptionr   r   r   handle_build_exceptionc   s    
rG   Fc                 C   s,   t | sd S td|  t| | d S )Nz$Remove directory and subdirectory {})r   LOGGERr   rD   r   r   )dnignore_errorsr   r   r   rmdirn   s    rK   c                 C   s(   t | rd S td|  t|  d S )NzCreate directory {0})r   rH   r   rD   r   )rI   r   r   r   
ensure_diru   s    rL   c                 C   s"   t d| | t| | d S )NzMoving {} to {})rH   r   rD   r   move)sourceZdestinationr   r   r   rM   |   s    rM   c                 C   s   t |   d S r7   )r	   touch)r1   r   r   r   rO      s    rO   )F)*
contextlibr   loggingos.pathr   r   osr   r   r   r   pathlibr	   platformr
   r   tempfiler   Zpythonforandroid.loggerr   r   r   r   	getLoggerrH   rD   r   r   lowerZbuild_platformcontextmanagerr   r   r&   r5   	Exceptionr6   rG   rK   rL   rM   rO   r   r   r   r   <module>   s4   
 



