3
^Ã_^ÛR  ã               @   s   d Z ddlZddlZddlZddlmZmZmZ ddlZej	dkr\ddl
Z
G dd„ deƒZnddlZdZdZd	Zd
ZdZdejej	ef ZdZdZdZdZdZdZdZdZdZdZejƒ ZdZ dZ!d Z"dZ#g Z$dd„ Z%G dd„ de&ƒZ'e'defi ƒZ(G dd„ de(ƒZ)i Z*d!dd„Z+d"dd„Z,dS )#z3
Classes and functions enabling the command system
é    N)ÚUtilsÚErrorsÚLogsi  c               @   s   e Zd Zdd„ ZdS )Úimpc             C   s
   t j| ƒS )N)ÚtypesÚ
ModuleType)Úx© r	   ú9/home/yuhw/wc/wct-package/waf-tools/waf/waflib/Context.pyÚ<lambda>   s    zimp.<lambda>N)Ú__name__Ú
__module__Ú__qualname__Ú
new_moduler	   r	   r	   r
   r      s   r   i  z2.0.19Z(e83405712e95b47c040763fdfa468c04dfe72e4bÚwafé   z.wafpickle-%s-%d-%dÚAPPNAMEÚVERSIONÚtopÚoutÚwscriptÚ é   c             O   s6   x t D ]}|j| kr|||ŽS qW t||Ž}| |_|S )a¿  
	Returns a new :py:class:`waflib.Context.Context` instance corresponding to the given command.
	Used in particular by :py:func:`waflib.Scripting.run_command`

	:param cmd_name: command name
	:type cmd_name: string
	:param k: arguments to give to the context class initializer
	:type k: list
	:param k: keyword arguments to give to the context class initializer
	:type k: dict
	:return: Context object
	:rtype: :py:class:`waflib.Context.Context`
	)ÚclassesÚcmdÚContextÚfun)Úcmd_nameÚkÚkwr   Úctxr	   r	   r
   Úcreate_contextS   s    


r!   c                   s    e Zd ZdZ‡ fdd„Z‡  ZS )Ústore_contexta  
	Metaclass that registers command classes into the list :py:const:`waflib.Context.classes`
	Context classes must provide an attribute 'cmd' representing the command name, and a function
	attribute 'fun' representing the function name that the command uses.
	c                sx   t t| ƒj|||ƒ | j}|dkr&d S y
| j W n" tk
rR   tjd| ƒ‚Y nX t| dd ƒsh| j| _	t
jd| ƒ d S )Nr    r   z.Missing command for the context class %r (cmd)r   r   )r    r   )Úsuperr"   Ú__init__r   r   ÚAttributeErrorr   ÚWafErrorÚgetattrr   r   Úinsert)ÚclsÚnameÚbasesÚdct)Ú	__class__r	   r
   r$   n   s    
zstore_context.__init__)r   r   r   Ú__doc__r$   Ú__classcell__r	   r	   )r-   r
   r"   h   s   r"   r    c               @   s    e Zd ZdZeZi Zd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d„ Zd%dd„Zdd„ Zdd„ Zdd„ Zd d!„ Zg fd"d#„ZdS )&r   aÒ  
	Default context for waf commands, and base class for new command contexts.

	Context objects are passed to top-level functions::

		def foo(ctx):
			print(ctx.__class__.__name__) # waflib.Context.Context

	Subclasses must define the class attributes 'cmd' and 'fun':

	:param cmd: command to execute as in ``waf cmd``
	:type cmd: string
	:param fun: function name to execute when the command is called
	:type fun: string

	.. inheritance-diagram:: waflib.Context.Context waflib.Build.BuildContext waflib.Build.InstallContext waflib.Build.UninstallContext waflib.Build.StepContext waflib.Build.ListContext waflib.Configure.ConfigurationContext waflib.Scripting.Dist waflib.Scripting.DistCheck waflib.Build.CleanContext

	c             K   sŒ   y|d }W n t k
