o
    a<                     @   s   d dl mZm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 e
jdkr:dZndZeejddd	eZd
d Zdd ZdZG dd deZdS )    )print_functionabsolute_importN)SqliteDatabase3gettextZugettextzcommand-not-foundT)fallbackc                    sv   d fddt td D }dd |D }dd |D } fdd|D } fdd|D }t|| | | S )	zw
    return a set with spelling1 distance alternative spellings

    based on http://norvig.com/spell-correct.html
    z&abcdefghijklmnopqrstuvwxyz-_0123456789c                    s$   g | ]} d |  |d  fqS N ).0i)wordr	   A/usr/lib/python3/dist-packages/CommandNotFound/CommandNotFound.py
<listcomp>#      $ z!similar_words.<locals>.<listcomp>   c                 S   s$   g | ]\}}|r||d d  qS r   Nr	   r
   abr	   r	   r   r   $   r   c                 S   s<   g | ]\}}t |d kr||d   |d  |dd  qS )r   r      Nlenr   r	   r	   r   r   %   s   < c                    s2   g | ]\}} D ]}|r|| |d d  qqS r   r	   r
   r   r   calphabetr	   r   r   &   s   2 c                    s&   g | ]\}} D ]}|| | qqS r	   r	   r   r   r	   r   r   '   s   & )ranger   set)r   sZdeletesZ
transposesZreplacesinsertsr	   )r   r   r   similar_words   s   r    c                  C   sD   zt  } tdd | v ptdd | v W S  ty!   Y dS w )Nsudor   ZadminF)posix	getgroupsgrpgetgrnamKeyError)groupsr	   r	   r   user_can_sudo+   s   r(   z&/var/lib/command-not-found/commands.dbc                   @   s   e Zd ZdZdZdZdZejZ	d*ddZ
d+d	d
Zdd Zdd Zdd Zdd Zdd Zd,ddZdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd-d'd(Zd)S ).CommandNotFoundz
programs.d   )z	/snap/binz/binz/usr/binz/usr/local/binz/sbinz	/usr/sbinz/usr/local/sbinz
/usr/gamesz/usr/bin/snap/usr/share/command-not-foundc                 C   sN   |   | _tjtrtttjrtt| _	nt
dt | _t | _d S )NzCannot find database)_getSourcesListsources_listospathexistsdbpathaccessR_OKr   dbFileNotFoundErrorr(   r"   geteuideuid)selfZdata_dirr	   r	   r   __init__K   s   
zCommandNotFound.__init__   c           	      C   sd   g }|t |  kr| jks|S  |S t|D ]}| |}|D ]\}}}|||||f q q|S )z try to correct the spelling )r   max_lenr    get_packagesappend)	r8   r   Zmin_lenZpossible_alternativeswpackagespackagevercompr	   r	   r   spelling_suggestionsU   s   
z$CommandNotFound.spelling_suggestionsc                 C   s   | j |S r   )r4   lookupr8   commandr	   r	   r   r<   `   s   zCommandNotFound.get_packagesc           	   
   C   sl  g }g }t j| jstd| j  g g fS z%tt j}tj	| jddd|g|dd}W d    n1 s6w   Y  W n tj
yZ } ztd|  g g fW  Y d }~S d }~ww td|  zt|}W n tjy } ztd	|  g g fW  Y d }~S d }~ww |D ]'}|d
 |kr||d |d
 |df q||d
 |d |df q||fS )Nz%s not existszadvise-snapz--format=jsonz	--commandT)stderrZuniversal_newlinesz.calling snap advice-snap returned an error: %szgot %s from snap advise-snapzcannot decoding json: %sZCommandZSnapZVersion)r.   r/   r0   snap_cmdloggingdebugopendevnull
subprocessZcheck_outputZCalledProcessErrorjsonloadsZJSONDecodeErrorr=   get)	r8   rF   Zexact_resultZmispell_resultrL   outputesnapssnapr	   r	   r   	get_snapsc   sD     zCommandNotFound.get_snapsc                 C   sj   z)t tjtdddf}dd |D W  d    W S 1 s"w   Y  W d S  ty4   g  Y S w )NHOMEz/rootz.command-not-found.blacklistc                 S   s    g | ]}|  d kr|  qS ) )strip)r
   liner	   r	   r   r      s     z0CommandNotFound.getBlacklist.<locals>.<listcomp>)rK   r.   sepjoingetenvIOError)r8   Z	blacklistr	   r	   r   getBlacklist   s   (zCommandNotFound.getBlacklistc              	   C   sz   zdd l }ddlm} |  W n ttfy   g  Y S w tg }|ddD ]}|js:|js:|j	D ]}|
| q2q'|S )Nr   )SourcesListF)ZwithMatcher)apt_pkgZaptsources.sourceslistr_   ZinitSystemErrorImportErrorr   ZdisabledZinvalidcompsadd)r8   r`   r_   r-   sourceZ	componentr	   r	   r   r,      s   
zCommandNotFound._getSourcesListc                 C   s   dt jvrd S |r[td}tjdkrt|}dd }n||}tjjr/t|t	r/|
tjj}| tdkr]| jdkr?d}nd	}d
||f }td| tjd tj| dd d S d S d S )NZ COMMAND_NOT_FOUND_INSTALL_PROMPTz Do you want to install it? (N/y)r   c                 S   s   | S r   r	   )xr	   r	   r   <lambda>   s    z0CommandNotFound.install_prompt.<locals>.<lambda>yr   rW   sudo z%sapt install %sz%sfileF)shell)r.   environ_sysversioninputstdinencoding
isinstancestrdecodelowerr7   printstdoutrM   callsplit)r8   Zpackage_namepromptZanswerZ	raw_inputZcommand_prefixZinstall_commandr	   r	   r   install_prompt   s&   




