
Yh                 @   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 Z d d l Z d d l Z d d l	 Z	 d d l
 m
 Z
 d d l Z e j   Z Gd d   d e  Z Gd d   d e  Z Gd d	   d	  Z Gd
 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# d$  Z e j d%  Z  d" d& d'  Z! d( d)   Z" d* d+   Z# d, d-   Z$ d. d/   Z% d0 d1   Z& d2 d3   Z' d4 d5   Z( d6 d7   Z) d8 d9   Z* d: d;   Z+ d< d=   Z, d> d?   Z- d@ dA   Z. d e j/ dB dC  Z0 dD dE   Z1 dF dG   Z2 dH dI   Z3 dJ dK   Z4 dL dM   Z5 dN dO   Z6 dP dQ   Z7 GdR dS   dS e	 j8  Z9 d S)Tz)A library of random helper functionality.    N)globc               @   s   e  Z d  Z d Z d S)MesonExceptionzExceptions thrown by MesonN)__name__
__module____qualname____doc__ r   r   O/mnt/raid6/a/bak/reczko/tools/protein/utils/meson-0.42.1/mesonbuild/mesonlib.pyr      s   r   c               @   s   e  Z d  Z d Z d S)EnvironmentExceptionzEExceptions thrown while processing and creating the build environmentN)r   r   r   r   r   r   r   r	   r
       s   r
   c               @   sR   e  Z d  Z e j d  Z d d d d d  Z d d   Z e d d    Z	 d S)	FileModez*[r-][w-][xsS-][r-][w-][xsS-][r-][w-][xtT-]Nc             C   s1   | |  _  |  j |  |  _ | |  _ | |  _ d  S)N)perms_sperms_s_to_bitspermsownergroup)selfr   r   r   r   r   r	   __init__=   s    		zFileMode.__init__c             C   s"   d } | j  |  j |  j |  j  S)Nz!<FileMode: {!r} owner={} group={})formatr   r   r   )r   retr   r   r	   __repr__C   s    zFileMode.__repr__c             C   s  | d k r d Sd } t  | t  sC d } t | j |    n  t |  d k sh |  j j |  r d } t | j | |    n  d } | d d k r | t j O} n  | d d	 k r | t j	 O} n  | d
 d k r | t j
 O} nM | d
 d k r| t j O} n- | d
 d k r<| t j
 O} | t j O} n  | d d k r\| t j O} n  | d d	 k r|| t j O} n  | d d k r| t j O} nM | d d k r| t j O} n- | d d k r| t j O} | t j O} n  | d d k r	| t j O} n  | d d	 k r)| t j O} n  | d d k rI| t j O} nM | d d k ri| t j O} n- | d d k r| t j O} | t j O} n  | S)z
        Does the opposite of stat.filemode(), converts strings of the form
        'rwxr-xr-x' to st_mode enums which can be passed to os.chmod()
        N   z	rwxr-xr-xz1Install perms must be a string. For example, {!r}	   z:File perms {!r} must be exactly 9 chars. For example, {!r}r   rw   xSs                  Tt)
isinstancestrr   r   lensymbolic_perms_regexmatchstatS_IRUSRS_IWUSRS_IXUSRS_ISUIDS_IRGRPS_IWGRPS_IXGRPS_ISGIDS_IROTHS_IWOTHS_IXOTHS_ISVTX)clsr   Zegmsgr   r   r   r	   r   G   sX    %zFileMode.perms_s_to_bits)
r   r   r   recompiler*   r   r   classmethodr   r   r   r   r	   r   #   s   r   c               @   s   e  Z d  Z d d   Z d d   Z d d   Z e d d    Z e d	 d
    Z e 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)Filec             C   sO   | |  _  | |  _ | |  _ t |  j t  s3 t  t |  j t  sK t  d  S)N)is_builtsubdirfnamer'   r(   AssertionError)r   r?   r@   rA   r   r   r	   r      s
    			zFile.__init__c             C   s
   |  j    S)N)relative_name)r   r   r   r	   __str__   s    zFile.__str__c             C   s9   d } |  j  s | d 7} n  | d 7} | j |  j    S)Nz