r$   t}Y nX tdtjjfi ƒ| _d| j_| | j_| jdd ƒ| _d | _	| jj
|ƒ| _g | _| | | | dœ| _d | _d S )NÚrun_dirZNod3zwaflib.Noder   )r    ÚconfÚbldÚopt)ÚKeyErrorr0   ÚtypeÚwaflibÚNodeZ
node_classr   r    ÚrootÚ
cur_scriptÚfind_dirÚpathÚ
stack_pathÚ	exec_dictÚlogger)Úselfr   Zrdr	   r	   r
   r$       s    
zContext.__init__c             C   s8   y
| j }W n tk
r   Y nX tj|ƒ t| dƒ dS )z3
		Called to free resources such as logger files
		r>   N)r>   r%   r   Úfree_loggerÚdelattr)r?   r>   r	   r	   r
   Úfinalize³   s    

zContext.finalizec       
      O   sj   t j|ƒ}t j|jddƒƒ}|jddƒ}x<|D ]4}t|||d�}t||jd| jƒdƒ}	|	r.|	| ƒ q.W dS )a+  
		Loads a Waf tool as a module, and try calling the function named :py:const:`waflib.Context.Context.fun`
		from it.  A ``tooldir`` argument may be provided as a list of module paths.

		:param tool_list: list of Waf tool names to load
		:type tool_list: list of string or space-separated string
		Útooldirr   Úwith_sys_pathT)rD   r*   N)r   Úto_listÚgetÚ	load_toolr'   r   )
r?   Ú	tool_listr   r   Útoolsr;   rD   ÚtÚmoduler   r	   r	   r
   Úload¿   s    

zContext.loadc             C   s   | j tjjtjƒgƒ dS )z‘
		Here, it calls the function name in the top-level wscript file. Most subclasses
		redefine this method to provide additional functionality.
		N)ÚrecurseÚosr;   ÚdirnameÚg_moduleÚ	root_path)r?   r	   r	   r
   ÚexecuteÑ   s    zContext.executec             C   s    | j j| jƒ || _|j| _dS )a!  
		Method executed immediately before a folder is read by :py:meth:`waflib.Context.Context.recurse`.
		The current script is bound as a Node object on ``self.cur_script``, and the current path
		is bound to ``self.path``

		:param node: script
		:type node: :py:class:`waflib.Node.Node`
		N)r<   Úappendr9   Úparentr;   )r?   Únoder	   r	   r
   Úpre_recurseØ   s    	zContext.pre_recursec             C   s    | j jƒ | _| jr| jj| _dS )z¸
		Restores ``self.cur_script`` and ``self.path`` right after :py:meth:`waflib.Context.Context.recurse` terminates.

		:param node: script
		:type node: :py:class:`waflib.Node.Node`
		N)r<   Úpopr9   rT   r;   )r?   rU   r	   r	   r
   Úpost_recurseæ   s    zContext.post_recurseNTc             C   sò  y
| j }W n tk
r(   i  }| _ Y nX �xÀtj|ƒD �]°}tjj|ƒs^tjj| jjƒ |ƒ}tjj|t	ƒ}|d |pz| j
 }	| jj|	ƒ}
