3
J_                 @   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m	Z	 ej
j d Zi adIddZdd Zdd	 Zd
d Zi e_dd Zdd Zdd Zdd Zdd Zdd Zdd ZdJddZdd Zd d! ZdKd"d#ZdLd$d%ZdMd&d'ZG d(d) d)e Z!d*d+ Z"d,d- Z#dNd/d0Z$dOd1d2Z%d3d4 Z&d5d6 Z'd7d8 Z(d9d: Z)d;d< Z*d=d> Z+d?d@ Z,dAdB Z-dCdD Z.dEdF Z/dGdH Z0dS )PzGHardware and driver package detection functionality for Ubuntu systems.    N)kerneldetectionc             C   sN  i }| rd|  pd}x2t j|D ]"\}}}d}d|kry.tt jj|d}|j j }W dQ R X W q tk
r } ztj	d|| w"W Y dd}~X qX n\d|krd|krtt jj|d4}	x,|	D ]$}
|
j
dr|
jd	d
d
 j }P qW W dQ R X |sq"t jj|d}t jj|d}t jj|r>t jj| r>q"|||< q"W |S )u   Get modaliases present in the system.

    This ignores devices whose drivers are statically built into the kernel, as
    you cannot replace them with other driver packages anyway.

    Return a modalias → sysfs path map.
    z
%s/devicesz/sys/devicesNmodaliasz0system_modaliases(): Cannot read %s/modalias: %sZssbZueventz	MODALIAS==   drivermodule)oswalkopenpathjoinreadstripIOErrorloggingdebug
startswithsplitislink)sys_pathaliasesZdevicesr   dirsfilesr   fefdlineZ
driverlinkZmodlink r   6/usr/lib/python3/dist-packages/UbuntuDrivers/detect.pysystem_modaliases   s4    

r   c             C   s   d }y*x$| d j jD ]}|jdr|}P qW W n" ttfk
rP   tjd dS X |sZdS y|d }W n tk
rz   dS X d|kr||krtjd|d | dS dS )	Nzxserver-xorg-corezxorg-video-abi-zL_check_video_abi_compat(): xserver-xorg-core not available, cannot check ABITFDependszBDriver package %s is incompatible with current X.org server ABI %sZPackage)	candidateZprovidesr   AttributeErrorKeyErrorr   r   )	apt_cacherecordZxorg_video_abipdepsr   r   r   _check_video_abi_compatI   s(    


r(   c       	      C   s  i }x| D ]}y|j jd }W n tttfk
r>   wY nX |j  s|j jdtfkrZqt| |j jsjqyxz|jdD ]l}|j	d}|sqx|jd\}}xH|jdD ]:}|j	 }|jddd	 }|j
|i j
|t j|j qW qxW W q tk
r   tjd
|j|f  Y qX qW |S )aE  Build a modalias map from an apt.Cache object.

    This filters out uninstallable video drivers (i. e. which depend on a video
    ABI that xserver-xorg-core does not provide).

    Return a map bus -> modalias -> [package, ...], where "bus" is the prefix of
    the modalias up to the first ':' (e. g. "pci" or "usb").
    
Modaliasesall)z, (,:r   r   z*Package %s has invalid modalias header: %s)r!   r%   r#   r"   UnicodeDecodeErrorarchitecturesystem_architecturer(   r   r   
setdefaultsetaddname
ValueErrorr   error)	r$   resultpackagempartr   ZlstaliasZbusr   r   r   _apt_cache_modalias_mapd   s2    	
*r=   c                s   t  }t }ytj| }W n& tk
rB   t }|tj|< Y nX |j|jddd i }x<|D ]4}tj|j	 |j	 rbx|| D ]}|j
| qW qbW  fdd|D S )z_Search packages which match the given modalias.

    Return a list of apt.Package objects.
    r.   r   r   c                s   g | ]} | qS r   r   ).0r&   )r$   r   r   
