B
    bU                 @   s  d Z ddlZddlZddlZddlZddlmZ ddlZddlm	Z	 G dd dZ
G dd dejZG d	d
 d
Zdd Zdd Zdd ZeedZdd Zdd ZeedZG dd deedZi Zdd Zdd ZdaG dd dejjZdd  ZG d!d" d"eedZdS )#z?Execute actions with specific lists of target and source Nodes.    N)NoSlotsPyPy)logInstanceCreationc               @   s"   e Zd ZdZdZg g fddZdS )BatchzHRemembers exact association between targets
    and sources of executor.)targetssourcesc             C   s   || _ || _d S )N)r   r   )selfr   r    r   -lib/python3.7/site-packages/SCons/Executor.py__init__*   s    zBatch.__init__N)__name__
__module____qualname____doc__	__slots__r
   r   r   r   r	   r   #   s   r   c               @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )TSLista  A class that implements $TARGETS or $SOURCES expansions by wrapping
    an executor Method.  This class is used in the Executor.lvars()
    to delay creation of NodeList objects until they're needed.

    Note that we subclass collections.UserList purely so that the
    is_Sequence() function will identify an object of this class as
    a list during variable expansion.  We're not really using any
    collections.UserList methods in practice.
    c             C   s
   || _ d S )N)func)r   r   r   r   r	   r
   :   s    zTSList.__init__c             C   s   |   }t||S )N)r   getattr)r   attrnlr   r   r	   __getattr__<   s    zTSList.__getattr__c             C   s   |   }|| S )N)r   )r   ir   r   r   r	   __getitem__?   s    zTSList.__getitem__c             C   s   |   }t|S )N)r   str)r   r   r   r   r	   __str__B   s    zTSList.__str__c             C   s   |   }t|S )N)r   repr)r   r   r   r   r	   __repr__E   s    zTSList.__repr__N)	r   r   r   r   r
   r   r   r   r   r   r   r   r	   r   0   s   	r   c               @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )TSObjectz^A class that implements $TARGET or $SOURCE expansions by wrapping
    an Executor method.
    c             C   s
   || _ d S )N)r   )r   r   r   r   r	   r
   M   s    zTSObject.__init__c             C   s   |   }t||S )N)r   r   )r   r   nr   r   r	   r   O   s    zTSObject.__getattr__c             C   s   |   }|rt|S dS )N )r   r   )r   r   r   r   r	   r   R   s    zTSObject.__str__c             C   s   |   }|rt|S dS )Nr   )r   r   )r   r   r   r   r	   r   W   s    zTSObject.__repr__N)r   r   r   r   r
   r   r   r   r   r   r   r	   r   I   s
   r   c             C   s*   y
| j }W n tk
r   | S X | S dS )z
    A function to return the results of a Node's rfile() method,
    if it exists, and the Node itself otherwise (if it's a Value
    Node, e.g.).
    N)rfileAttributeError)noder   r   r   r	   r   ]   s
    
r   c             C   s   dS )Nr   r   )objtargetkwr   r   r	   execute_nothingk   s    r%   c             C   s   |   }| |}d}xf|  D ]Z}g g |f}|||}t|tjjrR| |_|q |r d| }tjj|| jd j	| |dq W |S )z!Actually execute the action list.r   zError %s)Zerrstrr!   executoraction)
get_build_envget_kwget_action_list
isinstanceSConsErrorsZ
BuildErrorr&   batchesr   )r"   r#   r$   envZstatusactargsmsgr   r   r	   execute_action_listn   s"    



r3   )r      c                s&      d fdd D S )N
c                s"   g | ]}|    qS r   )Z	genstringget_all_targetsget_all_sources).0r'   )r/   r"   r   r	   
<listcomp>   s   z'execute_actions_str.<locals>.<listcomp>)r(   joinr*   )r"   r   )r/   r"   r	   execute_actions_str   s    r;   c             C   s   dS )Nr   r   )r"   r   r   r	   execute_null_str   s    r<   c               @   s  e Zd ZdZdZdi gg g i fddZdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zejjd*d+ Zd,d- Zi fd.d/Zd0d1 Zd2d3 Zd4d5 Z d6d7 Z!d8d9 Z"d:d; Z#d<d= Z$d>d? Z%d@dA Z&dBdC Z'ejjdDdE Z(dFdG Z)dHdI Z*dJdK Z+dLdM Z,dUdOdPZ-ej.e-dVdQdRZ/dSdT Z0dS )WExecutorzA class for controlling instances of executing an action.

    This largely exists to hold a single association of an action,
    environment, list of environment override dictionaries, targets
    and sources for later processing as needed.
    )pre_actionspost_actionsr/   overridelistr.   
