
    PJ`                         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e      Z G d de      Zy)    )absolute_importdivisionprint_functionunicode_literalsN)Backendremove_aliases_from_apic                   "     e Zd ZdZ fdZ xZS )BackendExceptionz8Saves the traceback of an exception raised by a backend.c                 F    t         t        |           || _        || _        y)z?
        :type backend_name: str
        :type tb: str
        N)superr
   __init__backend_name	traceback)selfr   tb	__class__s      .lib/python3.12/site-packages/stone/compiler.pyr   zBackendException.__init__   s"    
 	.0(    )__name__
__module____qualname____doc__r   __classcell__)r   s   @r   r
   r
      s    B r   r
   c                   J    e Zd ZdZdZ	 d	dZd Zed        Ze	d        Z
d Zy)
Compilerzh
    Applies a collection of backends found in a single backend module to an
    API specification.
    z.stonegc                 H   t        j                  d      | _        || _        || _        || _        || _        |rjt        j                  j                  | j                        r@t        j                  d| j                         t        j                  | j                         yyy)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#   clean_builds         r   r   zCompiler.__init__$   sz    ( (()9:,($ 277>>$//:LLB*MM$//* ;;r   c                 >   t         j                  j                  | j                        rEt         j                  j	                  | j                        s| j
                  j                  d       yt        j                  | j                         | j                          y)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   s    r   buildzCompiler.buildE   s[    77>>$//*277==3QLLRS(%%'r   c                 |    	 t        j                  |        y# t        $ r}|j                  dk7  r Y d}~yd}~ww xY w)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%   es     r   r.   zCompiler._mkdirM   s6    	KK 	ww"} 	s    	;6;c                     t         j                  j                  |      \  }}t         j                  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%   path_without_ext_
second_exts        r   is_stone_backendzCompiler.is_stone_backendZ   sH     !gg..t4!(()9::S2222r   c                 `   d}t        | j                        D ]  }t        | j                  |      }t        j                  |      s/t        |t              s@t        j                  |      rV| j                  j                  d|j                          || j                  | j                        }|j                  r| j                  }n|st        | j                        }|}	 |j!                  |        y# t"        $ r, t%        |j                  t'        j(                         dd       w xY w)z*Renders a source file into its final form.NzRunning backend: %s)dirr!   getattrinspectisclass
issubclassr   
isabstractr   r'   r   r#   r"   preserve_aliasesr    r   generate	Exceptionr
   r   
format_exc)r   api_no_aliases_cacheattr_key
attr_valuebackendr    s         r   r/   z!Compiler._execute_backend_on_specd   s	     $D//0 	JH !4!4h?J
+z73**:6!!"79L9LM$T__d6G6GH++((C//Ftxx/P,.CJ$$S)!	J" ! J +"++Y-A-A-CCR-HJ J	Js   $C885D-N)F)r   r   r   r   r9   r   r0   staticmethodr.   classmethodr>   r/    r   r   r   r      sM    
 " #+B( 
 
 3 3Jr   r   )
__future__r   r   r   r   r   rC   r$   r(   r   stone.backendr   r   rI   r
   objectr   rQ   r   r   <module>rU      s;    R R   	  
y 
bJv bJr   