ó
ß™xTc           @   s  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 d  d l j Z	 d  d l
 Z
 d  d l Z d „  Z d „  Z e j e e e d „ Z d „  Z d d „ Z e e j d „ Z d	 „  Z d
 „  Z e j d „ Z d d d „ Z e e d „ Z d „  Z d S(   iÿÿÿÿ(   t   divisionNc           C   sY   t  j d k s" t d d ƒ ‚ n3 t  j d	 k  rU t j d d t ƒ t j d ƒ n  d  S(
   Ni   i   s3   Python version 2.3 or greater required to run this s   script.i   s;   You are running an obsolete version of Python; this script s9   should still run, but not all functionality is available.(   i   i   (   i   i   (   t   syst   version_infot	   Exceptiont   warningst   warnt   DeprecationWarningt   timet   sleep(    (    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/auto_build/installer_utils.pyt   check_python_version   s    	c         C   s0  d  } t j d d k r( d d  l } nX t j j t j j t j j t ƒ ƒ ƒ } | t j k rt t j j	 | ƒ n  d d  l
 } t |  t ƒ r¡ d j |  ƒ }  n  | j d |  d t d d d d  d	 | d
 | j d t d t ƒ } | j ƒ  | j ƒ  | j ƒ  | j } | d k r,t d |  | f ƒ ‚ n  d  S(   Ni   i   iÿÿÿÿt    t   argst   shellt   bufsizet   stdint   stdoutt   stderrt   universal_newlinest	   close_fdsi    s%   Call to '%s' failed with exit code %d(   t   NoneR   R   t
   subprocesst   ost   patht   abspatht   dirnamet   __file__t   appendt   subprocess_with_fixest
   isinstancet   listt   joint   Popent   Truet   STDOUTt   Falset   flusht   waitt
   returncodet   RuntimeError(   R   t   logt   rcR   t   libtbx_patht   p(    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/auto_build/installer_utils.pyt   call   s0    *			


	c   
      C   so  t  j j |  ƒ s t |  ƒ ‚ d } } | r7 d } n  | rF d } n  d d | | f g } |  j d ƒ r„ d d | | f g } n( |  j d ƒ r¬ d d	 | | f g } n  | |  g } t d
 j | ƒ | ƒ t j d d t j d d t j d d t  j j	 |  ƒ ƒ ƒ ƒ }	 | rkt  j j
 |	 ƒ s[t j d ƒ t  j j
 |	 ƒ s[t d |	 ƒ ‚ q[n  t  j j |	 ƒ Sd  S(   Nt    t   vt   ot   tars   x%s%sft   gzs   zx%s%sft   bz2s   jx%s%sfR
   s   .tgzs   .tar.gzs   .tar.bz2i   s"   Expected directory '%s' not found!(   R   R   t   isfilet   AssertionErrort   endswithR+   R   t   ret   subt   basenamet   isdirR   R   R&   R   R   (
   t   pkg_nameR'   t   verboset   change_ownershipt   check_output_patht   verbose_flagt
   owner_flagt   cmdR   t   dir_name(    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/auto_build/installer_utils.pyt   untar7   s0    
		c          C   s8   t  j ƒ  }  |  d } | j d ƒ \ } } } t | ƒ S(   Ni   t   .(   R   t   unamet   splitt   int(   RC   t   versiont   majort   minort   rev(    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/auto_build/installer_utils.pyt   detect_osx_versionR   s    
c         C   s–   t  j j |  ƒ s t ‚ t | d ƒ j t |  d ƒ j ƒ  ƒ t  j |  t  j ƒ sX | r’ t  j	 | ƒ j
 } t  j | | t	 j Bt	 j Bt	 j Bƒ n  d  S(   Nt   wbt   rb(   R   R   R2   R3   t   opent   writet   readt   accesst   X_OKt   statt   st_modet   chmodt   S_IXUSRt   S_IXGRPt   S_IXOTH(   t   src_patht	   dest_patht
   executablet   mode(    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/auto_build/installer_utils.pyt	   copy_fileX   s
    %c         C   sU  t  j j |  ƒ s t |  ƒ ‚ t  j j | ƒ s= t | ƒ ‚ | rT | d | IJn  t  j | ƒ xí t  j |  ƒ D]Ü } t  j j |  | ƒ } t  j j | | ƒ } t  j j | ƒ rÕ t  j	 | ƒ } t  j
 | | ƒ qq t  j j | ƒ r| r| d | | f IJn  t | | ƒ qq t  j j | ƒ r6t | | ƒ qq | rq | d | IJqq qq Wd  S(   Ns   creating %ss     copy %s -> %ss     skipping %s(   R   R   R8   R3   t   existst   makedirst   listdirR   t   islinkt   readlinkt   symlinkR2   R\   t	   copy_tree(   RX   RY   R:   R'   t	   path_namet   node_src_patht   node_dest_patht   target_path(    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/auto_build/installer_utils.pyRc   d   s&    c          C   sw   t  j ƒ  }  |  d } |  d d k rs i d d 6d d 6d d	 6d
 d 6d d 6d d 6} | j | j d ƒ d d ƒ S| S(   Ni   i    t   Darwins   10.9t   13s   10.8t   12s   10.7t   11s   10.6t   10s   10.5t   9s   10.4t   8RB   t   unknown(   R   RC   t   getRD   (   RC   t   kernel_versiont   os_versions(    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/auto_build/installer_utils.pyt   get_os_versionz   s    

c          C   sÀ   d d  l  }  |  j ƒ  } | d d k r1 d } n# | d d k rJ d } n
 | d } | d d k rq | d 7} nK | d k r¼ | d	 j d
 ƒ } t | d ƒ } | d k r¼ | d 7} q¼ n  | S(   Niÿÿÿÿi    t   Linuxs   intel-linux-2.6Rh   s   mac-intel-osxt   x86_64s   -x86_64i   RB   i
   (   R   RC   RD   RE   (   R   RC   t   platformt   version_fieldst   major_version(    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/auto_build/installer_utils.pyt   machine_type‰   s    		
c         C   s0  t  j ƒ  } t  j |  ƒ xt  j |  ƒ D]ÿ } t j |  | ƒ } t j | ƒ r) t j | ƒ } t j | | g ƒ } t j	 | | ƒ } t j	 | | ƒ } g  t j
 | ƒ D] }	 d ^ q¨ d }
 t |
 ƒ d k râ t j |
 Œ  } n d } t j | | ƒ } | d | IJt  j | ƒ t  j | | ƒ q) q) Wd S(   s½   
  Rewrite all symlinks in a directory with relative paths (allows later
  relocation).  This is mostly just for Mac OS where there are a lot of links
  into the Python.framework bundle.
  s   ..i   i    RB   s     creating symlink to %sN(   R   t   getcwdt   chdirR_   t   opR   R`   t   realpatht   commonprefixt   relpathRD   t   lent   removeRb   (   R@   R'   t   old_cwdt	   file_namet	   full_patht	   real_patht   prefixt   rel_path_linkt   rel_path_targett   dt   parent_dirst
   parent_dirt   new_path(    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/auto_build/installer_utils.pyt   regenerate_relative_symlinks›   s"    &c   
      C   sý   g  } xð t  j |  ƒ D]ß \ } } } xI | D]A } | | k r, t j | | ƒ } t j | ƒ | j | ƒ q, q, Wx� | D]y }	 t j | |	 ƒ } |	 | k r¹ t  j | ƒ | j | ƒ qx | d k	 rx |	 j	 | ƒ rx t  j | ƒ | j | ƒ qx qx Wq W| S(   sË   
  Recursively walk through a directory and delete any files (or directories)
  with the specified file name or extension.  Effectively equivalent to
  running 'find . -name "PATTERN" | xargs rm -rf'.
  N(
   R   t   walkR|   R   t   shutilt   rmtreeR   R�   R   R4   (
   R@   Rƒ   t   file_extt   deletedR   t   dirnamest	   filenamest   dnR„   t   fn(    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/auto_build/installer_utils.pyt   find_and_delete_filesµ   s     c         C   s¸  t  j |  ƒ }  t  j |  ƒ r0 |  t j ƒ  k s6 t ‚ t  j |  ƒ } t  j t j ƒ  | ƒ } | rv t j	 |  | ƒ n t
 |  | ƒ t  j t  j | d ƒ ƒ rÝ y! t d | | f d t j ƒWqÝ t k
 rÙ } | GHqÝ Xn  t | d d ƒt | d d ƒt | d d ƒt | d d ƒt | d d	 ƒt | d d
 ƒ| r¢t d | | f d t j ƒt  j t j ƒ  | d ƒ } t  j | ƒ s‘t ‚ t j | ƒ | St  j t j ƒ  | ƒ S(   sa   
  Create a clean copy of a source repository, optionally bundling it as a
  gzipped tar file.
  s   .svns   svnversion %s > %s/.svnversionR'   R‘   s   .pycs   .pyos   .swpRƒ   s   .gits	   .sconsigns   tar -czf %s.tar.gz %ss   .tar.gz(   R|   R   R8   R   Rz   R3   R7   R   R�   t   copytreeRc   R]   R+   R   R   R&   R—   R2   R�   (   R@   t   create_tarfilet
   use_shutilt   module_namet
   local_patht   et   tar_file(    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/auto_build/installer_utils.pyt   archive_distÌ   s4    'c         C   s—   x� t  j |  ƒ D] \ } } } xm | D]e } t j | | ƒ } | j d ƒ r& t j | ƒ r& y t d | d | ƒWq‹ t k
 r‡ q‹ Xq& q& Wq Wd  S(   Ns   .sos   strip %sR'   (   R   RŽ   R|   R   R4   R2   R+   R&   (   R@   R'   R   R“   R”   R–   R„   (    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/auto_build/installer_utils.pyt
   strip_libsí   s    (   t
   __future__R    R   R�   R   RR   R5   t   os.pathR   R|   R   R   R	   R+   R   R"   R    RA   RJ   R   R\   Rc   Rs   Ry   R�   R—   RŸ   R    (    (    (    sO   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/auto_build/installer_utils.pyt   <module>   s*   	
				!