<File: {0}z (not built)>)r?   r   rC   )r   r   r   r   r	   r      s
    	
zFile.__repr__c             C   sG   t  j j t  j j |  | |   s7 t d |   n  t d | |  S)NzFile %s does not exist.F)ospathisfilejoinr   r>   )Zsource_rootr@   rA   r   r   r	   from_source_file   s    $zFile.from_source_filec             C   s   t  d |  |  S)NT)r>   )r@   rA   r   r   r	   from_built_file   s    zFile.from_built_filec             C   s   t  d d |   S)NF )r>   )rA   r   r   r	   from_absolute_file   s    zFile.from_absolute_filec             C   s3   |  j  r |  j   St j j | |  j |  j  Sd  S)N)r?   rC   rF   rG   rI   r@   rA   )r   Zbuild_to_srcr   r   r	   rel_to_builddir   s    	
zFile.rel_to_builddirc             C   s1   | } |  j  r | } n  t j j | |  j    S)N)r?   rF   rG   rI   rC   )r   srcdirZbuilddirZabsdirr   r   r	   absolute_path   s    		zFile.absolute_pathc             C   s   |  j  j |  S)N)rA   endswith)r   Zendingr   r   r	   rQ      s    zFile.endswithc             C   s   |  j  j |  S)N)rA   split)r   r   r   r   r	   rR      s    z
File.splitc             C   s.   |  j  |  j |  j f | j  | j | j f k S)N)rA   r@   r?   )r   otherr   r   r	   __eq__   s    zFile.__eq__c             C   s   t  |  j |  j |  j f  S)N)hashrA   r@   r?   )r   r   r   r	   __hash__   s    zFile.__hash__c             C   s   t  j j |  j |  j  S)N)rF   rG   rI   r@   rA   )r   r   r   r	   rC      s    zFile.relative_nameN)r   r   r   r   rD   r   staticmethodrJ   rK   rM   rN   rP   rQ   rR   rT   rV   rC   r   r   r   r	   r>   ~   s   r>   c             C   sG   |  j    } t j j |  \ } } t j j t j j |  | |  S)z|
    Given the path of `meson.py`/`meson`, get the path of a meson script such
    as `mesonintrospect` or `mesontest`.
    )get_build_commandrF   rG   splitextrI   dirname)envscriptZmeson_pybaseextr   r   r	   get_meson_script   s    r_   c             C   s=   x! |  D] } | j  |  r | Sq Wt d j |    d  S)Nz*No specified compiler can handle file {!s})Zcan_compileRuntimeErrorr   )	compilerssrccompr   r   r	   get_compiler_for_source   s    rd   c             C   sW   i  } xJ | D]B } t  |  |  } | | k r> | g | | <q | | j |  q W| S)N)rd   append)ra   ZsourcesZcompsrclistrb   rc   r   r   r	   classify_unity_sources   s    rf   c             C   s`   t  |  t  s |  g Sg  } x= |  D]5 } t  | t  rK | t |  7} q# | j |  q# W| S)N)r'   listflattenre   )itemresultir   r   r	   rh      s    rh   c               C   s   t  j   j   d k S)Ndarwin)platformsystemlowerr   r   r   r	   is_osx   s    rp   c               C   s   t  j   j   d k S)Nlinux)rm   rn   ro   r   r   r   r	   is_linux   s    rr   c              C   s(   t  j   j   }  |  d k p' d |  k S)NwindowsZmingw)rm   rn   ro   )platnamer   r   r	   
is_windows   s    ru   c              C   s   t  j   j   }  |  j d  S)Ncygwin)rm   rn   ro   
startswith)rt   r   r   r	   	is_cygwin   s    rx   c               C   s   t  j j d  S)Nz/etc/debian_version)rF   rG   rH   r   r   r   r	   is_debianlike   s    ry   c             C   s[   yB t  j |  d t  j d t  j } | j   | j d k rA d SWn t k
 rV Yn Xd S)Nstdoutstderrr   TF)
subprocessPopenPIPEcommunicate
returncodeFileNotFoundError)Zarglistpr   r   r	   
exe_exists   s    !
r   c             C   s`  t  d d d d d d d d d d	 d
 d  t  d d d d d d d d d d	 d
 d  t  d d d d d d d d d d d
 d  t  d d d d d d d d d d	 d
 d  g } |  j d d  j d  } x t t |  d d   D]v } d j | d  |   } xT | D]L } t j j t j j | | d   rt	 j
 | d  r| | d <| SqWq Wd  S)!NnamegitcmdZrepo_dirz.gitZget_revzgit describe --dirty=+Z	rev_regexz(.*)Zdepz.git/logs/HEADZ	mercurialZhgz.hgzhg id -iz.hg/dirstate