<listcomp>   s    z)packages_for_modalias.<locals>.<listcomp>)r3   hashpackages_for_modalias
cache_mapsr#   r=   getr   fnmatchlowerr4   )r$   r   pkgsZapt_cache_hashZ	cache_mapZbus_mapr<   r&   r   )r$   r   rA      s    
rA   c             C   sB   | j d k	st| jjdrdS x| j jD ]}|jdkr(dS q(W dS )NnvidiaF
restricted
multiverseT)rH   rI   )r!   AssertionErrorr5   r   originsZ	component)pkgor   r   r   _is_package_free   s    
rN   c             C   s2   | j d krdS x| j jD ]}|jdkrdS qW dS )NFZUbuntuT)r!   rK   origin)rL   rM   r   r   r   _is_package_from_distro   s    

rP   c             C   sl   y| j jd }W n& ttfk
r6   tjd| j dS X |jd}|dkr\tjd| j dS |d| }|S )z-Determine module name from apt Package objectr)   zM_pkg_get_module %s: package has no Modaliases header, cannot determine moduleNr,   r   zR_pkg_get_module %s: package has invalid Modaliases header, cannot determine module)	r!   r%   r#   r"   r   r   r5   findwarning)rL   r:   Zparenr   r   r   r   _pkg_get_module   s    
rS   c             C   sX   y| j jd }W n& ttfk
r6   tjd| j dS X |dkrTtjd| j| dS |S )z/Determine support level from apt Package objectZSupportzR_pkg_get_support %s: package has no Support header, cannot determine support levelNNFBLTSBzY_pkg_get_support %s: package has invalid Support %sheader, cannot determine support level)rT   rU   )r!   r%   r#   r"   r   r   r5   rR   )rL   supportr   r   r   _pkg_get_support   s    
rW   c                s~   y| j jd }W n tttfk
r*   dS X |jddkr>dS ||jdd |jd }|jd}t fd	d
|D S dS )z>Check if the package supports runtimepm for the given modaliasZ	PmAliasesFznvidia(r   r,   r   r+   z, c             3   s"   | ]}t j  j |j V  qd S )N)rD   rE   )r>   Zregex)r<   r   r   	<genexpr>   s    z*_is_runtimepm_supported.<locals>.<genexpr>N)r!   r%   r#   r"   r/   rQ   r   any)rL   r<   r:   n
modaliasesr   )r<   r   _is_runtimepm_supported   s    
r\   c             C   s   | j r
dS | jjdrd}n| jjdr.d}nt| }|s>dS y$tjd|gtjtjd}|j  W n8 tt	tj
fk
r } ztjdt| dS d}~X n X |jdkrtjd	| j| d
S tjd| j| dS )zIDetermine if the kernel module from an apt.Package is manually installed.FrG   fglrxmodinfo)stdoutstderrz_is_manual_install failed: %sNr   zJ_is_manual_install %s: builds module %s which is available, manual installTzQ_is_manual_install %s: builds module %s which is not available, no manual install)	installedr5   r   rS   
subprocessPopenPIPEZcommunicateOSErrorFileNotFoundErrorCalledProcessErrorr   r   str
returncode)rL   r   r^   r   r   r   r   _is_manual_install   s.    



rj   c       	      C   s   yt jddd| gdd}W n: tt jfk
rV } ztjd| |t| dS d}~X nX tjd| d}d}x8|j D ],}|jd	d
\}}d|kr|}d|krv|}qvW tjd| ||| ||fS )zTReturn (vendor, model) names for given device.

    Values are None if unknown.
    ZudevadmZhwdbz--test=T)Zuniversal_newlinesz-_get_db_name(%s, %s): udevadm hwdb failed: %sNz_get_db_name: output
