
    7jo$                     D   d Z ddlmZ ddlZddlmZmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZ dd	lmZ  G d
 de      Z e
 e             j$                  Zei di di di di di di dddidddii	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!d# Z"d$ Z#d% Z$d'd&Z%y)(zCheck for specific programs.
    )EnumN)DEVNULLTimeoutExpired)TemporaryDirectory)Path)cli)
submodules)	find_makec                       e Zd ZdZdZdZy)CheckStatus         N)__name__
__module____qualname__OKWARNINGERROR     M/home/hanthebot/chat/keyboard/qmk_firmware/lib/python/qmk/cli/doctor/check.pyr   r      s    	
BGEr   r   gitdos2unixdiffdfu-programmeravrdudedfu-utilavr-gccversion_argz-dumpversionarm-none-eabi-gccc                      t         t           d   j                  d      d   } | j                         d   }t        j                  j                  dt        |       t        j                  S )Noutput
r   r   zFound %s version %s)ESSENTIAL_BINARIES
WHICH_MAKEsplitr   loginfor   r   	last_lineversion_numbers     r   _check_make_versionr-   '   sQ    ":.x8>>tDQGI__&q)NGGLL&
NC>>r   c                      t         d   d   j                  d      d   } | j                         d   }t        j                  j	                  d|       t
        j                  S )Nr   r#   r$   r   r   zFound git version %sr%   r'   r   r(   r)   r   r   r*   s     r   _check_git_versionr0   /   sO    "5)(399$?BI__&q)NGGLL'8>>r   c                      t         d   d   j                  d      d   } | j                         d   }t        j                  j	                  d|       t
        j                  S )Nr   r#   r$   r   r   zFound dos2unix version %sr/   r*   s     r   _check_dos2unix_versionr2   7   sO    ":.x8>>tDQGI__&q)NGGLL,n=>>r   c                      t         d   d   j                  d      d   } d| v r| }n| j                         d   }t        j                  j	                  d|       t
        j                  S )Nr   r#   r$   r   z
Apple diffr   zFound diff version %sr/   r*   s     r   _check_diff_versionr4   ?   s\    "6*84::4@CIy ""*1-GGLL(.9>>r   c                      t         d   d   j                         } t        j                  j	                  d|        t               S )zRReturns True if the arm-none-eabi-gcc version is not known to cause problems.
    r!   r#   z"Found arm-none-eabi-gcc version %s)r%   stripr   r(   r)   _check_arm_gcc_installationr,   s    r   _check_arm_gcc_versionr9   J   s;     ((;<XFLLNNGGLL5~F '((r   c                     t               5 } t        |       dz  }t        |       dz  }|j                  dd       dddd	d
ddddt        |      t        |      g}t	        j
                  |dd      }|j                  dk(  r t        j                  j                  d       nvt        j                  j                  d|j                          t        j                  j                  ddj                  |              t        j                  cddd       S dt        |      g}t	        j
                  |dd      }|j                  dk(  r t        j                  j                  d       nvt        j                  j                  d|j                          t        j                  j                  ddj                  |              t        j                  cddd       S t        j                  cddd       S # 1 sw Y   yxY w)zUReturns OK if the arm-none-eabi-gcc is fully installed and can produce binaries.
    test.ctest.elfz`#include <newlib.h>
int main() { return __NEWLIB__ * __NEWLIB_MINOR__ * __NEWLIB_PATCHLEVEL__; }utf-8encodingr!   z-mcpu=cortex-m0z-mthumbz-mno-thumb-interworkz--specs=nosys.specsz--specs=nano.specs-xc-oNstdoutstderrr   z-Successfully compiled using arm-none-eabi-gcczGFailed to compile a simple program with arm-none-eabi-gcc, return code 	Command:  zarm-none-eabi-sizezCSuccessfully tested arm-none-eabi-binutils using arm-none-eabi-sizezZFailed to execute arm-none-eabi-size, perhaps corrupt arm-none-eabi-binutils, return code r   r   
write_textstrr   run
returncoder(   r)   errorjoinr   r   r   temp_dirtemp_intemp_outargsresults        r   r7   r7   T   s    
	 x.8+>J.~  JQ  	R#%6	CY[p  sG  IM  OR  TX  Z]  ^f  Zg  il  mt  iu  vd48!GGLLHIGGMMcdjduducvwxGGMMIchhtn%567$$  %c(m4d48!GGLL^_GGMMvw}  xI  xI  wJ  K  LGGMMIchhtn%567$$- 0 ~~1  s   DG:C G:!G::Hc                      t         d   d   j                         } t        j                  j	                  d|        t               S )zHReturns True if the avr-gcc version is not known to cause problems.
    r   r#   zFound avr-gcc version %s)r%   r6   r   r(   r)   _check_avr_gcc_installationr8   s    r   _check_avr_gcc_versionrW   r   s:     (	28<BBDNGGLL+^< '((r   c            
         t               5 } t        |       dz  }t        |       dz  }|j                  dd       dddd	d
t        |      t        |      g}t	        j
                  |dd      }|j                  dk(  r t        j                  j                  d       nvt        j                  j                  d|j                          t        j                  j                  ddj                  |              t        j                  cddd       S dt        |      g}t	        j
                  |dd      }|j                  dk(  r t        j                  j                  d       nvt        j                  j                  d|j                          t        j                  j                  ddj                  |              t        j                  cddd       S t        j                  cddd       S # 1 sw Y   yxY w)zKReturns OK if the avr-gcc is fully installed and can produce binaries.
    r;   r<   zint main() { return 0; }r=   r>   r   z-mmcu=atmega32u4r@   rA   rB   NrC   r   z#Successfully compiled using avr-gccz=Failed to compile a simple program with avr-gcc, return code rF   rG   zavr-sizez/Successfully tested avr-binutils using avr-sizezFFailed to execute avr-size, perhaps corrupt avr-binutils, return code rH   rO   s        r   rV   rV   |   s    
	 x.8+>J.5H-tS$HsSZ|\d48!GGLL>?GGMMYZ`ZkZkYlmnGGMMIchhtn%567$$  CM*d48!GGLLJKGGMMbcictctbuvwGGMMIchhtn%567$$- 0 ~~1  s   C>G6C G6G66G?c                      t         d   d   j                  d      d   } | j                         d   d d }t        j                  j	                  d|       t
        j                  S )Nr   r#   r$   r   zFound avrdude version %sr/   r*   s     r   _check_avrdude_versionr\      sV    "9-h7==dCBGI__&q)#2.NGGLL+^<>>r   c                      t         d   d   j                  d      d   } | j                         d   }t        j                  j	                  d|       t
        j                  S )Nr   r#   r$   r   r   zFound dfu-util version %sr/   
