B
    ›³ëb¾á  ã               @   sÈ  d 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 ddlZddlm	Z	 ddl
Zddl
mZ ddlZddlZddlZddlmZmZ G dd„ dƒZd	Zd	Zd
ZdZdd„ Zdd„ Ze d¡Zdd„ Zdd„ Zd@dd„Zdd„ Zdd„ Zdd„ Z dd„ Z!dd „ Z"d!d"„ Z#d#d$„ Z$G d%d&„ d&ƒZ%G d'd(„ d(e%ƒZ&d)d*„ Z'da(d+d,„ Z)d-d.„ Z*dAd0d1„Z+G d2d3„ d3e&ƒZ,G d4d5„ d5e%ƒZ-G d6d7„ d7e-e,ƒZ.G d8d9„ d9e&ƒZ/G d:d;„ d;e%ƒZ0G d<d=„ d=ƒZ1G d>d?„ d?ƒZ2dS )Ba¯  SCons Actions.

Information about executing any sort of action that
can build one or more target Nodes (typically files) from one or more
source Nodes (also typically files) given a specific Environment.

The base class here is ActionBase.  The base class supplies just a few
utility methods and some generic methods for displaying information
about an Action in response to the various commands that control printing.

A second-level base class is _ActionAction.  This extends ActionBase
by providing the methods that can be used to show and perform an
action.  True Action objects will subclass _ActionAction; Action
factory class objects will subclass ActionBase.

The heavy lifting is handled by subclasses for the different types of
actions we might execute:

    CommandAction
    CommandGeneratorAction
    FunctionAction
    ListAction

The subclasses supply the following public interface methods used by
other modules:

    __call__()
        THE public interface, "calling" an Action object executes the
        command or Python function.  This also takes care of printing
        a pre-substitution command for debugging purposes.

    get_contents()
        Fetches the "contents" of an Action for signature calculation
        plus the varlist.  This is what gets checksummed to decide
        if a target needs to be rebuilt because its action changed.

    genstring()
        Returns a string representation of the Action *without*
        command substitution, but allows a CommandGeneratorAction to
        generate the right action based on the specified target,
        source and env.  This is used by the Signature subsystem
        (through the Executor) to obtain an (imprecise) representation
        of the Action operation for informative purposes.


Subclasses also supply the following methods for internal use within
this module:

    __str__()
        Returns a string approximation of the Action; no variable
        substitution is performed.

    execute()
        The internal method that really, truly, actually handles the
        execution of a command or Python function.  This is used so
        that the __call__() methods can take care of displaying any
        pre-substitution representations, and *then* execute an action
        without worrying about the specific Actions involved.

    get_presig()
        Fetches the "contents" of a subclass for signature calculation.
        The varlist is added to this to produce the Action's contents.
        TODO(?): Change this to always return bytes and not str?

    strfunction()
        Returns a substituted string representation of the Action.
        This is used by the _ActionAction.show() command to display the
        command/function that will be executed to generate the target(s).

There is a related independent ActionCaller class that looks like a
regular Action, and which serves as a wrapper for arbitrary functions
that we want to let the user specify the arguments to now, but actually
execute later (when an out-of-date check determines that it's needed to
be executed, for example).  Objects of this class are returned by an
ActionFactory class that provides a __call__() method as a convenient
way for wrapping up the functions.

é    N)ÚDEVNULL)ÚOrderedDict)ÚlogInstanceCreation)Ú	is_StringÚis_Listc               @   s   e Zd ZdS )Ú_nullN)Ú__name__Ú
__module__Ú__qualname__© r   r   ú+lib/python3.7/site-packages/SCons/Action.pyr   y   s   r   TFé   c             C   s"   y|   ¡ S  tk
r   | S X d S )N)ÚrfileÚAttributeError)Únr   r   r   r   ‰   s    r   c             C   s   | S )Nr   )Úsr   r   r   Údefault_exitstatfunc   s    r   z^[\'"](.*)[\'"]$c             C   sh   y
t | jƒS  tk
rb   yt | jjƒS  tk
r\   yt| ƒS  tk
rV   t | ƒS X Y nX Y nX dS )z?Return the signature contents of a callable Python object.
    N)Ú_function_contentsÚ__func__r   Ú__call__Ú_code_contents)Úobjr   r   r   Ú_callable_contents–   s    
r   c             C   sÎ   y
t | jƒS  tk
rÈ   yt | jjƒS  tk
rÂ   yt| ƒS  tk
r¼   yt | ƒS  tk
r¶ } zByt| ƒS  tjttfk
r¤ } zt	t
| ƒdƒS d}~X Y nX W dd}~X Y nX Y nX Y nX Y nX dS )z¢Return the signature contents of any Python object.

    We have to handle the case where object contains a code object
    since it can be pickled directly.
    zutf-8N)r   r   r   r   r   Ú_object_instance_contentÚpickleZPicklingErrorÚ	TypeErrorÚ	bytearrayÚrepr)r   ZaeÚexr   r   r   Ú_object_contents¬   s     
r   c                sþ   t d | jt| jƒ¡dƒ}| d¡ | t tt| jƒƒdƒ¡ | d¡ | t tt| jƒƒdƒ¡ ‡ fdd„| j	D ƒ}| d¡ | t ddƒ 
|¡¡ | d¡ d	d„ | jD ƒ}| d¡ | t ddƒ 
|¡¡ | d¡ | d¡ | | j¡ | d¡ |S )
a  Return the signature contents of a code object.

    By providing direct access to the code object of the
    function, Python makes this extremely easy.  Hooray!

    Unfortunately, older versions of Python include line
    number indications in the compiled byte code.  Boo!
    So we remove the line number byte codes to prevent
    recompilations from moving a Python function.

    See:
      - https://docs.python.org/2/library/inspect.html
      - http://python-reference.readthedocs.io/en/latest/docs/code/index.html

    For info on what each co\_ variable provides

    The signature is as follows (should be byte/chars):
    co_argcount, len(co_varnames), len(co_cellvars), len(co_freevars),
    ( comma separated signature for each object in co_consts ),
    ( comma separated signature for each object in co_names ),
    ( The bytecode with line number bytecodes removed from  co_code )

    co_argcount - Returns the number of positional arguments (including arguments with default values).
    co_varnames - Returns a tuple containing the names of the local variables (starting with the argument names).
    co_cellvars - Returns a tuple containing the names of local variables that are referenced by nested functions.
    co_freevars - Returns a tuple containing the names of free variables. (?)
    co_consts   - Returns a tuple containing the literals used by the bytecode.
    co_names    - Returns a tuple containing the names used by the bytecode.
    co_code     - Returns a string representing the sequence of bytecode instructions.

    z{}, {}zutf-8s   , c                s   g | ]}|ˆ krt |ƒ‘qS r   )r   )Ú.0Úcc)Ú	docstringr   r   ú