%s
r   r   Z_VENDORZ_MODELz-_get_db_name(%s, %s): vendor "%s", model "%s")NN)	rb   Zcheck_outputre   rg   r   r   rh   
splitlinesr   )	syspathr<   outr   vendormodelr   kvr   r   r   _get_db_name  s$    
rr   FTc             C   s  t |}| sDytj } W n* tk
rB } ztj| i S d}~X nX i }x|j D ]\}}xt| |D ]}	|rzt|	 rzqf| rt	j	|	j
drqf||t|	t|	t|	t|	|d||	j
< t||\}
}|
dk	r|
||	j
 d< |dk	rf|||	j
 d< qfW qRW dd |D }|rvx*|j D ]\}}|jdr|t|< qW |jtjtd	 |d }x|D ]}	|	|k||	 d< q\W xJt| j D ]:\}}x.|D ]&}	| |	 }t|t||d||	< qW qW |S )u  Get driver packages that are available for the system.

    This calls system_modaliases() to determine the system's hardware and then
    queries apt about which packages provide drivers for those. It also adds
    available packages from detect_plugin_packages().

    If you already have an apt.Cache() object, you should pass it as an
    argument for efficiency. If not given, this function creates a temporary
    one by itself.

    If freeonly is set to True, only free packages (from main and universe) are
    considered

    Return a dictionary which maps package names to information about them:

      driver_package → {'modalias': 'pci:...', ...}

    Available information keys are:
      'modalias':    Modalias for the device that needs this driver (not for
                     drivers from detect plugins)
      'syspath':     sysfs directory for the device that needs this driver
                     (not for drivers from detect plugins)
      'plugin':      Name of plugin that detected this package (only for
                     drivers from detect plugins)
      'free':        Boolean flag whether driver is free, i. e. in the "main"
                     or "universe" component.
      'from_distro': Boolean flag whether the driver is shipped by the distro;
                     if not, it comes from a (potentially less tested/trusted)
                     third party source.
      'vendor':      Human readable vendor name, if available.
      'model':       Human readable product name, if available.
      'recommended': Some drivers (nvidia, fglrx) come in multiple variants and
                     versions; these have this flag, where exactly one has
                     recommended == True, and all others False.
    Nz
oem-*-meta)r   rl   freefrom_distrorV   Z	runtimepmrn   ro   c             S   s   g | ]}|j d r|qS )znvidia-)r   )r>   r&   r   r   r   r?   x  s    z*system_driver_packages.<locals>.<listcomp>znvidia-)keyr   recommended)rs   rt   plugin)r   aptCache	Exceptionr   r7   itemsrA   rN   rD   r5   rP   rW   r\   rr   r   lookup_cachesort	functools
cmp_to_key_cmp_gfx_alternativesdetect_plugin_packages)r$   r   freeonlyinclude_oemr[   expackagesr<   rl   r&   rn   ro   nvidia_packagesru   valuerv   rw   rF   Zapt_pr   r   r   system_driver_packages4  sR    %


r   c             C   s@   t jd}|j| }|r<|jddd  |jddd  fS dS )Nz$(.+):v(.+)d(.+)sv(.+)sd(.+)bc(.+)i.*         )NN)recompilematchgroup)r<   Zmodalias_patternZdetailsr   r   r   _get_vendor_model_from_alias  s
    

$r   c       	      C   s   | j d k	std }| j}tjd}|j|}|r@tjd| |S tjd}|j|}|shtjd| |S d|jd|jdf }d| }y(|j	|}|j r|j j
d	tfkr|}W n tk
r   Y nX |S )
Nznvidia-([0-9]+)z%Legacy driver detected: %s. Skipping.znvidia-driver-([0-9]+)(.*)z(No flavour can be found in %s. Skipping.z%s%sr   r   znvidia-headless-no-dkms-%sr*   )r!   rJ   Z	shortnamer   r   r   r   r   r   __getitem__r0   r1   r#   )	rL   r$   metapackager5   patternr   candidate_flavourr!   r9   r   r   r   !_get_headless_no_dkms_metapackage  s.    




r   c       	      C   s   |si S t |}| sLytj } W n* tk
rJ } ztj| i S d}~X nX i }x\|j D ]P\}}xFt| |D ]8}tj|j	dsqn||t
|t|dt|d||j	< qnW qZW |S )u  Get device specific metapackages for this system

    This calls system_modaliases() to determine the system's hardware and then
    queries apt about which packages provide hardware enablement support for
    those.

    If you already have an apt.Cache() object, you should pass it as an
    argument for efficiency. If not given, this function creates a temporary
    one by itself.

    Return a dictionary which maps package names to information about them:

      driver_package → {'modalias': 'pci:...', ...}

    Available information keys are:
      'modalias':    Modalias for the device that needs this driver (not for
                     drivers from detect plugins)
      'syspath':     sysfs directory for the device that needs this driver
                     (not for drivers from detect plugins)
      'plugin':      Name of plugin that detected this package (only for
                     drivers from detect plugins)
      'free':        Boolean flag whether driver is free, i. e. in the "main"
                     or "universe" component.
      'from_distro': Boolean flag whether the driver is shipped by the distro;
                     if not, it comes from a (potentially less tested/trusted)
                     third party source.
      'vendor':      Human readable vendor name, if available.
      'model':       Human readable product name, if available.
      'recommended': Always True; we always recommend you install these
                     packages.
    Nz
