
    Vhz%                         d dl mZmZmZ eZdZdZdZd dl	m
Z
 d dlZd dlmZ d dlZdZ G d	 d
e      Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
module: mas
short_description: Manage Mac App Store applications with mas-cli
description:
  - Installs, uninstalls and updates macOS applications from the Mac App Store using the C(mas-cli).
version_added: '0.2.0'
author:
  - Michael Heap (@mheap)
  - Lukas Bestle (@lukasbestle)
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  id:
    description:
      - The Mac App Store identifier of the app(s) you want to manage.
      - This can be found by running C(mas search APP_NAME) on your machine.
    type: list
    elements: int
  state:
    description:
      - Desired state of the app installation.
      - The V(absent) value requires root permissions, also see the examples.
    type: str
    choices:
      - absent
      - latest
      - present
    default: present
  upgrade_all:
    description:
      - Upgrade all installed Mac App Store apps.
    type: bool
    default: false
    aliases: ["upgrade"]
requirements:
  - macOS 10.11 or higher.
  - "mas-cli (U(https://github.com/mas-cli/mas)) 1.5.0+ available as C(mas) in the bin path"
  - The Apple ID to use already needs to be signed in to the Mac App Store (check with C(mas account)).
  - The feature of "checking if user is signed in" is disabled for anyone using macOS 12.0+.
  - Users need to sign in to the Mac App Store GUI beforehand for anyone using macOS 12.0+ due to U(https://github.com/mas-cli/mas/issues/417).
a  
- name: Install Keynote
  community.general.mas:
    id: 409183694
    state: present

- name: Install Divvy with command mas installed in /usr/local/bin
  community.general.mas:
    id: 413857545
    state: present
  environment:
    PATH: /usr/local/bin:{{ ansible_facts.env.PATH }}

- name: Install a list of apps
  community.general.mas:
    id:
      - 409183694 # Keynote
      - 413857545 # Divvy
    state: present

- name: Ensure the latest Keynote version is installed
  community.general.mas:
    id: 409183694
    state: latest

- name: Upgrade all installed Mac App Store apps
  community.general.mas:
    upgrade_all: true

- name: Install specific apps and also upgrade all others
  community.general.mas:
    id:
      - 409183694 # Keynote
      - 413857545 # Divvy
    state: present
    upgrade_all: true

- name: Uninstall Divvy
  community.general.mas:
    id: 413857545
    state: absent
  become: true # Uninstallation requires root permissions
z # )AnsibleModuleN)LooseVersionz12.0c                   T    e 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y)Masc                    || _         | j                   j                  d      | _        d| _        t	        j
                         d   xs d| _        d | _        d | _        d| _	        d| _
        d| _        ddi| _        | j                          y )NmasFr   z0.0changed)moduleget_bin_pathmas_path_checked_signinplatformmac_ver_mac_version
_installed	_outdatedcount_installcount_upgradecount_uninstallresultcheck_mas_tool)selfr   s     i/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/mas.py__init__zMas.__init__u   s     007$$,,.q1:U u
 	    c                 `   | j                   j                  s~|dk7  r| j                          | j                  |t	        |      g      \  }}}|dk7  rD| j                   j                  dj                  |t	        |      |j                                      | j                  d|z   xx   dz  cc<   y)zY Runs a `mas` command on a given app; command can be 'install', 'upgrade' or 'uninstall' 	uninstallr   z-Error running command '{0}' on app '{1}': {2}msgcount_   N)	r   
check_modecheck_signinrunstr	fail_jsonformatrstrip__dict__)r   commandidrcouterrs         r   app_commandzMas.app_command   s     {{%%+%!!#88Wc"g$67LBSQw%%GNNwX[\^X_adakakamn & 
 	h()Q.)r   c                 P   | j                   s| j                  j                  d       | j                  dg      \  }}}|dk7  s5|j	                         r%t        |j	                               t        d      k  r.| j                  j                  d|j	                         z          yy)z? Verifies that the `mas` tool is available in a recent version z$Required `mas` tool is not installedr!   versionr   z1.5.0z)`mas` tool in version 1.5.0+ needed, got N)r   r   r)   r'   stripr   r   r/   r0   r1   s       r   r   zMas.check_mas_tool   s     }}KK!!&L!M xx,C7#))+ciik)B\RYEZ)ZKK!!&QTWT]T]T_&_!` *[r   c                 l   | j                   ryt        | j                        t        t              k\  r#| j                  j                  d       d
| _         y| j                  dg      \  }}}|j                  dd      d   j                         dk(  r| j                  j                  d	       d
| _         y)z: Verifies that the user is signed in to the Mac App Store NzYWARNING: You must be signed in via the Mac App Store GUI beforehand else error will occuraccount
r$   r   zNot signed inz*You must be signed in to the Mac App Storer!   T)
r   r   r   #NOT_WORKING_MAC_VERSION_MAS_ACCOUNTr   logr'   splitr+   r)   r6   s       r   r&   zMas.check_signin   s     ))*l;^.__KKOOwx  $	  88YK0LBSyyq!!$++-@%%*V%W#r   c                    g }| j                   dkD  r*|j                  dj                  | j                                | j                  dkD  r*|j                  dj                  | j                               | j                  dkD  r*|j                  dj                  | j                               |r-d| j
                  d<   dj                  |      | j
                  d<    | j                  j                  d
