
    Sep                         d Z ddlZddlZddlZddlZddlmZ ddlmZ ddl	m
Z
 dZdZ G d	 d
e
j                        Z G d d      Zy)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)Path)request   )utilzhttps://keyserver.ubuntu.com/z/pks/lookup?op=get&options=mr&exact=on&search=0xc                   (     e Zd ZdZdd fd
Z xZS )KeyFileErrorz% Exceptions related to apt key files.r   )codec                2    t        |   |i | || _        y)zExceptions related to apt key files.

        Arguments:
            code (:obj:`int`, optional, default=1): Exception error code.
    N)super__init__r	   )selfr	   argskwargs	__class__s       -/usr/lib/python3/dist-packages/repolib/key.pyr   zKeyFileError.__init__'   s     	$)&)	    )__name__
__module____qualname____doc__r   __classcell__)r   s   @r   r   r   $   s    /#$  r   r   c            	       \    e Zd ZdZddeddfdZddedededdfdZdd	Zdd
ZddZ	ddZ
y)	SourceKeyz A signing key for an apt source.namereturnNc                    t        j                  t              | _        t	               | _        t	               | _        t        j                         | _	        d| _
        |r#| j                  |       | j                          y y )Nr   )r   )logging	getLoggerr   logr   tmp_pathpathgnupgGPGgpgdata
reset_path	setup_gpg)r   r   s     r   r   zSourceKey.__init__3   sZ    $$X.F	99;	OOO&NN r   r!   suffixc                    | j                   j                  d       |s|st        d      |r9| d| d}t        j                  |z  | _        t        j                  |z  | _        n>|r<t        |      | _        t        j                  | j                  j                  z  | _        | j                          | j                   j                  d| j                         | j                   j                  d| j
                         y)a%  Set the path for this key
        
        Arguments:
            suffix(str): The suffix to append to the end of the name to get the
                file name (default: 'archive-keyring')
            name(str): The name of the source
            path(str): The entire path to the key
        zSetting pathz/A name is required to set the path for this key-z.gpgzKey Path: %szTemp Path: %sN)r   infor   r   TEMP_DIRr    KEYS_DIRr!   r   r   r'   debug)r   r   r!   r(   	file_names        r   r&   zSourceKey.reset_path>   s     	n%DPQQ&&.I MMI5DM	1DIT
DI MMDIINN:DM~tyy16r   c                    | j                   j                  d       | j                   j                  d| j                  | j                         	 t        j                  | j                  | j                         t        j                  t        | j                              | _        | j                   j                  d| j                  j                         y# t        $ r Y jw xY w)z#Set up the GPG object for this key.zSetting up GPGzCopying %s to %s)keyringzGPG Setup: %sN)r   r+   r.   r!   r    shutilcopy2FileNotFoundErrorr"   r#   strr$   r1   )r   s    r   r'   zSourceKey.setup_gpgX   s    &')499dmmD	LLDMM2
 99S%78(8(89	 ! 		s   *C 	C#"C#c                    | j                   j                  d| j                  | j                         | j                   j	                  d| j
                  j                                | j                   j	                  d| j                  j                                t        j                  j                         s!	 t        j                  j                  d       	 t        j                  | j                  | j                         y# t        $ r | j                   j                  d       Y Rw xY w# t        $ r t        j                          }	 |j#                  dd      }|j%                  t'        | j                        t'        | j                               Y y# t        j(                  j*                  $ r* | j                   j-                  d	       t/        d
       Y Y yw xY ww xY w)zSaves the key to disk.zSaving key file %s from %szKey contents: %szTemp key exists? %sT)parentszPKey destination path does not exist and cannot be created Failures expected now.org.pop_os.repolib/RepoDBus service not found!%Permission denied. Please use `sudo`.N)r   r+   r!   r    r.   r$   	list_keysexistsr   r-   mkdirPermissionErrorerrorr2   copydbus	SystemBus
