
    Vh                        d dl mZmZmZ eZdZdZdZd dl	Z	d dl
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 d dlmZ  ej.                  d      5  d dlZddd        G d de      Z ej6                  e	j8                         G d de             Z G d de      Z G d de      Z  G d de      Z!d Z" G d de      Z#d Z$e%dk(  r e$        yy# 1 sw Y   ~xY w)    )absolute_importdivisionprint_functiona  
module: pids
description: "Retrieves a list of PIDs of given process name in Ansible controller/controlled machines. Returns an empty list
  if no process in that name exists."
short_description: Retrieves process IDs list if the process is running otherwise return empty list
author:
  - Saranya Sridharan (@saranyasridharan)
requirements:
  - psutil(python module)
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  name:
    description: The name of the process(es) you want to get PID(s) for.
    type: str
  pattern:
    description: The pattern (regular expression) to match the process(es) you want to get PID(s) for.
    type: str
    version_added: 3.0.0
  ignore_case:
    description: Ignore case in pattern if using the O(pattern) option.
    type: bool
    default: false
    version_added: 3.0.0
a  
# Pass the process name
- name: Getting process IDs of the process
  community.general.pids:
    name: python
  register: pids_of_python

- name: Printing the process IDs obtained
  ansible.builtin.debug:
    msg: "PIDS of python:{{pids_of_python.pids|join(',')}}"

- name: Getting process IDs of processes matching pattern
  community.general.pids:
    pattern: python(2(\.7)?|3(\.6)?)?\s+myapp\.py
  register: myapp_pids
z
pids:
  description: Process IDs of the given process.
  returned: list of none, one, or more process IDs
  type: list
  sample: [100, 200]