<listcomp>  s    z"_code_contents.<locals>.<listcomp>s   ,(ú,ó   )c             S   s   g | ]}t t|ƒƒ‘qS r   )r   r   )r    r!   r   r   r   r#     s    )r   ÚformatÚco_argcountÚlenÚco_varnamesÚextendÚstrÚco_cellvarsÚco_freevarsÚ	co_constsÚjoinÚco_namesÚco_code)Úcoder"   ÚcontentsÚzr   )r"   r   r   Õ   s"    %







r   c             C   sÒ   t | j| jƒg}| jrXdd„ | jD ƒ}tdƒ}| tdƒ |¡¡ | d¡ | |¡ n
| d¡ | jpjg }ydd„ |D ƒ}W n t	k
r–   g }Y nX | d¡ | tdƒ |¡¡ | d¡ tdƒ |¡}|S )	aâ  
    The signature is as follows (should be byte/chars):
    < _code_contents (see above) from func.__code__ >
    ,( comma separated _object_contents for function argument defaults)
    ,( comma separated _object_contents for any closure contents )


    See also: https://docs.python.org/3/reference/datamodel.html
      - func.__code__     - The code object representing the compiled function body.
      - func.__defaults__ - A tuple containing default argument values for those arguments that have defaults, or None if no arguments have a default value
      - func.__closure__  - None or a tuple of cells that contain bindings for the function's free variables.

    :Returns:
      Signature contents of a function. (in bytes)
    c             S   s   g | ]}t |ƒ‘qS r   )r   )r    r!   r   r   r   r#   1  s    z&_function_contents.<locals>.<listcomp>s   ,(ó   ,r%   s   ,()c             S   s   g | ]}t |jƒ‘qS r   )r   Úcell_contents)r    Úxr   r   r   r#   ?  s    ó    )
r   Ú__code__Ú__doc__Ú__defaults__r   r*   r/   ÚappendÚ__closure__r   )Úfuncr3   Zfunction_defaults_contentsÚdefaultsZclosureZclosure_contentsÚretvalr   r   r   r     s$    





r   c                sd  t ƒ }ˆ dkrdS tˆ tjjƒr,tj ˆ ¡S ˆ j}t ˆ jjdƒ}t ˆ jjdƒ}t t	t
 ˆ jg¡ƒdƒ}‡ fdd„tˆ ƒD ƒ}| ¡  d dd„ |D ƒ¡}t |dƒ}‡ fdd„tˆ ƒD ƒ}	|	 ¡  g }
x$|	D ]}ttˆ |ƒƒ}|
 |¡ qÊW t d	ƒ}| |¡ | d
¡ | |¡ | d¡ | |¡ | d¡ | t dƒ |
¡¡ | d¡ | |¡ | d¡ |S )a  
    Returns consistant content for a action class or an instance thereof

    :Parameters:
      - `obj` Should be either and action class or an instance thereof

    :Returns:
      bytearray or bytes representing the obj suitable for generating a signature from.
    Ns   N.zutf-8c                sL   g | ]D}|d d… dkst  tˆ |ƒ¡st  tˆ |ƒ¡s|tˆ |dƒf‘qS )Né   Ú__ÚNone)ÚinspectÚismethodÚgetattrZ	isbuiltin)r    Úp)r   r   r   r#   c  s    z,_object_instance_content.<locals>.<listcomp>r$   c             S   s    g | ]}d |d |d f ‘qS )z%s=%sr   r   r   )r    rG   r   r   r   r#   e  s    c                s    g | ]}t  tˆ |ƒ¡r|‘qS r   )rD   rE   rF   )r    rG   )r   r   r   r#   h  s    ó   {ó   :s   }[[s   ]]{{r5   s   }}{{{s   }}})r   Ú
isinstanceÚSConsÚUtilZBaseStringTypesÚto_bytesÚ	__class__r   r	   r   rD   ZgetclasstreeÚdirÚsortr/   r   rF   r<   r*   )r   r@   Z
inst_classZinst_class_nameZinst_class_moduleZinst_class_hierarchyZ
propertiesZproperties_strZproperties_bytesÚmethodsZmethod_contentsÚmÚvr   )r   r   r   K  s>    











r   c             C   sˆ   t | ƒ}t |ƒ}|d kr|S |d kr(|S t|tƒr^t|tƒrLt|j|j ƒS t|j|g ƒS n&t|tƒrxt|g|j ƒS t||gƒS d S )N)ÚActionrJ   Ú
ListActionÚlist)Zact1Zact2Za1Za2r   r   r   Ú_actionAppend†  s    


rW   c             C   sÊ   |  dd¡}t|ƒr|f}t|ƒ|d< | rš| d }|dksBt|ƒrL||d< nt|ƒr^||d< ntj d¡‚t| ƒdkršttj 	| dd… ¡ƒ|d  |d< |  dt
¡t
k	rÆ|  dt
¡t
k	rÆtj d	¡‚dS )
zThis converts any arguments after the action argument into
    their equivalent keywords and adds them to the kw argument.
    Úvarlistr   r   NÚcmdstrÚstrfunctionz…Invalid command display variable type. You must either pass a string or a callback which accepts (target, source, env) as parameters.r   z8Cannot have both strfunction and cmdstr args to Action())Úgetr   ÚtupleÚcallablerK   ÚErrorsÚ	UserErrorr(   rL   Zflattenr   )ÚargsÚkwrS   Z
cmdstrfuncr   r   r   Ú_do_create_keywordsœ  s$     