oem-*-metaT)r   rl   rs   rt   rv   rV   )r   ry   rz   r{   r   r7   r|   rA   rD   r5   rN   rP   rW   )	r$   r   r   r[   r   r   r<   rl   r&   r   r   r   #system_device_specific_metapackages  s*     
r   c             C   s  dg}t |}| sJytj } W n* tk
rH } ztj| i S d}~X nX i }x|j D ]\}}xt| |D ]}t||\}	}
t	|\}}|dk	o|j
 |krl||t|t|t|d||j< |	dk	r|	||j d< |
dk	r|
||j d< t|| }|dk	rl|||j d< qlW qXW dd |D }|rx*|j D ]\}}|jd	r2|t|< q2W |jtjtd
 |d }x|D ]}||k|| d< qtW |S )u  Get driver packages, for gpgpu purposes, that are available for the system.

    This calls system_modaliases() to determine the system's hardware and then
    queries apt about which packages provide drivers for those. Finally, it looks
    for the correct metapackage, by calling _get_headless_no_dkms_metapackage().

    If you already have an apt.Cache() object, you should pass it as an
    argument for efficiency. If not given, this function creates a temporary
    one by itself.

    Return a dictionary which maps package names to information about them:

      driver_package → {'modalias': 'pci:...', ...}

    Available information keys are:
      'modalias':    Modalias for the device that needs this driver (not for
                     drivers from detect plugins)
      'syspath':     sysfs directory for the device that needs this driver
                     (not for drivers from detect plugins)
      'plugin':      Name of plugin that detected this package (only for
                     drivers from detect plugins)
      'free':        Boolean flag whether driver is free, i. e. in the "main"
                     or "universe" component.
      'from_distro': Boolean flag whether the driver is shipped by the distro;
                     if not, it comes from a (potentially less tested/trusted)
                     third party source.
      'vendor':      Human readable vendor name, if available.
      'model':       Human readable product name, if available.
      'recommended': Some drivers (nvidia, fglrx) come in multiple variants and
                     versions; these have this flag, where exactly one has
                     recommended == True, and all others False.
    Z10deN)r   rl   rs   rt   rV   rn   ro   r   c             S   s   g | ]}|j d r|qS )znvidia-)r   )r>   r&   r   r   r   r?   B  s    z0system_gpgpu_driver_packages.<locals>.<listcomp>znvidia-)ru   r   rv   rx   )r   ry   rz   r{   r   r7   r|   rA   rr   r   rE   rN   rP   rW   r5   r   r   r}   r~   r   r   _cmp_gfx_alternatives_gpgpu)r$   r   Zvendors_whitelistr[   r   r   r<   rl   r&   rn   ro   Z	vendor_idZmodel_idr   r   ru   r   rv   r   r   r   system_gpgpu_driver_packages  sH    "


r   c             C   s:  i }| s@yt j } W n* tk
r> } ztj| i S d}~X nX xt| ||dj D ]\}}d|krn|d }n|d }|j|i  x$dD ]}||kr|| || |< qW || jdi }	|d	 |d
 d|	|< d|krT|d |	| d< qTW xB|j D ]6\}
}x*|d D ]}t| | sP qW d|d<  qW t	| |S )u  Get by-device driver packages that are available for the system.

    This calls system_modaliases() to determine the system's hardware and then
    queries apt about which packages provide drivers for each of those. It also
    adds available packages from detect_plugin_packages(), using the name of
    the detction plugin as device name.

    If you already have an apt.Cache() object, you should pass it as an
    argument for efficiency. If not given, this function creates a temporary
    one by itself.

    If freeonly is set to True, only free packages (from main and universe) are
    considered

    Return a dictionary which maps devices to available drivers:

      device_name →  {'modalias': 'pci:...', <device info>,
                      'drivers': {'pkgname': {<driver package info>}}

    A key (device name) is either the sysfs path (for drivers detected through
    modaliases) or the detect plugin name (without the full path).

    Available keys in <device info>:
      'modalias':    Modalias for the device that needs this driver (not for
                     drivers from detect plugins)
      'vendor':      Human readable vendor name, if available.
      'model':       Human readable product name, if available.
      'drivers':     Driver package map for this device, see below. Installing any
                     of the drivers in that map will make this particular
                     device work. The keys are the package names of the driver
                     packages; note that this can be an already installed
                     default package such as xserver-xorg-video-nouveau which
                     provides a free alternative to the proprietary NVidia
                     driver; these will have the 'builtin' flag set.
      'manual_install':
                     None of the driver packages are installed, but the kernel
                     module that it provides is available; this usually means
                     that the user manually installed the driver from upstream.

    Aavailable keys in <driver package info>:
      'builtin':     The package is shipped by default in Ubuntu and MUST
                     NOT be uninstalled. This usually applies to free
                     drivers like xserver-xorg-video-nouveau.
      'free':        Boolean flag whether driver is free, i. e. in the "main"
                     or "universe" component.
      'from_distro': Boolean flag whether the driver is shipped by the distro;
                     if not, it comes from a (potentially less tested/trusted)
                     third party source.
      'recommended': Some drivers (nvidia, fglrx) come in multiple variants and
                     versions; these have this flag, where exactly one has
                     recommended == True, and all others False.
    N)r   rl   rw   r   rn   ro   driversrs   rt   )rs   rt   rv   TZmanual_install)r   rn   ro   )