|
ræ| sœ|
|kræd||
< | j|
ƒ z(|
jd|ƒ}tt||
jƒ dƒ| jƒ W d| j|
ƒ X q8|
s8| jj|ƒ}
|
|�p| j
f}|
�rœ| �s||k�rœd||< | j|
ƒ z\t|
jƒ |d�}t||�pP| j
dƒ}|�s‚|�sdw8tjd|�pt| j
|
jƒ f ƒ‚|| ƒ W d| j|
ƒ X q8|
s8|�s¨q8ytj|ƒ W n$ tk
�rÚ   tjd| ƒ‚Y nX tjd	| ƒ‚q8W dS )
aÅ  
		Runs user-provided functions from the supplied list of directories.
		The directories can be either absolute, or relative to the directory
		of the wscript file

		The methods :py:meth:`waflib.Context.Context.pre_recurse` and
		:py:meth:`waflib.Context.Context.post_recurse` are called immediately before
		and after a script has been executed.

		:param dirs: List of directories to visit
		:type dirs: list of string or space-separated string
		:param name: Name of function to invoke from the wscript
		:type  name: string
		:param mandatory: whether sub wscript files are required to exist
		:type  mandatory: bool
		:param once: read the script file once for a particular context
		:type once: bool
		Ú_TÚrÚexecN)ÚencodingzNo function %r defined in %szCannot read the folder %rzNo wscript file in directory %s)Zrecurse_cacher%   r   rE   rN   r;   ÚisabsÚjoinÚabspathÚWSCRIPT_FILEr   r8   Ú	find_noderV   Úreadr[   Úcompiler=   rX   Úload_moduler'   r   r&   ÚlistdirÚOSError)r?   Údirsr*   Ú	mandatoryÚoncer\   ÚcacheÚdZWSCRIPTZWSCRIPT_FUNrU   Zfunction_codeÚtupZwscript_moduleÚuser_functionr	   r	   r
   rM   ñ   sP    


zContext.recursec             C   sJ   t jrFtjjdƒ}|dkr.t|tƒs.tj|ƒ}t j	d|ƒ t j	d|ƒ d S )NZWAF_CMD_FORMATÚstringz
runner: %rzrunner_env: kw=%s)
r   ÚverboserN   ÚenvironrF   Ú
isinstanceÚstrr   Úshell_escapeÚdebug)r?   r   r   Úfmtr	   r	   r
   Úlog_command4  s    

zContext.log_commandc       
      K   s  t j}t|tƒ|d< | j||ƒ | jr2| jj|ƒ d|krD|j|d< d|krV|j|d< tj	rˆ|d  rˆt j
|d ƒ rˆtjd|d  ƒ‚i }d|krÀtjdkrº|d |d< d|krºd	|d< |d= d
|krì|d
 ræ|d
 |d
< |j|d< |d
= d|k�rt|d tƒ�s|d jƒ |d< |jdtƒ}yt j|||ƒ\}}}W n: tk
�rt }	 ztjdt|	ƒ |	d�‚W Y dd}	~	X nX |�rÄt|tƒ�s–|j|dd�}| j�r®| jjd|ƒ ntj|tjddœd� |�rt|tƒ�sä|j|dd�}| j�rþ| jjd| ƒ ntj|tjddœd� |S )a.  
		Runs an external process and returns the exit status::

			def run(tsk):
				ret = tsk.generator.bld.exec_command('touch foo.txt')
				return ret

		If the context has the attribute 'log', then captures and logs the process stderr/stdout.
		Unlike :py:meth:`waflib.Context.Context.cmd_and_log`, this method does not return the
		stdout/stderr values captured.

		:param cmd: command argument for subprocess.Popen
		:type cmd: string or list
		:param kw: keyword arguments for subprocess.Popen. The parameters input/timeout will be passed to wait/communicate.
		:type kw: dict
		:returns: process exit status
		:rtype: integer
		:raises: :py:class:`waflib.Errors.WafError` if an invalid executable is specified for a non-shell process
		:raises: :py:class:`waflib.Errors.WafError` in case of execution failure
		ÚshellÚstdoutÚstderrr   zProgram %s not found!Útimeouti  Ústart_new_sessionTÚinputÚstdinÚcwdÚ	decode_aszExecution failure: %s)ÚexNÚreplace)Úerrorszout: %sr   )ÚstreamÚc1)Úextrazerr: %s)r   Ú
