ó
ÍÿŠTc           @   s¸   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 m	 Z	 d  d l
 m Z d  d l m Z d e f d „  ƒ  YZ d e f d	 „  ƒ  YZ d S(
   iÿÿÿÿN(   t   succeed(   t   spawn_process(   t   ManagerPlugin(   t   find_reporter_commandt   ProcessErrorc           B   s   e  Z d  Z RS(   s.   Exception raised when running a process fails.(   t   __name__t
   __module__t   __doc__(    (    (    s@   /usr/lib/python2.7/dist-packages/landscape/manager/aptsources.pyR      s   t
   AptSourcesc           B   sh   e  Z d  Z d Z d Z d „  Z d d d „ Z d „  Z d „  Z	 d „  Z
 d „  Z d	 „  Z d
 „  Z RS(   s'   A plugin managing sources.list content.s   /etc/apt/sources.lists   /etc/apt/sources.list.dc         C   s-   t  t |  ƒ j | ƒ | j d |  j ƒ d  S(   Ns   apt-sources-replace(   t   superR   t   registert   register_messaget   _handle_repositories(   t   selft   registry(    (    s@   /usr/lib/python2.7/dist-packages/landscape/manager/aptsources.pyR
      s    	c         C   s   t  | | d | d | ƒS(   sW   
        Run the process in an asynchronous fashion, to be overriden in tests.
        t   uidt   gid(   R   (   R   t   commandt   argsR   R   (    (    s@   /usr/lib/python2.7/dist-packages/landscape/manager/aptsources.pyt   _run_process   s    c         C   s2   | \ } } } | r. t  d | | f ƒ ‚ n  d S(   sQ   
        Turn a failed process command (code != 0) to a C{ProcessError}.
        s   %s
%sN(   R   (   R   t   resultt   outt   errt   code(    (    s@   /usr/lib/python2.7/dist-packages/landscape/manager/aptsources.pyt   _handle_process_error%   s    c         C   sB   | j  t ƒ s: | j \ } } } t d | | f ƒ ‚ n | Sd S(   sG   
        Turn a signaled process command to a C{ProcessError}.
        s   %s
%sN(   t   checkR   t   value(   R   t   failureR   R   t   signal(    (    s@   /usr/lib/python2.7/dist-packages/landscape/manager/aptsources.pyt   _handle_process_failure-   s    c         C   s   t  j | ƒ | S(   sd   
        Remove the temporary file created for the process, and forward the
        result.
        (   t   ost   unlink(   R   t   passthrought   path(    (    s@   /usr/lib/python2.7/dist-packages/landscape/manager/aptsources.pyt   _remove_and_continue7   s    c            sâ   t  d ƒ ‰ x“ | d D]‡ } t j ƒ  \ } } t j | ƒ t | d ƒ } | j | ƒ | j ƒ  ˆ j | ‡  f d † ƒ ˆ j ˆ  j	 ƒ ˆ j
 ˆ  j | ƒ q Wˆ j ˆ  j ƒ ˆ j ˆ  j | d ƒ ˆ  j | ‡ f d †  ƒ S(   sE  
        Handle a list of repositories to set on the machine.

        The format is the following:

        {"sources": [
          {"name": "repository-name",
           "content":
              "deb http://archive.ubuntu.com/ubuntu/ maverick main
              "deb-src http://archive.ubuntu.com/ubuntu/ maverick main"}
          {"name": "repository-name-dev",
           "content":
              "deb http://archive.ubuntu.com/ubuntu/ maverick universe
              "deb-src http://archive.ubuntu.com/ubuntu/ maverick universe"}],
         "gpg-keys": ["-----BEGIN PGP PUBLIC KEY BLOCK-----
                      XXXX
                      -----END PGP PUBLIC KEY BLOCK-----",
                      "-----BEGIN PGP PUBLIC KEY BLOCK-----
                      YYY
                      -----END PGP PUBLIC KEY BLOCK-----"]}
        s   gpg-keyst   wc            s   ˆ  j  d d | g ƒ S(   Ns   /usr/bin/apt-keyt   add(   R   (   t   ignoreR!   (   R   (    s@   /usr/lib/python2.7/dist-packages/landscape/manager/aptsources.pyt   <lambda>]   s    t   sourcesc              s   ˆ  S(   N(    (    (   t   deferred(    s@   /usr/lib/python2.7/dist-packages/landscape/manager/aptsources.pyR&   c   s    N(   R    t   Nonet   tempfilet   mkstempR   t   closet   filet   writet   addCallbackR   t   addBothR"   t
   addErrbackR   t   _handle_sourcest   call_with_operation_result(   R   t   messaget   keyt   fdR!   t   key_file(    (   R   R(   s@   /usr/lib/python2.7/dist-packages/landscape/manager/aptsources.pyR   ?   s    
c         C   s­  t  j ƒ  \ } } t j | ƒ t | d ƒ } xZ t |  j ƒ D]I } | j ƒ  } | sf | j d ƒ rv | j | ƒ q> | j d | ƒ q> W| j ƒ  t j	 |  j ƒ } t
 j | |  j ƒ t j |  j | j ƒ t j |  j | j | j ƒ x= t j t j j |  j d ƒ ƒ D] }	 t
 j |	 d |	 ƒ qWxh | D]` }
 t j j |  j d |
 d ƒ }	 t |	 d ƒ } | j |
 d ƒ | j ƒ  t j |	 d	 ƒ q3W|  j ƒ  j d
 „  ƒ S(   s   Handle sources repositories.R#   t   #s   #%ss   *.lists   %s.saves   landscape-%s.listt   namet   contenti¤  c         S   s   d  S(   N(   R)   (   t   ignored(    (    s@   /usr/lib/python2.7/dist-packages/landscape/manager/aptsources.pyR&      s    (   R*   R+   R   R,   R-   t   SOURCES_LISTt   stript
   startswithR.   t   statt   shutilt   movet   chmodt   st_modet   chownt   st_uidt   st_gidt   globR!   t   joint   SOURCES_LIST_Dt   _run_reporterR/   (   R   R;   R'   R6   R!   t   new_sourcest   linet   stripped_linet   original_statt   filenamet   sourcet   sources_file(    (    s@   /usr/lib/python2.7/dist-packages/landscape/manager/aptsources.pyR2   e   s.    
(
c         C   s¥   t  ƒ  } d g } |  j j j d k	 rD | j d |  j j j ƒ n  t j ƒ  d k r} t j d ƒ j	 } t
 j d ƒ j } n d } d } |  j | | d | d | ƒS(   s;   Once the repositories are modified, trigger a reporter run.s   --force-apt-updates   --config=%si    t	   landscapeR   R   N(   R   R   t   configR)   t   appendR   t   getuidt   pwdt   getpwnamt   pw_uidt   grpt   getgrnamt   gr_gidR   (   R   t   reporterR   R   R   (    (    s@   /usr/lib/python2.7/dist-packages/landscape/manager/aptsources.pyRJ   ƒ   s    		N(   R   R   R   R<   RI   R
   R)   R   R   R   R"   R   R2   RJ   (    (    (    s@   /usr/lib/python2.7/dist-packages/landscape/manager/aptsources.pyR      s   			
		&	(   RG   R   RV   RY   R@   R*   t   twisted.internet.deferR    t   landscape.lib.twisted_utilR   t   landscape.manager.pluginR   t   landscape.package.reporterR   t	   ExceptionR   R   (    (    (    s@   /usr/lib/python2.7/dist-packages/landscape/manager/aptsources.pyt   <module>   s   