get_objectinstall_signing_keyr5   
exceptionsDBusExceptioncriticalprintr   busprivileged_objects      r   save_gpgzSourceKey.save_gpge   s\   2DIIt}}M)488+=+=+?@,dmm.B.B.DE}}##%##D#1	KKtyy1 # -  	.."C	$'NN3G$Q!!55&		N ??00 !!";<=>	s>   9 D *D/ $D,+D,/G$A
FAG G$G  G$c                    	 | j                   j                          | j                  j                          y# t        $ r t	        j
                         }	 |j                  dd      }|j                  t        | j                               Y y# t        j                  j                  $ r* | j                  j                  d       t        d       Y Y yw xY wt        $ r Y yw xY w)zDeletes the key file from disk.r8   r9   r:   r;   N)r    unlinkr!   r?   rB   rC   rD   delete_signing_keyr5   rF   rG   r   rH   rI   r4   rJ   s      r   
delete_keyzSourceKey.delete_key   s    	MM  "II 	.."C$'NN3G$Q!!44S^D??00 !!";<=>
 ! 		s.   47 C"6BACC"CC"!C"c                    | j                   j                         r6t        | j                   d      5 }|j                         | _        ddd       y| j
                  j                          d|v r0|d   | _        | j                  j                  | j                         yd|v ro| j                  j                  |d          | j
                  j                         r6t        | j
                  d      5 }|j                         | _        ddd       yyd|v rt        j                  |d         }t        j                  |      5 }|j                         j                  d      | _        | j                  j                  | j                         ddd       yd|v rd	|vr	t        |d	<   d
|vr	t        |d
<   |d	   |d
   z   |d   z   }t        j                  |      }t        j                  |      5 }|j                         j                  d      | _        | j                  j                  | j                         ddd       yt        d|j!                          d      # 1 sw Y   yxY w# 1 sw Y   yxY w# 1 sw Y   yxY w# 1 sw Y   yxY w)a@  Loads the key data from disk into the object for processing.

        Each of the keyword options specifies one place to look to import key 
        data. Once one is successfully imported, the method returns, so passing 
        multiple won't import multiple keys. 
        
        Keyword Arguments:
            raw(bytes): Raw data to import to the keyring
            ascii(str): ASCII-armored key data to import directly
            url(str): A URL to download key data from
            fingerprint(str): A key fingerprint to download from `keyserver`
                keyserver(str): A keyserver to download from.
                keypath(str): The path on the keyserver from which to download.
        
        NOTE: The keyserver and keypath args only affect the operation of the 
            `fingerprint` keyword.
        rb)modeNrawasciiurlzUTF-8fingerprint	keyserverkeypathz4load_key_data() got an unexpected keyword argument "z? Expected keyword arguments are: [raw, ascii, url, fingerprint])r!   r=   openreadr%   r    touchr$   import_keysr   RequesturlopendecodeSKS_KEYSERVERSKS_KEYLOOKUP_PATH	TypeErrorkeys)r   r   keyfilereqresponsekey_urls         r   load_key_datazSourceKey.load_key_data   s!   & 99diid+ +w#LLN	+F?uDIHH  +fHH  1}}##%$--d3 /w 'DI/F?//&-0C% 0$MMO227;	$$TYY/0 F"&(&3{#&$6y![)F9,==}@UUG//'*C% 0$MMO227;	$$TYY/0 B6;;=/RM
 	
O+/0 0 s1   I2I+A
I7/A
JI(+I47J J) )rk   rk   zarchive-keyring)r   N)r   r   r   r   r5   r   r&   r'   rM   rQ   rj    r   r   r   r   0   sN    *	C 	 	7s 7c 7 7\` 74::&>
r   r   )r   r   r2   rB   r"   pathlibr   urllibr   rk   r   rb   rc   	RepoErrorr   r   rl   r   r   <module>rp      sH   (       /F 
4>> 
c
 c
r   