first_liner,   s     r   _check_dfu_util_versionr`      sQ    #J/9??EaHJ%%'*NGGLL,n=>>r   c                      t         d   d   j                  d      d   } | j                         d   }t        j                  j	                  d|       t
        j                  S )Nr   r#   r$   r   r   zFound dfu-programmer version %sr/   r^   s     r   _check_dfu_programmer_versionrb      sR    #$45h?EEdKANJ%%'*NGGLL2NC>>r   c                      t         j                  } g }t        t              D ]K  }	 t	        |      s"t         j
                  } |j                  |       nt        |      st         j
                  } M |rJt        |      dk(  rdnd}t        j                  j                  ddj                  |      z   d| dz          | S # t        $ rF t        j                  j                  d|       | t         j
                  k7  rt         j                  } Y w xY w)	z8Iterates through ESSENTIAL_BINARIES and tests them.
    zTimeout checking %sr   zits locationztheir locationsz{fg_red}z, z may need to be installed, or z added to your path.)r   r   sortedr%   
is_in_pathr   appendis_executabler   r   r(   debugr   lenrM   rN   )okmissing_from_pathbinarylocation_nouns       r   check_binariesrn      s     
B+, 
)		)f% &&!((0"6* &&
) *-.?*@A*EK\j499->#??Dbcpbq  rF  CG  G  	HI  	)GGMM/8[&&& ((	)s   AB>>ADDc                  v   t         t        dt        dt        dt        dt
        dt        dt        dt        dt        i	} g }t        t              D ]h  }d	t        |   vr@t        j                  j                  d
|       |j                  t         j"                         N| |   }|j                   |              j |S )z-Check the versions of ESSENTIAL_BINARIES
    r   r   r   r!   r   r   r   r   r#   zUnknown version for %s)r&   r-   r0   r2   r4   r9   rW   r\   r`   rb   rd   r%   r   r(   warningrf   r   r   )checksversionsrl   checks       r   check_binary_versionsrt      s     	'!+#3))+7
F H+, !-f55GGOO4f=OOK//0v ! Or   c                      t        j                         j                         D ]0  } | d   t        j                  c S | d   r t        j
                  c S  t        j                  S )zPIterates through all submodules to make sure they're cloned and up to date.
    status)r	   rv   valuesr   r   r   r   )	submodules    r   check_submodulesry      s\      &&(//1 '	X&$$$8$&&&	' >>r   c                 p    t        j                  |       !t        j                  j	                  d|        yy)z2Returns True if command is found in the path.
    z#{fg_red}Can't find %s in your path.FT)shutilwhichr   r(   rM   )commands    r   re   re      s,     ||G$;WEr   c                 @   t         |    j                  dd      }t        j                  | |gdt        d      }|j
                  t         |    d<   |j                  dv r!t        j                  j                  d|        yt        j                  j                  d	| |       y
)z-Returns True if command can be executed.
    r    z	--versionT   )combined_outputstdintimeoutr#   )r   r   zFound {fg_cyan}%sz{fg_red}Can't run `%s %s`F)
r%   getr   rK   r   rD   rL   r(   rh   rM   )r}   r    rs   s      r   rg   rg      s     %W-11-MKGGWk*DYZ[E,1LLw)6!)73GGMM-wDr   c           	      F   i }	 t        |       5 }|D ]`  }d|v st        t        j                  |j	                  dd            \  }}|j                  d      r|j                  d      r|dd }|||<   b 	 ddd       |S # 1 sw Y   |S xY w# t        t        f$ r Y |S w xY w)zParse release info to dict
    =r   "r[   N)	openmaprJ   r6   r'   
startswithendswithPermissionErrorFileNotFoundError)fileretflinekeyvalues         r   release_infor     s     C	$Z 	%1 %$;!$SYY

30B!CJC'',1D %a$CH%	% J	% J ./ Js4   B 
B AB 6B  B
B 
B B B )z/etc/os-release)&__doc__enumr   r{   
subprocessr   r   tempfiler   pathlibr   milcr   qmkr	   qmk.commandsr
   r   namer&   r%   r-   r0   r2   r4   r9   r7   rW   rV   r\   r`   rb   rn   rt   ry   re   rg   r   r   r   r   <module>r      s      . '    "$  )+##
 	2
Bbr~ ~ ")<)<26	"r   