ry   rz   r{   r   r7   r   r|   r2   rj   _add_builtins)r$   r   r   r8   r   rL   ZpkginfoZdevice_nameZopt_keyr   r   infor   r   r   system_device_driversQ  s6    5


r   c               @   s   e Zd ZdddZdd ZdS )_GpgpuDriverNc             C   s   d| _ || _|| _d S )NrG   )rG   )_vendors_whitelistrn   flavour)selfrn   r   r   r   r   __init__  s    z_GpgpuDriver.__init__c             C   s,   | j rtj| j| j sdS | j  o(| j  S )NF)rn   rD   filterr   r   )r   r   r   r   is_valid  s    z_GpgpuDriver.is_valid)NN)__name__
__module____qualname__r   r   r   r   r   r   r     s   
r   c             C   s   t  }tjd}tjd}tjd}|j| }|j| }|j| }|rn|jd|_d|jd|jdf |_n*|rz| |_n|rd|jd|jdf |_|S )zReturns a _GpgpuDriver objectz(.+):([0-9]+)(.*)z([a-z]+)z([0-9]+)(.*)r   z%s%sr   r   )r   r   r   r   r   rn   r   )stringr   Zfull_patternZvendor_only_patternZseries_only_patternZ
full_matchZvendor_matchZseries_matchr   r   r   _process_driver_string  s    





r   c             C   s  g }g }i }| s|S |rLxB|j dD ]"}t|}|r$|j r$|j| q$W nt }|j| t|dk rl|S d}x$|D ]}|jsd|| _|d7 }qvW d}g }x<|D ]4}|j}	|j|	rtj	d |S |j|	 |d7 }qW d}x0|D ](}|j
 o|j rd|| _|d7 }qW xD|D ]<}|j
