
    Vh!                     ~    d dl mZmZmZ eZdZdZd dlZd dl	Z	d dl
Z
d dlmZ  G d de      Zd Zed	k(  r e        yy)
    )absolute_importdivisionprint_functionac  
module: cargo
short_description: Manage Rust packages with cargo
version_added: 4.3.0
description:
  - Manage Rust packages with cargo.
author: "Radek Sprta (@radek-sprta)"
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  executable:
    description:
      - Path to the C(cargo) installed in the system.
      - If not specified, the module will look C(cargo) in E(PATH).
    type: path
    version_added: 7.5.0
  name:
    description:
      - The name of a Rust package to install.
    type: list
    elements: str
    required: true
  path:
    description: The base path where to install the Rust packages. Cargo automatically appends V(/bin). In other words, V(/usr/local)
      will become V(/usr/local/bin).
    type: path
  version:
    description: The version to install. If O(name) contains multiple values, the module will try to install all of them in
      this version.
    type: str
    required: false
  locked:
    description:
      - Install with locked dependencies.
      - This is only used when installing packages.
    required: false
    type: bool
    default: false
    version_added: 7.5.0
  state:
    description:
      - The state of the Rust package.
    required: false
    type: str
    default: present
    choices: ["present", "absent", "latest"]
  directory:
    description:
      - Path to the source directory to install the Rust package from.
      - This is only used when installing packages.
    type: path
    required: false
    version_added: 9.1.0
requirements:
  - cargo installed
aN  
- name: Install "ludusavi" Rust package
  community.general.cargo:
    name: ludusavi

- name: Install "ludusavi" Rust package with locked dependencies
  community.general.cargo:
    name: ludusavi
    locked: true

- name: Install "ludusavi" Rust package in version 0.10.0
  community.general.cargo:
    name: ludusavi
    version: '0.10.0'

- name: Install "ludusavi" Rust package to global location
  community.general.cargo:
    name: ludusavi
    path: /usr/local

- name: Remove "ludusavi" Rust package
  community.general.cargo:
    name: ludusavi
    state: absent

- name: Update "ludusavi" Rust package its latest version
  community.general.cargo:
    name: ludusavi
    state: latest

- name: Install "ludusavi" Rust package from source directory
  community.general.cargo:
    name: ludusavi
    directory: /path/to/ludusavi/source
N)AnsibleModulec                   x    e Zd Zd Zed        Zej                  d        Z	 ddZd ZddZ	d Z
d	 Zd
 ZddZy)Cargoc                     || _         |d   xs |j                  dd      g| _        |d   | _        |d   | _        |d   | _        |d   | _        |d   | _        |d	   | _        y )
N
executablecargoTnamepathstateversionlocked	directory)	moduleget_bin_pathr
   r   r   r   r   r   r   )selfr   kwargss      k/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/cargo.py__init__zCargo.__init__w   sp    !,/U63F3FwPT3UV6N	6N	G_
i(X&,    c                     | j                   S N)_path)r   s    r   r   z
Cargo.path   s    zzr   c                     |>t         j                  j                  |      s| j                  j	                  d|z         || _        y )NzPath %s is not a directorymsg)osr   isdirr   	fail_jsonr   )r   r   s     r   r   z
Cargo.path   s9    BGGMM$$7KK!!&BT&I!J
r   c                     | j                   j                  r| j                   j                  r6|r4| j                  |z   }| j                   j                  ||      \  }}}||fS y)N)check_rc) r$   )r   
check_moder
   run_command)	r   argsrun_in_check_moder#   add_package_namecmdrcouterrs	            r   _execzCargo._exec   sY     {{%%$++*@*@EV//D(C;;2232JLBS8Or   c                 r   ddg}| j                   r,|j                  d       |j                  | j                          | j                  |ddd      \  }}t        j                  d      }i }|j                         D ]9  }|j                  |      }|s|j                  d      ||j                  d      <   ; |S )	Ninstallz--list--rootTFz^([\w\-]+) v(\S+).*:$      )r   appendr.   recompile
splitlinesmatchgroup)r   r*   datadummypackage_regex	installedlinepackage_infos           r   get_installedzCargo.get_installed   s    (#99JJx JJtyy!jjdE59e

#;<	OO% 	ID(..t4L3?3E3Ea3H	,,,Q/0	I
 r   Nc                    dg}|j                  |xs | j                         | j                  r|j                  d       | j                  r,|j                  d       |j                  | j                         | j
                  r,|j                  d       |j                  | j
                         | j                  r,|j                  d       |j                  | j                         | j                  |      S )Nr0   z--lockedr1   z	--versionz--path)extendr   r   r4   r   r   r   r.   r   packagesr*   s      r   r0   zCargo.install   s    k

