ó
È”Mc           @   sÂ   d  d l  m Z m Z d  d l m Z d  d l Td  d l Td  d l Z d  d l Z d  d l	 Z	 d  d l
 Z
 e d ƒ Z e d ƒ Z e d ƒ Z e d ƒ Z d	 „  Z d
 „  Z d „  Z d „  Z d S(   iÿÿÿÿ(   t   OptionParsert
   append_all(   t   getFileDigest(   t   *Ns   smart mirror [options]sº  
This command allows one to manipulate mirrors. Mirrors are URLs
that supposedly provide the same contents as are available in
other URLs, named origins in this help text. There is no internal
restriction on the kind of information which is mirrored. Once
an origin URL is provided, and one or more mirror URLs are
provided, these mirrors will be considered for any file which
is going to be fetched from an URL starting with the origin URL.
Whether the mirror will be chosen or not will depend on the
history of downloads from this mirror and from other mirrors for
the same URL, since mirrors are automatically balanced so that
the fastest mirror and with less errors is chosen. When errors
occur, the next mirror is tried.

For instance, if a mirror "http://mirror.url/path/" is provided
for the origin "ftp://origin.url/other/path/", and a file in
"ftp://origin.url/other/path/subpath/somefile" is going to be
fetched, the mirror will be considered for being used, and the
URL "http://mirror.url/path/subpath/somefile" will be used if
the mirror is chosen. Notice that strings are compared and
replaced without any pre-processing, so that it's possible to
use URLs ending in prefixes of directory entries.
sŸ  
smart mirror --show
smart mirror --add ftp://origin.url/some/path/ http://mirror.url/path/
smart mirror --remove ftp://origin.url/some/path/ http://mirror.url/path/
smart mirror --add http://some.url/path/to/mirrors.txt
smart mirror --sync http://some.url/path/to/mirrors.txt
smart mirror --clear-history ftp://origin.url/some/path/
smart mirror --clear-history ftp://mirror.url/path/
smart mirror --clear-history
sæ   #
# Example:
#
# http://origin.url/some/path/
#     http://mirror.url/another/path/
#     http://some.mirror.url/to/the/same/content/
#
# http://another.origin.url/another/path/
#     http://another.mirror.url/yet/another/path/
#
c       	   C   sˆ  t  d t d t d t ƒ }  g  |  j d <g  |  j d <g  |  j d <d  |  j d <|  j d d	 d
 d t d ƒ ƒ|  j d d	 d
 d t d ƒ ƒ|  j d d	 d d t d t d ƒ ƒ|  j d d	 d d t d t d ƒ ƒ|  j d d	 d d t d t d ƒ ƒ|  j d d	 d d d d t d ƒ ƒ|  j d d	 d
 d t d ƒ ƒ|  j d d	 d d t d t d ƒ ƒ|  j d d	 d
 d t d  ƒ ƒ|  S(!   Nt   usaget   descriptiont   examplest   addt   removet
   remove_allt   clear_historys   --showt   actiont
   store_truet   helps   show current mirrorss   --yamls#   show current mirrors in YAML formats   --addt   callbacks   add to the given origin URL the given mirror URL, provided either in pairs, or in a given file/url in the format used by --shows   --removes„   remove from the given origin URL the given mirror URL, provided either in pairs, or in a given file/url in the format used by --shows   --remove-alls,   remove all mirrors for the given origin URLss   --synct   storet   metavart   FILEsv   synchronize mirrors from the given file/url, so that origins in the given file will have exactly the specified mirrorss   --edits%   edit mirrors in editor set by $EDITORs   --clear-historys?   clear history for the given origins/mirrors, or for all mirrorss   --show-penalitiessM   show current penalities for origins/mirrors, based on the history information(	   R    t   USAGEt   DESCRIPTIONt   EXAMPLESt   defaultst   Nonet
   add_optiont   _R   (   t   parser(    (    s9   /usr/lib/python2.7/dist-packages/smart/commands/mirror.pyt   option_parserP   s4    	c         C   s+   t  ƒ  } | j |  ƒ \ } } | | _ | S(   N(   R   t
   parse_argst   args(   t   argvR   t   optsR   (    (    s9   /usr/lib/python2.7/dist-packages/smart/commands/mirror.pyt   parse_optionst   s    		c   
   	   C   sþ  t  } d | k r’ | } |  j | g t d ƒ ƒ \ } } | r t t d ƒ d j g  | D] } d | | | f ^ qX ƒ ‚ n  | | } t } n( t j j | ƒ sº t t d ƒ | ‚ n  zg  } d  } d  } xÆ t
 | ƒ D]¸ }	 |	 j ƒ  } | sÜ | d d k rqÜ n  |	 d j ƒ  r$| } n> | rS| d  k rS| j | ƒ | j d  ƒ n  | } d  } qÜ | szt t d	 ƒ ‚ n  | j | ƒ | j | ƒ qÜ W| rÇ| d  k rÇ| j | ƒ | j d  ƒ n  Wd  | rù| j t j d
 ƒ ƒ rùt j | ƒ n  X| S(   Ns   :/s   mirror descriptionss(   Failed to download mirror descriptions:
s   
s
       %s: %ss   File not found: %si    t   #s   Invalid mirrors files   data-dir(   t   Falset   downloadURLsR   t   Errort   joint   Truet   ost   patht   isfileR   t   opent   stript   isspacet   appendt
   startswitht   sysconft   gett   unlink(
   t   ctrlt   filenamet   fetchedt   urlt   succt   failt   resultt   origint   mirrort   line(    (    s9   /usr/lib/python2.7/dist-packages/smart/commands/mirror.pyt   read_mirrorsz   sL    !	+
		c         C   sõ  | j  rÍ t | j  ƒ d k r: t |  | j  d ƒ | _  n  t | j  ƒ d d k re t t d ƒ ‚ n  xe t d t | j  ƒ d ƒ D]E } | j  | | d !\ } } | r t j  d | f | d t ƒq q Wn  | j rÖt | j ƒ d k rt |  | j d ƒ | _ n  t | j ƒ d d k r2t t d ƒ ‚ n  x¡ t d t | j ƒ d ƒ D] } | j | | d !\ } } t j	 d | f ƒ st
 j t d ƒ | ƒ n  t j d | f | ƒ sNt
 j t d	 ƒ | ƒ qNqNWn  | j r%xC | j D]5 } t j d | f ƒ sét
 j t d ƒ | ƒ qéqéWn  | j rÔi  } t |  | j ƒ } x‹ t d t | ƒ d ƒ D]n } | | | d !\ } } | | k r¨t | | <t j d | f ƒ n  | r_t j  d | f | d t ƒq_q_Wn  | j d  k	 rP| j r>t j d
 g  ƒ } g  | D] } | d | j k r| ^ q| (t j d
 | ƒ qPt j d
 ƒ } n  | j r£t j d d ƒ }	 x5 |	 D]* } | GHx |	 | D] } d G| GHqˆWHqrWn  | j r d d  l }
 i  } t j d d ƒ }	 x |	 D] } |	 | | | <q×W|
 j | ƒ GHn  | j ròt j ƒ  t j d ƒ \ } } t j | d ƒ } | t IJt j d ƒ } | j ƒ  xR | D]J } | | IJt j d | f ƒ }	 x |	 D] } | d I| IJqW| JqdW| j ƒ  t j j d d ƒ } t  | ƒ } xt rát j! d | | f ƒ t  | ƒ } | | k rPn  y t |  | ƒ } WnE t k
 rs} t
 j" t# | ƒ ƒ t
 j$ t d ƒ t ƒ sàPqtqàn Xi  } xP t d t | ƒ d ƒ D]6 } | | | d !\ } } | j% | g  ƒ j& | ƒ q“Wt j d | ƒ PqàWt j' | ƒ n  | j( rñ|  j) ƒ  |  j* ƒ  j+ ƒ  } | j, ƒ  j- ƒ  } t j d d ƒ }	 xU |	 D]M } | | k rad | | <n  x+ |	 | D] } | | k rld | | <qlqlWqBWg  | j. ƒ  D] \ } } | | f ^ q } | j ƒ  x& | D] \ } } d | | f GHqÏWn  d  S(   Ni   i    i   s   Invalid arguments for --addt   mirrorst   uniques   Invalid arguments for --removes   Origin not found: %ss   Mirror not found: %ss   mirrors-historys      iÿÿÿÿs   .txtt   wt   EDITORt   vis   %s %ss	   Continue?s   %s %d(    (    (    (/   R   t   lenR;   R#   R   t   rangeR.   R%   R   t   hast   ifacet   warningR	   t   syncR
   R   R/   t   sett   showt   yamlt   dumpt   editt   assertWritablet   tempfilet   mkstempR&   t   fdopent   EDITEXAMPLEt   keyst   sortt   closet   environR   t   systemt   errort   unicodet   askYesNot
   setdefaultR,   R0   t   show_penalitiest   reloadMirrorst
   getFetchert   getMirrorSystemt   getPenalitiest   copyt   items(   R1   R   t   iR8   R9   t   resett   lstt   historyt   xR<   RI   t   yamlmirrorst   fdt   namet   filet   originst   editort	   olddigestt	   newdigestt   et
   newmirrorst   mirrorsystemt
   penalitiest   yt   penalityR4   (    (    s9   /usr/lib/python2.7/dist-packages/smart/commands/mirror.pyt   main¤   sÐ    	"&	"!	!	
&	-			




		
+
(   t   smart.optionR    R   t   smart.util.filetoolsR   t   smart.channelt   smartt   textwrapRM   t   sysR&   R   R   R   R   RP   R   R   R;   Rt   (    (    (    s9   /usr/lib/python2.7/dist-packages/smart/commands/mirror.pyt   <module>   s"   

					$		*