U
    ÝZßfŽ  ã                   @   st   d dl mZmZmZ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
mZmZ G dd„ deƒZG dd„ deƒZdS )é    )Úabsolute_importÚdivisionÚprint_functionÚunicode_literalsN)ÚBackendÚremove_aliases_from_apic                       s    e Zd ZdZ‡ fdd„Z‡  ZS )ÚBackendExceptionz8Saves the traceback of an exception raised by a backend.c                    s   t t| ƒ ¡  || _|| _dS )z?
        :type backend_name: str
        :type tb: str
        N)Úsuperr   Ú__init__Úbackend_nameÚ	traceback)Úselfr   Útb©Ú	__class__© ú2/tmp/pip-unpacked-wheel-vqb3mfeu/stone/compiler.pyr
      s    zBackendException.__init__)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   Ú__classcell__r   r   r   r   r      s   r   c                   @   sF   e Zd ZdZdZddd„Zdd„ Zedd	„ ƒZe	d
d„ ƒZ
dd„ ZdS )ÚCompilerzh
    Applies a collection of backends found in a single backend module to an
    API specification.
    z.stonegFc                 C   sT   t  d¡| _|| _|| _|| _|| _|rPtj 	| j¡rPt  
d| j¡ t | j¡ dS )aŽ  
        Creates a Compiler.

        :param stone.ir.Api api: A Stone description of the API.
        :param backend_module: Python module that contains at least one
            top-level class definition that descends from a
            :class:`stone.backend.Backend`.
        :param list(str) backend_args: A list of command-line arguments to
            pass to the backend.
        :param str build_path: Location to save compiled sources to. If None,
            source files are compiled into the same directories.
        :param bool clean_build: If True, the build_path is removed before
            source files are compiled into them.
        zstone.compilerz'Cleaning existing build directory %s...N)ÚloggingÚ	getLoggerÚ_loggerÚapiÚbackend_moduleÚbackend_argsÚ
build_pathÚosÚpathÚexistsÚinfoÚshutilÚrmtree)r   r   r   r   r   Zclean_buildr   r   r   r
   $   s    ÿzCompiler.__init__c                 C   sD   t j | j¡r,t j | j¡s,| j d¡ dS t | j¡ |  	¡  dS )z5Creates outputs. Outputs are files made by a backend.z1Output path must be a folder if it already existsN)
r    r!   r"   r   Úisdirr   Úerrorr   Ú_mkdirÚ_execute_backend_on_spec)r   r   r   r   ÚbuildE   s
    zCompiler.buildc              
   C   sB   zt  | ¡ W n. tk
r< } z|jdkr,‚ W 5 d}~X Y nX dS )zÆ
        Creates a directory at path if it doesn't exist. If it does exist,
        this function does nothing. Note that if path is a file, it will not
        be converted to a directory.
        é   N)r    ÚmakedirsÚOSErrorÚerrno)r!   Úer   r   r   r(   M   s
    
zCompiler._mkdirc                 C   s*   t j |¡\}}t j |¡\}}|| jkS )zž
        Returns True if the file name matches the format of a stone backend,
        ie. its inner extension of "stoneg". For example: xyz.stoneg.py
        )r    r!   ÚsplitextÚbackend_extension)Úclsr!   Zpath_without_extÚ_Z
second_extr   r   r   Úis_stone_backendZ   s    zCompiler.is_stone_backendc              	   C   s¼   d}t | jƒD ]¨}t| j|ƒ}t |¡rt|tƒrt |¡s| j 	d|j
¡ || j| jƒ}|jrh| j}n|svt| jƒ}|}z| |¡ W q tk
r´   t|j
t ¡ dd… ƒ‚Y qX qdS )z*Renders a source file into its final form.NzRunning backend: %séÿÿÿÿ)Údirr   ÚgetattrÚinspectÚisclassÚ
issubclassr   Ú
isabstractr   r#   r   r   r   Zpreserve_aliasesr   r   ÚgenerateÚ	Exceptionr   r   Ú
format_exc)r   Zapi_no_aliases_cacheZattr_keyÚ
attr_valueÚbackendr   r   r   r   r)   d   s,    
ÿþ
 ÿz!Compiler._execute_backend_on_specN)F)r   r   r   r   r1   r
   r*   Ústaticmethodr(   Úclassmethodr4   r)   r   r   r   r   r      s    û
!

	r   )Ú
__future__r   r   r   r   r   r8   r    r$   r   Zstone.backendr   r   r=   r   Úobjectr   r   r   r   r   Ú<module>   s   