ó
“w(Nc           @   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 m Z 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   ManagerPlugint	   SUCCEEDEDt   FAILED(   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   sq   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 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   _wrap_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            so   ˆ j  | ƒ } i d d 6| d d 6‰  ‡  f d †  } ‡  f d †  } | j | | ƒ | j ‡ f d †  ƒ d S(   sr   
        Wrap C{_handle_repositories} to generate an activity result based on
        the returned value.
        s   operation-resultt   types   operation-idc            s   t  ˆ  d <ˆ  S(   Nt   status(   R   (   t   ignored(   t   operation_result(    s@   /usr/lib/python2.7/dist-packages/landscape/manager/aptsources.pyt   success/   s    
c            s1   t  ˆ  d <d |  j j |  j f } | ˆ  d <ˆ  S(   NR   s   %s: %ss   result-text(   R   R   R   t   value(   t   failuret   text(   R   (    s@   /usr/lib/python2.7/dist-packages/landscape/manager/aptsources.pyt   fail3   s    

c            s   ˆ  j  j j |  d t ƒS(   Nt   urgent(   t   managert   brokert   send_messaget   True(   t   result(   R   (    s@   /usr/lib/python2.7/dist-packages/landscape/manager/aptsources.pyt   <lambda>:   s    N(   t   _handle_repositoriest   addCallbackst   addBoth(   R   t   messaget   deferredR   R   (    (   R   R   s@   /usr/lib/python2.7/dist-packages/landscape/manager/aptsources.pyR   %   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   R$   t   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   R   (   R   R   R+   R,   t   signal(    (    s@   /usr/lib/python2.7/dist-packages/landscape/manager/aptsources.pyt   _handle_process_failureE   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_continueO   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 ƒ 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   ignoreR5   (   R   (    s@   /usr/lib/python2.7/dist-packages/landscape/manager/aptsources.pyR%   u   s    t   sourcesN(   R    t   Nonet   tempfilet   mkstempR2   t   closet   filet   writet   addCallbackR.   R(   R6   t
   addErrbackR1   t   _handle_sources(   R   R)   R*   t   keyt   fdR5   t   key_file(    (   R   s@   /usr/lib/python2.7/dist-packages/landscape/manager/aptsources.pyR&   W   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 ƒ  S(
   s   Handle sources repositories.R7   t   #s   #%ss   *.lists   %s.saves   landscape-%s.listt   namet   contenti¤  (   R<   R=   R2   R>   R?   t   SOURCES_LISTt   stript
   startswithR@   t   statt   shutilt   movet   chmodt   st_modet   chownt   st_uidt   st_gidt   globR5   t   joint   SOURCES_LIST_Dt   _run_reporter(   R   R   R:   RE   R5   t   new_sourcest   linet   stripped_linet   original_statt   filenamet   sourcet   sources_file(    (    s@   /usr/lib/python2.7/dist-packages/landscape/manager/aptsources.pyRC   }   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-smart-updates   --config=%si    t	   landscapeR   R   N(   R   R   t   configR;   t   appendR2   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.pyRX   ›   s    		N(   R   R   R	   RJ   RW   R   R;   R   R   R.   R1   R6   R&   RC   RX   (    (    (    s@   /usr/lib/python2.7/dist-packages/landscape/manager/aptsources.pyR
      s   				
		&	(   RU   R2   Rd   Rg   RN   R<   t   twisted.internet.deferR    t   landscape.lib.twisted_utilR   t   landscape.manager.pluginR   R   R   t   landscape.package.reporterR   t	   ExceptionR   R
   (    (    (    s@   /usr/lib/python2.7/dist-packages/landscape/manager/aptsources.pyt   <module>   s   