ó
ß™xTc           @   sç   d  Z  d d l m Z d d l Z d d l Z d d l Z d d l Z d d l Z e Z	 d d „ Z
 d d d „ Z d „  Z d	 „  Z d
 „  Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d „  Z e d k rã e e j d ƒ n  d S(   s.   Fix RPATH and ORIGIN for relocatable binaries.iÿÿÿÿ(   t   divisionNt   .c         C   s˜   t  d |  d d d d g ƒ } t d	 g  | j d ƒ D] } | j ƒ  ^ q4 ƒ } g  } x< | D]4 } t  d | g ƒ } d | k r\ | j | ƒ q\ q\ W| S(
   s(   Find executables (using +x permissions).t   finds   -typet   fs   -perms   +111s   
t   files   Mach-ON(   t   check_outputt   filtert   Nonet   splitt   stript   append(   t   roott   pt   it   foundt
   found_execR   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/auto_build/rpath.pyt	   find_exec   s    1t    c         C   sk   g  } x^ t  j | ƒ D]M \ } } } | j g  | D]* } | j |  ƒ r2 t  j j | | ƒ ^ q2 ƒ q W| S(   sF   Find files with a particular extension. Include the ".", e.g. ".txt". (   t   ost   walkt   extendt   endswitht   patht   join(   t   extR   R   t   dirst   filesR   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/auto_build/rpath.pyt   find_ext&   s    Bc          O   sÀ   d Gd j  |  Œ  GHt r d  S| j d t ƒ } t j | d <t j | d <t j |  | Ž  } | j ƒ  \ } } | j ƒ  } | r¼ | r£ d d j  |  Œ  GH| GH| GHq¼ | GH| GHt	 d ƒ ‚ n  d  S(   Ns   Running:t    t
   ignorefailt   stdoutt   stderrs0   WARNING: Command returned non-zero exit code: %ss#   Command returned non-zero exit code(
   R   t   DRY_RUNt   popt   Falset
   subprocesst   PIPEt   Popent   communicatet   waitt	   Exception(   t	   popenargst   kwargsR   t   processt   at   bt   exitcode(    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/auto_build/rpath.pyt   cmd-   s$    c          O   s   d Gd j  |  Œ  GHd  S(   Ns   Running:R   (   R   (   R)   R*   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/auto_build/rpath.pyt   echoB   s    c          O   s›   d | k r t  d ƒ ‚ n  t j d t j |  | Ž } | j ƒ  \ } } | j ƒ  } | r— | j d ƒ } | d k r‚ |  d } n  t j | | ƒ ‚ n  | S(   s!   Copy of subprocess.check_output()R   s3   stdout argument not allowed, it will be overridden.t   argsi    N(	   t
   ValueErrorR#   R%   R$   R&   t   pollt   getR   t   CalledProcessError(   R)   R*   R+   t   outputt
   unused_errt   retcodeR/   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/auto_build/rpath.pyR   I   s    t   FixLinuxRpathc           B   s   e  Z d  „  Z d d „ Z RS(   c         C   sž   g  } t  d | g ƒ } x | j d ƒ D]n } | j d ƒ } y; | d | d } } d | k rw | j | j ƒ  ƒ n  Wq( t k
 r• d GH| GHq( Xq( W| S(   Nt   ldds   
s   =>i    i   s	   not founds   PARSER ERROR:(   R   R   R
   R	   R(   (   t   selft   filenamet   retR   t   linet   fieldst   libR   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/auto_build/rpath.pyt	   find_depsX   s    c         C   sb  | p	 i  } t  ƒ  } | t  t d d | ƒƒ O} i  } x6 t | ƒ D]( } t j j | ƒ | t j j | ƒ <qD Wxë t | ƒ D]Ý } d G| GH|  j | ƒ } t  ƒ  } x‹ | D]ƒ } g  | D] }	 |	 | k r¸ |	 ^ q¸ }
 xU |
 D]M }	 t j j | |	 t j j | ƒ ƒ } t j j	 d | ƒ } | j
 | ƒ qÝ Wq« W| r} t d d d j	 | ƒ | g ƒ q} q} Wd  S(   Ns   .soR   s   

====s   $ORIGINt   patchelfs   --set-rpatht   :(   t   setR   t   sortedR   R   t   dirnamet   basenameRA   t   relpathR   t   addR/   (   R;   R   t   replacet   targetst   originst   targett   depst   rpatht   depR   R   RH   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/auto_build/rpath.pyt   runf   s&    	&		%%&N(   t   __name__t
   __module__RA   R   RQ   (    (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/auto_build/rpath.pyR9   W   s   	t   FixMacRpathc           B   s,   e  Z d  Z d „  Z d „  Z d d „ Z RS(   sM   Process all binary files (executables, libraries) to rename linked libraries.c         C   sj   t  d d | g ƒ } d | k r- t d ‚ n  g  | j d ƒ d D] } | j ƒ  j d ƒ d ^ qA } | S(	   s%   Find linked libraries using otool -L.t   otools   -Ls   not an object files   Not Mach-O binarys   
i   R   i    (   R   R(   R   R	   t	   partition(   R;   R<   R   R   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/auto_build/rpath.pyRA   ‡   s
    9c         C   s9   t  | j d ƒ ƒ d } t j j d d g | Œ } | S(   sQ   Generate the @rpath for a file, relative to the current directory as @rpath root.t   /i   s   @loader_paths   ..(   t   lenR   R   R   R   (   R;   R<   R   R   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/auto_build/rpath.pyt   id_rpath‘   s    c         C   sä  | p	 i  } d | | <t  ƒ  } | t  t d d | ƒƒ O} | t  t d d | ƒƒ O} | t  t d | ƒ ƒ O} d Gt | ƒ GHx^t | ƒ D]P} d G| GHy |  j | ƒ } Wn t k
 rÆ } qŒ n Xt j j	 d t j j
 | | ƒ ƒ } t d d | | g d	 | d
 t ƒt j j	 d t j j
 | t j j | ƒ ƒ ƒ } t d d | | g d	 | d
 t ƒxz | D]r }	 |	 }
 x/ | j ƒ  D]! \ } } t j | | |
 ƒ }
 qW|	 |
 k rft d d |	 |
 | g d	 | d
 t ƒqfqfWqŒ Wd  S(   Ns   @rpaths   .soR   s   .dylibs   Targets:s   
==== Target:t   install_name_tools   -idt   cwdR   s   @loader_paths
   -add_rpaths   -change(   RD   R   R   RX   RE   RA   R(   R   R   R   RH   R/   t   TrueRF   t   itemst   ret   sub(   R;   R   RJ   RK   R   t   libst   et   install_name_idRO   R@   t   rlibt   kt   v(    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/auto_build/rpath.pyRQ   —   s.    
		$"0"N(   RR   RS   t   __doc__RA   RY   R   RQ   (    (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/auto_build/rpath.pyRT   „   s   	
	c         C   sÍ   t  j ƒ  } | j d d d ƒ| j d d d d d ƒ| j |  ƒ \ } }  | j r_ t } n  |  d } i  } d	 | d
 <| j r’ d | | j <n  t } t j	 d k r° t
 } n  | ƒ  j d | d | ƒ d  S(   Ns   --otherroott   helps   Other build paths   --drys   Dry runt   actiont
   store_trueiÿÿÿÿs
   @rpath/libs   ^libs   @rpatht   darwinR   RJ   (   t   optparset   OptionParsert
   add_optiont
   parse_argst   dryR\   t	   otherrootR9   t   syst   platformRT   RQ   (   R1   t   parsert   optionsR    R   RJ   t   cls(    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/auto_build/rpath.pyRQ   »   s    		

		t   __main__i   (   Rf   t
   __future__R    R   R^   R#   Rk   Rq   R"   R    R   R   R/   R0   R   t   objectR9   RT   RQ   RR   t   argv(    (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/auto_build/rpath.pyt   <module>   s"   			-7	