
    Vh!<                         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mZ d dlmZ dZd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd ZddZd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
module: flatpak
short_description: Manage flatpaks
description:
  - Allows users to add or remove flatpaks.
  - See the M(community.general.flatpak_remote) module for managing flatpak remotes.
author:
  - John Kwiatkoski (@JayKayy)
  - Alexander Bethke (@oolongbrothers)
requirements:
  - flatpak
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: partial
    details:
      - If O(state=latest), the module will always return C(changed=true).
  diff_mode:
    support: none
options:
  executable:
    description:
      - The path to the C(flatpak) executable to use.
      - By default, this module looks for the C(flatpak) executable on the path.
    type: path
    default: flatpak
  method:
    description:
      - The installation method to use.
      - Defines if the C(flatpak) is supposed to be installed globally for the whole V(system) or only for the current V(user).
    type: str
    choices: [system, user]
    default: system
  name:
    description:
      - The name of the flatpak to manage. To operate on several packages this can accept a list of packages.
      - When used with O(state=present), O(name) can be specified as a URL to a C(flatpakref) file or the unique reverse DNS
        name that identifies a flatpak.
      - Both C(https://) and C(http://) URLs are supported.
      - When supplying a reverse DNS name, you can use the O(remote) option to specify on what remote to look for the flatpak.
        An example for a reverse DNS name is C(org.gnome.gedit).
      - When used with O(state=absent) or O(state=latest), it is recommended to specify the name in the reverse DNS format.
      - When supplying a URL with O(state=absent) or O(state=latest), the module will try to match the installed flatpak based
        on the name of the flatpakref to remove or update it. However, there is no guarantee that the names of the flatpakref
        file and the reverse DNS name of the installed flatpak do match.
    type: list
    elements: str
    required: true
  no_dependencies:
    description:
      - If installing runtime dependencies should be omitted or not.
      - This parameter is primarily implemented for integration testing this module. There might however be some use cases
        where you would want to have this, like when you are packaging your own flatpaks.
    type: bool
    default: false
    version_added: 3.2.0
  remote:
    description:
      - The flatpak remote (repository) to install the flatpak from.
      - By default, V(flathub) is assumed, but you do need to add the flathub flatpak_remote before you can use this.
      - See the M(community.general.flatpak_remote) module for managing flatpak remotes.
    type: str
    default: flathub
  state:
    description:
      - Indicates the desired package state.
      - The value V(latest) is supported since community.general 8.6.0.
    choices: [absent, present, latest]
    type: str
    default: present
a  
- name: Install the spotify flatpak
  community.general.flatpak:
    name: https://s3.amazonaws.com/alexlarsson/spotify-repo/spotify.flatpakref
    state: present

- name: Install the gedit flatpak package without dependencies (not recommended)
  community.general.flatpak:
    name: https://git.gnome.org/browse/gnome-apps-nightly/plain/gedit.flatpakref
    state: present
    no_dependencies: true

- name: Install the gedit package from flathub for current user
  community.general.flatpak:
    name: org.gnome.gedit
    state: present
    method: user

- name: Install the Gnome Calendar flatpak from the gnome remote system-wide
  community.general.flatpak:
    name: org.gnome.Calendar
    state: present
    remote: gnome

- name: Install multiple packages
  community.general.flatpak:
    name:
      - org.gimp.GIMP
      - org.inkscape.Inkscape
      - org.mozilla.firefox

- name: Update the spotify flatpak
  community.general.flatpak:
    name: https://s3.amazonaws.com/alexlarsson/spotify-repo/spotify.flatpakref
    state: latest

- name: Update the gedit flatpak package without dependencies (not recommended)
  community.general.flatpak:
    name: https://git.gnome.org/browse/gnome-apps-nightly/plain/gedit.flatpakref
    state: latest
    no_dependencies: true

- name: Update the gedit package from flathub for current user
  community.general.flatpak:
    name: org.gnome.gedit
    state: latest
    method: user

- name: Update the Gnome Calendar flatpak from the gnome remote system-wide
  community.general.flatpak:
    name: org.gnome.Calendar
    state: latest
    remote: gnome

- name: Update multiple packages
  community.general.flatpak:
    name:
      - org.gimp.GIMP
      - org.inkscape.Inkscape
      - org.mozilla.firefox
    state: latest

- name: Remove the gedit flatpak
  community.general.flatpak:
    name: org.gnome.gedit
    state: absent

- name: Remove multiple packages
  community.general.flatpak:
    name:
      - org.gimp.GIMP
      - org.inkscape.Inkscape
      - org.mozilla.firefox
    state: absent
a  
command:
  description: The exact flatpak command that was executed.
  returned: When a flatpak command has been executed
  type: str
  sample: "/usr/bin/flatpak install --user --nontinteractive flathub org.gnome.Calculator"
msg:
  description: Module error message.
  returned: failure
  type: str
  sample: "Executable '/usr/local/bin/flatpak' was not found on the system."
rc:
  description: Return code from flatpak binary.
  returned: When a flatpak command has been executed
  type: int
  sample: 0
stderr:
  description: Error output from flatpak binary.
  returned: When a flatpak command has been executed
  type: str
  sample: "error: Error searching remote flathub: Can't find ref org.gnome.KDE"
stdout:
  description: Output from flatpak binary.
  returned: When a flatpak command has been executed
  type: str
  sample: "org.gnome.Calendar/x86_64/stable\tcurrent\norg.gnome.gitg/x86_64/stable\tcurrent\n"
)urlparse)AnsibleModule)LooseVersionz$Unknown option --columns=applicationc                    g }g }|D ]G  }|j                  d      s|j                  d      r|j                  |       7|j                  |       I |ddj                  |      g}	t        | |      }
t	        |
      t	        d      k  r|	dgz  }	n|	dgz  }	|r|	dgz  }	|r|	|z   }t        | | j                  |       |r |	|gz   |z   }t        | | j                  |       d	t        d