builder_kw_memolvars_changed_sources_list_changed_targets_list_unchanged_sources_list_unchanged_targets_listaction_list_do_execute_execute_strNc             C   s~   t jjrt| d | | g | _g | _|| _|| _|s<|r\t	|d d  |d d  g| _
ng | _
|| _d| _d| _i | _d S )NzExecutor.Executorr4   )r,   Debugtrack_instancesr   set_action_listr>   r?   r/   r@   r   r.   rA   rI   rJ   rB   )r   r'   r/   r@   r   r   rA   r   r   r	   r
      s     

 zExecutor.__init__c             C   sj   y| j S  tk
rd   t| jt| jt| jt| jt| jt| j	t| j
t| jd| _ | j S X d S )N)ZCHANGED_SOURCESZCHANGED_TARGETSZSOURCEZSOURCESZTARGETZTARGETSZUNCHANGED_SOURCESZUNCHANGED_TARGETS)rC   r    r   _get_changed_sources_get_changed_targetsr   _get_source_get_sources_get_target_get_targets_get_unchanged_sources_get_unchanged_targets)r   r   r   r	   	get_lvars   s    zExecutor.get_lvarsc             C   s   g }g }g }g }xn| j D ]d}|jd jsZ|jd  rZ|ttt|j ||j q|ttt|j ||j qW t	j
|| _t	j
|| _t	j
|| _t	j
|| _d S )Nr   )r.   r   Zalways_buildZis_up_to_dateextendlistmapr   r   r,   UtilNodeListrD   rE   rF   rG   )r   ZcsZctusutbr   r   r	   _get_changes   s    zExecutor._get_changesc             O   s*   y| j S  tk
r$   |   | j S X d S )N)rD   r    r_   )r   r1   r$   r   r   r	   rN      s
    zExecutor._get_changed_sourcesc             O   s*   y| j S  tk
r$   |   | j S X d S )N)rE   r    r_   )r   r1   r$   r   r   r	   rO      s
    zExecutor._get_changed_targetsc             O   s   t | jd jd  S )Nr   )r   r.   r   get_subst_proxy)r   r1   r$   r   r   r	   rP      s    zExecutor._get_sourcec             O   s   t jdd |  D S )Nc             S   s   g | ]}t | qS r   )r   r`   )r8   r   r   r   r	   r9      s    z)Executor._get_sources.<locals>.<listcomp>)r,   rZ   r[   r7   )r   r1   r$   r   r   r	   rQ      s    zExecutor._get_sourcesc             O   s   | j d jd  S )Nr   )r.   r   r`   )r   r1   r$   r   r   r	   rR      s    zExecutor._get_targetc             O   s   t jdd |  D S )Nc             S   s   g | ]}|  qS r   )r`   )r8   r   r   r   r	   r9      s    z)Executor._get_targets.<locals>.<listcomp>)r,   rZ   r[   r6   )r   r1   r$   r   r   r	   rS      s    zExecutor._get_targetsc             O   s*   y| j S  tk
r$   |   | j S X d S )N)rF   r    r_   )r   r1   r$   r   r   r	   rT      s
    zExecutor._get_unchanged_sourcesc             O   s*   y| j S  tk
r$   |   | j S X d S )N)rG   r    r_   )r   r1   r$   r   r   r	   rU     s
    zExecutor._get_unchanged_targetsc             C   sB   | j s
g S | j d | j| }|d dkr6|dd  }|  | S )Nr   $r4   )rH   Zget_targetsr/   rV   )r   Ztargets_stringr   r   r	   get_action_targets  s    zExecutor.get_action_targetsc             C   s,   t j|s"|st jd|g}|| _d S )NzExecutor must have an action.)r,   rZ   Zis_Listr-   Z	UserErrorrH   )r   r'   r   r   r	   rM     s
    zExecutor.set_action_listc             C   s    | j d krg S | j| j  | j S )N)rH   r>   r?   )r   r   r   r	   r*     s    
zExecutor.get_action_listc             C   s$   g }x| j D ]}||j qW |S )z5Returns all targets for all batches of this Executor.)r.   rW   r   )r   resultbatchr   r   r	   r6     s    zExecutor.get_all_targetsc             C   s$   g }x| j D ]}||j qW |S )z5Returns all sources for all batches of this Executor.)r.   rW   r   )r   rc   rd   r   r   r	   r7   #  s    zExecutor.get_all_sourcesc             C   s0   t jg }x|  D ]}||  qW |S )a  Returns all unique children (dependencies) for all batches
        of this Executor.

        The Taskmaster can recognize when it's already evaluated a
        Node, so we don't have to make this list unique for its intended
        canonical use case, but we expect there to be a lot of redundancy
        (long lists of batched .cc files #including the same .h files
        over and over), so removing the duplicates once up front should
        save the Taskmaster a lot of work.
        )r,   rZ   
UniqueListr6   rW   children)r   rc   r#   r   r   r	   get_all_children*  s    zExecutor.get_all_childrenc             C   s8   t jg }x&|  D ]}|jdk	r||j qW |S )z`Returns all unique (order-only) prerequisites for all batches
        of this Executor.
        N)r,   rZ   re   r6   ZprerequisitesrW   )r   rc   r#   r   r   r	   get_all_prerequisites:  s
    
