
    Se"                     $   d Z ddlZddlmZ ddlmZmZ ddlmZ ddl	m
Z
 	 ddlmZ dd	lmZmZmZ e
j$                  j&                  ZdZdZdZe
j$                  j&                  ZdZdZ G d de      Z G d d      Zd Zy# e$ r	  ed
      w xY w)a  
Copyright (c) 2022, Ian Santopietro
All rights reserved.

This file is part of RepoLib.

RepoLib is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

RepoLib is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with RepoLib.  If not, see <https://www.gnu.org/licenses/>.
    N)	SourceKey   )SourceSourceError)
SourceFile)util)	Launchpad)
BadRequestNotFoundUnauthorizedzfMissing optional dependency "launchpadlib". Try `sudo apt install python3-launchpadlib` to install it.zhttp://ppa.launchpad.netubuntumainppa:c            	            e Zd ZdZeZededefd       Z	ddd fd
Z
defd	Zd
eddfdZddedededdfdZedefd       Zej"                  dd       Zedefd       Zej"                  dd       Z xZS )	PPASourcea   PPA Source shortcut

    These are given in the format ppa:owner/name. Much of this code is adapted 
    from SoftwareProperties.

    Arguments:
        shortcut (str): The ppa: shortcut to process
        fetch_data (bool): Whether to try and fetch metadata from Launchpad.
    shortcutreturnc                 t    | j                  t         d      r | j                  d      }t        |      dkD  ryy)zDetermine whether a PPA shortcut is valid.
        
        Arguments:
            shortcut(str): The shortcut to validate
        
        Returns: bool
            `True` if the PPA is valid, otherwise False
        r   /   TF)
startswithprefixsplitlen)r   	shortlists     7/usr/lib/python3/dist-packages/repolib/shortcuts/ppa.py	validatorzPPASource.validator?   s8     &|, s+I9~!     T)line
fetch_datac                (   |r |j                  d      st        d| d      t        |   ||       t	        j
                  t              | _        || _        d | _	        d| _
        d| _        d| _        |r| j                  | j                         y y )Nzppa:zThe PPA shortcut  is malformedTr    )r   r   super__init__logging	getLogger__name__logr!   r   twin_source_displayname_descriptionload_from_shortcut)selfr!   r"   argskwargs	__class__s        r   r&   zPPASource.__init__P   s    ??6*!$5dV="IJJv&$$X.	##DII. r   c                 P    d}|| j                   z  }|dz  }|| j                  z  }|S )Nr    z

)displaynamedescription)r/   outputs     r   get_descriptionzPPASource.get_description^   s6    $"""&$"""r   dataNc                 .    | j                  |d          y )Nr   )r   )r.   )r/   r8   s     r   load_from_datazPPASource.load_from_datae   s    a1r   metakeyc                    | j                          |r|| _        | j                  st        d      | j                  | j                        st        d| j                   d      | j                  j	                  t
        t        z   d      }|j                  d      | _        | j                  d   }| j                  d   }t
         d| d| | _	        | j                   d	t        j                   t        j                  vrct        | j                  
      }t        |_        || _        | j                   t        j                  t#        | j                   j$                        <   n5t        j                  t#        | j                   j$                           | _        | j                   j'                  |        | j                  | _        t*         d| d| dt,         g| _        t        j0                  g| _        t4        g| _        |s|rGt9        ||      | _        | j:                  j<                  | _        | j:                  j>                  | _        | j:                  r|r| j:                  j<                  | _        | j:                  r|rtA        | j                  
      }t#        |j$                        t        jB                  vrT|jE                  | j:                  jF                         |t        jB                  t#        |j$                        <   || _$        n"t        jB                  |j$                     | _$        t#        | jH                  j$                        | _%        d| _&        y)a+  Translates the shortcut line into a full repo.
        
        Arguments:
            shortcut(str): The shortcut to load, if one hasn't been loaded yet.
            meta(bool): Whether to fetch repo metadata from Launchpad
            key(bool): Whether to fetch and install a signing key
        zNo PPA shortcut providedz	The line r$   r    r   r   r   -.name)fingerprintTN)'reset_valuesr!   r   r   replacer   
delineatorr   
info_partsidentBASE_FORMATvaluer   filesr   formatfilestrpath
add_sourcerA   BASE_URL	BASE_DISTurisDISTRO_CODENAMEsuites
BASE_COMPS
componentsget_info_from_lpr   r4   r5   r   keysload_key_datarB   r<   	signed_byenabled)	r/   r   r;   r<   r!   	ppa_ownerppa_namenew_filerepo_keys	            r   r.   zPPASource.load_from_shortcuth   si    	 DIyy899~~dii(	$))MBCCyy  *!4b9**S/OOA&	??1%xq1XJ7
jj\;,,-.djj@!tzz2H)HO DI.2iiDJJs499>>*+

