
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 Z d  d l m Z d  d l	 m
 Z
 d  d l Z e j d d d d e [ 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 m Z m Z m Z d  d l m 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 e& f d     YZ' d S(   iN(   t   StringIO(   t
   attrgettert   ignoret   modulet   aptt   category(   t   SourcesList(   t   AcquireProgress(   t   InstallProgress(   t   append_filet   create_filet	   read_filet
   touch_file(   t   UBUNTU_PATH(   t   build_skeleton_aptt   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   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   LandscapeAcquireProgressc           B   s   e  Z d    Z RS(   c         G   s   d S(   sD  Override trying to get the column count of the buffer.

        We always send the output to a file, not to a terminal, so the
        default width (80 columns) is fine for us.

        Overriding this method means that we don't have to care about
        fcntl.ioctl API differences for different Python versions.
        N(    (   R   t   dummy(    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   _winch3   s    (   R   R   R   (    (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyR   1   s   t   LandscapeInstallProgressc           B   s/   e  Z d Z d Z d    Z d   Z d   Z RS(   c         C   s+   t  t |   j   } t j |  |  _ | S(   s  Override to find out whether dpkg exited or not.

        The C{run()} method returns os.WEXITSTATUS(res) without checking
        os.WIFEXITED(res) first, so it can signal that everything is ok,
        even though something causes dpkg not to exit cleanly.

        Save whether dpkg exited cleanly into the C{dpkg_exited}
        attribute. If dpkg exited cleanly the exit code can be used to
        determine whether there were any errors. If dpkg didn't exit
        cleanly it should mean that something went wrong.
        (   t   superR   t
   wait_childt   ost	   WIFEXITEDt   dpkg_exited(   R   t   res(    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyR!   C   s    c         C   s@   t  t |   j   } | d k r< t j |  _ |  j t _ n  | S(   s6   Fork and override the excepthook in the child process.i    (   R    R   t   forkt   syst
   excepthookt   old_excepthookt   _prevent_dpkg_apport_error(   R   t   pid(    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyR&   S   s
    c         C   s:   | t  k r# t j | | |  d S|  j | | |  d S(   s  Prevent dpkg errors from generating Apport crash reports.

        When dpkg reports an error, a SystemError is raised and cleaned
        up in C code. However, it seems like the Apport except hook is
        called before the C code clears the error, generating crash
        reports even though nothing crashed.

        This exception hook doesn't call the Apport hook for
        SystemErrors, but it calls it for all other errors.
        N(   t   SystemErrorR'   t   __excepthook__R)   (   R   t   exc_typet   exc_objt   exc_tb(    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyR*   ]   s    N(   R   R   t   NoneR$   R)   R!   R&   R*   (    (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyR   >   s
   		
t	   AptFacadec           B   s$  e  Z d  Z d Z d Z d Z d< d  Z d   Z d   Z	 d   Z
 d   Z d	   Z d
   Z d   Z d   Z e d  Z d   Z d   Z d< d  Z 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+   Z0 d,   Z1 d-   Z2 d.   Z3 d/   Z4 d0   Z5 d1   Z6 d2   Z7 d3   Z8 d4   Z9 d5   Z: d6   Z; d7   Z< d8   Z= d9   Z> d:   Z? d;   Z@ RS(=   st  Wrapper for tasks using Apt.

    This object wraps Apt features, in a way that makes using and testing
    these features slightly more comfortable.

    @param root: The root dir of the Apt configuration files.
    @ivar refetch_package_index: Whether to refetch the package indexes
        when reloading the channels, or reuse the existing local
        database.
    i   i   s   /var/lib/dpkg/statusc         C   s   | |  _  g  |  _ |  j  d  k	 rG |  j   |  j j d |  j  g  n  t j j d |  |  _ t	 |  _
 i  |  _ i  |  _ g  |  _ t   |  _ t	 |  _ g  |  _ g  |  _ g  |  _ t	 |  _ d  S(   Ns   --roott   rootdir(   t   _roott
   _dpkg_argsR1   t   _ensure_dir_structuret   extendR   t   cachet   Cachet   _cachet   Falset   _channels_loadedt	   _pkg2hasht	   _hash2pkgt   _version_installst   sett   _package_installst   _global_upgradet   _version_removalst   _version_hold_creationst   _version_hold_removalst   refetch_package_index(   R   t   root(    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyR   ~   s     		
								c         C   s   |  j  d  } |  j  d  |  j  d  |  j  d  |  j  d  } |  j  d  |  j  d  |  j  d  t t j j | d	  d
  t j j | d  |  _ t j j |  j  s t |  j d
  n  t t j j | d   d  S(   Ns   etc/apts   etc/apt/sources.list.ds   var/cache/apt/archives/partials   var/lib/apt/lists/partials   var/lib/dpkgs   var/lib/dpkg/infos   var/lib/dpkg/updatess   var/lib/dpkg/triggerst	   availablet    t   statuss   trusted.gpg(   t   _ensure_sub_dirR
   R"   t   pathR   t   _dpkg_statust   existsR   (   R   t   apt_dirt   dpkg_dir(    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyR6      s    c         C   s>   t  j j |  j |  } t  j j |  s: t  j |  n  | S(   s)   Ensure that a dir in the Apt root exists.(   R"   RL   R   R4   RN   t   makedirs(   R   t   sub_dirt	   full_path(    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyRK      s    c         C   s   |  j  j   S(   s/   Get all the packages available in the channels.(   R>   t
   itervalues(   R   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   get_packages   s    c         C   s>   g  |  j    D]- } |  j |  r |  j | j  r | ^ q S(   sq   Get all packages in the channels that are locked.

        For Apt, it means all packages that are held.
        (   RU   t   is_package_installedt   _is_package_heldR   (   R   t   version(    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   get_locked_packages   s    c         C   s#   g  |  j    D] } | j j ^ q S(   s5   Return the name of all the packages that are on hold.(   RY   R   t   name(   R   RX   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   get_package_holds   s    c         C   s6   t  j d d g |  j d t  j } | j |  d S(   sf   Set the dpkg selection.

        It basically does "echo $selection | dpkg --set-selections".
        t   dpkgs   --set-selectionst   stdinN(   t
   subprocesst   PopenR5   t   PIPEt   communicate(   R   t	   selectiont   process(    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   _set_dpkg_selections   s    c         C   s   |  j  | j j d  d S(   sd   Add a dpkg hold for a package.

        @param version: The version of the package to hold.
        s    holdN(   Rd   R   RZ   (   R   RX   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   set_package_hold   s    c         C   sB   |  j  |  s# |  j | j  r' d S|  j | j j d  d S(   sj   Removes a dpkg hold for a package.

        @param version: The version of the package to unhold.
        Ns    install(   RV   RW   R   Rd   RZ   (   R   RX   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   remove_package_hold   s    c      	   C   s{  |  j  j d  |  j   } |  j s= | r t j j |  r i  } | r` |  j r` | | d <n  y9 y |  j  j |   Wn t	 k
 r |  j  j   n XWn- t
 j j k
 r t d |  j     n X|  j  j d  n  |  j j   |  j j   xu |  j  D]j } |  j |  sq n  xL | j D]A } |  j | d t j   } | |  j | | f <| |  j | <q%Wq Wt |  _ d S(   s   Reload the channels and update the cache.

        @param force_reload_binaries: Whether to always reload
            information about the binaries packages that are in the facade's
            internal repo.
        t   sources_lists"   Apt failed to reload channels (%r)t	   with_infoN(   R:   t   openR1   t   _get_internal_sources_listRF   R"   RL   RN   t   updatet	   TypeErrorR   R8   t   FetchFailedExceptionR   t   get_channelsR=   t   clearR>   t   _is_main_architecturet   versionst   get_package_skeletonR;   t   get_hasht   TrueR<   (   R   t   force_reload_binariest   internal_sources_listt   new_apt_argsR   RX   t   hash(    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   reload_channels   s8    	c         C   s   |  j  r d S|  j   d S(   s6   Reload the channels if they haven't been reloaded yet.N(   R<   Ry   (   R   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   ensure_channels_reloaded  s    	c         C   s%   t  j j d  } t j j | d  S(   s@   Return the path to the source.list file for the facade channels.s   Dir::Etc::sourcepartss   _landscape-internal-facade.list(   t   apt_pkgt   configt   find_dirR"   RL   R   (   R   t   sources_dir(    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyRj     s    c         C   s   |  j    } d | | f } | r< | d d j |  7} n  t j j |  rv t |  j d  } | | k rv d Sn  | d 7} t | |  d S(   s   Add a deb URL which points to a repository.

        @param url: The base URL of the repository.
        @param codename: The dist in the repository.
        @param components: The components to be included.
        s	   deb %s %ss    %st    s   
N(   Rj   R   R"   RL   RN   R   t   splitR	   (   R   t   urlt   codenamet
   componentst   sources_file_patht   sources_linet   current_content(    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   add_channel_apt_deb  s    
c         C   s(   |  j  |  |  j d | d d  d S(   s   Add a directory with packages as a channel.

        @param path: The path to the directory containing the packages.

        A Packages file is created in the directory with information
        about the deb files.
        s	   file://%ss   ./N(   t   _create_packages_fileR   R1   (   R   RL   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   add_channel_deb_dir  s    c         C   s2   |  j    } t j j |  r. t j |  n  d S(   s   Clear the channels that have been added through the facade.

        Channels that weren't added through the facade (i.e.
        /etc/apt/sources.list and /etc/apt/sources.list.d) won't be
        removed.
        N(   Rj   R"   RL   RN   t   remove(   R   R   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   clear_channels)  s    c            sQ   d j     f d   t t j     D  } t t j j    d  |  d S(   s0   Create a Packages file in a directory with debs.s   
c         3   s-   |  ]# }   j  t j j  |   Vq d  S(   N(   t   get_package_stanzaR"   RL   R   (   t   .0t   filename(   R   t   deb_dir(    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pys	   <genexpr>7  s   t   PackagesN(   R   t   sortedR"   t   listdirR
   RL   (   R   R   t   packages_contents(    (   R   R   s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyR   4  s    c         C   sZ   t    } g  | D]F } | j s i | j d 6| j d 6d j | j  d 6| j d 6^ q S(   s   Return a list of channels configured.

        A channel is a deb line in sources.list or sources.list.d. It's
        represented by a dict with baseurl, distribution, components,
        and type keys.
        t   baseurlt   distributionR   R   t   type(   R   t   disabledt   urit   distR   t   compsR   (   R   Rg   t   entry(    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyRn   ;  s    	c         C   s5   t    } x | D] } | j t  q W| j   d S(   s#   Remove all the configured channels.N(   R   t   set_enabledR;   t   save(   R   Rg   R   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   reset_channelsG  s    	c      	   C   s   t  |  } t j |  } | j j d  } | j   t j j |  } t j j	 |  } t
 |  } t j |  j   } t j |  j   }	 t j |  j   }
 t j t j |  t j d | f d t |  f d | f d |	 f d |
 f g  S(   s   Return a stanza for the package to be included in a Packages file.

        @param deb_path: The path to the deb package.
        t   controlt   Filenamet   Sizet   MD5sumt   SHA1t   SHA256(   Ri   t   apt_instt   DebFileR   t   extractdatat   closeR"   RL   t   basenamet   getsizeR   t   hashlibt   md5t	   hexdigestt   sha1t   sha256R{   t   rewrite_sectiont
   TagSectiont   REWRITE_PACKAGE_ORDERR   (   R   t   deb_patht   deb_filet   debR   R   t   sizet   contentsR   R   R   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyR   N  s    
c         C   s   t  j j d  S(   s1   Return the architecture APT is configured to use.s   APT::Architecture(   R{   R|   t   get(   R   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   get_archc  s    c         C   sa   | d k r d } n  t j j d  t j j d |  t j j d |  } |  j j d  | S(   sk   Set the architecture that APT should use.

        Setting multiple architectures isn't supported.
        RI   s   APT::Architecturess   APT::Architectures::s   APT::ArchitectureN(   R1   R{   R|   Ro   R@   R:   Ri   (   R   t   architecturet   result(    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   set_archg  s    	c         C   s   t  | d | d 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.
        Rh   t   with_unicode(   R   Rt   (   R   t   pkgRh   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyRr   z  s    c         C   s   |  j  j | j | f  S(   si   Return a hash from the given package.

        @param version: an L{apt.package.Version} object.
        (   R=   R   R   (   R   RX   (    (    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   |  j  j |  S(   s   Get the package having the provided hash.

        @param hash: The hash the package should have.

        @return: The L{apt.package.Package} that has the given hash.
        (   R>   R   (   R   Rx   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   get_package_by_hash  s    c         C   s   | | j  j k S(   s!   Is the package version installed?(   R   t	   installed(   R   RX   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyRV     s    c         C   s   | j  S(   s*   Is the package available for installation?(   t   downloadable(   R   RX   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   is_package_available  s    c         C   s.   | j  j s | j  j r t S| | j  j k S(   s8   Is the package an upgrade for another installed package?(   R   t   is_upgradableR   R;   (   R   RX   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   is_package_upgrade  s    c         C   s#   t  | d  s t S| j | j k S(   s2   Is the package for the facade's main architecture?t	   shortname(   t   hasattrRt   RZ   R   (   R   R   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyRp     s    c         C   s   | j  j t j k S(   s   Is the package marked as held?(   t   _pkgt   selected_stateR{   t   SELSTATE_HOLD(   R   R   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyRW     s    c         C   s/   g  |  j    D] } | j j | k r | ^ q S(   s   Get all available packages matching the provided name.

        @param name: The name the returned packages should have.
        (   RU   R   RZ   (   R   RZ   RX   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   get_packages_by_name  s    c         C   s<   | j  r t S| j r8 | j r8 | j r8 | |  j k St S(   s]  Is the package broken?

        It's considered broken if it's one that we marked for install,
        but it's not marked for install, upgrade or downgrade
        anymore.

        Before Trusty, checking is_inst_broken was enough, but
        in Trusty the behaviour changed, so the package simply gets
        unmarked for installation.
        (   t   is_inst_brokenRt   t   marked_installt   marked_upgradet   marked_downgradeRA   R;   (   R   R   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   _is_package_broken  s    	


c            s    t    f d     j   D  S(   s/   Return the packages that are in a broken state.c         3   s*   |  ]  }   j  | j  r | j Vq d  S(   N(   R   R   (   R   RX   (   R   (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pys	   <genexpr>  s    (   R@   RU   (   R   (    (   R   s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   _get_broken_packages  s    c         C   sL   | j  r | j g S| j s% | j r5 | j | j g S| j rH | j g Sd S(   s]  Return the versions that will be changed for the package.

        Apt gives us that a package is going to be changed and have
        variables set on the package to indicate what will change. We
        need to convert that into a list of versions that will be either
        installed or removed, which is what the server expects to get.
        N(   R   t	   candidateR   R   R   t   marked_deleteR1   (   R   R   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   _get_changed_versions  s    	
	
c            s   g  | D] } | j  | f ^ q } t   } xX |  j j   D]G   |  j    sV q; n  |  j    } | j   f d   | D  q; W| j |  } | r t g  | D] \   } | ^ q   n  t	 |  d k S(   s  Check that the changes Apt will do have all been requested.

        @raises DependencyError: If some change hasn't been explicitly
            requested.
        @return: C{True} if all the changes that Apt will perform have
            been requested.
        c         3   s   |  ] }   | f Vq d  S(   N(    (   R   RX   (   R   (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pys	   <genexpr>  s    i    (
   R   R@   R:   t   get_changesRp   R   Rk   t
   differenceR   t   len(   R   t   requested_changesRX   t   all_changest   versions_to_be_changedRq   t   dependencies(    (   R   s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   _check_changes  s    "	%c         C   s   | j  j } | j r2 | d | j | j f 7} n  d } | j  j |  j k r |  j | j  j } | j sr | j r | j j } | |  j j	   k r | j j } n  d | } q n  | | 7} | S(   sA   Return a string representation of a specific dependency relation.s    (%s %s)s    but is not installables    but %s is to be installed(
   t
   target_pkgRZ   t
   target_vert	   comp_typeR:   R   R   R   RX   R   (   R   t   dep_relationt   infot   reasont   dep_packageRX   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   _get_unmet_relation_info  s    	
c         C   s   | d k } |  j  j } xc | D][ } xR | j   D]D } | j } | j t j k sb | j |  r2 | j |  r2 | Sq2 Wq W| S(   s+  Return whether a dependency is satisfied.

        For positive dependencies (Pre-Depends, Depends) it means that
        one of its targets is going to be installed. For negative
        dependencies (Conflicts, Breaks), it means that none of its
        targets are going to be installed.
        t   Breakst	   Conflicts(   R   R   (	   R:   t	   _depcachet   all_targetst
   parent_pkgt   current_stateR{   t   CURSTATE_INSTALLEDR   R   (   R   t
   dependencyt   dep_typet   is_positivet   depcachet   or_dept   targetR   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   _is_dependency_satisfied  s    	c         C   sM  |  j    } | s d Sd g } xt | d t d  D]} t } x d d d d g D] } | j j j j | g   } x | D] } |  j | |  r q n  g  } x$ | D] }	 | j	 |  j
 |	   q Wd	 | j | f }
 d
 d t |
  } | j	 |
 | j |   t } q WqW W| s8 | j	 d | j d f  q8 q8 Wd j |  S(   s  Get information about unmet dependencies in the cache state.

        Go through all the broken packages and say which dependencies
        haven't been satisfied.

        @return: A string with dependency information like what you get
            from apt-get.
        RI   s/   The following packages have unmet dependencies:t   keyRZ   t
   PreDependst   DependsR   R   s
     %s: %s: s    or
R   s     %s: %ss   Unknown dependency errors   
(   R   R   R   R;   R   t   _candt   depends_listR   R   t   appendR   RZ   R   R   Rt   (   R   t   broken_packagest   all_infoR   t   found_dependency_errorR   R   R   t   relation_infosR   R   t
   or_divider(    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   _get_unmet_dependency_info(  s0    
	c         C   s+   d t  j d <d t  j d <d t  j d <d S(   sW   
        Set the environment to avoid attempts by apt to interact with a user.
        t   noninteractivet   DEBIAN_FRONTENDt   nonet   APT_LISTCHANGES_FRONTENDt   APT_LISTBUGS_FRONTENDN(   R"   t   environ(   R   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   _set_frontend_noninteractiveK  s    c         C   s#   d t  j k r t t  j d <n  d S(   s   
        If no PATH is set in the environment, use the Ubuntu default PATH.

        When the client is launched from the landscape-client-settings-ui the
        PATH variable is incorrectly set, this method rectifies that.
        t   PATHN(   R"   R  R   (   R   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   _default_path_when_missingS  s    c         C   s;   |  j    |  j   t j j d  t j j d d  d S(   sV   
        Setup environment and apt options for successful package operations.
        s   DPkg::optionss   DPkg::options::s   --force-confoldN(   R  R	  R{   R|   Ro   R@   (   R   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   _setup_dpkg_for_changes^  s    

c         C   s   t  |  j  d k p' t  |  j  d k } | s4 d Sg  |  j D] } |  j |  s> | ^ q> } | r t d d d j g  t |  D] } | j j	 ^ q    n  x |  j D] } |  j
 |  q Wx |  j D] } |  j |  q Wd S(   s>   
        Perform pending hold operations on packages.
        i    s0   Cannot perform the changes, since the following s   packages are not installed: %ss   , s#   Package holds successfully changed.N(   R   RD   RE   R1   RV   R   R   R   R   RZ   Re   Rf   (   R   t   hold_changesRX   t   not_installed(    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   _perform_hold_changesg  s$    	-c   
      C   s  t    } t j   \ } } t j d  } t j d  } t j | d  t j | d  t   } z!d } x | |  j k rld	 } | d k r t	 j
 |  j  t j d |  j |  n  | d 7} y; |  j j d t |  d |  | j s t d   n  WnP t j j t f k
 rQ} | j   t |  }	 t | j d d |	  } qn X| j   t |  }	 Pqn W| d	 k	 r|  n  Wd	 t j | d  t j | d  t j |  X|	 S(
   sd   
        Commit cached APT operations and give feedback on the results as a
        string.
        i   i   i    sM   dpkg process might be in use. Retrying package changes. %d retries remaining.t   fetch_progresst   install_progresss   dpkg didn't exit cleanly.s   

Package operation log:
N(   R    t   tempfilet   mkstempR"   t   dupt   dup2R   t   max_dpkg_retriesR1   t   timet   sleept   dpkg_retry_sleept   loggingt   warningR:   t   commitR   R$   R,   R   R8   t   LockFailedExceptiont   getvalueR   R   t   argsR   (
   R   t   fetch_outputt   fdt   install_output_patht
   old_stdoutt
   old_stderrR  t
   dpkg_triest   errort   result_text(    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   _commit_package_changes  sL    		
			c         C   s   x |  j  D]v } | | j j k r( q
 n  | | j _ | j j d t  |  j j | j  | j | j j	  | j
 | j j	  q
 Wd  S(   Nt   auto_fix(   R?   R   R   R   t   mark_installR;   RA   t   addRo   R   t   protect(   R   t   fixerRX   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   _preprocess_installs  s    c            s  t    } t  d   |  j D    t    f d   |  j D  } x |  j D] } |  j | j  ry | j | j j  n  | j | k r qK n  | j j d t  | j	 | j j
  | j | j j
  | j | j j
  | j   qK W| rt d d d j t |     n  d  S(   Nc         s   s   |  ] } | j  Vq d  S(   N(   R   (   R   RX   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pys	   <genexpr>  s    c         3   s'   |  ] } | j    k r | j  Vq d  S(   N(   R   (   R   RX   (   t   package_installs(    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pys	   <genexpr>  s    R'  s7   Can't perform the changes, since the following packagess    are held: %ss   , (   R@   R?   RC   RW   R   R)  RZ   t   mark_deleteR;   Ro   R   R*  R   t   install_protectR   R   R   (   R   R+  t   held_package_namest   package_upgradesRX   (    (   R-  s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   _preprocess_removes  s&    	c         C   s#   |  j  r |  j j d t  n  d  S(   Nt   dist_upgrade(   RB   R:   t   upgradeRt   (   R   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   _preprocess_global_upgrade  s    	c         C   s   |  j    } | | k r y | j t  Wn4 t k
 r_ } t | j d d |  j     q X|  j    } | | k r t |  j     q n  d S(   sQ   
        Attempt to automatically resolve problems with broken packages.
        i    s   
N(   R   t   resolveRt   R,   R   R  R   (   R   R+  t   already_broken_packagest   now_broken_packagesR$  (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   _resolve_broken_packages  s    c         C   s   |  j  } | j |  j  | r/ |  j r/ g  S|  j   } t j |  j j  } |  j	 |  |  j
   |  j |  |  j | |  | S(   N(   R?   R7   RC   RB   R   R{   t   ProblemResolverR:   R   R,  R5  R2  R9  (   R   t   version_changesR7  R+  (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   _preprocess_package_changes  s    

c         C   s)   |  j    } |  j |  s d S|  j   S(   sD   
        Perform pending install/remove/upgrade operations.
        N(   R<  R   R1   R&  (   R   R;  (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   _perform_package_changes
  s    c         C   s   |  j    |  j   } |  j   } g  } | d k	 rD | j |  n  | d k	 r` | j |  n  t |  d k r d j |  Sd S(   s9   
        Perform the pending package operations.
        i    R   N(   R
  R  R=  R1   R   R   R   (   R   t   hold_result_textt   package_result_textt   results(    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   perform_changes  s    
c         C   sC   |  j  2|  j j   |  j 2|  j 2|  j 2t |  _ |  j j   d S(   s%   Clear the pending package operations.N(	   R?   RA   Ro   RC   RE   RD   R;   RB   R:   (   R   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   reset_marks"  s    	c         C   s   |  j  j |  d S(   s"   Mark the package for installation.N(   R?   R   (   R   RX   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyR(  ,  s    c         C   s   t  |  _ d S(   s   Upgrade all installed packages.N(   Rt   RB   (   R   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   mark_global_upgrade0  s    c         C   s   |  j  j |  d S(   s   Mark the package for removal.N(   RC   R   (   R   RX   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   mark_remove4  s    c         C   s   |  j  j |  d S(   s   Mark the package to be held.N(   RD   R   (   R   RX   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt	   mark_hold8  s    c         C   s   |  j  j |  d S(   s*   Mark the package to have its hold removed.N(   RE   R   (   R   RX   (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   mark_remove_hold<  s    N(A   R   R   R   R  R  RM   R1   R   R6   RK   RU   RY   R[   Rd   Re   Rf   R;   Ry   Rz   Rj   R   R   R   R   Rn   R   R   R   R   Rt   Rr   R   R   R   RV   R   R   Rp   RW   R   R   R   R   R   R   R   R   R  R	  R
  R  R&  R,  R2  R5  R9  R<  R=  RA  RB  R(  RC  RD  RE  RF  (    (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyR2   n   sx   
				
		
		
*																		
										#						9									
				((   R  R  R   R"   R^   R'   R  t	   cStringIOR    t   operatorR   t   warningst   filterwarningst   FutureWarningR   R   R{   t   aptsources.sourceslistR   t   apt.progress.textR   t   apt.progress.baseR   t   landscape.lib.fsR	   R
   R   R   t   landscape.constantsR   t   landscape.package.skeletonR   t	   ExceptionR   R   R   R   R   t   objectR2   (    (    (    s<   /usr/lib/python2.7/dist-packages/landscape/package/facade.pyt   <module>   s4   "0