i | j
                   y	)z0 Exit with the data we have collected over time r   zInstalled {0} app(s)zUpgraded {0} app(s)zUninstalled {0} app(s)Tr   z, r"   N )	r   appendr*   r   r   r   joinr   	exit_json)r   msgss     r   exitzMas.exit   s     !KK.55d6H6HIJ!KK-44T5G5GHI!#KK0778L8LMN%)DKK	"!%4DKK,,r   c                     | j                  |g      \  }}}|j                  d      }|d   dk(  rg }g }|D ]@  }|j                  dd      }t        |      dk(  s$|j                  t	        |d                B |S )zM Returns the list of all app IDs; command can either be 'list' or 'outdated' r9   r   zNo installed apps found r$      )r'   r<   lenr?   int)r   r-   r/   raw_appsr1   rowsappsrs           r   get_current_statezMas.get_current_state   s     !HHgY/Hc~~d#7//D 	'AQA1v{C!I&		' r   c                 ^    | j                   | j                  d      | _         | j                   S )z$ Returns the list of installed apps list)r   rM   r   s    r   	installedzMas.installed   s*     ??""44V<DOr   c                 8    t        |      | j                         v S )z+ Checks whether the given app is installed )rH   rQ   r   r.   s     r   is_installedzMas.is_installed   s     2w$..***r   c                 8    t        |      | j                         v S )z9 Checks whether the given app is installed, but outdated )rH   outdatedrS   s     r   is_outdatedzMas.is_outdated   s     2w$--/))r   c                 ^    | j                   | j                  d      | _         | j                   S )z2 Returns the list of installed, but outdated apps rV   )r   rM   rP   s    r   rV   zMas.outdated   s*     >>!!33J?DN~~r   c                 r    |j                  d| j                         | j                  j                  |d      S )z" Runs a command of the `mas` tool r   F)insertr   r   run_command)r   cmds     r   r'   zMas.run   s-     	

1dmm${{&&sE22r   c                 <   | j                         }| j                  j                  sX| j                          | j	                  dg      \  }}}|dk7  r-| j                  j                  d|j                         z          | xj                  t        |      z  c_        y)z> Upgrades all installed apps and sets the correct result data upgrader   zCould not upgrade all apps: r!   N)	rV   r   r%   r&   r'   r)   r+   r   rG   )r   rV   r/   r0   r1   s        r   upgrade_allzMas.upgrade_all   s{     ==?{{%%88YK0LBSQw%%*H3::<*W%Xc(m+r   N)__name__
__module____qualname__r   r2   r   r&   rC   rM   rQ   rT   rW   rV   r'   r_   r>   r   r   r	   r	   s   s?    $/ 
a$-" +
*
3,r   r	   c                  P   t        t        t        dd      t        ddg d      t        dd	d
g            d      } t        |       }| j                  d   r| j                  d   }ng }| j                  d   }| j                  d   }t	        t        |            D ]  }|dk(  r%|j                  |      r|j                  d|       -|dk(  rN|j                  |      sDt        j                         dk7  r| j                  d       |j                  d|       |dk(  s|j                  |      s|j                  d|       |j                  |      s|j                  d
|        d |_        |r |j                         r|j                          |j                          y )NrO   rH   )typeelementsr(   present)absentlatestrf   )rd   defaultchoicesboolFr^   )rd   ri   aliases)r.   stater_   T)argument_specsupports_check_moder.   rm   r_   installrg   r   z<Uninstalling apps requires root permissions ('become: true')r!   r    rh   )r   dictr	   paramssortedsetrT   r2   osgetuidr)   rW   r   rV   r_   rC   )r   r   rK   rm   r^   apps         r   mainrx     sm   %0E9>]^&%)M

 !F f+C}}T}}T"MM'"EmmM*G c$i  0I##C(	3/h$99;!#$$)g$hS1h##C(	3/%	3/#0( CM3<<> HHJr   __main__)
__future__r   r   r   rd   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   ru   Bansible_collections.community.general.plugins.module_utils.versionr   r   r:   objectr	   rx   r`   r>   r   r   <module>r      se    A @-^*X 
 4 	 [ &, #N,& N,b-` zF r   