zCommandNotFound.install_prompt   c           
      C   sV  t |t | |kr ttd|t |f | jd | j  dS t |t | dkrtttd| | jd |D ]\}}}|rCd| }nd}ttd|||f | jd q7|D ]\}}}	}|rcd| }nd}ttd	|||f | jd qVt |dkrttd
| jd n t |dkr| jrttdd | jd nttdd | jd | j  dS )z3 print spelling suggestions for packages and snaps z6Command '%s' not found, but there are %s similar ones.rj   Nr   %Command '%s' not found, did you mean:z (%s)rW   z  command '%s' from snap %s%s  command '%s' from deb %s%s3See 'snap info <snapname>' for additional versions.zTry: %s <deb name>zsudo apt installzapt install)r   rx   rn   	output_fdflushr(   )
r8   r   mispell_packagesmispell_snapsZmax_altrF   rT   rA   r@   rB   r	   r	   r   print_spelling_suggestions   s.   


z*CommandNotFound.print_spelling_suggestionsc                 C   s   t tdd|i | jd d S )Nz;Command '%(command)s' not found, but can be installed with:rF   rj   )rx   rn   r   rE   r	   r	   r   _print_exact_header   s
   
z#CommandNotFound._print_exact_headerc                 C   s   |  | |d }| jdkrtd|d  | jd n | jr)td|d  | jd ntd|d  | jd ttd | j  d S )Nr   zsnap install %srj   zsudo snap install %sPlease ask your administrator.)r   r7   rx   r   r(   rn   r   )r8   rF   r?   rS   rT   r	   r	   r   advice_single_snap_package   s   

z*CommandNotFound.advice_single_snap_packagec                 C   s   |  | | jdkr!td|d d  | jd | |d d  nE| jr;td|d d  | jd | |d d  n+td|d d  | jd ttd |d d | jvrfttd|d d  | jd | j  d S )Nr   zapt install %srj   zsudo apt install %sr   r   z1You will have to enable the component called '%s')	r   r7   rx   r   r}   r(   rn   r-   r   )r8   rF   r?   rS   r	   r	   r   advice_single_deb_package   s   

z)CommandNotFound.advice_single_deb_packagec                 C   s   | j dkr
| jr
dS dS )Nr   ri   rW   )r7   r(   )r8   r	   r	   r   r!      s   zCommandNotFound.sudoc                 C   s  |  | tdd || D }t|D ]W\}}d}|d r5|dkr/t|dkr/d|d  }nd|d  }|d | jv rNtd	|  ||d |f | jd
 qtd	|  ||d |f d td|d   d | jd
 q| j	dkr}| j
s}ttd| jd
 | j  d S )Nc                 S      g | ]}t |d  qS r   r   r
   r   r	   r	   r   r          z<CommandNotFound.advice_multi_deb_package.<locals>.<listcomp>rW   r   r     # version %s, or  # version %sr   z%sapt install %-*s%srj   z (z-You will have to enable component called '%s')z.Ask your administrator to install one of them.)r   max	enumerater   r-   rx   r!   r   rn   r7   r(   r   )r8   rF   r?   rS   padr   r@   rA   r	   r	   r   advice_multi_deb_package   s   