8(tyy);;JJz"99JJx JJtyy!<<JJ{#JJt||$>>JJx JJt~~&zz#r   c                     | j                         j                  |      }| j                  s| j                  |      n| j	                  |      }||k7  S r   )r@   getr   get_latest_published_versionget_source_directory_version)r   r   installed_versionlatest_versions       r   is_outdatedzCargo.is_outdated   sU     ..044T: >> --d32248 	
 !N22r   c                     d|ddg}| j                  |ddd      \  }}t        j                  d|      }|s| j                  j	                  d|z         |j                  d	      S )
Nsearchz--limit1TFz"(.+)"z)No published version for package %s foundr   r3   )r.   r5   rM   r   r!   r9   )r   r   r*   r:   r;   r8   s         r   rG   z"Cargo.get_latest_published_version   sj    y#.jjdE59e		)T*KK!!?$F "  {{1~r   c           
      p   dddddt         j                  j                  | j                  d      g}| j	                  |ddd      \  }}t        j                  |      }t        fd	|d
   D        d       }|s6| j                  j                  dd|d
   D cg c]  }|d   	 c}       |d   S c c}w )Nmetadataz--format-versionrN   z	--no-depsz--manifest-pathz
Cargo.tomlTFc              3   4   K   | ]  }|d    k(  s|  yw)r   N ).0packager   s     r   	<genexpr>z5Cargo.get_source_directory_version.<locals>.<genexpr>   s     TGFOt<SWTs   rD   zPackage z not defined in source, found: r   r   r   )
r   r   joinr   r.   jsonloadsnextr   r!   )r   r   r*   r:   r;   manifestrT   xs    `      r   rH   z"Cargo.get_source_directory_version   s    GGLL6
 jjdE59e::d#THZ$8T
 KK!!Xj-AB!F)BD "  y!! Cs   B3c                 h    dg}|j                  |xs | j                         | j                  |      S )N	uninstall)rB   r   r.   rC   s      r   r]   zCargo.uninstall   s,    m

8(tyy)zz#r   )FTTr   )__name__
__module____qualname__r   propertyr   setterr.   r@   r0   rK   rG   rH   r]   rR   r   r   r   r   v   s]    -   
[[  NR" 3	".r   r   c                     t        t        d d      t        ddd      t        d d      t        dg d	      t        d d      t        d
d      t        d d            } t        | d      }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|s|j                  d       |1t        j
                  j                  |      s|j                  d       t        dddd      |_        t        |fi |j                  }d\  }}}	|j                         }
|dk(  r6|D cg c]  }||
vs
|r
||
|   k7  r| }}|rd}|j                  |      \  }}	no|dk(  r>|D cg c]  }||
vs|j                  |      s| }}|rCd}|j                  |      \  }}	n,|D cg c]	  }||
v s| }}|rd}|j                  |      \  }}	|j                  |||	       y c c}w c c}w c c}w )Nr   )defaulttypeTliststr)requiredre   elementspresent)rj   absentlatest)rd   choicesFbool)r
   r   r   r   r   r   r   )argument_specsupports_check_moder   r   r   r   zPackage name must be specifiedr   zSource directory does not existC)LANGLC_ALLLC_MESSAGESLC_CTYPE)FNNrl   )changedstdoutstderr)dictr   paramsr!   r   r   r    run_command_environ_updater   r@   r0   rK   r]   	exit_json)arg_specr   r   r   r   r   r   rv   r,   r-   installed_packagesn
to_install	to_updateto_uninstalls                  r   mainr      s)   624fu=$V,9.MNT.E/t&1H tLF== DMM'"EmmI&Gk*I=>RWW]]9%=>? )-#)F% &*FMM*E)GS#,,.	 
++G'9!'<< 

 
 G}}Z0HC	(	
q(::e>O>OPQ>RA
	 
 G}}Y/HC#'Ca10B+BCCG|4HC
WS=-

 Ds   <G27G7G75	G<?G<__main__)
__future__r   r   r   re   __metaclass__DOCUMENTATIONEXAMPLESrW   r   r5   ansible.module_utils.basicr   objectr   r   r^   rR   r   r   <module>r      sZ    A @<|"H  	 	 4mF m`6>r zF r   