zExecutor.get_all_prerequisitesc             C   s.   t jg }x|  D ]}||j qW |S )ziReturns all side effects for all batches of this
        Executor used by the underlying Action.
        )r,   rZ   re   rb   rW   Zside_effects)r   rc   r#   r   r   r	   get_action_side_effectsD  s    z Executor.get_action_side_effectsc             C   sn   y
| j d S  tk
r   Y nX i }x| jD ]}|| q,W ddl}| jpT|j }||}|| j d< |S )zUFetch or create the appropriate build Environment
        for this Executor.
        r(   r   N)	rB   KeyErrorr@   updateZSCons.Defaultsr/   ZDefaultsZDefaultEnvironmentZOverride)r   Z	overridesZodictr,   r/   	build_envr   r   r	   r(   N  s    


zExecutor.get_build_envc          	   C   sT   |   }y| jd jd j}W n ttfk
r:   d}Y nX ||||  |  S )zHFetch the scanner path for this executor's targets and sources.
        r   N)	r(   r.   r   cwd
IndexErrorr    pathr6   r7   )r   scannerr/   rm   r   r   r	   get_build_scanner_pathi  s    
zExecutor.get_build_scanner_pathc             C   s    | j  }|| | |d< |S )Nr&   )rA   copyrk   )r   r$   rc   r   r   r	   r)   u  s    

zExecutor.get_kwc             K   s   t | j | ||S )N)_do_execute_maprI   )r   r#   r$   r   r   r	   __call__  s    zExecutor.__call__c             C   s
   i | _ d S )N)rB   )r   r   r   r	   cleanup  s    zExecutor.cleanupc                s:   t  jdkst fdd|D } jd j| dS )zAdd source files to this Executor's list.  This is necessary
        for "multi" Builders that can be called repeatedly to build up
        a source file list for a given target.r4   c                s    g | ]}| j d  jkr|qS )r   )r.   r   )r8   x)r   r   r	   r9     s    z(Executor.add_sources.<locals>.<listcomp>r   N)lenr.   AssertionErrorr   rW   )r   r   r   )r   r	   add_sources  s    zExecutor.add_sourcesc             C   s   | j d jS )Nr   )r.   r   )r   r   r   r	   get_sources  s    zExecutor.get_sourcesc             C   s   | j t|| dS )ao  Add pair of associated target and source to this Executor's list.
        This is necessary for "batch" Builders that can be called repeatedly
        to build up a list of matching target and source files that will be
        used in order to update multiple target files at once from multiple
        corresponding source files, for tools like MSVC that support it.N)r.   appendr   )r   r   r   r   r   r	   	add_batch  s    zExecutor.add_batchc             C   sB   x<|   D ]0}| r
