
6Nc           @   s)  d  d l  m  Z d  d l m Z m Z m Z d  d l Z e j   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 Z d  d l Z d  d l Z d  d l Z d  d l m Z d  d l m Z d e f d     YZ d   Z d	   Z d
   Z d d  Z d   Z d d d  Z d   Z d   Z d   Z  d   Z! d   Z" d e j# f d     YZ$ d d  Z% d 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 e4 d& k r%e3   GHn  d S('   i(   t   gettext(   t   S_IMODEt   ST_MODEt   S_IXUSRN(   t   copy(   t   urlsplitt   ExecutionTimec           B   s,   e  Z d  Z d d  Z d   Z d   Z RS(   s   
    Helper that can be used in with statements to have a simple
    measure of the timming of a particular block of code, e.g.
    with ExecutinTime("db flush"):
        db.flush()
    t    c         C   s   | |  _  d  S(   N(   t   info(   t   selfR   (    (    s5   /usr/lib/python2.7/dist-packages/DistUpgrade/utils.pyt   __init__3   s    c         C   s   t  j    |  _ d  S(   N(   t   timet   now(   R	   (    (    s5   /usr/lib/python2.7/dist-packages/DistUpgrade/utils.pyt	   __enter__5   s    c         C   s#   d |  j  t j   |  j f GHd  S(   Ns   %s: %s(   R   R   R   (   R	   t   typet   valuet   stack(    (    s5   /usr/lib/python2.7/dist-packages/DistUpgrade/utils.pyt   __exit__7   s    (   t   __name__t
   __module__t   __doc__R
   R   R   (    (    (    s5   /usr/lib/python2.7/dist-packages/DistUpgrade/utils.pyR   ,   s   	c         C   ss   t  |   } t | j  } | j rB | j j | j d  | _ n  | j ri | j j | j d  | _ n  t |  S(   Ns   hidden-us   hidden-p(   R   R   t   urit   usernamet   replacet   passwordt   str(   t   entryt   tmpt	   url_parts(    (    s5   /usr/lib/python2.7/dist-packages/DistUpgrade/utils.pyt)   get_string_with_no_auth_from_source_entry:   s    		c          C   sN   d }  t  j   d } x1 t j d |  D] } |  t  j j |  7}  q* W|  S(   sE    estimate the amount of space that the current kernel takes in /boot i    i   s
   /boot/*%s*(   t   ost   unamet   globt   patht   getsize(   t   sizet   kvert   f(    (    s5   /usr/lib/python2.7/dist-packages/DistUpgrade/utils.pyt   estimate_kernel_size_in_bootC   s
    c          C   sL   t  }  y+ d d l } | j   } | j d  }  Wn t j d  n X|  S(   s+    return True if Unity is currently running iNs   com.canonical.Unitys&   could not check for Unity dbus service(   t   Falset   dbust
   SessionBust   name_has_ownert   loggingt	   exception(   t   unity_runningR(   t   bus(    (    s5   /usr/lib/python2.7/dist-packages/DistUpgrade/utils.pyt   is_unity_runningK   s    c         C   s   | s t  j   } n  x | d k r d | } t |  j   } | j d  d j d  d } | |  k rp t St | j d  d j   d  } q Wt S(   Ni    s   /proc/%s/statt   (i   t   )i   (	   R   t   getpidt   opent   readt	   partitiont   Truet   intt   splitR'   (   t   processnamet   pidt	   stat_filet   statt   command(    (    s5   /usr/lib/python2.7/dist-packages/DistUpgrade/utils.pyt   is_child_of_process_nameV   s    
 'c           C   sF   t  j j d  s& t  j j d  r* t St  j d  t  j d  k S(   s-    returns True if we are inside a chroot 
    s   /procs   /proc/1t   /s   /proc/1/root(   R   R!   t   existsR6   R<   (    (    (    s5   /usr/lib/python2.7/dist-packages/DistUpgrade/utils.pyt   inside_chrootd   s    &iF   R   c         C   si   d } x\ |  j    D]N } t |  | j d  t |  | k rS | d | 7} n  | | d 7} q W| S(   s    helpers inspired after textwrap - unfortunately
        we can not use textwrap directly because it break
        packagenames with "-" in them into new lines
    R   s   
t    (   R8   t   lent   rfind(   t   tt   widtht   subsequent_indentt   outt   s(    (    s5   /usr/lib/python2.7/dist-packages/DistUpgrade/utils.pyt   wrapm   s    )c         K   sG   d } |  j  d  } x+ | D]# } t | |  }  | |  d 7} q W| S(   NR   s   
(   R8   RJ   (   RI   t   kwargst   msgt   parast   par(    (    s5   /usr/lib/python2.7/dist-packages/DistUpgrade/utils.pyt   twrapy   s    c          C   s   g  }  t  j j d  s g  St j d g d t j } | j   d j d  } | d =x. | D]& } | r^ |  j | j   d  q^ q^ W|  S(   s=    return list of loaded modules (or [] if lsmod is not found) s   /sbin/lsmodt   stdouti    s   
(	   R   R!   R@   t
   subprocesst   Popent   PIPEt   communicateR8   t   append(   t   modulest   pt   linest   line(    (    s5   /usr/lib/python2.7/dist-packages/DistUpgrade/utils.pyt   lsmod   s    c         C   sZ   t  j j |   s d St t  j |   t  } | t @t k sV t  j |  | t B n  d S(   sB    check if a given binary has the executable bit and if not, add itN(   R   R!   R@   R   R<   R   R   t   chmod(   R!   t   mode(    (    s5   /usr/lib/python2.7/dist-packages/DistUpgrade/utils.pyt   check_and_fix_xbit   s
    c          C   s   i d d 6}  d t  j k r  d St  j d j   } | d  |  k rO |  | d  Sd | k r | j d  d j d  d } d	 | k r | j d	  d } n  | d S| d
  d Sd S(   s:    helper to get the country mirror from the current locale R   t   ct   LANGi   t   _t   .i    i   t   @i   (   R   t   environt   lowerR8   (   t   lang_mirrort   langt   country(    (    s5   /usr/lib/python2.7/dist-packages/DistUpgrade/utils.pyt   country_mirror   s     c          C   s   t  j j d  }  |  r- t j d |   |  Sd d l m } m } | d d d g d | } | j   } | d	 k r t	 j
 j d
 |  d S| j j   j   }  |  S(   s2    return the codename of the current runing distro t   META_RELEASE_FAKE_CODENAMEsQ   using fake release name '%s' (because of META_RELEASE_FAKE_CODENAME environment) i(   RR   RS   t   lsb_releases   -cs   -sRP   i    s"   lsb_release returned exitcode: %i
s   unknown distribution(   R   Rc   t   getR+   t   warnRQ   RR   RS   t   waitt   syst   stderrt   writeRP   t   readlinet   strip(   t   distRR   RS   RW   t   res(    (    s5   /usr/lib/python2.7/dist-packages/DistUpgrade/utils.pyt   get_dist   s    t   HeadRequestc           B   s   e  Z d    Z RS(   c         C   s   d S(   Nt   HEAD(    (   R	   (    (    s5   /usr/lib/python2.7/dist-packages/DistUpgrade/utils.pyt
   get_method   s    (   R   R   Rx   (    (    (    s5   /usr/lib/python2.7/dist-packages/DistUpgrade/utils.pyRv      s   c         C   s  | s d   n  | d |   t  j |   \ } } } } } | d | | | | | f  | d k r y: t j t |    } | j   | j d k r t St SWqt	 k
 r } | d |  t SXn | d k rd d	 l
 }	 y |	 j |  }
 |
 j   |
 j t j j |   |
 j t j j |   } |
 j   | rP| d
 |  n  | d k r`t SWqt	 k
 r} | r| d |  n  t SXn  t S(   s   
  helper that checks if the given uri exists and is downloadable
  (supports optional debug_func function handler to support 
   e.g. logging)

  Supports http (via HEAD) and ftp (via size request)
  c         S   s   t  S(   N(   R6   (   t   x(    (    s5   /usr/lib/python2.7/dist-packages/DistUpgrade/utils.pyt   <lambda>   s    s   url_downloadable: %ss"   s='%s' n='%s' p='%s' q='%s' f='%s't   httpi   s   error from httplib: '%s't   ftpiNs   ftplib.size() returned: %si    s   error from ftplib: '%s'(   t   urlparseR   t   urllib2t   urlopenRv   t   closet   codeR6   R'   t	   Exceptiont   ftplibt   FTPt   logint   cwdR   R!   t   dirnameR#   t   basenamet   quit(   R   t
   debug_funct   schemet   netlocR!   t   querryt   fragmentt	   http_filet   eR   R%   R#   (    (    s5   /usr/lib/python2.7/dist-packages/DistUpgrade/utils.pyt   url_downloadable   s@    



c   	      C   s>  d } d
 } t j j d  d k r9 t j j d  } n t j j |  r t j   } t j | |  | j	 d t
  } | r | j d  } t | j d   } | r | r d | | f } q q n  | r:t j d |  s t j d	 | IJd
 St j i | d 6 } t j |  } t j |  t j d |  n  | S(   s    init proxy settings 

  * first check for http_proxy environment (always wins),
  * then check the apt.conf http proxy, 
  * then look into synaptics conffile
  * then into gconf  (if gconfclient was supplied)
  s   /root/.synaptic/synaptic.confs   Acquire::http::ProxyR   s   Synaptic::useProxys   Synaptic::httpProxys   Synaptic::httpProxyPorts   http://%s:%s/s
   http://\w+s   proxy '%s' looks invalidNR{   t
   http_proxy(   t   Nonet   apt_pkgt   configt   findR   R!   R@   t   Configurationt   read_config_filet   find_bR'   R   t   find_it   ret   matchRn   Ro   R~   t   ProxyHandlert   build_openert   install_openert   putenv(	   t	   gsettingst   SYNAPTIC_CONF_FILEt   proxyt   cnft	   use_proxyt
   proxy_hostt
   proxy_portt   proxy_supportt   opener(    (    s5   /usr/lib/python2.7/dist-packages/DistUpgrade/utils.pyt
   init_proxy   s,    c          C   s   y d d l  }  |  j |  j j  } y8 | j d d  } |  j | d  } | j d d  SWn_ |  j j k
 r } | j d k r   n  | j d d	  } |  j | d  } | j d d
  SXWn t	 k
 r } t
 SXd S(   s   
  Check via dbus if the system is running on battery.
  This function is using UPower per default, if UPower is not
  available it falls-back to DeviceKit.Power. 
  iNs   org.freedesktop.UPowers   /org/freedesktop/UPowers   org.freedesktop.DBus.Propertiest	   OnBatterys)   org.freedesktop.DBus.Error.ServiceUnknowns   org.freedesktop.DeviceKit.Powers    /org/freedesktop/DeviceKit/Powert
   on_battery(   R(   t   Bust   TYPE_SYSTEMt
   get_objectt	   Interfacet   Gett
   exceptionst   DBusExceptiont   _dbus_error_nameR   R'   (   R(   R.   t   devobjt   devR   (    (    s5   /usr/lib/python2.7/dist-packages/DistUpgrade/utils.pyR      s"    				c          C   sa   d d l  }  |  j |  j j  } | j d d  } |  j | d  } | j d d  } | | f S(   s   
  Send a dbus signal to org.gnome.SettingsDaemon.Power to not suspend
  the system, this is to support upgrades from pre-gutsy g-p-m
  iNs   org.gnome.SettingsDaemons   /org/gnome/SettingsDaemon/Powers   org.gnome.SettingsDaemon.Powert   UpdateManagers   Updating system(   R(   R   t   TYPE_SESSIONR   R   t   Inhibit(   R(   R.   R   R   t   cookie(    (    s5   /usr/lib/python2.7/dist-packages/DistUpgrade/utils.pyt   _inhibit_sleep_old_interface:  s    		c          C   sa   d d l  }  |  j |  j j  } | j d d  } |  j | d  } | j d d  } | | f S(   sK   
  Send a dbus signal to gnome-power-manager to not suspend
  the system
  iNs   org.freedesktop.PowerManagements(   /org/freedesktop/PowerManagement/Inhibits'   org.freedesktop.PowerManagement.InhibitR   s   Updating system(   R(   R   R   R   R   R   (   R(   R.   R   R   R   (    (    s5   /usr/lib/python2.7/dist-packages/DistUpgrade/utils.pyt   _inhibit_sleep_new_interfaceG  s    		c           C   sI   y t    SWn7 t k
 rD y t   SWqE t k
 r@ t t f SXn Xd S(   s   
  Send a dbus signal to power-manager to not suspend
  the system, try both the new freedesktop and the
  old gnome dbus interface
  N(   R   R   R   R'   (    (    (    s5   /usr/lib/python2.7/dist-packages/DistUpgrade/utils.pyt   inhibit_sleepT  s    c         C   s4   y |  j  |  Wn t k
 r/ } d | GHn Xd S(   sL   Send a dbus signal to gnome-power-manager to allow a suspending
  the systems,   could not send the dbus UnInhibit signal: %sN(   t	   UnInhibitR   (   R   R   R   (    (    s5   /usr/lib/python2.7/dist-packages/DistUpgrade/utils.pyt   allow_sleepc  s    c         C   s&   |  d k s |  j    d k r" t St S(   Nt   0t   FALSE(   t   upperR'   R6   (   R   (    (    s5   /usr/lib/python2.7/dist-packages/DistUpgrade/utils.pyt   str_to_booll  s    c         C   s   t  |  d  j d  S(   Nt   latin1s   utf-8(   t   unicodet   encode(   R   (    (    s5   /usr/lib/python2.7/dist-packages/DistUpgrade/utils.pyt   utf8q  s    c          C   sL   d d  l  }  y t j   \ } } | SWn t k
 rG |  j d  d  SXd  S(   Nis   gedefaultlocale() failed(   R+   t   localet   getdefaultlocaleR   R,   R   (   R+   t   locale_st   encoding(    (    s5   /usr/lib/python2.7/dist-packages/DistUpgrade/utils.pyt   get_langt  s    c          C   sU   t  j j d d  }  d |  k r% d Sd |  k r5 d Sd |  k sM d |  k rQ d Sd S(	   s6    try to guess the flavor based on the running desktop t   DESKTOP_SESSIONR   t   gnomet   ubuntut   kdet   kubuntut   xfcet   xubuntu(   R   Rc   Rk   (   t   denv(    (    s5   /usr/lib/python2.7/dist-packages/DistUpgrade/utils.pyt   get_ubuntu_flavor}  s    c      	   C   s   d d l  m } m } | j d |  d | j j d | j j d | j j	  } | j
 d | | f  | j   | j j | j  | j d  | j   | j   t S(	   Ni(   t   Gtkt   Gdkt   parentt   flagsR   t   buttonss   <big><b>%s</b></big>

%sR   (   t   gi.repositoryR   R   t   MessageDialogt   DialogFlagst   MODALt   MessageTypet   ERRORt   ButtonsTypet   CLOSEt
   set_markupt   realizet   windowt   set_functionst	   FUNC_MOVEt	   set_titlet   runt   destroyR'   (   R   t   summaryt   messageR   R   t   d(    (    s5   /usr/lib/python2.7/dist-packages/DistUpgrade/utils.pyt   error  s    


c         C   st   |  d k r t  d  S|  d k  r, t  d  S|  d	 k  rR t j t  d  |  d  St j t  d  |  d d  Sd S(
   sG   
    Convert a given size in bytes to a nicer better readable unit
    i    s   0 kBi  s   1 kBs   %.0f kBs   %.1f MBg     @@Ni@B (   R`   R   t   format_string(   t   bytes(    (    s5   /usr/lib/python2.7/dist-packages/DistUpgrade/utils.pyt   humanize_size  s    

c           C   s   t  j j d  S(   Ns   APT::Architecture(   R   t   ConfigR   (    (    (    s5   /usr/lib/python2.7/dist-packages/DistUpgrade/utils.pyt   get_arch  s    c          C   sv   d }  t  j   d k r$ t d  n  t  j j d  s: t St j d d g d t j j	   d } | |  k rr t St
 S(   s#    Return True if iptables is active s  Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
i    s%   Need root to check the iptables states   /sbin/iptablest   iptabless   -LRP   (   R   t   getuidt   OSErrorR!   R@   R'   RQ   RR   RS   RT   R6   (   t   iptables_emptyRH   (    (    s5   /usr/lib/python2.7/dist-packages/DistUpgrade/utils.pyt   iptables_active  s    t   __main__(5   R    R`   R<   R   R   R   R   t   init_configR   R+   R   R   t   os.pathR    RQ   Rn   R   R~   R}   R   R   t   objectR   R   R&   R/   R   R>   RA   RJ   RO   RZ   R]   Rh   Ru   t   RequestRv   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   (    (    (    s5   /usr/lib/python2.7/dist-packages/DistUpgrade/utils.pyt   <module>   s\   
											)3															