U
    Íø.eg  ã                   @   s´   d Z ddlZddlmZmZmZmZmZmZm	Z	m
Z
mZ ddlmZmZ dZedƒjZG dd„ deƒZeeƒZdd
d„Zddd„Zddd„Zddd„Zdd„ Zddd„Zdd„ ZdS )zZ
    billiard._win
    ~~~~~~~~~~~~~

    Windows utilities to terminate process groups.

é    N)	ÚbyrefÚsizeofÚwindllÚ	StructureÚWinErrorÚPOINTERÚc_size_tÚc_charÚc_void_p)ÚDWORDÚLONGé   éÿÿÿÿc                   @   sP   e Zd Zdefdefdefdefdefdefdefdefd	efd
ed fg
ZdS )ÚPROCESSENTRY32ÚdwSizeZcntUsageÚth32ProcessIDZth32DefaultHeapIDZth32ModuleIDZ
cntThreadsÚth32ParentProcessIDZpcPriClassBaseÚdwFlagsZ	szExeFilei  N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r	   Ú_fields_© r   r   ú1/tmp/pip-unpacked-wheel-39f38r4u/billiard/_win.pyr      s   
ör   é   c                 C   s    t j | |¡}|tkrtƒ ‚|S ©N)r   Úkernel32ÚCreateToolhelp32SnapshotÚINVALID_HANDLE_VALUEr   )r   r   Ú	hSnapshotr   r   r   r   +   s    ÿr   c                 C   s   t tjj| |ƒS r   )Ú_Process32nr   r   ÚProcess32First©r   Úper   r   r   r!   3   s    r!   c                 C   s   t tjj| |ƒS r   )r    r   r   ÚProcess32Nextr"   r   r   r   r$   7   s    r$   c                 C   sF   |d krt ƒ }tt ƒ|_| |t|ƒƒ}|sBtj ¡ tkr<d S tƒ ‚|S r   )	r   r   r   r   r   r   ZGetLastErrorÚERROR_NO_MORE_FILESr   )Zfunr   r#   Úsuccessr   r   r   r    ;   s    
r    c                  C   s8   t ƒ } i }t| ƒ}|r4|jr(|j||j< t| |ƒ}q|S )z}Return a dictionary with all processes pids as keys and their
       parents as value. Ignore processes with no parents.
    )r   r!   r   r   r$   )ÚhÚparentsr#   r   r   r   Úget_all_processes_pidsG   s    r)   Tc                 C   sj   t ƒ }t| ¡ ƒ}| h}| ¡ }|D ]}|| |kr$| |¡ q$||krJqT| ¡ }q|sb| | ¡ t|ƒS )z1Return a list with all the pids of a process tree)r)   ÚlistÚkeysÚcopyÚaddÚremove)ÚpidZinclude_parentr(   Zall_pidsZpidsZpids_newÚ_pidr   r   r   Úget_processtree_pidsV   s    

r1   c                 C   s"   t | ƒ}|D ]}t ||¡ qdS )z&Kill a process and all its descendantsN)r1   ÚosÚkill)r/   ÚsignumZfamily_pidsr0   r   r   r   Úkill_processtreem   s    r5   )r   r   )N)N)N)T)Ú__doc__r2   Úctypesr   r   r   r   r   r   r   r	   r
   Zctypes.wintypesr   r   r%   Úvaluer   r   ZLPPROCESSENTRY32r   r!   r$   r    r)   r1   r5   r   r   r   r   Ú<module>   s   ,