<   y)zAdd new flatpaks.http://https://install--{0}1.1.3-y--noninteractive	--no-depsTchangedN)
startswithappendformat_flatpak_versionr   _flatpak_command
check_moderesult)modulebinaryremotenamesmethodno_dependencies	uri_namesid_namesnamebase_commandflatpak_versioncommands               m/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/flatpak.pyinstall_flatr'      s     IH "??9%)DT"OOD!	"
 Iw~~f'=>L&vv6OO$|G'<<+,,%*!2!2G<&)H4!2!2G<F9    c           	      v   |D cg c]  }t        | |||       }}|ddj                  |      g}t        | |      }t        |      t        d      k  r|dgz  }n|dgz  }|r|dgz  }||z  }t	        | | j
                  |      }	| j
                  r
dt        d
<   y|	j                  d      d	k(  t        d
<   yc c}w )zUpdate existing flatpaks.updater   r   r   r   r   TzNothing to do.r   N)_match_installed_flat_namer   r   r   r   r   findr   )
r   r   r   r   r   r"   installed_flat_namesr%   r$   stdouts
             r&   update_flatr0      s    
  	#664@  x!78G&vv6OO$|G'<<D6&''K= ##Gff&7&7AF!! 9'-{{3C'D'J 9s   B6c           	         |D cg c]  }t        | |||       }}|dg}t        | |      }t        |      t        d      k  r|dgz  }n|dgz  }|dj                  |      g|z   z  }t	        | | j
                  |       dt        d<   yc c}w )	zRemove existing flatpaks.	uninstallr   r   r   r   Tr   N)r,   r   r   r   r   r   r   )r   r   r   r   r"   r.   r%   r$   s           r&   uninstall_flatr3      s    
  	#664@  {#G&vv6OO$|G'<<D6&''v&'*>>>GVV..8F9s   B	c                     |ddj                  |      g}t        | d|      }g }g }|D ]P  }t        |      j                         }	|	|j                         v r|j	                  |       @|j	                  |       R ||fS )z$Check if the flatpaks are installed.listr   F)r   r   _parse_flatpak_namelowerr   )