subversionsvnz.svnzsvn infozRevision: (.*)z
.svn/wc.dbZbazaarZbzrz.bzrz	bzr revno\/r   Zwc_dirr&   r&   )dictreplacerR   ranger)   rI   rF   rG   isdirshutilwhich)
source_dirZvcs_systemsZsegsrk   curdirZvcsr   r   r	   
detect_vcs   s    ***08
r   Fc             C   s   g  } x |  j  d  j d  D]t } y | j t |   Wq t k
 r } z4 | r d t |  } t | j |     n  PWYd  d  } ~ Xq Xq W| S)N.z\Invalid version to compare against: {!r}; only numeric digits separated by "." are allowed: )rstriprR   re   int
ValueErrorr(   r   r   )Zvstrstrictrj   r   er:   r   r   r	   grab_leading_numbers	  s    r   z[0-9.]+c             C   s  t  j |  j    } | d  k r? d } t | j |     n  | j d  }  | j d  ry t j } | d d   } n| j d  r t j	 } | d d   } n | j d  r t j
 } | d d   } n | j d  r t j } | d d   } n | j d  r%t j } | d	 d   } n_ | j d
  rPt j } | d	 d   } n4 | j d  r{t j } | d	 d   } n	 t j } t |  |  } t | |  } | | |  S)Nz!Uncomparable version string {!r}.r   z>=r   z<=z!=z===r   rE   <)numpartr+   stripr   r   r   rw   operatorgeleneeqgtltr   )vstr1Zvstr2r   r+   r:   ZcmpopZvarr1Zvarr2r   r   r	   version_compare  s<    								r   c             C   s   t  | t t t f  s$ | g } n  g  } g  } x@ | D]8 } t |  | d d sb | j |  q7 | j |  q7 W| g  k | | f S)Nr   T)r'   rg   tuple	frozensetr   re   )r   Z
conditionsfoundZ	not_foundZreqr   r   r	   version_compare_many9  s    r   c              C   s   t    r yf t j d d g d t j d t j }  |  j   \ } } |  j d k rn | j   j   } d | SWq t	 k
 r Yq Xn  t
 j j d  r t
 j j d  r d Sd	 S)