d}tj||| jd jd f q
W dS )zs
        Preparatory checks for whether this Executor can go ahead
        and (try to) build its targets.
        z-Source `%s' not found, needed by target `%s'.r   N)r7   Zmissingr,   r-   Z	StopErrorr.   r   )r   sr2   r   r   r	   prepare  s    zExecutor.preparec             C   s   | j | d S )N)r>   r{   )r   r'   r   r   r	   add_pre_action  s    zExecutor.add_pre_actionc             C   s   | j | d S )N)r?   r{   )r   r'   r   r   r	   add_post_action  s    zExecutor.add_post_actionc             C   s   t | j | S )N)_execute_str_maprJ   )r   r   r   r	   r     s    zExecutor.__str__c             C   s   |    d| _d| _d S )Nr   )ru   rI   rJ   )r   r   r   r	   nullify  s    zExecutor.nullifyc                sp   y
| j d S  tk
r   Y nX |  |  }|  |   tdd fdd|D }|| j d< |S )zFetch the signature contents.  This is the main reason this
        class exists, so we can compute this once and cache it regardless
        of how many target or source Nodes there are.

        Returns bytes
        get_contentsr   zutf-8c                s   g | ]}|  qS r   )r   )r8   r'   )all_sourcesall_targetsr/   r   r	   r9     s   z)Executor.get_contents.<locals>.<listcomp>)rB   rj   r(   r*   r6   r7   	bytearrayr:   )r   rH   rc   r   )r   r   r/   r	   r     s    


zExecutor.get_contentsc             C   s   dS )zFetch a time stamp for this Executor.  We don't have one, of
        course (only files do), but this is the interface used by the
        timestamp module.
        r   r   )r   r   r   r	   get_timestamp  s    zExecutor.get_timestampc             C   s   |  ||   d S )N)scanr6   )r   rp   r   r   r	   scan_targets  s    zExecutor.scan_targetsc             C   s    | j d jr| ||   d S )Nr   )r.   r   r   r7   )r   rp   r   r   r	   scan_sources  s    zExecutor.scan_sourcesc       	   	   C   st   |   }| j}|  }g }x*|D ]"}|  |||||| q W ||   x|  D ]}|| q^W dS )a  Scan a list of this Executor's files (targets or sources) for
        implicit dependencies and update all of the targets with them.
        This essentially short-circuits an N*M scan of the sources for
        each individual target, which is a hell of a lot more efficient.
        N)r(   rq   r)   ZdisambiguaterW   get_implicit_depsr6   Zadd_to_implicit)	r   rp   Z	node_listr/   ro   r$   depsr!   Ztgtr   r   r	   r     s    
zExecutor.scanr   c             C   s   |ft | S )N)tuple)r   r!   ignorer   r   r	   _get_unignored_sources_key  s    z#Executor._get_unignored_sources_keyc                s   |ft | }y| jd }W n" tk
r>   i }|| jd< Y n X y|| S  tk
r\   Y nX |rg }x*| jD ]}||jkrn|j}P qnW n|  }|ri  x|D ]}d |< qW  fdd|D }|||< |S )Nget_unignored_sourcesr4   c                s   g | ]}| kr|qS r   r   )r8   r}   )idictr   r	   r9     s    z2Executor.get_unignored_sources.<locals>.<listcomp>)r   rB   rj   r.   r   r   r7   )r   r!   r   keyZ	memo_dictZ
sourcelistr^   r   r   )r   r	   r     s0    

zExecutor.get_unignored_sourcesc             C   sB   g }|   }x0|  D ]$}||  |  |}|| qW |S )zcReturn the executor's implicit dependencies, i.e. the nodes of
        the commands to be executed.)r(   r*   r   r6   r7   rW   )r   rc   rl   r0   r   r   r   r	   r     s    
zExecutor.get_implicit_deps)r   )r   )1r   r   r   r   r   r
   rV   r_   rN   rO   rP   rQ   rR   rS   rT   rU   rb   rM   r*   r6   r7   rg   rh   ri   r,   ZMemoizeZCountMethodCallr(   rq   r)   rt   ru   ry   rz   r|   r~   r   r   r   r   r   r   r   r   r   r   ZCountDictCallr   r   r   r   r   r	   r=      sX   