$<z(CommandNotFound.advice_multi_deb_packagec                 C   s   |  | tdd || D }t|D ]2\}}d}|d r5|dkr/t|dkr/d|d  }nd|d  }td|  ||d |f | jd	 qttd
| jd	 | j  d S )Nc                 S   r   r   r   r   r	   r	   r   r     r   z>CommandNotFound.advice_multi_snap_packages.<locals>.<listcomp>rW   r   r   r   r   %ssnap install %-*s%srj   r   )	r   r   r   r   rx   r!   r   rn   r   )r8   rF   r?   rS   r   r   rT   rA   r	   r	   r   advice_multi_snap_packages  s   
$z*CommandNotFound.advice_multi_snap_packagesc           	      C   s  |  | tdd || D }t|D ],\}}d}|d r/|dkr)d|d  }nd|d  }td|  ||d |f | jd	 q|D ]}d}|d
 rQd|d
  }td|  ||d |f | jd	 qCt|d
kryttd|d d  | jd	 n	ttd| jd	 | j  d S )Nc                 S   r   r   r   r   r	   r	   r   r     r   z?CommandNotFound.advice_multi_mixed_packages.<locals>.<listcomp>rW   r   r   r   r   r   rj   r   z%sapt  install %-*s%sz+See 'snap info %s' for additional versions.r   )	r   r   r   rx   r!   r   r   rn   r   )	r8   rF   r?   rS   r   r   rT   rA   r@   r	   r	   r   advice_multi_mixed_packages  s$   
$$ z+CommandNotFound.advice_multi_mixed_packagesFc           
         sP  dd  t | jkrdS dr#tjr tjg}ng }n fdd| jD }|r|st |dkrNtt	dtj
|d	 d
 | jd n tt	ddi | jd |D ]}tdtj
| | jd q]tt|ttddd }t |d	krtt	dd
| | jd dd
|v rtt	d| jd dS tjdstjdsdS |  v rdS dkrtt	d | jd tt	dd | jd tt	dd | jd dS | }| \}}td||f  t |d	kr t |d	kr | }	t |	d	kst |d	kr| |	| nmt |d	kr6t |dkr6| || nWt |d	krLt |d	krL| || nAt |dkrbt |d	krb| || n+t |dkrxt |d	krx| || nt |d	krt |d	kr| || t |d	kpt |d	kpt |d	kpt |	d	kS )z7 give advice where to find the given command to stderr c                 S   s.   t jt j| |ot jt j| | S )z? helper that returns if a command is found in the given prefix )r.   r/   r0   r[   isdir)prefixrF   r	   r	   r   
_in_prefix'  s   z*CommandNotFound.advise.<locals>._in_prefixF/c                    s   g | ]	} |r|qS r	   r	   )r
   r   r   rF   r	   r   r   5  s    z*CommandNotFound.advise.<locals>.<listcomp>r   z1Command '%(command)s' is available in '%(place)s'r   )rF   Zplacerj   z:Command '%(command)s' is available in the following placesrF   z * %sPATHrW   :z_The command could not be located because '%s' is not included in the PATH environment variable.ZsbinzfThis is most likely caused by the lack of administrative privileges associated with your user account.z/usr/bin/aptz/usr/bin/aptitudepythonr   r   )python3r   rW   )r   zpython-is-python3rW   Tzgot debs: %s snaps: %s)r   r;   
startswithr.   r/   r0   dirnameprefixesrx   rn   r[   r   listr   r\   r{   r^   r<   rU   rI   rJ   rC   r   r   r   r   r   r   )
r8   rF   Zignore_installedr   r   missingr?   rS   r   r   r	   r   r   advise%  sl   
,"




zCommandNotFound.adviseN)r+   )r:   )r~   )F)__name__
__module____qualname__Zprograms_dirr;   r   rH   ro   rG   r   r9   rC   r<   rU   r^   r,   r}   r   r   r   r   r!   r   r   r   r   r	   r	   r	   r   r)   8   s,    




r)   )Z
__future__r   r   r   r$   rN   rI   r.   os.pathr"   ro   rM   ZCommandNotFound.db.dbr   rp   Z_gettext_methodgetattrtranslationrn   r    r(   r1   objectr)   r	   r	   r	   r   <module>   s&   

