
-v&ac           @   s  d  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 e j d k  r d d l m Z d d l m Z e e f n  d d l m Z d d l m 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! e" d k rd d l Z e# e j$  d k re j% j& d e j$ d  e j' d  n  e j$ d Z( e j)   Z* e+ e( d   Z, e* j- e,  Wd QXe. e* j/    Z0 e* j1   Z2 e e2 d  e. e2 j/    Z4 d Z5 x e6 e0 e4 B D] Z7 e7 e4 k re7 e0 k rd e7 e2 e7 f GHe5 d 7Z5 qMe7 e0 k re7 e4 k rd e7 GHe5 d 7Z5 qMe7 e0 k rMe7 e4 k rMe* e7 e2 e7 k rd e7 GHe5 d 7Z5 qqMqMWd e5 GHn  d S(   s  Attach generally useful information, not specific to any package.

Copyright (C) 2009 Canonical Ltd.
Authors: Matt Zimmerman <mdz@canonical.com>,
         Brian Murray <brian@ubuntu.com>

This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2 of the License, or (at your
option) any later version.  See http://www.gnu.org/copyleft/gpl.html for
the full text of the license.
iN(   t   unicode_gettext(   t   globt   3(   t   urljoin(   t   urlopenc         C   s  t  |   t |   t |   t |   t j j |  d d  g  } xI t |  j d d  j	 d   D]& } | j
 |  | j d  rf Pqf qf Wt |  } d j |  |  d <|  j d  g  |  j   D] } | j d  r | ^ q } | r t |  d  n  |  j d	  d
 k sEd |  j d d  k sEd |  j d d  k rsx+ d@ D]  } t d | j   | |   qLWn  y t j j d  |  d <Wn t k
 rn X|  j d	  d
 k r+t j j |  d d g  t |   d |  k r+t j d  } | j | j d } | d k  r(d | |  d <q(q+n  t |   x d d g D] }	 |	 |  k rBt |  |	  }
 |
 } t |  |	  t |  |	  } | j	 d  } g  } xR | j   D]D } | | k r| j
 t |   q| | k r| j  |  qqWd j |  } qBqBWxB dA D]: } | |  k r|  | j d  r|  | d |  | <qqW|  j d
 d  j d  rud t j! d  <n  d
 |  k r|  d
 j	   d! } t" |   } | rd" | k s| | k rd# |  d$ <qn  d
 |  k r=|  d
 j	   d% } | rt j j# |  | d& | n  | d' k r=d( |  d
 k r=d) |  d <q=n  d
 |  k r|  d
 j	   d% } | rt j j$ |  |  t j j% |  |  qn  |  j d	  d
 k rd |  k r|  d } n$ d |  k rt |  d  } n d  } | r|  d
 j	 d  d!  \ } } d* d+ d, d- d. d/ g } d0 } d1 } t' j( | |  sKt' j( | |  rt|  d2 | k rtt j j) |  d3  qtn  d } t* } t' j+ d4 t' j,  } x | j	 d  D]} | j( |  rd5 | } t- } qn  | d5 | 7} d6 | k r| j d7  rd8 | k r\t' j( d9 |  } | rLt j j. | j/ d!   rLt0 d:  |  d <n  t |  d;  n  | rd< | | | f |  d= <Pqn  | r| j1 |  d> k rt0 d?  |  d <qqqWqn  d  S(B   Ns   /proc/cpuinfot   ProcCpuinfot    s   
