
    Vh&                         d dl mZmZmZ eZdZdZd dlZd dl	m
Z
 d Zd ZddZd	 Zd
 Zd Zd Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
module: macports
author: "Jimmy Tang (@jcftang)"
short_description: Package manager for MacPorts
description:
  - Manages MacPorts packages (ports).
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: none
  diff_mode:
    support: none
options:
  name:
    description:
      - A list of port names.
    aliases: ['port']
    type: list
    elements: str
  selfupdate:
    description:
      - Update Macports and the ports tree, either prior to installing ports or as a separate step.
      - Equivalent to running C(port selfupdate).
    aliases: ['update_cache', 'update_ports']
    default: false
    type: bool
  state:
    description:
      - Indicates the desired state of the port.
    choices: ['present', 'absent', 'active', 'inactive', 'installed', 'removed']
    default: present
    type: str
  upgrade:
    description:
      - Upgrade all outdated ports, either prior to installing ports or as a separate step.
      - Equivalent to running C(port upgrade outdated).
    default: false
    type: bool
  variant:
    description:
      - A port variant specification.
      - O(variant) is only supported with O(state=installed) and O(state=present).
    aliases: ['variants']
    type: str
a  
- name: Install the foo port
  community.general.macports:
    name: foo

- name: Install the universal, x11 variant of the foo port
  community.general.macports:
    name: foo
    variant: +universal+x11

- name: Install a list of ports
  community.general.macports:
    name: "{{ ports }}"
  vars:
    ports:
      - foo
      - foo-tools

- name: Update Macports and the ports tree, then upgrade all outdated ports
  community.general.macports:
    selfupdate: true
    upgrade: true

- name: Update Macports and the ports tree, then install the foo port
  community.general.macports:
    name: foo
    selfupdate: true

- name: Remove the foo port
  community.general.macports:
    name: foo
    state: absent

- name: Activate the foo port
  community.general.macports:
    name: foo
    state: active

- name: Deactivate the foo port
  community.general.macports:
    name: foo
    state: inactive
N)AnsibleModulec                     | j                  |ddg      \  }}}|dk(  r2t        d |j                  d      D              }|rd}d}nd}d	}||||fS | j                  d
||       y)z% Update Macports and the ports tree. z-v
selfupdater   c              3      K   | ]P  }|rLt        j                  d |j                               xs$ t        j                  d|j                                R yw)z$Total number of ports parsed:\s+[^0]zInstalling new Macports releaseN)researchstrip).0ss     n/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/macports.py	<genexpr>zselfupdate.<locals>.<genexpr>u   sP      
  II=qwwyI EII8!'')DE
s   AA
TzMacports updated successfullyFzMacports already up-to-datezFailed to update MacportsmsgstdoutstderrN)run_commandanysplit	fail_json)module	port_pathrcouterrupdatedchangedr   s           r   r   r   o   s     %%y$&EFLBS	Qw 
 YYt_
 
 G1CG/Cc3''8SQ    c                     | j                  |ddg      \  }}}|j                         dk(  r
d}d}||||fS |dk(  r
d}d}||||fS | j                  d	||
       y)z Upgrade outdated ports. upgradeoutdatedzNothing to upgrade.FzPorts already upgradedr   Tz$Outdated ports upgraded successfullyz Failed to upgrade outdated portsr   N)r   r   r   )r   r   r   r   r   r    r   s          r   r#   r#      s     %%y)Z&HILBS yy{++&c3''	q4c3''?TWXr!   c                     |dk(  rB| j                  |dd|g      \  }}}|dk(  r#|j                         j                  |dz         ryy|dk(  r$| j                  |dd|g      \  }}}|dk(  rd	|v ryyy
)z- Returns whether a port is installed or not. presentz-q	installedr    TFactivez(active)N)r   r   
startswith)r   r   namestater   r   r   s          r   
query_portr-      s     	))9dK*NOC7syy{--dSj9	(	))9dK*NOC7zS( 
r!   c                 2   d}|D ]^  }t        | ||      s| j                  |d|g      \  }}}	||z  }||	z  }t        | ||      r| j                  d|d|	||       |dz  }` |dkD  r| j                  dd|z  ||	       | j                  d
d||	       y)z, Uninstalls one or more ports if installed. r   	uninstallzFailed to remove : r      TzRemoved %s port(s)r    r   r   r   FzPort(s) already absentNr-   r   r   	exit_json)
r   r   portsr   r   remove_cportr   r   r   s
             r   remove_portsr8      s     H &)T2))9k4*HIC##fi.dC!HQW`fgA !|+?(+JSYbhi
U(@X^_r!   c                 4   d}|D ]_  }t        | ||      r| j                  |d||g      \  }}	}
||	z  }||
z  }t        | ||      s| j                  d|d|
||       |dz  }a |dkD  r| j                  dd|z  ||	       | j                  d
d||	       y)z6 Installs one or more ports if not already installed. r   installzFailed to install r0   r   r1   TzInstalled %s port(s)r2   FzPort(s) already presentNr3   )r   r   r5   variantr   r   	install_cr7   r   r   r   s              r   install_portsr=      s     I 
fi.))9iw*OPC##&)T2tS!IRXaghQ	
 1}+AY+OX^gmn