$rb   c             C   sè   t | tƒr| S t| ƒrdtj | ¡}|r0t||ƒS t| ƒ d¡}t	|ƒdkrZt
|d f|ŽS t||ƒS t| ƒrxt
| f|ŽS t| ƒrÄy|d }|d= W n tk
rª   d}Y nX |r¶t}nt}|| |ƒS t | tƒsØt | tƒrätd|  ƒ‚dS )ab  This is the actual "implementation" for the
    Action factory method, below.  This handles the
    fact that passing lists to Action() itself has
    different semantics than passing lists as elements
    of lists.

    The former will create a ListAction, the latter
    will create a CommandAction by converting the inner
    list elements to strings.Ú
r   r   Ú	generatorz5Don't know how to create an Action from a number (%s)N)rJ   Ú
ActionBaser   rK   rL   Zget_environment_varÚ
LazyActionr+   Úsplitr(   ÚCommandActionÚ_do_create_list_actionr   r]   ÚKeyErrorÚCommandGeneratorActionÚFunctionActionÚintÚfloatr   )Úactra   ÚvarZcommandsÚgenZaction_typer   r   r   Ú_do_create_action¸  s2    





rr   c             C   sZ   g }x(| D ] }t ||ƒ}|dk	r
| |¡ q
W |s:tg ƒS t|ƒdkrN|d S t|ƒS dS )zhA factory for list actions.  Convert the input list into Actions
    and then wrap them in a ListAction.Nr   r   )rr   r<   rU   r(   )ro   ra   ZactsÚaZaar   r   r   ri   í  s    

 ri   c             O   s&   t ||ƒ t| ƒrt| |ƒS t| |ƒS )zA factory for action objects.)rb   r   ri   rr   )ro   r`   ra   r   r   r   rT   ü  s    

rT   c               @   s^   e Zd ZdZdd„ Zdd„ ZeZdd„ Zdd	„ Zd
d„ Z	dd„ Z
dd„ Zddd„Zdd„ ZdS )re   zÀBase class for all types of action objects that can be held by
    other objects (Builders, Executors, etc.)  This provides the
    common methods for manipulating and combining those actions.c             C   s
   | j |kS )N)Ú__dict__)ÚselfÚotherr   r   r   Ú__eq__
  s    zActionBase.__eq__c             C   s   d S )Nr   )ru   ÚenvÚtargetÚsourcer   r   r   Úno_batch_key  s    zActionBase.no_batch_keyc             C   s   t | ƒS )N)r+   )ru   ry   rz   rx   r   r   r   Ú	genstring  s    zActionBase.genstringc             C   sÆ   |   |||¡}t|ttfƒs(t|dƒ}nt|ƒ}|  |||¡}t|ƒrL|f}xR|D ]J}t|tƒrŒ| tj 	| 
d| d tjj||¡¡¡ qRtdt|ƒ ƒ‚qRW t|ttfƒr²|S tdt|ƒ ƒ‚d S )Nzutf-8z${Ú}z:WE SHOULD NEVER GET HERE result should be bytearray not:%sz?WE SHOULD NEVER GET HERE - #2 result should be bytearray not:%s)Ú
get_presigrJ   Úbytesr   Úget_varlistr   r*   rK   rL   rM   Úsubst_target_sourceÚSubstÚ	SUBST_SIGÚ	ExceptionÚtype)ru   ry   rz   rx   ÚresultZvlrS   r   r   r   Úget_contents  s     

,zActionBase.get_contentsc             C   s
   t | |ƒS )N)rW   )ru   rv   r   r   r   Ú__add__6  s    zActionBase.__add__c             C   s
   t || ƒS )N)rW   )ru   rv   r   r   r   Ú__radd__9  s    zActionBase.__radd__c             C   s   || _ t| ƒ d¡}d | _ |S )Nrc   )Ú
presub_envr+   rg   )ru   rx   Úlinesr   r   r   Úpresub_lines<  s    zActionBase.presub_linesNc             C   s   | j S )N)rX   )ru   ry   rz   rx   Úexecutorr   r   r   r€   H  s    zActionBase.get_varlistc             C   s   | j S )zg
        Returns the type of targets ($TARGETS, $CHANGED_TARGETS) used
        by this action.
        )Útargets)ru   rx   r   r   r   r   Úget_targetsK  s    zActionBase.get_targets)N)r   r	   r
   r:   rw   r{   Ú	batch_keyr|   r‡   rˆ   r‰   rŒ   r€   r   r   r   r   r   re     s   !
re   c               @   sH   e Zd ZdZeededdddfdd„Zdd„ Zeeeeedfd	d
„ZdS )Ú_ActionActionz2Base class for actions that create output objects.r   Nz$TARGETSc	             K   sv   || _ |tk	r$|d krd | _ n|| _|| _|| _|| _|s>t}|| _|| _|rrt	|ƒsbdd„ }
|
}t
j | |d¡ d S )Nc             S   s   t | ƒt |ƒfS )N)Úid)ru   rx   ry   rz   r   r   r   Údefault_batch_keyn  s    z1_ActionAction.__init__.<locals>.default_batch_keyr   )rY   r   rZ   rX   ÚpresubÚchdirr   ÚexitstatfuncrŽ   r]   rK   rL   Z	AddMethod)ru   rY   rZ   rX   r”   r•   r–   r   rŽ   ra   r“   r   r   r   Ú__init__U  s"    z_ActionAction.__init__c             C   s   t j |d ¡ dS )zµ
        In python 3, and in some of our tests, sys.stdout is
        a String io object, and it takes unicode strings only
        This code assumes s is a regular string.
        rc   N)ÚsysÚstdoutÚwrite)ru   r   ry   rz   rx   r   r   r   Úprint_cmd_lines  s    z_ActionAction.print_cmd_linec
          	   C   sŒ  t |ƒs|g}t |ƒs|g}|tkr6| j}|tkr6t}|tkrD| j}|tkrPt}|tkr\t}|tkrj| j}d }
|rÒt 	¡ }
yt
| ¡ ƒ}W nF tk
rÐ   t|ƒsÌ|	r¾t
|	jd jd jƒ}nt
|d jƒ}Y nX |r$|	rì|	 ¡ }|	 ¡ }d tt
|ƒ¡}d |  |¡¡}d||f }tj |¡ d }|rì| jrì|	rL|	 ¡ }|	 ¡ }y|  ||||	¡}W n$ tk
r„   |  |||¡}Y nX |rì|r¢dt|ƒ | }y
|j}W n tk
rÈ   | j}Y nX |dƒ}|sÞ| j}|||||ƒ d}|rf|rt |¡ zL| j||||	d}t|t j!j"ƒrH||j#ƒ}|rB||_#n|}n||ƒ}W d |
rdt |
¡ X |rˆ|
rˆ|dt|
ƒ |||ƒ |S )	Nr   z and z
  zBuilding %s with action:
  %s
zos.chdir(%s)
ZPRINT_CMD_LINE_FUNC)r   zos.chdir(%s))$r   r   r”   Úprint_actions_presubr–   Úprint_actionsÚexecute_actionsr•   ÚosÚgetcwdr+   Zget_abspathr   r   ZbatchesrŽ   rO   Úget_all_targetsÚget_all_sourcesr/   ÚmaprŒ   r˜   r™   rš   rZ   r   r   r[   r›   ÚexecuterJ   rK   r^   Ú
BuildErrorÚstatus)ru   ry   rz   rx   r–   r”   Úshowr¤   r•   r   Zsave_cwdÚtÚlÚoutÚcmdr[   Z
print_funcÚstatr   r   r   r   r   {  sŽ    


z_ActionAction.__call__)r   r	   r
   r:   r   r—   r›   r   r   r   r   r   r‘   S  s   	r‘   c             C   sH   g }x8t t| ƒD ]*}d|ks$d|kr0d| d }| |¡ qW d |¡S )z\Takes a list of command line arguments and returns a pretty
    representation for printing.ú ú	ú")r£   r+   r<   r/   )Úcmd_listZclÚargr   r   r   Ú_string_from_cmd_listÒ  s    r²   c             C   s<   y| d S  t k
r6   ts2ddl}|j ¡ d atS X dS )a_  
    A fiddlin' little function that has an 'import SCons.Environment' which
    can't be moved to the top level without creating an import loop.  Since
    this import creates a local variable named 'SCons', it blocks access to
    the global variable, so we move it here to prevent complaints about local
    variables being used uninitialized.
    ÚENVr   N)rj   Údefault_ENVZSCons.EnvironmentZEnvironment)rx   rK   r   r   r   Úget_default_ENVß  s    	rµ   c             C   sŠ   t | ƒ}|  d¡}|r†yt|ƒ}W n  tk
rB   tj d¡‚Y nDX | ¡ }x8|D ]0}|| |||ƒ}t|t	ƒsRtj d|› d¡‚qRW |S )zÁ
    First get default environment.
    Then if SHELL_ENV_GENERATORS is set and is iterable,
    call each callable in that list to allow it to alter
    the created execution environment.
    ZSHELL_ENV_GENERATORSz(SHELL_ENV_GENERATORS must be iteratable.zSHELL_ENV_GENERATORS function: z must return a dict.)
rµ   r[   Úiterr   rK   r^   r_   ÚcopyrJ   Údict)rx   ry   rz   r³   Z	shell_genZ
shell_gensrd   r   r   r   Ú_resolve_shell_envö  s    


r¹   Úignorec             K   s<  x.dD ]&}|  |¡}t|ƒr|dkrt||< qW |  dd¡}|dkrLt| ƒ}i }xL| ¡ D ]@\}}	t|	ƒrŽtj |	¡}	t	j
 tt|	ƒ¡||< qZt|	ƒ||< qZW ||d< zZytj|f|Ž}
W nB tk
rü } z$|dkrÖ‚ G dd„ dƒ}||ƒ}
W dd}~X Y nX W dx2| ¡ D ]&\}}t t|ddƒ¡r| ¡  qW X |
S )	aB  Wrapper for subprocess which pulls from construction env.

    Use for calls to subprocess which need to interpolate values from
    an SCons construction environment into the environment passed to
    subprocess.  Adds an an error-handling argument.  Adds ability
    to specify std{in,out,err} with "'devnull'" tag.
    )Ústdinr™   ÚstderrÚdevnullrx   NÚraisec               @   sR   e Zd Zdd„ Zdd„ Zdd„ Zddd	„Zd
d„ ZdZG dd„ dƒZ	e	ƒ  Z
ZdS )z_subproc.<locals>.dummyPopenc             S   s
   || _ d S )N)Ú	exception)ru   Úer   r   r   r—   9  s    z%_subproc.<locals>.dummyPopen.__init__c             S   s   | S )Nr   )ru   r   r   r   Ú	__enter__@  s    z&_subproc.<locals>.dummyPopen.__enter__c             W   s   d S )Nr   )ru   r`   r   r   r   Ú__exit__C  s    z%_subproc.<locals>.dummyPopen.__exit__Nc             S   s   dS )N)Ú rÃ   r   )ru   Úinputr   r   r   ÚcommunicateF  s    z(_subproc.<locals>.dummyPopen.communicatec             S   s
   | j j S )N)r¿   Úerrno)ru   r   r   r   ÚwaitI  s    z!_subproc.<locals>.dummyPopen.waitc               @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )z_subproc.<locals>.dummyPopen.fc             S   s   dS )NrÃ   r   )ru   r   r   r   ÚreadN  r8   z#_subproc.<locals>.dummyPopen.f.readc             S   s   dS )NrÃ   r   )ru   r   r   r   ÚreadlineO  r8   z'_subproc.<locals>.dummyPopen.f.readlinec             S   s   t dƒS )Nr   )r¶   )ru   r   r   r   Ú__iter__P  r8   z'_subproc.<locals>.dummyPopen.f.__iter__N)r   r	   r
   rÈ   rÉ   rÊ   r   r   r   r   ÚfM  s   rË   )N)r   r	   r
   r—   rÁ   rÂ   rÅ   rÇ   r»   rË   r™   r¼   r   r   r   r   Ú
