ó
‡ˆ\c           @   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 y e Wn e k
 ro e	 Z n Xd „  Z
 d „  Z d „  Z d Z d Z d d „ Z d d	 „ Z d S(
   s   
Disk management utilities.
iÿÿÿÿNc         C   s   d } xx t  j |  ƒ d g D]` } t  j t  j j |  | ƒ ƒ } t | d ƒ rd | | j d 7} q | | j d d d 7} q Wt | d ƒ S(   s&    Return the disk usage in a directory.i    t   .t	   st_blocksi   i   g      @(	   t   ost   listdirt   statt   patht   joint   hasattrR   t   st_sizet   int(   R   t   sizet   fileR   (    (    s<   lib/python2.7/site-packages/sklearn/externals/joblib/disk.pyt	   disk_used   s    c         C   s‰   d } t  d | d | d d | d ƒ } y& t | |  d t |  d  ƒ ƒ } Wn3 t t f k
 r„ t d |  t |  ƒ f ƒ ‚ n X| S(	   s2    Convert a memory text to its value in bytes.
    i   t   Kt   Mi   t   Gi   iÿÿÿÿsQ   Invalid literal for size give: %s (type %s) should be alike '10G', '500M', '50K'.(   t   dictR	   t   floatt   KeyErrort
   ValueErrort   type(   t   textt   kilot   unitsR
   (    (    s<   lib/python2.7/site-packages/sklearn/externals/joblib/disk.pyt   memstr_to_bytes)   s    #&c         C   sC   y t  j |  ƒ Wn+ t k
 r> } | j t j k r? ‚  q? n Xd S(   sg   Ensure directory d exists (like mkdir -p on Unix)
    No guarantee that the directory is writable.
    N(   R   t   makedirst   OSErrort   errnot   EEXIST(   t   dt   e(    (    s<   lib/python2.7/site-packages/sklearn/externals/joblib/disk.pyt   mkdirp7   s
    gš™™™™™¹?i   c         C   s•   g  } y t  j |  ƒ } Wn? t  j k
 rZ | d k	 rT | t  j |  t j ƒ  ƒ q[ ‚  n Xx3 | D]+ } t  j j |  | ƒ } t | d | ƒqb Wd S(   sÌ  Remove all subdirectories in this path.

    The directory indicated by `path` is left in place, and its subdirectories
    are erased.

    If onerror is set, it is called to handle the error with arguments (func,
    path, exc_info) where func is os.listdir, os.remove, or os.rmdir;
    path is the argument to that function that caused it to fail; and
    exc_info is a tuple returned by sys.exc_info().  If onerror is None,
    an exception is raised.
    t   onerrorN(	   R   R   t   errort   Nonet   syst   exc_infoR   R   t   delete_folder(   R   R    t   namest   namet   fullname(    (    s<   lib/python2.7/site-packages/sklearn/externals/joblib/disk.pyt
   rm_subdirsJ   s    c         C   sÂ   t  j j |  ƒ r¾ | d k	 r4 t j |  t | ƒ q¾ d } x t rº y t j |  t d ƒ PWq= t t	 f k
 r¶ | d 7} | t
 k r¦ t j d j |  t
 ƒ ƒ ‚  n  t j t ƒ q= Xq= Wn  d S(   sB   Utility function to cleanup a temporary folder if it still exists.i    i   s/   Unable to delete folder {} after {} tentatives.N(   R   R   t   isdirR"   t   shutilt   rmtreet   Falset   TrueR   t   WindowsErrort   RM_SUBDIRS_N_RETRYt   warningst   warnt   formatt   timet   sleept   RM_SUBDIRS_RETRY_TIME(   t   folder_pathR    t	   err_count(    (    s<   lib/python2.7/site-packages/sklearn/externals/joblib/disk.pyR%   h   s     	
(   t   __doc__R   R#   R4   R   R+   R1   R/   t	   NameErrorR   R   R   R   R6   R0   R"   R)   R%   (    (    (    s<   lib/python2.7/site-packages/sklearn/externals/joblib/disk.pyt   <module>   s"   
			