!r=   )	metaclassc             C   s   t |  S )N)_batch_executors)r   r   r   r	   GetBatchExecutor)  s    r   c             C   s   | t kst|t | < d S )N)r   rx   )r   r&   r   r   r	   AddBatchExecutor,  s    r   c               @   s,   e Zd ZddlZdZejdZdd ZdS )NullEnvironmentr   Nc             C   s   | j S )N)	_CacheDir)r   r   r   r	   get_CacheDir7  s    zNullEnvironment.get_CacheDir)	r   r   r   ZSCons.CacheDirr,   Z_CacheDir_pathZCacheDirr   r   r   r   r   r	   r   3  s   r   c               C   s   t dkrt a t S )z,Use singleton pattern for Null Environments.N)nullenvr   r   r   r   r	   get_NullEnvironment;  s    r   c               @   s   e Zd ZdZdZdd Zdd Zdd Zd	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)S )*Nulla,  A null Executor, with a null build Environment, that does
    nothing when the rest of the methods call it.

    This might be able to disappear when we refactor things to
    disassociate Builders from Nodes entirely, so we're not
    going to worry about unit tests for this--at least for now.
    )r>   r?   r/   r@   r.   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   c             O   s0   t jjrt| d t|d d d  g g| _d S )NzExecutor.Nullr   )r,   rK   rL   r   r   r.   )r   r1   r$   r   r   r	   r
   \  s    
zNull.__init__c             C   s   t  S )N)r   )r   r   r   r	   r(   `  s    zNull.get_build_envc             C   s   d S )Nr   )r   r   r   r	   rq   b  s    zNull.get_build_scanner_pathc             C   s   d S )Nr   )r   r   r   r	   ru   d  s    zNull.cleanupc             C   s   d S )Nr   )r   r   r   r	   r~   f  s    zNull.preparec             O   s   t dS )Nr   )r   )r   r1   r$   r   r   r	   r   h  s    zNull.get_unignored_sourcesc             C   s   g S )Nr   )r   r   r   r	   rb   j  s    zNull.get_action_targetsc             C   s   g S )Nr   )r   r   r   r	   r*   l  s    zNull.get_action_listc             C   s   | j d jS )Nr   )r.   r   )r   r   r   r	   r6   n  s    zNull.get_all_targetsc             C   s   | j d jd jS )Nr   )r.   r   r   )r   r   r   r	   r7   p  s    zNull.get_all_sourcesc             C   s   | j d jd  S )Nr   )r.   r   rf   )r   r   r   r	   rg   r  s    zNull.get_all_childrenc             C   s   g S )Nr   )r   r   r   r	   rh   t  s    zNull.get_all_prerequisitesc             C   s   g S )Nr   )r   r   r   r	   ri   v  s    zNull.get_action_side_effectsc             O   s   dS )Nr   r   )r   r1   r$   r   r   r	   rt   x  s    zNull.__call__c             C   s   dS )Nr   r   )r   r   r   r	   r   z  s    zNull.get_contentsc             C   s    | j }t| _| g  || _ dS )z3Morph this Null executor to a real Executor object.N)r.   r=   	__class__r
   )r   r.   r   r   r	   _morph|  s    
zNull._morphc             C   s   |    | | d S )N)r   r   )r   r'   r   r   r	   r     s    zNull.add_pre_actionc             C   s   |    | | d S )N)r   r   )r   r'   r   r   r	   r     s    zNull.add_post_actionc             C   s   |    | | d S )N)r   rM   )r   r'   r   r   r	   rM     s    zNull.set_action_listN)r   r   r   r   r   r
   r(   rq   ru   r~   r   rb   r*   r6   r7   rg   rh   ri   rt   r   r   r   r   rM   r   r   r   r	   r   C  s*   
r   )r   collectionsZSCons.Errorsr,   ZSCons.MemoizeZ
SCons.UtilZSCons.compatr   ZSCons.Debugr   r   UserListr   r   r   r%   r3   rs   r;   r<   r   objectr=   r   r   r   r   rZ   r   r   r   r   r   r   r	   <module>   s<      