Nzdpkg-architecturez-qDEB_HOST_MULTIARCHrz   r{   r   zlib/z
/usr/lib64Zlib64lib)ry   r|   r}   r~   DEVNULLr   r   decoder   	ExceptionrF   rG   r   islink)ZpcZstdo_Zarchpathr   r   r	   default_libdirE  s    		%r   c               C   s   d S)NZlibexecr   r   r   r   r	   default_libexecdirU  s    r   c               C   s   t    r d Sd S)Nzc:/z
/usr/local)ru   r   r   r   r	   default_prefixY  s    r   c              C   s  t    r d g St   r  d g Sd d d g }  t j d d g  j   j   } | d k re d } n  | j d	  r} d	 } n  |  t d
 | d  7}  t j	 j
 d  r |  j d  n  |  t d | d  7}  t j	 j
 d  r |  j d  n  |  t d | d  7}  |  S)NzC:/mingw/libz/usr/libz/usr/local/libz/libunamez-mi686Zi386armz	/usr/lib/*z
/usr/lib64z/lib/z/lib64)ru   rp   r|   check_outputr   r   rw   r   rF   rG   existsre   )Zunixdirsplatr   r   r	   get_library_dirs\  s$    		!		r   c             C   s   t  j |  |  } t   } x | r | j d  } | | k r | j |  \ } } t | t  rf q t | t  r t |  } q t d   n | j	 |  d } | j
 d | d |  } t  j |  |  } q W| | f S)Nr   zFTried to replace a variable with something other than a string or int.rL   @)r;   searchsetr   getr'   r(   r   r`   addr   )regexlineconfdatar+   missing_variablesvarnamevardescr   r   r	   do_replacementx  s     		r   c             C   s   |  j    } t |  d k r6 t d |  j     n  | d } y | j |  \ } } Wn t k
 ru d | SYn Xt | t  r | r d | Sd | SnJ t | t  r d | | f St | t	  r d | | f St d	 |   d  S)
Nr   z4#mesondefine does not contain exactly two tokens: %sr   z/* #undef %s */
z#define %s
z
#undef %s
z#define %s %d
z#define %s %s
z.#mesondefine argument "%s" is of unknown type.)
rR   r)   r   r   r   KeyErrorr'   boolr   r(   )r   r   Zarrr   vr   r   r   r	   do_mesondefine  s"    
r   c             C   sG  y+ t  |  d d  } | j   } Wd  QXWnA t k
 rn } z! t d |  t |  f   WYd  d  } ~ Xn Xt j d  } g  } t   } xa | D]Y }	 |	 j d  r t	 |	 |  }	 n% t
 | |	 |  \ }	 }
 | j |
  | j |	  q W| d } t  | d d d  } | j |  Wd  QXt j |  |  t | |  | S)Nencodingzutf-8z Could not read input file %s: %sz[^\\]?@([-a-zA-Z0-9_]+)@z#mesondefine~r   )open	readlinesr   r   r(   r;   r<   r   rw   r   r   updatere   
writelinesr   copymodereplace_if_different)rb   dstr   fdatar   r   rj   r   r   missingdst_tmpr   r   r	   do_conf_file  s(    /	
r   c             C   s   t  |  d d d  } | j d  x t | j    D] } | j |  \ } } | rm | j d |  n  t | t  r | r | j d |  q | j d |  q8 t | t t f  r | j d | | f  q8 t	 d	 |   q8 WWd  QXd  S)
Nr   r   zutf-8zm/*
 * Autogenerated by the Meson build system.
 * Do not edit, your changes will be lost.
 */

#pragma once

z	/* %s */
z#define %s

z#undef %s

z#define %s %s

z/Unknown data type in configuration file entry: )
r   writesortedkeysr   r'   r   r   r(   r   )Z	ofilenameZcdataZofilekr   r   r   r   r	   dump_conf_header  s    r   c             C   s   d } yU t  |  d  @ } t  | d  ( } | j   | j   k rN d } n  Wd  QXWd  QXWn t k
 ro Yn X| r t j | |   n t j |  d  S)NTr   F)r   readr   rF   r   unlink)r   r   Z	differentf1f2r   r   r	   r     s    $r   c             C   s   t  |  |  r |  g }  n  t  |  t  sB t d j |    n  xB |  D]: } | d k	 rI t  | |  rI t d j |    qI qI W|  S)zi
    Ensure that type(@item) is one of @types or a
    list of items all of which are of type @types
    z"Item must be a list or one of {!r}NzList item must be one of {!r})r'   rg   r   r   )ri   typesrk   r   r   r	   typeslistify  s    r   c             C   s   t  |  t  S)N)r   r(   )ri   r   r   r	   stringlistify  s    r   c             C   s   g  } x |  D] } | j  d  s5 | j |  q n  | d d   } y5 t |   } | j   j   } Wd  QX| | 7} Wq t k
 r } z! t d |  t |  d  SWYd  d  } ~ Xq Xq W| S)Nr   r   z4Error expanding command line arguments, %s not found)rw   re   r   r   rR   r   print)argsZexpended_argsarg	args_filer   Zextended_argsr   r   r   r	   expand_arguments  s    