dummyPopen8  s   
rÌ   Úclose)r[   r   r   rµ   Úitemsr   rK   rL   Úflatten_sequencerŸ   Úpathsepr/   r£   r+   Ú
subprocessÚPopenÚEnvironmentErrorrD   rE   rF   rÍ   )Z	scons_envr«   Úerrorra   ÚstreamÚior³   Znew_envÚkeyÚvalueZpobjrÀ   rÌ   ÚkrS   r   r   r   Ú_subproc  s2    


  rÚ   c                   sj   e Zd ZdZ‡ fdd„Zdd„ Zddd„Zdd	d
„Zddd„Zddd„Z	ddd„Z
dd„ Zdd„ Z‡  ZS )rh   z$Class for command-execution actions.c                sH   t jjrt| dƒ tƒ jf |Ž t|ƒr>dd„ |D ƒr>tdƒ‚|| _d S )NzAction.CommandActionc             S   s   g | ]}t |ƒr|‘qS r   )r   )r    Úcr   r   r   r#   l  s    z*CommandAction.__init__.<locals>.<listcomp>z3CommandAction should be given only a single command)	rK   ÚDebugÚtrack_instancesr   Úsuperr—   r   r   r°   )ru   r«   ra   )rN   r   r   r—   ^  s    
 
zCommandAction.__init__c             C   s&   t | jƒrd tt| jƒ¡S t| jƒS )Nr­   )r   r°   r/   r£   r+   )ru   r   r   r   Ú__str__q  s    
zCommandAction.__str__Nc       	      C   sä   |r|j | jd|d}n|  | jd||¡}d }d }xly|d d d }W n tk
r`   d }Y nX |dkrpd}n|dkr~d}nP |d d dd … |d d< q4W y$|d d sÂ|d dd … |d< W n tk
rØ   Y nX |||fS )Nr   )r   ú@r   ú-)Ú
subst_listr°   Ú
IndexError)	ru   ry   rz   rx   r   r†   Úsilentrº   rÛ   r   r   r   Úprocessv  s,      
   zCommandAction.processc       
      C   s€   | j d krd S | j tk	rVddlm} |r<|j| j ||d}n| | j |||¡}|rV|S |  ||||¡\}}}	|	rtdS t|d ƒS )Nr   )Ú	SUBST_RAW)r   rÃ   )rY   r   ÚSCons.Substræ   Úsubstrå   r²   )
ru   ry   rz   rx   r   ræ   rÛ   r°   rº   rä   r   r   r   rZ   ‹  s    

zCommandAction.strfunctionc             C   sŒ  t jj}t jj}y|d }W n  tk
r<   t j d¡‚Y nX y|d }W n  tk
rj   t j d¡‚Y nX t|ƒrˆ|j	|ddd„ d}| 
d	d
d„ ¡}	t|||ƒ}
xP|
 ¡ D ]D\}}t|ƒs®t|ƒræ||ƒ}tj tt|ƒ¡|
|< q®t|ƒ|
|< q®W |r| ¡ }| ¡ }|  |ttt|ƒƒ||¡\}}}xZtt|ƒD ]L}|||	ƒ}|||	|d ||
ƒ}|s8|r8d| }t jj||| |dS q8W dS )aV  Execute a command action.

        This will handle lists of commands as well as individual commands,
        because construction variable substitution may turn a single
        "command" into a list.  This means that this class can actually
        handle lists of commands, even though that's not how we use it
        externally.
        ZSHELLz$Missing SHELL construction variable.ZSPAWNz$Missing SPAWN construction variable.r   c             S   s   | S )Nr   )r7   r   r   r   Ú<lambda>²  r8   z'CommandAction.execute.<locals>.<lambda>)ÚrawÚconvZESCAPEc             S   s   | S )Nr   )r7   r   r   r   ré   ´  r8   r   zError %s)Zerrstrr¦   ÚactionÚcommand)rK   r‚   Úescape_listrL   rÏ   rj   r^   r_   r   rè   r[   r¹   rÎ   r   rŸ   rÐ   r/   r£   r+   r¡   r¢   rå   rV   r   Úfilterr(   r¥   )ru   ry   rz   rx   r   rî   rÏ   ÚshellZspawnÚescaper³   r×   rØ   r°   rº   rä   Úcmd_liner†   Úmsgr   r   r   r¤   ›  sD    	 
zCommandAction.executec             C   s\   ddl m} | j}t|ƒr,d tt|ƒ¡}nt|ƒ}|rH|j|||dS | ||||¡S dS )z¹Return the signature contents of this action's command line.

        This strips $(-$) and everything in between the string,
        since those parts don't affect signatures.
        r   )rƒ   r­   )r   N)rç   rƒ   r°   r   r/   r£   r+   r   )ru   ry   rz   rx   r   rƒ   r«   r   r   r   r~   Ú  s    zCommandAction.get_presigc             C   s®   |  dd¡}t|ƒr.|dd… dkr.| |¡}|rBt|ƒ ¡ dkrFg S yt|ƒ}W n tk
rj   d}Y nX |rx|dksˆt|ƒ ¡ dkrš|  |||||¡S |  ||||¡S dS )z?Return the implicit dependencies of this action's command line.ZIMPLICIT_COMMAND_DEPENDENCIESTNr   ú$)Ú0ZnoneZfalseZnoZoffÚall)	r[   r   rè   r+   Úlowerrm   Ú
ValueErrorÚ_get_implicit_deps_heavyweightÚ_get_implicit_deps_lightweight)ru   ry   rz   rx   r   ZicdÚicd_intr   r   r   Úget_implicit_depsë  s    

zCommandAction.get_implicit_depsc             C   s”   ddl m} |r$|j| j||d}n| | j|||¡}g }xT|D ]L}|r@t|d ƒ}	t |	¡}
|
rl|
 d¡}	| |	¡}	|	r@| 	|j
 |	¡¡ q@W |S )z
        Lightweight dependency scanning involves only scanning the first entry
        in an action string, even if it contains &&.
        r   )rƒ   )r   r   )rç   rƒ   râ   r°   r+   Ústrip_quotesÚmatchÚgroupÚWhereIsr<   ÚfsÚFile)ru   ry   rz   rx   r   rƒ   r°   Úresrò   ÚdrR   r   r   r   rú     s    



z,CommandAction._get_implicit_deps_lightweightc                s  ddl m} |j| j|dd„ d}g }xÔ|D ]Ì}	|	r,d}
x¾|	D ]¶}t|ƒ}|dksZ|
|k rà| tjdkrldnd	¡sàt 	|¡}|rŠ| 
d
¡}|rÖtj |¡r¦tj |¡nd}|sÀ|
dkrÀ| |¡}|rÖ| |j |¡¡ |
d
 }
q>|dkrìdn|
d
 }
q>W q,W ‡ ‡fdd„|D ƒS )aé  
        Heavyweight dependency scanning involves scanning more than just the
        first entry in an action string. The exact behavior depends on the
        value of icd_int. Only files are taken as implicit dependencies;
        directories are ignored.

        If icd_int is an integer value, it specifies the number of entries to
        scan for implicit dependencies. Action strings are also scanned after
        a &&. So for example, if icd_int=2 and the action string is
        "cd <some_dir> && $PYTHON $SCRIPT_PATH <another_path>", the implicit
        dependencies would be the path to the python binary and the path to the
        script.

        If icd_int is None, all entries are scanned for implicit dependencies.
        r   )rƒ   c             S   s   | S )Nr   )r7   r   r   r   ré   .  r8   z>CommandAction._get_implicit_deps_heavyweight.<locals>.<lambda>)rë   NÚnt)ú&rá   ú/)r  rá   r   z&&c                s    g | ]}|ˆkr|ˆ kr|‘qS r   r   )r    Úr)rz   ry   r   r   r#   Q  s    z@CommandAction._get_implicit_deps_heavyweight.<locals>.<listcomp>)rç   rƒ   râ   r°   r+   Ú
startswithrŸ   Únamerý   rþ   rÿ   ÚpathÚisfileÚabspathr   r<   r  r  )ru   ry   rz   rx   r   rû   rƒ   r°   r  rò   Zentry_countÚentryr  rR   rG   r   )rz   ry   r   rù     s.    





z,CommandAction._get_implicit_deps_heavyweight)N)N)N)N)N)r   r	   r
   r:   r—   rß   rå   rZ   r¤   r~   rü   rú   rù   Ú__classcell__r   r   )rN   r   rh   \  s   


?

rh   c               @   sx   e Zd ZdZdd„ Zddd„Zdd„ Zd	d
„ Zddd„Ze	e	e	e	e	dfdd„Z
ddd„Zddd„Zddd„Zdd„ ZdS )rk   z$Class for command-generator actions.c             C   s>   t jjrt| dƒ || _|| _| dd¡| _| dd¡| _d S )NzAction.CommandGeneratorActionrX   r   rŽ   z$TARGETS)	rK   rÜ   rÝ   r   rd   Úgen_kwr[   rX   rŽ   )ru   rd   ra   r   r   r   r—   V  s     
zCommandGeneratorAction.__init__Nc             C   s^   t |ƒs|g}|r"| ¡ }| ¡ }| j||||d}t|f| jŽ}|sZtj dt	|ƒ ¡‚|S )N)ry   rz   rx   Úfor_signaturezNObject returned from command generator: %s cannot be used to create an Action.)
r   r¡   r¢   rd   rT   r  rK   r^   r_   r   )ru   ry   rz   rx   r  r   ZretÚgen_cmdr   r   r   Ú	_generate]  s    z CommandGeneratorAction._generatec             C   sN   y
| j }W n tk
r"   d }Y nX |d kr6tj ¡ }|  g g |d¡}t|ƒS )Nr   )rŠ   r   rK   ZDefaultsZDefaultEnvironmentr  r+   )ru   rx   ro   r   r   r   rß   o  s    


zCommandGeneratorAction.__str__c             C   s   |   |||d¡ |||¡S )Nr   )r  r   )ru   rx   ry   rz   r   r   r   r   y  s    z CommandGeneratorAction.batch_keyc             C   s   |   |||d|¡ |||¡S )Nr   )r  r|   )ru   ry   rz   rx   r   r   r   r   r|   |  s    z CommandGeneratorAction.genstringc
          
   C   sb   |   |||d|	¡}
|
d krJtj dtttt|ƒƒƒtttt|ƒƒƒf ¡‚|
|||||||||	ƒ	S )Nr   zGWhile building `%s': Cannot deduce file extension from source files: %s)r  rK   r^   r_   r   rV   r£   r+   )ru   ry   rz   rx   r–   r”   r§   r¤   r•   r   ro   r   r   r   r     s    (zCommandGeneratorAction.__call__c             C   s   |   |||d|¡ |||¡S )z¹Return the signature contents of this action's command line.

        This strips $(-$) and everything in between the string,
        since those parts don't affect signatures.
        r   )r  r~   )ru   ry   rz   rx   r   r   r   r   r~   Œ  s    z!CommandGeneratorAction.get_presigc             C   s   |   |||d|¡ |||¡S )Nr   )r  rü   )ru   ry   rz   rx   r   r   r   r   rü   ”  s    z(CommandGeneratorAction.get_implicit_depsc             C   s   |   |||d|¡ ||||¡S )Nr   )r  r€   )ru   ry   rz   rx   r   r   r   r   r€   —  s    z"CommandGeneratorAction.get_varlistc             C   s   |   d d |d|¡ ||¡S )Nr   )r  r   )ru   rx   r   r   r   r   r   š  s    z"CommandGeneratorAction.get_targets)N)N)N)N)N)r   r	   r
   r:   r—   r  rß   r   r|   r   r   r~   rü   r€   r   r   r   r   r   rk   T  s   