r   r   r   r   r%   output	installednot_installedr"   parsed_names
             r&   flatpak_existsr<   	  s    vw~~f56GfeW5FIM ')$/557&,,.(T"  &' m##r(   c                 V   t        |      }|ddj                  |      ddg}t        | d|d       t        d   d	k7  rt        t        d
   v rt        | |||      }nt        | |||      }|r|S ddj                  t        |            z   dz   t        d<    | j                  di t         y )Nr5   r   --app--columns=applicationFT)ignore_failurercr   stderrzBFlatpak removal failed: Could not match any installed flatpaks to zthe name `{0}`. z@If you used a URL, try using the reverse DNS name of the flatpakmsg )r6   r   r   r   &OUTDATED_FLATPAK_VERSION_ERROR_MESSAGE(_match_flat_using_flatpak_column_feature)_match_flat_using_outdated_flatpak_format	fail_json)r   r   r"   r   r;   r%   matched_flatpak_names          r&   r,   r,     s    
 &d+Kvw~~f5w@WXGVUGDAd|qCvhGWW 5VV[RXY 	
 6ffkSYZ 	 ##\%%&9$&?@ANOu 	"6"r(   c                     |ddj                  |      ddg}t        | d|      }|j                  d      D ]'  }|j                         |j                         k(  s%|c S  y )Nr5   r   r>   r?   F
r   r   splitr7   r   r   r;   r   r%   r8   rows          r&   rG   rG   3  s^    vw~~f5w@WXGfeW5F||D! #))+-Jr(   c                     |ddj                  |      dg}t        | d|      }|j                  d      D ]7  }|j                         |j                         v s$|j                         d   c S  y )Nr5   r   r>   FrK   r   rL   rN   s          r&   rF   rF   <  sf    vw~~f5w?GfeW5F||D! "#))+-99;q>!"r(   c                     d| vry| j                  d      }t        |      dk  ry|d   }|j                         sy|dd  D ]  }|j                         r y y)N.F   r      T)rM   lenislowerisalnum)partsectionsdomainsections       r&   _is_flatpak_idr\   E  sg     $zz#H
8}qa[F>>AB<   r(   c                 :   | j                  d      s| j                  d      rNt        |       j                  j                  d      d   }|j                  d      dd }dj	                  |      }|S | j                  d      }|D ]  }t        |      s|} |S  | }|S )Nr
   r   /r+   rR   r   )r   r   pathrM   joinr\   )r"   	file_namefile_name_without_extensioncommon_namepartsrX   s         r&   r6   r6   Y  s    y!T__Z%@TN''--c226	&/ooc&:1R&@#hh:;  

3 	Dd#" 	
 Kr(   c                 N    |dg}t        | d|      }|j                         d   }|S )Nz	--versionFrT   )r   rM   )r   r   r%   r8   version_numbers        r&   r   r   i  s0    {#GfeW5F\\^A&Nr(   c                     dj                  |      t        d<   |r
dt        d<   y| j                  ||       \  t        d<   t        d<   t        d<   t        d   S )	N r%   r   rA    )check_rcr/   rB   )r`   r   run_command)r   noopr%   r@   s       r&   r   r   q  sd    )F9t7=7I7In, 8J 84F4L&"F8$4 (r(   c                     t        t        t        ddd      t        dd      t        ddddg	      t        dd
g d	      t        dd      t        dd            d      } | j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  |d       }t        d      a|s | j
                  d dd|z  it         t        dd      | _        t        | |||      \  }}	|dk(  r|rt        | |||       n,|dk(  r|rt        | ||||       |dv r|	rt        | |||	||        | j                  d i t         y )!Nr5   strT)typeelementsrequiredflathub)ro   defaultsystemuser)ro   rs   choicespresent)absentrw   latestboolFr_   flatpak)r"   r   r   stater   
executable)argument_specsupports_check_moder"   r|   r   r   r   r}   )r   rC   z,Executable '%s' was not found on the system.C)LANGUAGELC_ALLrx   ry   )rw   ry   rD   )r   dictparamsget_bin_pathr   rH   run_command_environ_updater<   r3   r0   r'   	exit_json)
r   r"   r|   r   r   r   r}   r   r9   r:   s
             r&   mainr   ~  s   6EDAUI6UH!' 24E9>@ fe<;	
 !F == DMM'"E]]8$Fmm$56O]]8$F|,J  T2F F
 cKjXc\bc(,c#(FF%-ffdFKI}Yvvy&9H	6?K))mXFvr(   __main__N)F)
__future__r   r   r   ro   __metaclass__DOCUMENTATIONEXAMPLESRETURN+ansible.module_utils.six.moves.urllib.parser   ansible.module_utils.basicr   Bansible_collections.community.general.plugins.module_utils.versionr   rE   r'   r0   r3   r<   r,   rG   rF   r\   r6   r   r   r   __name__rD   r(   r&   <module>r      s    C BGRJX
8 A 4 [)O &6,$$#6"( 
,^ zF r(   