s   processor	:t   ProcCpuinfoMinimalt
   HookError_s   apport-hook-errort   ProblemTypet   Packaget   pythont   InterpreterPatht   ExecutablePatht   python3s	   %sDetailst   apportt   ApportVersiont   dpkgt   aptt   LiveMediaBuilds   /cdromi@B i
   s   Your system partition has less than %s MB of free space available, which leads to problems using applications and installing updates. Please free some space.t   UnreportableReasont   DpkgTerminalLogt   VarLogDistupgradeApttermlogs   /target/i   s   update-managert   1t   APPORT_IGNORE_OBSOLETE_PACKAGESi   t   commons   canonical-oemt   CrashDBi    t   uis   upgrade-systems   not installeds   You do not have the upgrade-system package installed. Please report package upgrade failures against the package that failed to install, or against upgrade-manager.s   friendly-recoveryt   linuxs
   memtest86+t   plymouths   ubuntu-metas   virtualbox-osesJ   /etc/kernel/post(inst|rm)\.d/zz-update-grub exited with return code [1-9]+s8   /usr/sbin/grub-mkconfig.*/etc/default/grub: Syntax errort   SourcePackaget   EtcDefaultGrubs  ^(Authenticating|
                                             De-configuring|
                                             Examining|
                                             Installing|
                                             Preparing|
                                             Processing\ triggers|
                                             Purging|
                                             Removing|
                                             Replaced|
                                             Replacing|
                                             Setting\ up|
                                             Unpacking|
                                             Would remove).*
                                         \.\.\.\s*$s   %s