subprocessrq   rr   rv   r>   ÚinfoÚPIPEr   ro   Ú	check_exer   r&   ÚsysÚ
hexversionr_   rW   Údefault_encodingÚrun_processÚ	ExceptionÚdecodert   rx   Úerrorry   )
r?   r   r   r†   Úcargsr\   Úretr   ÚerrÚer	   r	   r
   Úexec_command=  sX    

 


(zContext.exec_commandc             K   sR  t j}t|tƒ|d< | j||ƒ |jddƒ}|jdtƒ}tjrj|d  rjt j	|d ƒ rjt
jd|d  ƒ‚|j |d< |d< |dkrŽ| j|ƒ i }d	|krÆtjd
krÀ|d	 |d	< d|krÀd|d< |d	= d|krò|d rì|d |d< |j|d< |d= d|k�rt|d tƒ�s|d jƒ |d< |jdtƒ}yt j|||ƒ\}}	}
W n: tk
�rz } zt
jdt|ƒ |d�‚W Y dd}~X nX t|	tƒ�s–|	j|dd�}	t|
tƒ�s°|
j|dd�}
|	�rØ|tk�rØ|tk�rØ| jd|	 ƒ |
�r |tk�r |tk�r | jd|
 ƒ |�r.t
jd||f ƒ}||_|
|_|	|_|‚|tk�r@|	|
fS |tk�rN|
S |	S )a»  
		Executes a process and returns stdout/stderr if the execution is successful.
		An exception is thrown when the exit status is non-0. In that case, both stderr and stdout
		will be bound to the WafError object (configuration tests)::

			def configure(conf):
				out = conf.cmd_and_log(['echo', 'hello'], output=waflib.Context.STDOUT, quiet=waflib.Context.BOTH)
				(out, err) = conf.cmd_and_log(['echo', 'hello'], output=waflib.Context.BOTH)
				(out, err) = conf.cmd_and_log(cmd, input='\n'.encode(), output=waflib.Context.STDOUT)
				try:
					conf.cmd_and_log(['which', 'someapp'], output=waflib.Context.BOTH)
				except Errors.WafError as e:
					print(e.stdout, e.stderr)

		:param cmd: args for subprocess.Popen
		:type cmd: list or string
		:param kw: keyword arguments for subprocess.Popen. The parameters input/timeout will be passed to wait/communicate.
		:type kw: dict
		:returns: a tuple containing the contents of stdout and stderr
		:rtype: string
		:raises: :py:class:`waflib.Errors.WafError` if an invalid executable is specified for a non-shell process
		:raises: :py:class:`waflib.Errors.WafError` in case of execution failure; stdout/stderr/returncode are bound to the exception object
		rw   ÚquietNÚoutputr   zProgram %r not found!rx   ry   rz   i  r{   Tr|   r}   r~   r   zExecution failure: %s)r€   r�   )r‚   zout: %szerr: %szCommand %r returned %r)r   r†   rq   rr   rv   rW   ÚSTDOUTr   ro   r‰   r   r&   rˆ   Úto_logrŠ   r‹   r_   rŒ   r�   rŽ   r�   ÚBOTHÚSTDERRÚ
returncodery   rx   )r?   r   r   r†   r–   Zto_retr‘   r\   r’   r   r“   r”   r	   r	   r
   Úcmd_and_logŠ  sb     



(

zContext.cmd_and_logc             C   sˆ   | j r | j jd| jjƒ |f ƒ y| j jd j}W n tk
rF   Y n.X tjj	dƒrhd|t
j|ƒf }nd||f }| jj||d�‚dS )af  
		Prints an error message in red and stops command execution; this is
		usually used in the configuration section::

			def configure(conf):
				conf.fatal('a requirement is missing')

		:param msg: message to display
		:type msg: string
		:param ex: optional exception object
		:type ex: exception
		:raises: :py:class:`waflib.Errors.ConfigurationError`
		zfrom %s: %sr   ZWAF_PRINT_FAILURE_LOGzLog from (%s):