3tyy~~#67DI		T"JJ	 z9+Qxj)EF	++,%,3'	8<DH#xx33D#xx33D88,,DI88 djj1H8==!2&&4883G3G&H08		#hmm,-%-99X]]3 /DNr   c                     | j                   r| j                   S | j                  r| j                  j                  | _         | j                   S )z)The name of the PPA provided by launchpad)r,   r   r4   r/   s    r   r4   zPPASource.displayname   >     $$$88 $ 4 4D   r   c                     || _         yz%Cache this for use without hitting LPN)r,   )r/   r4   s     r   r4   zPPASource.displayname   s     (r   c                     | j                   r| j                   S | j                  r| j                  j                  | _         | j                   S )z0The description of the PPA provided by Launchpad)r-   r   r5   ra   s    r   r5   zPPASource.description   rb   r   c                     || _         yrd   )r-   )r/   descs     r   r5   zPPASource.description   s     !r   )r    TT)r   N)r)   
__module____qualname____doc__rH   default_formatstaticmethodrM   boolr   r&   r7   listr:   r.   propertyr4   setterr5   __classcell__)r2   s   @r   r   r   2   s     !N3 4    $&$ / 24 2D 29# 9t 9d 9TX 9v !S ! ! ( ( !S ! ! ! !r   r   c                       e Zd ZdZd Zed        Zed        Zed        Zede	fd       Z
ede	fd       Zed	        Zy
)PPAzo An object to fetch data from PPAs. 
    
    Portions of this class were adapted from Software Properties
    c                 f    || _         || _        d | _        d | _        d | _        d | _        d | _        y )N)teamnameppaname_lap_lpteam_lpppa_signing_key_data_fingerprint)r/   ru   rv   s      r   r&   zPPA.__init__   s5     	!% r   c                     | j                   s@t        j                  | j                   d| j                  j
                   dd      | _         | j                   S )z The Launchpad Object.r?   
productiondevel)service_rootversion)rw   r	   login_anonymouslyrh   r2   r)   ra   s    r   lapzPPA.lap   sM     yy!33??#1T^^%<%<$=>)DI
 yyr   c                 J   | j                   s7	 | j                  j                  | j                        | _         | j                   S | j                   S # t        $ r!}d| j                   d}t        |      |d}~wt        $ r!}d| j                   d}t        |      |d}~ww xY w)z* The Launchpad object for the PPA's owner.zUser/Team "z" not foundNzInvalid user/team name "")rx   r   peopleru   r   r   r   r/   errmsgs      r   lpteamz
PPA.lpteam   s     ||0#xxt}}= ||t||  0#DMM?+>!#&C/ 00qA!#&C/0s#   *A 	B"A55B"BB"c                 f   | j                   s8	 | j                  j                  | j                        | _         | j                   S | j                   S # t        $ r.}d| j
                   d| j                   d}t        |      |d}~wt        $ r!}d| j                   d}t        |      |d}~ww xY w)z" The Launchpad object for the PPA.r@   zPPA "r   z"" not foundNzInvalid PPA name "r   )ry   r   getPPAByNamerv   r   ru   r   r
   r   s      r   lpppaz	PPA.lpppa   s     {{0"kk66DLL6I {{t{{  0dmm_Adll^<H!#&C/ 0*4<<.:!#&C/0s#   +A 	B0)BB0B++B0r   c                 6    | j                   j                  xs dS )z str: The description of the PPA.r    )r   r5   ra   s    r   r5   zPPA.description        zz%%++r   c                 6    | j                   j                  xs dS )z  str: the fancy name of the PPA.r    )r   r4   ra   s    r   r4   zPPA.displayname   r   r   c                 h    | j                   s| j                  j                  | _         | j                   S )z) str: the fingerprint of the signing key.)r{   r   signing_key_fingerprintra   s    r   rB   zPPA.fingerprint   s+        $

 B BD   r   N)r)   rh   ri   rj   r&   ro   r   r   r   rM   r5   r4   rB    r   r   rs   rs      s    
!       ,S , , ,S , , ! !r   rs   c                     t        | |      }|S )a   Attempt to get information on a PPA from launchpad over the internet.

    Arguments:
        owner_name (str): The Launchpad user owning the PPA.
        ppa (str): The name of the PPA

    Returns:
        json: The PPA information as a JSON object.
    )rs   )
owner_namer   s     r   rW   rW     s     j#
CJr   )rj   r'   repolib.keyr   sourcer   r   rL   r   r    r   launchpadlib.launchpadr	   lazr.restfulclient.errorsr
   r   r   ImportErrorSourceFormatLEGACYrH   rP   rQ   rU   DEFAULT_FORMATr   rE   r   rs   rW   r   r   r   <module>r      s   (  ! (  0LL &&%	
""))	
K! K!ZE! E!PK  
	/ s   B B