rk   c               @   sL   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	d
„Zdd„ Zdd„ Z	ddd„Z
dS )rf   a  
    A LazyAction is a kind of hybrid generator and command action for
    strings of the form "$VAR".  These strings normally expand to other
    strings (think "$CCCOM" to "$CC -c -o $TARGET $SOURCE"), but we also
    want to be able to replace them with functions in the construction
    environment.  Consequently, we want lazy evaluation and creation of
    an Action in the case of the function, but that's overkill in the more
    normal case of expansion to other strings.

    So we do this with a subclass that's both a generator *and*
    a command action.  The overridden methods all do a quick check
    of the construction variable, and if it's a string we just call
    the corresponding CommandAction method to do the heavy lifting.
    If not, then we call the same-named CommandGeneratorAction method.
    The CommandGeneratorAction methods work by using the overridden
    _generate() method, that is, our own way of handling "generation" of
    an action based on what's in the construction variable.
    c             C   sB   t jjrt| dƒ tj| d| d f|Ž t j |¡| _|| _	d S )NzAction.LazyActionz${r}   )
rK   rÜ   rÝ   r   rh   r—   rL   Z	to_Stringrp   r  )ru   rp   ra   r   r   r   r—   ²  s
     
zLazyAction.__init__c             C   s$   |  | j¡}t|ƒr d|kr tS tS )Nrc   )r[   rp   r   rh   rk   )ru   rx   rÛ   r   r   r   Úget_parent_class¸  s    zLazyAction.get_parent_classc             C   sH   |r|  | jd¡}nd}t|f| jŽ}|sDtj d| jt|ƒf ¡‚|S )NrÃ   z0$%s value %s cannot be used to create an Action.)r[   rp   rT   r  rK   r^   r_   r   )ru   rx   rÛ   r  r   r   r   Ú_generate_cache¾  s    zLazyAction._generate_cacheNc             C   s
   |   |¡S )N)r  )ru   ry   rz   rx   r  r   r   r   r   r  È  s    zLazyAction._generatec             O   s"   |   |¡}|j| |||f|ž|ŽS )N)r  r   )ru   ry   rz   rx   r`   ra   rÛ   r   r   r   r   Ë  s    