%s
z%s
(complete log in %s))r€   N)r>   r‡   r;   r_   ÚhandlersÚbaseFilenamer%   rN   rp   rF   r   Úreadfr‚   ÚConfigurationError)r?   Úmsgr€   Zlogfiler	   r	   r
   Úfatalß  s    zContext.fatalc             C   s:   |sdS | j r| j j|ƒ ntjjt|ƒƒ tjjƒ  dS )a  
		Logs information to the logger (if present), or to stderr.
		Empty messages are not printed::

			def build(bld):
				bld.to_log('starting the build')

		Provide a logger on the context class or override this method if necessary.

		:param msg: message
		:type msg: string
		N)r>   r‡   rŠ   ry   Úwriterr   Úflush)r?   r¢   r	   r	   r
   r™   û  s    zContext.to_logc             O   s–   y|d }W n t k
r(   |d }Y nX | j|f|Ž y|d }W n t k
r`   |d }Y nX |jdƒ}t|tƒs‚|r~dp€d}| j||f|Ž dS )	a  
		Prints a configuration message of the form ``msg: result``.
		The second part of the message will be in colors. The output
		can be disabled easly by setting ``in_msg`` to a positive value::

			def configure(conf):
				self.in_msg = 1
				conf.msg('Checking for library foo', 'ok')
				# no output

		:param msg: message to display to the user
		:type msg: string
		:param result: result to display
		:type result: string or boolean
		:param color: color to use, see :py:const:`waflib.Logs.colors_lst`
		:type color: string
		r¢   r   Úresultr   ÚcolorÚGREENÚYELLOWN)r4   Ú	start_msgrF   rq   rr   Úend_msg)r?   r   r   r¢   r¦   r§   r	   r	   r
   r¢     s    

zContext.msgc             O   sä   |j dƒrdS |j dƒp|d }y| jr:|  jd7  _dS W n tk
rV   d| _Y nX |  jd7  _yt| jt|ƒƒ| _W n$ tk
r    tdt|ƒƒ| _Y nX x | jd |fD ]}| j|ƒ q²W tjdd	|j	| jƒ d
d� dS )ze
		Prints the beginning of a 'Checking for xxx' message. See :py:meth:`waflib.Context.Context.msg`
		r–   Nr¢   r   r   é(   ú-ÚNORMALz%s :r   )Úsep)
rF   Úin_msgr%   ÚmaxZ	line_justÚlenr™   r   ÚpprintÚljust)r?   r   r   r¢   r   r	   r	   r
   rª   5  s"    
zContext.start_msgc             O   sÄ   |j dƒrdS |  jd8  _| jr&dS |j dƒp6|d }d}|dkrJd}n|sXd	}d
}nt|ƒ}| j|ƒ y|d }W n< tk
r²   t|ƒdkrª|d tjkrª|d }n|}Y nX tj||ƒ dS )zUPrints the end of a 'Checking for' message. See :py:meth:`waflib.Context.Context.msg`r–   Nr   r¦   r   r¨   TÚokz	not foundr©   r§   )	rF   r°   rr   r™   r4   r²   r   Ú
colors_lstr³   )r?   r   r   r¦   Zdefcolorr¢   r§   r	   r	   r
   r«   M  s*    



zContext.end_msgc             C   sê   t jjtƒrP| jjtƒjdƒj|ƒ}xÀ|D ] }|j|kr*t|jj	ddƒƒ q*W n–ddl
m} |tƒ}|jƒ }xx|D ]p}tjd|j	ddƒ |ƒs�qrt jj|ƒ}d	}x(|D ] }	|	j	ddƒ}
tj|
|ƒr¦d
}q¦W |sr|j	ddƒ}t|ƒ qrW dS )a   
		Loads third-party extensions modules for certain programming languages
		by trying to list certain files in the extras/ directory. This method
		is typically called once for a programming language group, see for
		example :py:mod:`waflib.Tools.compiler_c`

		:param var: glob expression, for example 'cxx\_\*.py'
		:type var: string
		:param ban: list of exact file names to exclude
		:type ban: list of string
		zwaflib/extrasz.pyr   r   )Ú	PyZipFilezwaflib/extras/%sÚ*z.*FTN)rN   r;   ÚisdirÚwaf_dirr8   ra   Úant_globr*   rG   r�   Úzipfiler·   ZnamelistÚreÚmatchÚbasename)r?   ÚvarZbanÚlstr   r·   ZwaflibsÚfZdobanÚbrZ   r	   r	   r
   Úload_special_toolsk  s(    



zContext.load_special_tools)NTTN)N)r   r   r   r.   r   r‚   rI   r$   rB   rL   rR   rV   rX   rM   rv   r•   r�   r£   r™   r¢   rª   r«   rÄ   r	   r	   r	   r
   r   ‚   s&   
C	MU
$r   c             C   s®   yt |  S  tk
r   Y nX tjtƒ}ytj| d|d�}W n" tk
r^   tj	d|  ƒ‚Y nX t
jj| ƒ}tjjd|ƒ ztt|| dƒ|jƒ W dtjj|ƒ X |t | < |S )zÎ
	Loads a wscript file as a python module. This method caches results in :py:attr:`waflib.Context.cache_modules`

	:param path: file path
	:type path: string
	:return: Loaded Python module
	:rtype: module
	rZ   )Úmr\   zCould not read the file %rr   r[   N)Úcache_modulesr4   r   r   r`   r   r    ÚEnvironmentErrorr   r&   rN   r;   rO   rŠ   r(   r[   rc   Ú__dict__Úremove)r;   r\   rK   ÚcodeZ
module_dirr	   r	   r
   rd   “  s     	
rd   Tc       	      C   s¦  | dkrd} n| j ddƒ} |s*tj}g t_�z^|rÄt|tƒs@t‚|tj t_zByt| ƒ W n0 tk
rŠ } zttjƒ|_‚ W Y dd}~X nX W dx|D ]}tjj	|ƒ q–W X tj
|  }|tj| < |S |sÖtjjdtƒ z€yHxBdD ]2}yt||  ƒ P W qà tk
�r   d}Y qàX qàW t| ƒ W n2 tk
�rR } zttjƒ|_‚ W Y dd}~X nX W d|�sjtjj	tƒ X tj
||   }|tj| < |S W d|�s t j|7  _X dS )a�  
	Imports a Waf tool as a python module, and stores it in the dict :py:const:`waflib.Context.Context.tools`

	:type  tool: string
	:param tool: Name of the tool
	:type  tooldir: list
	:param tooldir: List of directories to search for the tool module
	:type  with_sys_path: boolean
	:param with_sys_path: whether or not to search the regular sys.path, besides waf_dir and potentially given tooldirs
	ÚjavaZjavawz++ZxxNr   úwaflib.Tools.%súwaflib.extras.%sú	waflib.%sú%s)rÌ   rÍ   rÎ   rÏ   )r�   rŠ   r;   rq   ÚlistÚAssertionErrorÚ
__import__ÚImportErrorÚwaf_sys_pathrÉ   Úmodulesr   rI   r(   rº   )	ÚtoolrC   r    rD   Z	back_pathr”   rk   r’   r   r	   r	   r
   rG   ±  sP    




rG   éÿÿÿÿ)N)NNT)-r.   rN   r½   rŠ   r6   r   r   r   Zwaflib.Noder‹   r   Úobjectr   Ú
HEXVERSIONÚ
WAFVERSIONÚWAFREVISIONÚWAFNAMEÚABIÚplatformÚDBFILEr   r   ÚTOPÚOUTr`   Ú
launch_dirr0   Útop_dirÚout_dirrº   Úconsole_encodingrŒ   rP   r˜   r›   rš   r   r!   r5   r"   r    r   rÆ   rd   rG   r	   r	   r	   r
   Ú<module>   sP   
    