U(A&Y_`r!   c                    d}|D ]  }t        | ||      s| j                  d|z  ||       t        | ||d      r7| j                  |d|g      \  }}}	||z  }||	z  }t        | ||d      s| j                  d|d|	||       |d	z  } |dkD  r| j                  d
d|z  ||       | j                  dd||       y)z$ Activate a port if it is inactive. r   z*Failed to activate %s, port(s) not presentr   r)   r,   activatezFailed to activate r0   r1   TzActivated %s port(s)r2   FzPort(s) already activeNr-   r   r   r4   )
r   r   r5   r   r   
activate_cr7   r   r   r   s
             r   activate_portsrC      s     J &)T2!MQU!V_entufiX>))9j$*GHC##&)TBc!JSYbhia
  A~+AZ+PY_hno
U(@X^_r!   c                    d}|D ]  }t        | ||      s| j                  d|z  ||       t        | ||d      s7| j                  |d|g      \  }}}	||z  }||	z  }t        | ||d      r| j                  d|d|	||       |d	z  } |dkD  r| j                  d
d|z  ||       | j                  dd||       y)z$ Deactivate a port if it is active. r   z,Failed to deactivate %s, port(s) not presentr   r)   r?   
deactivatezFailed to deactivate r0   r1   TzDeactivated %s port(s)r2   FzPort(s) already inactiveNrA   )
r   r   r5   r   r   deactivated_cr7   r   r   r   s
             r   deactivate_portsrG      s     M &)T2!OSW!Xagpvw&)TB))9lD*IJC##fiX>$!LU[djk q+C}+U^dmst
U(B6Z`ar!   c                     t        t        t        dddg      t        ddgdd	      t        d
g d      t        dd      t        dgd d	                  } d}d}| j                  dddg      }| j                  }|d   r:t	        | |      \  }}}}||z  }||z  }|d   s|d   s| j                  ||||       |d   r5t        | |      \  }}}}||z  }||z  }|d   s| j                  ||||       |d   }	|d   }
|d   dv rt        | ||	|
||       y |d   dv rt        | ||	||       y |d   dk(  rt        | ||	||       y |d   dk(  rt        | ||	||       y y )Nliststrr7   )typeelementsaliasesupdate_cacheupdate_portsFbool)rM   defaultrK   r&   )r&   r'   absentremovedr)   inactive)rQ   choices)rQ   rK   variants)r+   r   r,   r#   r;   )argument_spec Tz/opt/local/binr   r+   r#   r2   r;   r,   )r&   r'   )rR   rS   r)   rT   )r   dictget_bin_pathparamsr   r4   r#   r=   r8   rC   rG   )r   r   r   r   pr    r   r   r   pkgsr;   s              r   mainr^     s   6EF8D^^$DeZ`ay2uvV4*t%H
F FF##FD3C2DEIA#-fi#@ #sC##&	Qy\W#fVT|#*69#= #sC##yW#fVTV9D	lGz--fiwG	
7,	,VYff=	
7x	vy$?	
7z	!D&&A 
"r!   __main__)r&   )
__future__r   r   r   rK   __metaclass__DOCUMENTATIONEXAMPLESr
   ansible.module_utils.basicr   r   r#   r-   r8   r=   rC   rG   r^   __name__ r!   r   <module>rg      so    A @-^*X 
 4R0Y$,`2a.`6b4.Bb zF r!   