zLazyAction.__call__c             C   s   |   |¡}| | |||¡S )N)r  r~   )ru   ry   rz   rx   rÛ   r   r   r   r~   Ï  s    
zLazyAction.get_presigc             C   s   |   |¡}| | ||||¡S )N)r  r€   )ru   ry   rz   rx   r   rÛ   r   r   r   r€   Ó  s    
zLazyAction.get_varlist)N)N)r   r	   r
   r:   r—   r  r  r  r   r~   r€   r   r   r   r   rf   ž  s   

rf   c                   sT   e Zd ZdZ‡ fdd„Zdd„ Zddd„Zd	d
„ Zddd„Zdd„ Z	dd„ Z
‡  ZS )rl   z"Class for Python function actions.c                sz   t jjrt| dƒ || _yt|ƒ| _W n@ tk
rf   y|j| _	W n tk
r`   t
|ƒ| _Y nX Y nX tƒ jf |Ž d S )NzAction.FunctionAction)rK   rÜ   rÝ   r   Úexecfunctionr   Úfunccontentsr   r‡   Úgcr   rÞ   r—   )ru   r  ra   )rN   r   r   r—   Û  s     
zFunctionAction.__init__c             C   sB   y| j jS  tk
r<   y
| j jjS  tk
r6   dS X Y nX d S )NZunknown_python_function)r  r   r   rN   )ru   r   r   r   Úfunction_nameë  s    
zFunctionAction.function_nameNc             C   sÆ   | j d krd S | j tk	rVddlm} |r<|j| j ||d}n| | j |||¡}|rV|S dd„ }y| jj}W n tk
r~   Y n"X |d krŒd S t|ƒr ||||ƒS |  	¡ }	||ƒ}
||ƒ}d|	|
|f S )Nr   )ræ   )r   c             S   s    dd„ }dd  t|| ƒ¡ d S )Nc             S   s2   y
| j }W n tk
r&   t| ƒ} Y nX |ƒ } | S )N)Ústr_for_displayr   r   )r   r  r   r   r   Úquote  s    
z8FunctionAction.strfunction.<locals>.array.<locals>.quoteú[z, ú])r/   r£   )rs   r  r   r   r   Úarray   s    z)FunctionAction.strfunction.<locals>.arrayz
%s(%s, %s))
rY   r   rç   ræ   rè   r  rZ   r   r]   r  )ru   ry   rz   rx   r   ræ   rÛ   r  Ústrfuncr
  ZtstrZsstrr   r   r   rZ   ô  s,    


zFunctionAction.strfunctionc             C   s"   |   ¡ }|dkrt| jƒS d| S )NÚActionCallerz%s(target, source, env))r  r+   r  )ru   r
  r   r   r   rß     s    
zFunctionAction.__str__c       	   
   C   s:  d}z*|r|  ¡ }| ¡ }ttt|ƒƒ}y| j|||d}W nr tk
rb } z‚ W d d }~X Y nP tk
r„ } z‚ W d d }~X Y n. tk
r° } z|}t	 
¡ }W d d }~X Y nX |r.tj ||¡}||_| |_y|  ||||¡|_W n& tk
r   |  |||¡|_Y nX |d r.t|d tƒs.|‚|S ~X d S )N)NNN)ry   rz   rx   r   )r¡   r¢   rV   r£   r   r  ÚKeyboardInterruptÚ
SystemExitr„   r˜   Úexc_inforK   r^   Zconvert_to_BuildErrorZnoderì   rZ   rí   r   rJ   rÓ   )	ru   ry   rz   rx   r   r#  Zrsourcesr†   rÀ   r   r   r   r¤     s8    
zFunctionAction.executec             C   s*   y|   |||¡S  tk
r$   | jS X dS )z6Return the signature contents of this callable action.N)r  r   r  )ru   ry   rz   rx   r   r   r   r~   I  s    zFunctionAction.get_presigc             C   s   g S )Nr   )ru   ry   rz   rx   r   r   r   rü   P  s    z FunctionAction.get_implicit_deps)N)N)r   r	   r
   r:   r—   r  rZ   rß   r¤   r~   rü   r  r   r   )rN   r   rl   Ø  s   	
$
+rl   c               @   s`   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Ze	e	e	e	e	dfdd„Z
dd„ Zddd„ZdS )rU   z!Class for lists of other actions.c             C   s:   t jjrt| dƒ dd„ }tt||ƒƒ| _d| _d| _d S )NzAction.ListActionc             S   s   t | tƒr| S t| ƒS )N)rJ   re   rT   )r7   r   r   r   Úlist_of_actionsW  s    
z,ListAction.__init__.<locals>.list_of_actionsr   z$TARGETS)rK   rÜ   rÝ   r   rV   r£   rX   rŽ   )ru   Z
actionlistr$  r   r   r   r—   U  s     
zListAction.__init__c                s   d  ‡ ‡‡fdd„| jD ƒ¡S )Nrc   c                s   g | ]}|  ˆˆˆ ¡‘qS r   )r|   )r    rs   )rx   rz   ry   r   r   r#   b  s    z(ListAction.genstring.<locals>.<listcomp>)r/   rV   )ru   ry   rz   rx   r   )rx   rz   ry   r   r|   a  s    zListAction.genstringc             C   s   d  tt| jƒ¡S )Nrc   )r/   r£   r+   rV   )ru   r   r   r   rß   d  s    zListAction.__str__c                s   t j ‡ fdd„| jD ƒ¡S )Nc                s   g | ]}|  ˆ ¡‘qS r   )rŒ   )r    rs   )rx   r   r   r#   i  s    z+ListAction.presub_lines.<locals>.<listcomp>)rK   rL   rÏ   rV   )ru   rx   r   )rx   r   rŒ   g  s    zListAction.presub_linesc                s   d  ‡ ‡‡fdd„| jD ƒ¡S )z|Return the signature contents of this action list.

        Simple concatenation of the signatures of the elements.
        r8   c                s   g | ]}t | ˆˆˆ ¡ƒ‘qS r   )r   r‡   )r    r7   )rx   rz   ry   r   r   r#   p  s    z)ListAction.get_presig.<locals>.<listcomp>)r/   rV   )ru   ry   rz   rx   r   )rx   rz   ry   r   r~   k  s    zListAction.get_presigNc
             C   sH   |	r|	  ¡ }|	 ¡ }x.| jD ]$}
