ó
½-'Nc           @   sÞ   d  d l  m Z m Z m Z m Z m Z d  d l m Z m Z m	 Z	 m
 Z
 m Z d  d l Z d  d l m Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   iÿÿÿÿ(   t   Transactiont   PolicyInstallt   PolicyUpgradet   PolicyRemovet   Failed(   t   INSTALLt   REMOVEt   UPGRADEt   ALWAYSt   NEVERN(   t   build_skeletont   TransactionErrorc           B   s   e  Z d  Z RS(   s)   Raised when the transaction fails to run.(   t   __name__t
   __module__t   __doc__(    (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyR   
   s   t   DependencyErrorc           B   s    e  Z d  Z d „  Z d „  Z RS(   s9   Raised when a needed dependency wasn't explicitly marked.c         C   s   | |  _  d  S(   N(   t   packages(   t   selfR   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   __init__   s    c         C   s-   d d j  g  |  j D] } t | ƒ ^ q ƒ S(   Ns   Missing dependencies: %ss   , (   t   joinR   t   str(   R   t   package(    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   __str__   s    (   R   R   R   R   R   (    (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyR      s   	t
   SmartErrorc           B   s   e  Z d  Z RS(   s,   Raised when Smart fails in an undefined way.(   R   R   R   (    (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyR      s   t   ChannelErrorc           B   s   e  Z d  Z RS(   s"   Raised when channels fail to load.(   R   R   R   (    (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyR      s   t   SmartFacadec           B   s[  e  Z d  Z d# Z i  d# d „ Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z e d	 „ Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z  d „  Z! d „  Z" d „  Z# d „  Z$ d# d# d  „ Z% d# d# d! „ Z& d" „  Z' RS($   sž   Wrapper for tasks using Smart.

    This object wraps Smart features, in a way that makes using and testing
    these features slightly more comfortable.
    c         C   s?   | j  ƒ  |  _ |  j j d d ƒ | p+ i  |  _ |  j ƒ  d S(   sŽ   
        @param smart_init_kwargs: A dictionary that can be used to pass
            specific keyword parameters to to L{smart.init}.
        t	   interfacet	   landscapeN(   t   copyt   _smart_init_kwargst
   setdefaultt   _sysconfig_argst   _reset(   R   t   smart_init_kwargst   sysconf_args(    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyR   *   s    c         C   s:   d  |  _ i  |  _ i  |  _ i  |  _ t |  _ t |  _ d  S(   N(	   t   Nonet   _ctrlt	   _pkg2hasht	   _hash2pkgt   _marksR   t   _cachingt   Falset   _channels_reloaded(   R   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyR    4   s    					c         C   s$   |  j  r t j ƒ  n  |  j ƒ  d S(   s.   Deinitialize the Facade and the Smart library.N(   R$   t   smartt   deinitR    (   R   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyR,   =   s    	c         C   s  |  j  d  k r|  j j d ƒ d k rA d d l m } | ƒ  n  t j |  j   |  _  x6 |  j j	 ƒ  D]% \ } } t j
 j | | d t ƒqf Wt j |  j  ƒ t j ƒ  t j
 j d t d t ƒt j
 j d t d t ƒd d l m } | |  _ |  j ƒ  n  |  j  S(	   NR   R   iÿÿÿÿ(   t   install_landscape_interfacet   softs   pm-iface-outputs   deb-non-interactive(   t
   DebPackage(   R$   R#   R   t   gett   landscape.package.interfaceR-   R+   t   initR   t   itemst   sysconft   sett   Truet
   initDistrot   initPluginst   smart.backends.deb.baseR/   t   _deb_package_typet   smart_initialized(   R   R-   t   keyt   valueR/   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt	   _get_ctrlC   s    

	c         C   s   d S(   s2   Hook called when the Smart library is initialized.N(    (   R   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyR;   Y   s    c         C   s$   |  j  r d St |  _  |  j ƒ  d S(   s6   Reload the channels if they haven't been reloaded yet.N(   R*   R6   t   reload_channels(   R   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   ensure_channels_reloaded\   s    		c         C   sî   |  j  ƒ  } y | j d |  j ƒ } Wn t j k
 rA t } n X| oU |  j t k } | r} t d t j j	 d ƒ ƒ ‚ n  |  j
 j ƒ  |  j j ƒ  xF |  j ƒ  D]8 } |  j | t ƒ j ƒ  } | |  j
 | <| |  j | <q¤ W|  j ƒ  d S(   sœ   
        Reload Smart channels, getting all the cache (packages) in memory.

        @raise: L{ChannelError} if Smart fails to reload the channels.
        t   cachings$   Smart failed to reload channels (%s)t   channelsN(   R>   t   reloadChannelsR(   R+   t   ErrorR6   R	   R   R4   R0   R&   t   clearR%   t   get_packagest   get_package_skeletonR)   t   get_hasht   channels_reloaded(   R   t   ctrlt   reload_resultt   failedt   pkgt   hash(    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyR?   c   s     
c         C   s   d S(   s.   Hook called after Smart channels are reloaded.N(    (   R   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyRI   €   s    c         C   s   t  | | ƒ S(   sµ  Return a skeleton for the provided package.

        The skeleton represents the basic structure of the package.

        @param pkg: Package to build skeleton from.
        @param with_info: If True, the skeleton will include information
            useful for sending data to the server.  Such information isn't
            necessary if the skeleton will be used to build a hash.

        @return: a L{PackageSkeleton} object.
        (   R
   (   R   RM   t	   with_info(    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyRG   ƒ   s    c         C   s   |  j  j | ƒ S(   ss   Return a hash from the given package.

        @param pkg: a L{smart.backends.deb.base.DebPackage} objects
        (   R%   R0   (   R   RM   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   get_package_hash‘   s    c         C   s   |  j  j ƒ  S(   s=   Get the hashes of all the packages available in the channels.(   R%   t   values(   R   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   get_package_hashes˜   s    c         C   s;   g  |  j  ƒ  j ƒ  j ƒ  D] } t | |  j ƒ r | ^ q S(   sŽ   
        Get all the packages available in the channels.

        @return: a C{list} of L{smart.backends.deb.base.DebPackage} objects
        (   R>   t   getCachet   getPackagest
   isinstanceR:   (   R   RM   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyRF   œ   s    c         C   s   t  j j d |  j ƒ  ƒ S(   s8   Get all packages in the channels matching the set locks.t   lock(   R+   t   pkgconft   filterByFlagRF   (   R   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   get_locked_packages¥   s    c         C   s>   g  |  j  ƒ  j ƒ  j | ƒ D] } t | |  j ƒ r | ^ q S(   s•   
        Get all available packages matching the provided name.

        @return: a C{list} of L{smart.backends.deb.base.DebPackage} objects
        (   R>   RS   RT   RU   R:   (   R   t   nameRM   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   get_packages_by_name©   s    "c         C   s   |  j  j | ƒ S(   s•   
        Get all available packages matching the provided hash.

        @return: a C{list} of L{smart.backends.deb.base.DebPackage} objects
        (   R&   R0   (   R   RN   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   get_package_by_hash²   s    c         C   s   t  |  j | <d  S(   N(   R   R'   (   R   RM   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   mark_installº   s    c         C   s   t  |  j | <d  S(   N(   R   R'   (   R   RM   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   mark_remove½   s    c         C   s   t  |  j | <d  S(   N(   R   R'   (   R   RM   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   mark_upgradeÀ   s    c         C   s   |  j  j ƒ  d  S(   N(   R'   RE   (   R   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   reset_marksÃ   s    c         C   s×  |  j  ƒ  } | j ƒ  } t | ƒ } t } t } xW |  j j ƒ  D]F \ } } | t k ra t } n  | t	 k rv t
 } n  | j | | ƒ q@ W| r™ t } n  | j | ƒ y | j ƒ  Wn& t k
 rÜ } t | j d ƒ ‚ n X| j ƒ  }	 |	 só d  Sg  }
 xB |	 j ƒ  D]4 \ } } |  j j | ƒ | k r|
 j | ƒ qqW|
 rSt |
 ƒ ‚ n  y |  j j |	 ƒ Wn) t j k
 r’} t | j d ƒ ‚ n Xt j j ƒ  } t j j ƒ  } t j j ƒ  | rÓt | ƒ ‚ n  | S(   Ni    (    R>   RS   R    R   R6   R'   R3   R   R   R   R)   t   enqueueR   t	   setPolicyt   runR   R   t   argst   getChangeSetR#   R0   t   appendR   R$   t   commitChangeSetR+   RD   t   ifacet   get_output_for_landscapet   has_failed_for_landscapet   reset_for_landscapeR   (   R   RJ   t   cachet   transactiont   policyt   only_removeRM   t   opert   et	   changesett   missingt   opt   outputRL   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   perform_changesÆ   sJ    			c         C   s*   |  j  ƒ  j ƒ  } | j ƒ  | j ƒ  d  S(   N(   R>   RS   t   resett   load(   R   Rl   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   reload_cacheù   s    
c         C   s   |  j  ƒ  d d l m } | S(   s1   
        Get the host dpkg architecture.
        iÿÿÿÿ(   t   DEBARCH(   R>   t   smart.backends.deb.loaderRz   (   R   Rz   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   get_archþ   s    
c         C   s?   |  j  ƒ  t j j d | ƒ d d l j j j } | | _ d S(   sº   
        Set the host dpkg architecture.

        To take effect it must be called before L{reload_channels}.

        @param arch: the dpkg architecture to use (e.g. C{"i386"})
        s   deb-archiÿÿÿÿN(	   R>   R+   R4   R5   R{   t   backendst   debt   loaderRz   (   R   t   archR   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   set_arch  s    
c         C   s   | |  _  d S(   sä   
        Set Smart's caching mode.

        @param mode: The caching mode to pass to Smart's C{reloadChannels}
            when calling L{reload_channels} (e.g C{smart.const.NEVER} or
            C{smart.const.ALWAYS}).
        N(   R(   (   R   t   mode(    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   set_caching  s    c         C   s'   |  j  ƒ  t j j d i  d t ƒd S(   s%   Remove all configured Smart channels.RB   R.   N(   R>   R+   R4   R5   R6   (   R   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   reset_channels   s    
c         C   s=   |  j  ƒ  } | j i | | 6ƒ t j j d | d t ƒd S(   s“  
        Add a Smart channel.

        This method can be called more than once to set multiple channels.
        To take effect it must be called before L{reload_channels}.

        @param alias: A string identifying the channel to be added.
        @param channel: A C{dict} holding information about the channel to
            add (see the Smart API for details about valid keys and values).
        RB   R.   N(   t   get_channelst   updateR+   R4   R5   R6   (   R   t   aliast   channelRB   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   add_channel%  s    c         C   s<   | } i | d 6| d 6| d 6d d 6} |  j  | | ƒ d S(   sP   Add a Smart channel of type C{"apt-deb"}.

        @see: L{add_channel}
        t   baseurlt   distributiont
   componentss   apt-debt   typeN(   R‰   (   R   t   urlt   codenameRŒ   R‡   Rˆ   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   add_channel_apt_deb4  s    c         C   s.   | } i | d 6d d 6} |  j  | | ƒ d S(   sP   Add a Smart channel of type C{"deb-dir"}.

        @see: L{add_channel}
        t   paths   deb-dirR   N(   R‰   (   R   R‘   R‡   Rˆ   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   add_channel_deb_dir>  s    c         C   s   |  j  ƒ  t j j d ƒ S(   s@   
        @return: A C{dict} of all configured channels.
        RB   (   R>   R+   R4   R0   (   R   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyR…   G  s    
c         C   s‚   |  j  ƒ  g  } t j j d ƒ } xY | D]Q } xH | | D]< } | d pM d } | d p] d } | j | | | f ƒ q: Wq) W| S(   s½   Return all set package locks.

        @return: A C{list} of ternary tuples, contaning the name, relation
            and version details for each lock currently set on the system.
        RV   i    t    i   (   R>   R+   RW   t   getFlagTargetsRf   (   R   t   lockst   locks_by_nameRZ   t	   conditiont   relationt   version(    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   get_package_locksN  s    
c         C   s<   | r | r t  d ƒ ‚ n  | r8 | r8 t  d ƒ ‚ n  d  S(   Ns!   Package lock version not provideds"   Package lock relation not provided(   t   RuntimeError(   R   R˜   R™   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   _validate_lock_condition^  s    c         C   s7   |  j  | | ƒ |  j ƒ  t j j d | | | ƒ d S(   s&  Set a new package lock.

        Any package matching the given name and possibly the given version
        condition will be locked.

        @param name: The name a package must match in order to be locked.
        @param relation: Optionally, the relation of the version condition the
            package must satisfy in order to be considered as locked.
        @param version: Optionally, the version associated with C{relation}.

        @note: If used at all, the C{relation} and C{version} parameter must be
           both provided.
        RV   N(   Rœ   R>   R+   RW   t   setFlag(   R   RZ   R˜   R™   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   set_package_lockd  s    
c         C   s@   |  j  | | ƒ |  j ƒ  t j j d d | d | d | ƒd S(   s   Remove a package lock.RV   RZ   R˜   R™   N(   Rœ   R>   R+   RW   t	   clearFlag(   R   RZ   R˜   R™   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   remove_package_lockv  s    
c         C   s   |  j  ƒ  } | j ƒ  d S(   s.   Flush the current smart configuration to disk.N(   R>   t   saveSysConf(   R   t   control(    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   save_config}  s    N((   R   R   R   R#   R:   R   R    R,   R>   R;   R@   R?   RI   R6   RG   RP   RR   RF   RY   R[   R\   R]   R^   R_   R`   Rv   Ry   R|   R   Rƒ   R„   R‰   R   R’   R…   Rš   Rœ   Rž   R    R£   (    (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyR   !   sH   
																					3				
			
					(   t   smart.transactionR    R   R   R   R   t   smart.constR   R   R   R   R	   R+   t   landscape.package.skeletonR
   t	   ExceptionR   R   R   R   t   objectR   (    (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   <module>   s   ((