r   c             K   sO   t  j |  d d d d d t  j d | | } | j |  \ } } | | | f S)Nuniversal_newlinesT	close_fdsFrz   r{   )r|   r}   r~   r   )r   r   r{   kwargsr   or   r   r   r	   
Popen_safe  s    	r   c             C   s   d d l  } |  s! t d   n  | j |  d  } x |  d d  D] } g  } | j |  } x@ t | j | j  D]) \ } } | | k r Pn  | j |  qv W| s d } Pn  t j j |   } | j |  } qE Wt	 |  S)z
    For use on Python 3.4 where os.path.commonpath is not available.
    We currently use it everywhere so this receives enough testing.
    r   Nzarg is an empty sequencer   rL   )
pathlibr   PurePathzippartsre   rF   rG   rI   r(   )pathsr   ZcommonrG   newcr   r   r   r	   
commonpath  s"    "r   c             C   s]   xV |  D]N } xE | D]= } t  | t  s/ q n  t j | |  } | r | j   Sq Wq Wd S)z
    Takes each regular expression in @regexiter and tries to search for it in
    every item in @initer. If there is a match, returns that match.
    Else returns False.
    F)r'   r(   r;   r   r   )Z	regexiterZiniterr   iir+   r   r   r	   iter_regexin_iter&  s    r   c             C   s  d } d } d | k rN t  | |   } | r#d } t | j |    q#n t | d  d k r t  | d d   |   } | r t d	 j |    q n  x |  D]z } t | t  s q n  t j | d
 |  } | r | j   | k r d } t | j | j   t | d     q q Wd | k ret  | |   } | rd } t | j |    qn x |  D]z } t | t  sqln  t j | d
 |  } | rl| j   | k rld } t | j | j   t | d     qlqlWd  S)N@INPUT([0-9]+)?@@PLAINNAME@
@BASENAME@@OUTPUT([0-9]+)?@@OUTDIR@z@INPUT@z=Command cannot have {!r}, since no input files were specifiedr   z?Command cannot have {!r} when there is more than one input filer   z9Command cannot have {!r} since there are only {!r} inputsz@OUTPUT@z3Command cannot have {!r} since there are no outputsz:Command cannot have {!r} since there are only {!r} outputs)r   r   r   )r   r   )	r   r   r   r)   r'   r(   r;   r   r   )commandvaluesZinregexZoutregexr+   mZeachr   r   r	   _substitute_values_check_errors5  s>    /r  c             C   s  t  |  |  g  } x{|  D]s} t | t  s? | j |  q d | k r | d } | d k rn | | 7} qt |  d k r | j | j d | d   qt d   q d | k r| d } | d k r | | 7} qt |  d k r| j | j d | d   qt d   q | | k r?| j | |  q x> | j   D]0 \ } } | d k rjqLn  | j | |  } qLW| j |  q W| S)a  
    Substitute the template strings in the @values dict into the list of
    strings @command and return a new list. For a full list of the templates,
    see get_filenames_templates_dict()

    If multiple inputs/outputs are given in the @values dictionary, we
    substitute @INPUT@ and @OUTPUT@ only if they are the entire string, not
    just a part of it, and in that case we substitute *all* of them.
    @INPUT@r   r   zFCommand has '@INPUT@' as part of a string and more than one input file@OUTPUT@zHCommand has '@OUTPUT@' as part of a string and more than one output file)r  r  )r  r'   r(   re   r)   r   r   items)r  r  Zoutcmdvvinputsoutputskeyvaluer   r   r	   substitute_values^  s6    
 
 r  c             C   s  i  } |  r |  | d <x- t  |   D] \ } } | | d j |  <q# Wt |   d k r t j j |  d  d | d <} t j j |  d | d <q n  | r| | d <x- t  |  D] \ } } | | d j |  <q Wt j j | d  d | d	 <| d	 d
 k rd | d	 <qn  | S)a   
    Create a dictionary with template strings as keys and values as values for
    the following templates:

    @INPUT@  - the full path to one or more input files, from @inputs
    @OUTPUT@ - the full path to one or more output files, from @outputs
    @OUTDIR@ - the full path to the directory containing the output files

    If there is only one input file, the following keys are also created:

    @PLAINNAME@ - the filename of the input file
    @BASENAME@ - the filename of the input file with the extension removed

    If there is more than one input file, the following keys are also created:

    @INPUT0@, @INPUT1@, ... one for each input file

    If there is more than one output file, the following keys are also created:

    @OUTPUT0@, @OUTPUT1@, ... one for each output file
    z@INPUT@z	@INPUT{}@r   r   z@PLAINNAME@z