N)basename)six)AnsibleModule)deps)	to_native)LooseVersionpsutilc                       e Zd Zy)PSAdapterErrorN)__name__
__module____qualname__     j/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/pids.pyr   r   Q   s    r   r   c                   |    e Zd ZdZdZd Zed        Zd Zd Z	d Z
d Zeej                  d	               Zd
 Zd Zy)	PSAdapter)namecmdline)r   exer   c                     || _         y N)_psutil)selfr   s     r   __init__zPSAdapter.__init__Z   s	    r   c                     t        | j                        }|t        d      k  rt        |       S |t        d      k  rt        |       S t	        |       S )Nz2.0.0z5.3.0)r   __version__PSAdapter100PSAdapter200PSAdapter530)r   versions     r   from_packagezPSAdapter.from_package]   sM    v112\'**''|G,,''''r   c                      | j                   | j                   D cg c]!  }| j                  ||      s|j                  # c}S c c}w r   )_process_iter
NAME_ATTRS	_has_namepid)r   r   ps      r   get_pids_by_namezPSAdapter.get_pids_by_nameg   s:    1t114??C_!t~~VWY]G^___s
   AAc                 6    | j                   j                         S r   r   process_iterr   attrss     r   r'   zPSAdapter._process_iterj   s    ||((**r   c                      | j                   |g| j                   }t        |d   |      xs |d   xr t        |d   d   |      S )Nr   r   r   )_get_proc_attributesr(   compare_lower)r   procr   
attributess       r   r)   zPSAdapter._has_namem   sW    .T..tFdooF
j0$7 X9%W-
98Ma8PRV*W	Yr   c                 N    |D ci c]  }|| j                  ||       c}S c c}w r   )_get_attribute_from_proc)r   r5   r6   	attributes       r   r3   zPSAdapter._get_proc_attributesr   s(    [efi	488yIIfffs   "c                      y r   r   r5   r9   s     r   r8   z"PSAdapter._get_attribute_from_procu   s     	r   c           	      Z   d}|r|t         j                  z  }	 t        j                  ||      } | j                  | j                   D cg c]!  }| j                  ||      s|j                  # c}S # t         j                  $ r}t	        d|dt        |            d }~ww xY wc c}w )Nr   'z%' is not a valid regular expression: )
reIcompileerrorr   r
   r'   PATTERN_ATTRS_matches_regexr*   )r   patternignore_caseflagsregexer+   s          r   get_pids_by_patternzPSAdapter.get_pids_by_patternz   s    RTTME	iJJw.E  2t1143E3EFh!$J]J]^_afJghh xx 	i QXZcdeZf!ghh	i is#   A3 
B("B(3B%B  B%c                 R    | j                   |g| j                   }|j                  t        |d               }|d   xr& |j                  t	        t        |d                     }|d   xr, |j                  t        dj                  |d                     }t        |||g      S )Nr   r   r    )r3   rB   searchr
   r   joinany)r   r5   rG   r6   matches_namematches_exematches_cmds          r   rC   zPSAdapter._matches_regex   s    .T..tId6H6HI
||Ij.@$AB '`ELL)JW\L]B^9_,` +hYsxxPZ[dPeGf=g0hL+{;<<r   N)r   r   r   r(   rB   r   staticmethodr%   r,   r'   r)   r3   abcabstractmethodr8   rI   rC   r   r   r   r   r   U   si    $J.M ( (`+Y
g   
i=r   r   c                   .     e Zd Z fdZed        Z xZS )r!   c                 ,    t         t        |   |       y r   )superr!   r   r   r   	__class__s     r   r   zPSAdapter100.__init__       lD*62r   c                     t        | |      S r   getattrr;   s     r   r8   z%PSAdapter100._get_attribute_from_proc   s    tY''r   r   r   r   r   rR   r8   __classcell__rY   s   @r   r!   r!      s    3 ( (r   r!   c                   .     e Zd Z fdZed        Z xZS )r"   c                 ,    t         t        |   |       y r   )rW   r"   r   rX   s     r   r   zPSAdapter200.__init__   rZ   r   c                 (    t        | |      } |       S r   r\   )r5   r9   methods      r   r8   z%PSAdapter200._get_attribute_from_proc   s    y)xr   r^   r`   s   @r   r"   r"      s    3  r   r"   c                   4     e Zd Z fdZd Zed        Z xZS )r#   c                 ,    t         t        |   |       y r   )rW   r#   r   rX   s     r   r   zPSAdapter530.__init__   rZ   r   c                 :    | j                   j                  |      S )N)r1   r.   r0   s     r   r'   zPSAdapter530._process_iter   s    ||((u(55r   c                      | j                   |   S r   )infor;   s     r   r8   z%PSAdapter530._get_attribute_from_proc   s    yy##r   )r   r   r   r   r'   rR   r8   r_   r`   s   @r   r#   r#      s!    36 $ $r   r#   c                 V    | || |k(  S | j                         |j                         k(  S r   )lower)abs     r   r4   r4      s+    yAIAv779	!!r   c                   (    e Zd Zd Zd Zed        Zy)Pidsc                     t        j                  |       t        j                  t              | _        || _        |j                  d   | _        |j                  d   | _	        |j                  d   | _
        g | _        y )Nr   rD   rE   )r	   validater   r%   r   _ps_moduleparams_name_pattern_ignore_case_pids)r   modules     r   r   zPids.__init__   s_    f))&1]]6*
i0"MM-8
r   c                    | j                   r+| j                  j                  | j                         | _        n6	 | j                  j	                  | j
                  | j                        | _         | j                  j                  di | j                  S # t        $ r/}| j                  j                  t        |             Y d }~Yd }~ww xY w)N)msgr   )ru   rr   r,   rx   rI   rv   rw   r   rs   	fail_jsonr
   	exit_jsonresult)r   rH   s     r   executezPids.execute   s    ::224::>DJ9!XX99$--IZIZ[
 &t||%%444 " 9&&9Q<&889s   5B 	C%CCc                     d| j                   iS )Npids)rx   )r   s    r   r~   zPids.result   s     DJJ
 	
r   N)r   r   r   r   r   propertyr~   r   r   r   ro   ro      s     	5 
 
r   ro   c            
          t        t        t        d      t        d      t        dd            dgdgd	      } t        |       j                          y )
Nstr)typeboolF)r   default)r   rD   rE   )r   rD   T)argument_specrequired_one_ofmutually_exclusivesupports_check_mode)r   dictro   r   )ry   s    r   mainr      sY    5!e$&%8
  
  
 !F 	Lr   __main__)&
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESRETURNrS   r>   os.pathr   ansible.module_utilsr   ansible.module_utils.basicr   :ansible_collections.community.general.plugins.module_utilsr	   +ansible.module_utils.common.text.convertersr
   Bansible_collections.community.general.plugins.module_utils.versionr   declarer   	Exceptionr   add_metaclassABCMetaobjectr   r!   r"   r#   r4   ro   r   r   r   r   r   <module>r      s    C B>"
  	  $ 4 K A [T\\( 	Y 	 3;;7= 7=  7=t(9 (9 	$9 	$"
6 
@& zF { s   CC