|
|||||||||	ƒ	}|r|S qW dS )Nr   )r¡   r¢   rV   )ru   ry   rz   rx   r–   r”   r§   r¤   r•   r   ro   r¬   r   r   r   r   r  s    zListAction.__call__c             C   s,   g }x"| j D ]}| | |||¡¡ qW |S )N)rV   r*   rü   )ru   ry   rz   rx   r†   ro   r   r   r   rü   ~  s    zListAction.get_implicit_depsc             C   sD   t ƒ }x0| jD ]&}x | ||||¡D ]}d||< q$W qW t| ¡ ƒS )NT)r   rV   r€   Úkeys)ru   ry   rz   rx   r   r†   ro   rp   r   r   r   r€   „  s
    zListAction.get_varlist)N)r   r	   r
   r:   r—   r|   rß   rŒ   r~   r   r   rü   r€   r   r   r   r   rU   S  s   rU   c               @   sR   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zddd„Z	dd„ Z
dd„ ZdS )r   ac  A class for delaying calling an Action function with specific
    (positional and keyword) arguments until the Action is actually
    executed.

    This class looks to the rest of the world like a normal Action object,
    but what it's really doing is hanging on to the arguments until we
    have a target, source and env to use for the expansion.
    c             C   s   || _ || _|| _d S )N)Úparentr`   ra   )ru   r&  r`   ra   r   r   r   r—   •  s    zActionCaller.__init__c             C   s\   | j j}y|jj}W nB tk