s   dpkg: errort    s   trying to overwrites   in package (.*) sZ   An Ubuntu package has a file conflict with a package that is not a genuine Ubuntu package.s   package-conflicts   package:%s:%s
%st   DuplicateSignatureis?   You have already encountered this package installation failure.(   R   R   (   R   R   (2   t   add_release_infot   add_kernel_infot   add_cloud_infot   add_proposed_infoR   t	   hookutilst   attach_filet   reversedt   gett   splitt   appendt
   startswitht   joint   popt   keyst   add_tagt   add_python_detailst   titlet	   packagingt   get_versiont
   ValueErrort   attach_related_packagest   check_for_disk_errort   ost   statvfst   f_bavailt   f_frsizet   match_error_messagest   get_attachment_contentst   check_attachment_for_errorst
   splitlinest   strt   removet   environt   get_oem_projectt   attach_conffilest   attach_upstart_overridest   attach_upstart_logst   Nonet   ret   searcht   attach_default_grubt   Falset   compilet   Xt   Truet   is_distro_packaget   groupt   _t   find(   t   reportR   t   short_cpuinfot   itemt   kt   hook_errorsR   t   stt   free_mbt
   attachmentt   log_filet   untrimmed_dpkg_logt   trimmed_logt   linest   linet   dpkg_log_without_errort   ft   vt   oem_projectt   packaget   termlogt   versiont   UPDATE_BOOTt
   ug_failuret   mkconfig_failuret   dupe_sigt   dupe_sig_createdt   pkg_mngr_msgst   conflict_pkg(    (    s)   /usr/share/apport/general-hooks/ubuntu.pyt   add_info   s    



(.!

	$
"c         C   sf   |  j  d  d k rb d |  j  d d  k r: d |  d <n  d |  j  d	 d  k rb d
 |  d <qb n  d  S(   NR	   R
   s7   failed to install/upgrade: corrupted filesystem tarfilet   TitleR   sR   This failure was caused by a corrupted package download or file system corruption.R   s#   is already installed and configuredt   ErrorMessageR   R   (   R)   (   RS   (    (    s)   /usr/share/apport/general-hooks/ubuntu.pyR<      s
    c         C   s   |  j  d  d k rt d  } t |   t |  |  } d |  k rk t j d |  d t j  rk t } n t } |  d d4 k r=d d	 d
 g } x | D] } | |  k r t j | | t j  r t	 j
 j d  } | d  k s | d k r)d | k r)d |  d <t j j d  r3| r3| |  d <q3q6d |  d <q q Wn  |  d d k r| |  k rt j d | t j  rd |  d <qn  |  d j d  r4| |  k r4t j d | t j  } | r4| j d  }	 t	 j
 j |	  }
 |
 r$|
 |  d <| j d  |  d <|
 d k r.| r.| |  d <q.q1d |  d <q4n  |  j  d  } d } d } d |  j  d d  k rt| |  d <n  d | k rd |  d <n  d  | k r| |  d <n  d! | k r| |  d <n  t j d" |  r| |  d <n  t j d# |  r| |  d <n  t j d$ |  r| |  d <n  | |  k rMt j d% | t j  rM| |  d <n  d& |  j  d d  k rd' |  d <t	 j
 j d(  |  d) <t	 j j |  d* d+  t	 j j |  d, d-  t d.  } | rt	 j j d/ d0 d1 g |  |  d2 <n  t |  d3  qn  d  S(5   NR	   R
   s#  Your system was initially configured with grub version 2, but you have removed it from your system in favor of grub 1 without configuring it.  To ensure your bootloader configuration is updated whenever a new kernel is available, open a terminal and run:

      sudo apt-get install grub-pc
R   s-   ^Not creating /boot/grub/menu.lst as you wisht   grubt   grub2s>   ^User postinst hook script \[.*update-grub\] exited with valuesU   ^run-parts: /etc/kernel/post(inst|rm).d/zz-update-grub exited with return code [1-9]+s   ^/usr/sbin/grub-probe: errors   /usr/sbin/update-grubs
   grub-probeR   s   /boot/grub/grub.cfgR   s   initramfs-toolss   ^update-initramfs: failed for s   linux-image-s@   ^run-parts: (/etc/kernel/\S+\.d/\S+) exited with return code \d+i   i    Rp   s   grub-pcsH   This failure was caused by a program which did not originate from UbuntusR   This failure was caused by a corrupted package download or file system corruption.s<   This failure was caused by the system running out of memory.s7   failed to install/upgrade: corrupted filesystem tarfileRo   R   s*   dependency problems - leaving unconfiguredsI   This failure is a followup error from a previous package install failure.s   failed to allocate memorys   cannot access archives:   (failed to read|failed in write|short read) on buffer copysP   (failed to read|failed to write|failed to seek|unexpected end of file or stream)s@   (--fsys-tarfile|dpkg-deb --control) returned error exit status 2s/   dpkg-deb: error.*is not a debian format archives#   is already installed and configuredR   t	   aptdaemont   AptdaemonVersions   /var/log/dpkg.logt   DpkgLogs   /var/log/apt/term.logt
   AptTermLogs   /var/crash/*t   stats   -cs   %a:%u:%g:%s:%y:%x:%nt   CrashReportss   already-installed(   Rq   Rr   (   R)   RQ   t   trim_dpkg_logR=   RH   RI   t	   MULTILINERN   RK   R   R3   t   get_file_packageRG   R8   t   patht   existsR,   RP   R4   R&   t   attach_file_if_existsR   t   command_outputR0   (   RS   RZ   t   wrong_grub_msgR[   t   grub_hook_failuret   grub_errorst
   grub_errort   grub_packaget   mR|   Rd   t   error_messaget   corrupt_packaget   out_of_memoryt   reports(    (    s)   /usr/share/apport/general-hooks/ubuntu.pyR>      s    	
		$$
$
$
	c   	      C   sr  g  } d |  k r" d |  k r" d  Sd |  k r2 d  S|  d } t  } x | D] } | j d  } | j d  s | j d  s | j d  rI | j d  d	 j d
  } | j d d  } | j |  qI qI W|  j d |  d  } xq | D]i } | j d  } d | k r d | k rq n  x3 | D]( } t j | |  r$| } t	 } Pq$q$Wq q W| rnd | |  d <n  d  S(   Ns	   Dmesg.txts   CurrentDmesg.txts   Df.txts   
t   /s   /usrs   /varR    i    t
   0123456789s   /dev/R   s	   I/O errors   journal commit I/O errors6   This failure was caused by a hardware error on /dev/%sR   (
   RK   t   stript   endswithR*   t   replaceR+   R)   RH   RI   RN   (	   RS   t   devs_to_checkt   dft   device_errorR_   t   devicet   dmesgt   devt   error_device(    (    s)   /usr/share/apport/general-hooks/ubuntu.pyR7   +  s4    
-c         C   s   t  j j |  d d  d  S(   Ns   /proc/version_signaturet   ProcVersionSignature(   R   R&   R~   (   RS   (    (    s)   /usr/share/apport/general-hooks/ubuntu.pyR#   J  s    c         C   s  d } t  j j |  | d  t  j j |  d d  t j j d  r] t  j j d  |  d <n  t  j j |  d d  t  j j d	 d
 g d d  } | j
 d  r d  } n t |  |  t j j |  r t j |  j } t j d t j |   } t j   | } d | | d f |  d <n  d } t j j |  rt j |  j } t j d t j |   } t j   | } d | | | d f |  d <n
 d |  d <t  j j d  rd t  j j d d g d d  |  d <n  d  S(   Ns   /var/log/installer/media-infot   InstallationMedias   /cdrom/.disk/infoR   t   caspert   CasperVersions   /var/lib/ubuntu_dist_channelt   DistributionChannelDescriptort   lsb_releases   -sct   stderrt   Errors   %Y-%m-%ds   Installed on %s (%d days ago)iQ t   InstallationDates   /var/log/dist-upgrade/main.logs"   Upgraded to %s on %s (%d days ago)t   UpgradeStatuss/   No upgrade log present (probably fresh install)s   system-image-clis   %ss   -it   SystemImageInfo(   R   R&   R~   R8   R|   R}   R3   R4   R   RG   R,   R0   Rw   t   st_mtimet   timet   strftimet   gmtimet   command_available(   RS   t   mediat   release_codenamet   mtimet   human_mtimet   deltat   log(    (    s)   /usr/share/apport/general-hooks/ubuntu.pyR"   O  s6    			
c   	      C   sI  d |  k r d Sy |  d j    d  \ } } Wn t k
 rO d |  d GHd SXt j d d | g d t j d t } | j   d	 } | j d	 k r d
 | GHd St } t } t } xi | j	   D][ } | j
 | d  r d | k r t } n  d | k rt } n  d | k rt } qq q W| rE| rE| rEt |  d  n  d S(   s#   Tag if package comes from -proposedR
   Ni   s"   WARNING: malformed Package field: s	   apt-cachet   showpkgt   stdoutt   universal_newlinesi    s$   WARNING: apt-cache showpkg %s faileds    (s
   -proposed_s	   -updates_s	   -securitys   package-from-proposed(   R*   R5   t
   subprocesst   Popent   PIPERN   t   communicatet
   returncodeRK   R?   R,   R0   (	   RS   Rd   Rf   t	   apt_cachet   outt   found_proposedt   found_updatest   found_securityR_   (    (    s)   /usr/share/apport/general-hooks/ubuntu.pyR%   {  s6    					c   	      C   sl  t  } xB d D]: } y  t j j |  r2 t } Pn  Wq t k
 rF q Xq W| rhd } t | d  } y t | d d j   } Wn t	 k
 r d  } n X| rX| j d  rXt |  d  i d	 d
 6d d 6d d 6d d 6d d 6} | |  d <xq | j   D]S \ } } y) t t | |  d d j   |  | <Wq t	 k
 rPd |  | <q Xq Wqht |  d  n  d  S(   Ns   ec2-inits
   cloud-inits(   http://169.254.169.254/latest/meta-data/s   ami-idt   timeouti   t   amis
   ec2-imagess   ami-manifest-patht   Ec2AMIManifests	   kernel-idt	   Ec2Kernels
   ramdisk-idt
   Ec2Ramdisks   instance-typet   Ec2InstanceTypes   placement/availability-zonet   Ec2AvailabilityZonet   Ec2AMIt   unavailables
   uec-images(   s   ec2-inits
   cloud-init(   RK   R   R3   R4   RN   R5   R   R   t   readt	   ExceptionRG   R,   R0   t   items(	   RS   t   ec2_instancet   pkgt   metadata_urlt
   ami_id_urlR   t   fieldst   keyt   value(    (    s)   /usr/share/apport/general-hooks/ubuntu.pyR$     s:    



)c         C   sB   |  j  d d  | |  d j   k r* d  S|  d c d | 7<d  S(   Nt   TagsR   R    (   t
   setdefaultR*   (   RS   t   tag(    (    s)   /usr/share/apport/general-hooks/ubuntu.pyR0     s    c         C   s<   |  j  d d  } | r8 | j d  r8 | j d  d Sd S(   s   Determine OEM project name from Distribution Channel Descriptor

    Return None if it cannot be determined or does not exist.
    R   s   canonical-oem-t   -i   N(   R)   RG   R,   R*   (   RS   t   dcd(    (    s)   /usr/share/apport/general-hooks/ubuntu.pyRC     s    c         C   sI  d |  k r d S|  d j    s. d |  d <d Sg  } |  d } t | t  rn t j d  } t j d  } n t j d  } t j d  } xK | j   D]= } | j |  s | j |  r g  } q n  | j |  q W| s d St | d t  rd j	 |  |  d <n8 d j	 g  | D] } t | j
 d	 d
   ^ q |  d <d S(   s=   Trim DpkgTerminalLog to the most recent installation session.R   Ns/   /var/log/apt/term.log does not contain any dataR   s   ^\(.* ... \d+ .*\)$s   ^Log started:i    s   
s   UTF-8R   (   R   t
   isinstancet   bytesRH   RL   R?   t   matchR+   R@   R-   t   decode(   RS   R^   t   dpkg_logt   trim_ret   start_reR_   (    (    s)   /usr/share/apport/general-hooks/ubuntu.pyRy     s,    

c         C   s@   t  |  | t j  r2 |  | j   j d  } n
 |  | } | S(   Ns   UTF-8(   R   t   problem_reportt   CompressedValuet	   get_valueR   (   RS   RZ   t   contents(    (    s)   /usr/share/apport/general-hooks/ubuntu.pyR=     s    
c   	      C   s   t  j j d | g  } | j d  r5 d | |  <d St  j j d d | g  } t  j j |  } | r} t  j j |  } n  t  j j | d g  } d | | f } | r | d	 | | f 7} n
 | d
 7} | | |  <d S(   s<   Add comma separated details about which python is being usedt   whichs   Error: s   N/ANt   readlinks   -fs	   --versions   %s, %ss   , %s, %ss   , unpackaged(   R   R&   R   R,   t	   fileutilst   find_file_packageR3   R4   (	   R   R   RS   t   python_patht   python_linkt
   python_pkgt   python_pkg_versiont   python_versiont   data(    (    s)   /usr/share/apport/general-hooks/ubuntu.pyR1     s     

t   __main__i   s.   Usage for testing this hook: %s <report file>
i    i   t   rbs   +%s: %ss   -%s: (deleted)s   ~%s: (changed)s   %d items changed(8   t   __doc__RH   R8   t   os.pathR   t   sysR   t   apport.packagingR   t   apport.hookutilsR   R    RQ   R   Rf   t   urlparseR   t   urllib2R   t   urllib.parset   urllib.requestRn   R<   R>   R7   R#   R"   R%   R$   R0   RC   Ry   R=   R1   t   __name__t   lent   argvR   t   writet   exitt   report_filet   ReportRS   t   openRa   t   loadt   setR/   t   report_keyst   copyt
   new_reportRG   t   new_report_keyst   changedt   sortedR   (    (    (    s)   /usr/share/apport/general-hooks/ubuntu.pyt   <module>   sf   H			b			,	#	%							