r6d|j|j
f }
n
d|j }
|jtj| |
 qW x`|D ]X}xP|D ]H}|j|jrh|j
r| | ||< n| | jdr| | ||< P qhW q^W |S )	Nr-   r   r   rG   z0Multiple nvidia versions passed at the same timez%s*%sz%s*rv   )r   r   r   appendr   lenrn   __contains__r   r   r   extendrD   r   rC   )r   drivers_strr   allowr8   itemr   itZvendors_temprn   r   r&   r   r   r   gpgpu_install_filter  s^    












r    c             C   s   ddddddg}|r"t | |}|S g }x|D ]}|jtj| | q,W i }x0|D ](}d| | ksl| | d rP| | ||< qPW |S )a  Get packages which are appropriate for automatic installation.

    Return the subset of the given list of packages which are appropriate for
    automatic installation by the installer. This applies to e. g. the Broadcom
    Wifi driver (as there is no alternative), but not to the FGLRX proprietary
    graphics driver (as the free driver works well and FGLRX does not provide
    KMS).
    zbcmwl*z	pvr-omap*zvirtualbox-guest*znvidia-*zopen-vm-tools*z
oem-*-metarv   )r   r   rD   r   )r   r   Z	whitelistZresultsr   r   r8   r&   r   r   r   auto_install_filter7  s    



r   c       
   #   C   s  i }t jjdd}t jj|s.tjd| |S | dkrnytj } W n* t	k
rl } ztj
| i S d}~X nX x:t j|D ]*}|jdsq|t jj||}tjd| i }t|}y4tt|j |d| |d | }tjd	|| W n$ t	k
r   tjd
| w|Y nX |dkrw|t|ttfkrBtj
d|t| w|xZ|D ]R}	|	| kr| |	 jrt| | |	 jjr|j|g j|	 ntjd|	| qHW W dQ R X q|W |S )a]  Get driver packages from custom detection plugins.

    Some driver packages cannot be identified by modaliases, but need some
    custom code for determining whether they apply to the system. Read all *.py
    files in /usr/share/ubuntu-drivers-common/detect/ or
    $UBUNTU_DRIVERS_DETECT_DIR and call detect(apt_cache) on them. Filter the
    returned lists for packages which are available for installation, and
    return the joined results.

    If you already have an existing apt.Cache() object, you can pass it as an
    argument for efficiency.

    Return pluginname -> [package, ...] map.
    ZUBUNTU_DRIVERS_DETECT_DIRz(/usr/share/ubuntu-drivers-common/detect/z3Custom detection plugin directory %s does not existNz.pyz"Loading custom detection plugin %sexecZdetectzplugin %s return value: %szplugin %s failed:z6plugin %s returned a bad type %s (must be list or set)z.Ignoring unavailable package %s from plugin %s)r   environrC   r   isdirr   r   ry   rz   r{   r7   listdirendswithr   r
   r   r   r   Z	exceptiontypelistr3   r!   r(   r%   r2   r   )
r$   r   Z	plugindirr   fnamerw   Zsymbr   r8   rL   r   r   r   r   T  sJ    




"r   c             C   s   t j| rt j| jdS dS )z5Look up driver package and return their support levelrV   N)r}   rC   )xr   r   r   _pkg_support_from_cache  s    
r   c             C   s  | j dr|j d rdS | j d r4|j dr4dS t| dkrPt|dkrPdS t| dkrlt|dkrldS t| dkrt|dkrd	S t| dkrt|dkrdS t| dkrt|dkrd
S t| dkrt|dkrdS | |k rdS | |krdS | |kstdS )zCompare two graphics driver names in terms of preference. (desktop)

    -server always sorts after non-server.
    LTSB (Long Term Support Branch) always sorts before NFB (New Feature Branch).
    Legacy always sorts before Beta.
    z-serverr   rU   LegacyBetar   rx   rx   rx   rx   rx   )r   r   rJ   )r   yr   r   r   r     s,    r   c             C   s  | j dr|j d rdS | j d r4|j dr4dS t| dkrPt|dkrPdS t| dkrlt|dkrldS t| dkrt|dkrd	S t| dkrt|dkrdS t| dkrt|dkrd
S t| dkrt|dkrdS | |k rdS | |krdS | |kstdS )zCompare two graphics driver names in terms of preference. (server)

    -server always sorts before non-server.
    LTSB (Long Term Support Branch) always sorts before NFB (New Feature Branch).
    Legacy always sorts before Beta.
    z-serverr   rU   r   r   r   rx   rx   rx   rx   rx   )r   r   rJ   )r   r   r   r   r   r     s,    r   c             C   s   x| j  D ]\}}xv|d D ]j}|jdrBddddd|d d< P |jdrx |d D ]}d|d | d< qVW ddddd|d d	< P qW q
W d
S )zAdd builtin driver alternativesr   rG   TF)rs   Zbuiltinrt   rv   zxserver-xorg-video-nouveaur]   rv   zxserver-xorg-video-atiN)r|   r   )r   Zdevicer   rL   dr   r   r   r     s    

r   c             C   s   t j| }|j S )z0Return the linux headers for the system's kernel)r   KernelDetectionZget_linux_headers_metapackage)r$   kernel_detectionr   r   r   get_linux_headers  s    
r   c             C   s   t j| }|j S )z.Return the linux image for the system's kernel)r   r   Zget_linux_image_metapackage)r$   r   r   r   r   get_linux_image  s    
r   c             C   s   t j| }|j S )z.Return the linux image for the system's kernel)r   r   get_linux_version)r$   r   r   r   r   r     s    
r   c             C   s   t j| }|j S )z4Return the linux metapackage for the system's kernel)r   r   Zget_linux_metapackage)r$   r   r   r   r   	get_linux  s    
r   c             C   s   t  }x| D ]z}|jj|rg }|jr4|j|jj |jrH|j|jj x<|D ]4}x.|D ]&}|jdkrhqX|j|krX|j|j qXW qNW qW t	|S )z-Return the reverse dependencies for a packager    )
r3   r5   r   r!   r   dependenciesra   Zrawtyper4   r   )r$   r9   prefixr'   rL   r   Zordepdepr   r   r   find_reverse_dependencies  s    




r   c             C   sf   | | j r| | j j}y|d }W n tk
r6   d S X |j jd}x|D ]}|jdrL|S qLW d S )Nr    z, zlinux-image-)r!   r%   r#   r   r   r   )r$   rL   r%   r'   Z	deps_listr   r   r   r   get_linux_image_from_meta  s    


r   c             C   s&  |dk	st d}d}d}d|kr0tjd| |S tjd}|j|}|rXtjd| |S t| }t| |}|r||jdd}ntjd| |S tjd	}|j|}|stjd
| |S d|j	d|j	df }	d|	|f }
y~| j
|
}|jo|jjdtfkrTt| }d|	|f }tjd|  | j
|}|jrT|jjdtfkrTtjd|  |
}W n" tk
rx   tjd|
 Y nX |rt| |d}d}x|D ]}||kr|}qW |r|}|S d|	 }tjd|  y,| j
|}|jr|jjdtfkr|}W n" tk
r    tjd| Y nX |S )zDReturn the linux-modules-$driver metapackage for the system's kernelNr   rG   zNNon NVIDIA linux-modules packages are not supported at this time: %s. Skippingznvidia-([0-9]+)z%Legacy driver detected: %s. Skipping.zlinux-image-z-No linux-image can be found for %s. Skipping.znvidia-.*-([0-9]+)(.*)z(No flavour can be found in %s. Skipping.z%s%sr   r   zlinux-modules-nvidia-%s-%sr*   zlinux_modules_abi_candidate: %szFound ABI compatible %szNo "%s" can be found.zlinux-modules-nvidia-znvidia-dkms-%szFalling back to %s)rJ   r   r   r   r   r   r   r   replacer   r   r!   r0   r1   r   r#   r   r7   )r$   r!   r   Zlinux_flavourZlinux_modules_matchr   r   Zlinux_image_metaZlinux_imager   Zlinux_modules_candidater9   Zlinux_versionZlinux_modules_abi_candidateZabi_specificZreverse_depsZpickr   Zdkms_packager   r   r   get_linux_modules_metapackage+  sx    










r   )N)NNFT)NNT)NN)NNF)r   )N)1__doc__r   r   rD   rb   r   r   ry   ZUbuntuDriversr   apt_pkgZget_architecturesr1   r}   r   r(   r=   rA   rB   rN   rP   rS   rW   r\   rj   rr   r   r   r   r   r   r   objectr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sX   

/,
#
]&
=
R
\d

=