rV   y|jjjj}W n tk
rP   t|ƒ}Y nX Y nX |S )N)r&  Úactfuncr9   r1   r   r   r   r   )ru   ry   rz   rx   r'  r3   r   r   r   r‡   š  s    zActionCaller.get_contentsc          	   C   sl   t |ƒr<g }x"|D ]}| |  ||||¡¡ qW | j |¡S |dkrH|S t|ƒr`| |d||¡S | j |¡S )Nz$__env__r   )r   r<   rè   r&  Úconvertr   )ru   r   ry   rz   rx   r†   Úelemr   r   r   rè   ª  s    
zActionCaller.substc                s   ‡ ‡‡‡fdd„ˆj D ƒS )Nc                s   g | ]}ˆ  |ˆˆˆ ¡‘qS r   )rè   )r    r7   )rx   ru   rz   ry   r   r   r#   ½  s    z+ActionCaller.subst_args.<locals>.<listcomp>)r`   )ru   ry   rz   rx   r   )rx   ru   rz   ry   r   Ú
subst_args¼  s    zActionCaller.subst_argsc             C   s:   i }x0t | j ¡ ƒD ]}|  | j| |||¡||< qW |S )N)rV   ra   r%  rè   )ru   ry   rz   rx   ra   r×   r   r   r   Úsubst_kw¿  s    zActionCaller.subst_kwNc             C   s*   |   |||¡}|  |||¡}| jj||ŽS )N)r*  r+  r&  r'  )ru   ry   rz   rx   r   r`   ra   r   r   r   r   Å  s    zActionCaller.__call__c             C   s*   |   |||¡}|  |||¡}| jj||ŽS )N)r*  r+  r&  r  )ru   ry   rz   rx   r`   ra   r   r   r   rZ   Ê  s    zActionCaller.strfunctionc             C   s   | j j| j| jŽS )N)r&  r  r`   ra   )ru   r   r   r   rß   Ï  s    zActionCaller.__str__)N)r   r	   r
   r:   r—   r‡   rè   r*  r+  r   rZ   rß   r   r   r   r   r   Œ  s   
r   c               @   s(   e Zd ZdZdd„ fdd„Zdd„ ZdS )	ÚActionFactoryaf  A factory class that will wrap up an arbitrary function
    as an SCons-executable Action object.

    The real heavy lifting here is done by the ActionCaller class.
    We just collect the (positional and keyword) arguments that we're
    called with and give them to the ActionCaller object we create,
    so it can hang onto them until it needs them.
    c             C   s   | S )Nr   )r7   r   r   r   ré   Ü  r8   zActionFactory.<lambda>c             C   s   || _ || _|| _d S )N)r'  r  r(  )ru   r'  r  r(  r   r   r   r—   Ü  s    zActionFactory.__init__c             O   s   t | ||ƒ}t||jd}|S )N)rZ   )r   rT   rZ   )ru   r`   ra   Zacrì   r   r   r   r   á  s    zActionFactory.__call__N)r   r	   r
   r:   r—   r   r   r   r   r   r,  Ó  s   r,  )N)rº   )3r:   rŸ   r   Úrer˜   rÑ   r   rD   Úcollectionsr   ZSCons.DebugrK   r   ZSCons.ErrorsZ
SCons.Utilrç   r   r   r   r   rž   rœ   Z ACTION_SIGNATURE_PICKLE_PROTOCOLr   r   Úcompilerý   r   r   r   r   r   rW   rb   rr   ri   rT   re   r‘   r²   r´   rµ   r¹   rÚ   rh   rk   rf   rl   rU   r   r,  r   r   r   r   Ú<module>e   s^   
)
F0;5	N

O yJ:{9G