@BASENAME@z@OUTPUT@z
@OUTPUT{}@z@OUTDIR@rL   r   )	enumerater   r)   rF   rG   rR   rY   )r	  r
  r  r   r  plainr   r   r	   get_filenames_templates_dict  s     
" 
r  c             C   s   d d d d d d d d d d d d g } xK | D]C } y t  j |   d  SWq1 t t f k
 rs t j |  Yq1 Xq1 Wt  j |   d  S)Ng?g?g      ?r   r   )r   rmtreeOSErrorPermissionErrortimesleep)r   Zdelaysdr   r   r	   windows_proof_rmtree  s    *r  c             C   sO   g  } t  |   }  x6 |  D]. } t | d  r: | j } n  | j |  q W| S)Nheld_object)rh   hasattrr  re   )Zentriesrj   r   r   r   r	   unholder_array  s    r  c               @   s   e  Z d  Z 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 d d   Z d S)
OrderedSetzUA set that preserves the order in which items are added, by first
    insertion.
    Nc             C   s)   t  j   |  _ | r% |  j |  n  d  S)N)collectionsOrderedDict_OrderedSet__containerr   )r   iterabler   r   r	   r     s    zOrderedSet.__init__c             C   s   | |  j  k S)N)r  )r   r  r   r   r	   __contains__  s    zOrderedSet.__contains__c             C   s   t  |  j j    S)N)iterr  r   )r   r   r   r	   __iter__  s    zOrderedSet.__iter__c             C   s   t  |  j  S)N)r)   r  )r   r   r   r	   __len__  s    zOrderedSet.__len__c             C   s9   |  j  r5 d j d j d d   |  j  j   D   Sd S)NzOrderedSet("{}")z", "c             s   s   |  ] } t  |  Vq d  S)N)repr).0r   r   r   r	   	<genexpr>  s    z&OrderedSet.__repr__.<locals>.<genexpr>zOrderedSet())r  r   rI   r   )r   r   r   r	   r     s    	&zOrderedSet.__repr__c             C   s   d  |  j  | <d  S)N)r  )r   r  r   r   r	   r     s    zOrderedSet.addc             C   s    | |  j  k r |  j  | =n  d  S)N)r  )r   r  r   r   r	   discard  s    zOrderedSet.discardc             C   s"   x | D] } d  |  j  | <q Wd  S)N)r  )r   r  ri   r   r   r	   r     s    zOrderedSet.updatec                s#   t  |     f d d   |  D  S)Nc             3   s!   |  ] } |   k r | Vq d  S)Nr   )r%  r   )set_r   r	   r&    s    z(OrderedSet.difference.<locals>.<genexpr>)type)r   r(  r   )r(  r	   
difference  s    zOrderedSet.difference)r   r   r   r   r   r   r"  r#  r   r   r'  r   r*  r   r   r   r	   r    s   r  ):r   r,   r  rm   r|   r   rF   r   r;   r  r   	threadingLockZan_unpicklable_objectr   r   r
   r   r>   r_   rd   rf   rh   rp   rr   ru   rx   ry   r   r   r   r<   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r~   r   r   r   r  r  r  r  r  
MutableSetr  r   r   r   r	   <module>   s^   H[;	

!)1/	