
(qhc           @   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  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.    Z/ e) j0   Z1 e e1 d  e- e1 j.    Z3 d Z4 x~ e5 e/ e3 B D]l Z6 e6 e3 k re6 e/ k rd e6 e1 e6 f GHe4 d 7Z4 qDe6 e/ k rDe6 e3 k rDd e6 GHe4 d 7Z4 qDqDWd e4 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  y t j j d  |  d <Wn t k
 r n X|  j d	  d
 k rtt j j |  d d g  t |   d |  k rtt j d  } | j | j d } | d k  rqd | |  d <qqqtn  t |   x d d g D] } | |  k rt |  |  } | } t |  |  t |  |  }	 |	 j	 d  }	 g  }
 xR | j   D]D } | |	 k r|
 j
 t |   q| |	 k r|	 j |  qqWd j |
  } qqWxB d; D]: } | |  k rU|  | j d  rU|  | d |  | <qUqUW|  j d
 d  j d  r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  } | rZ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 } d+ } d, } t# j$ | |  st# j$ | |  r|  d- | k rt j j% |  d.  qn  d } t& } t# j' d/ t# j(  } x | j	 d  D]} | j$ |  rd0 | } t) } qn  | d0 | 7} d1 | k r| j d2  rd3 | k rt# j$ d4 |  } | rt j j* | j+ d   rt, d5  |  d <n  t- |  d6  n  | rd7 | | | f |  d8 <Pqn  | r| j. |  d9 k rt, d:  |  d <qqqWqn  d  S(<   Ns   /proc/cpuinfot   ProcCpuinfot    s   
s   processor	:t   ProcCpuinfoMinimalt   apportt   ApportVersiont   ProblemTypet   Packaget   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   VarLogDistupgradeApttermlogt   ExecutablePatht   InterpreterPaths   /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   (/   t   add_release_infot   add_kernel_infot   add_cloud_infot   add_proposed_infoR   t	   hookutilst   attach_filet   reversedt   gett   splitt   appendt
   startswitht   joint   popt	   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   add_tagt   find(   t   reportR   t   short_cpuinfot   itemt   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&   (   RM   (    (    s)   /usr/share/apport/general-hooks/ubuntu.pyR5      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    Rh   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 tarfileRg   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(   Ri   Rj   (   R&   RJ   t   trim_dpkg_logR6   RA   RB   t	   MULTILINERG   RD   R   R,   t   get_file_packageR@   R1   t   patht   existsR)   RI   R-   R#   t   attach_file_if_existsR   t   command_outputRK   (   RM   RR   t   wrong_grub_msgRS   t   grub_hook_failuret   grub_errorst
   grub_errort   grub_packaget   mRt   R\   t   error_messaget   corrupt_packaget   out_of_memoryt   reports(    (    s)   /usr/share/apport/general-hooks/ubuntu.pyR7      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   (
   RD   t   stript   endswithR'   t   replaceR(   R&   RA   RB   RG   (	   RM   t   devs_to_checkt   dft   device_errorRW   t   devicet   dmesgt   devt   error_device(    (    s)   /usr/share/apport/general-hooks/ubuntu.pyR0     s4    
-c         C   s   t  j j |  d d  d  S(   Ns   /proc/version_signaturet   ProcVersionSignature(   R   R#   Rv   (   RM   (    (    s)   /usr/share/apport/general-hooks/ubuntu.pyR    >  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#   Rv   R1   Rt   Ru   R,   R-   Rw   R@   R)   RK   Ro   t   st_mtimet   timet   strftimet   gmtimet   command_available(   RM   t   mediat   release_codenamet   mtimet   human_mtimet   deltat   log(    (    s)   /usr/share/apport/general-hooks/ubuntu.pyR   C  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'   R.   t
   subprocesst   Popent   PIPERG   t   communicatet
   returncodeRD   R8   R)   RK   (	   RM   R\   R^   t	   apt_cachet   outt   found_proposedt   found_updatest   found_securityRW   (    (    s)   /usr/share/apport/general-hooks/ubuntu.pyR"   o  s6    					c   	      C   sX  t  } xB d D]: } y  t j j |  r2 t } Pn  Wq t k
 rF q Xq W| rTd } t | d  } y t | d d j   } Wn d  } n X| rD| j
 d  rDt |  d  i d	 d
 6d d 6d d 6d d 6d d 6} | |  d <xg | j   D]I \ } } y) t t | |  d d j   |  | <Wq d |  | <q Xq WqT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(   RD   R   R,   R-   RG   R.   R   R   t   readR@   R)   RK   t   items(	   RM   t   ec2_instancet   pkgt   metadata_urlt
   ami_id_urlR   t   fieldst   keyt   value(    (    s)   /usr/share/apport/general-hooks/ubuntu.pyR!     s:    



)c         C   s(   |  j  d d  |  d c d | 7<d  S(   Nt   TagsR   R   (   t
   setdefault(   RM   t   tag(    (    s)   /usr/share/apport/general-hooks/ubuntu.pyRK     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&   R@   R)   R'   (   RM   t   dcd(    (    s)   /usr/share/apport/general-hooks/ubuntu.pyR<     s    c         C   sF  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 <n5 d j	 g  | D] } t | j
 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-8(   R   t
   isinstancet   bytesRA   RE   R8   t   matchR(   R9   R*   t   decode(   RM   RV   t   dpkg_logt   trim_ret   start_reRW   (    (    s)   /usr/share/apport/general-hooks/ubuntu.pyRq     s,    

c         C   s@   t  |  | t j  r2 |  | j   j d  } n
 |  | } | S(   Ns   UTF-8(   R   t   problem_reportt   CompressedValuet	   get_valueR   (   RM   RR   t   contents(    (    s)   /usr/share/apport/general-hooks/ubuntu.pyR6     s    
t   __main__i   s.   Usage for testing this hook: %s <report file>
i    i   t   rbs   +%s: %ss   -%s: (deleted)s   %d items changed(7   t   __doc__RA   R1   t   os.pathR   t   sysR   t   apport.packagingR   t   apport.hookutilsR   R    RJ   R   R^   t   urlparseR   t   urllib2R   t   urllib.parset   urllib.requestRf   R5   R7   R0   R    R   R"   R!   RK   R<   Rq   R6   t   __name__t   lent   argvR   t   writet   exitt   report_filet   ReportRM   t   openRY   t   loadt   sett   keyst   report_keyst   copyt
   new_reportR@   t   new_report_keyst   changedt   sortedR   (    (    (    s)   /usr/share/apport/general-hooks/ubuntu.pyt   <module>   s\   H			b			,	#	%					