
    Sea.                        U 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dlmZmZ ddlZ ed      a ed      ad	Zd
Z ej&                  e      Z G d de      Z	 ddlZ ej2                         Z G d de      Z G d de      Z G d de      Zg dZddgZ dddddddddd d!d"d#d$Z!d%d&d'd(d)d*d+d,d-d.d/d0d1d2Z"d3d4d5d6d7d8d9d:ejF                  d;g
Z$d<d=d>d?d@Z%dAa& ejN                         Z( ee(jR                        Z* ejV                  dB      Z, ejV                  dC      Z-i dDddEdFdGdFdHdFdIdFdJdFdKdFdLdFdMddNddOdFdPdFdQddRddSddTddUddVddVdFdVdVdVdddVdddWZ.i Z/e0e1dX<   i Z2e0e1dY<   i Z3e0e1dZ<   i Z4e0e1d[<   dmd\e5d]e5fd^Z6dnd_e7d]dfd`Z8dodaZ9 ejt                  e9       db Z;dce<d]e7fddZ=dce<d]e0fdeZ>dodfZ?dndge7d]dfdhZ@di ZAdj ZBdke5d]e5fdlZCy# e$ r dZY pw xY w)pa  
Copyright (c) 2019-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)Enum)Path)urlparse)requesterror/etc/apt/sources.list.dz/etc/apt/keyrings/Fz7http://keyserver.ubuntu.com/pks/lookup?op=get&search=0xc                   (     e Zd ZdZdd fd
Z xZS )	RepoErrorz Exception from this module.   )codec                2    t        |   |i | || _        y)zException with a source object

        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/util.pyr   zRepoError.__init__-   s     	$)&)	    )__name__
__module____qualname____doc__r   __classcell__)r   s   @r   r
   r
   *   s    &#$  r   r
   linuxc                       e Zd ZdZdZdZy)SourceFormatzEnum of SourceFile FormatssourceslistN)r   r   r   r   DEFAULTLEGACY r   r   r   r   <   s    $GFr   r   c                   $    e Zd ZdZdZdZdefdZy)
SourceTypezEnum of repository typesdebdeb-srcreturnc                 h    | j                    }|j                  dd      }|j                  dd      }|S )z3Used for getting a version of the format for identsr&   sourcer%   binary)valuereplace)r   idents     r   r-   zSourceType.identF   s2    ::,i2eX.r   N)r   r   r   r   BINARY
SOURCECODEstrr-   r"   r   r   r$   r$   A   s    "FJs r   r$   c                       e Zd ZdZdZdZd Zy)AptSourceEnabledzC Helper Enum to translate between bool data and the Deb822 format. yesnoc                 $    | j                   dk(  ryy)z# Return a bool based on the value. r3   TF)r+   )r   s    r   get_boolzAptSourceEnabled.get_boolR   s     ::r   N)r   r   r   r   TRUEFALSEr6   r"   r   r   r2   r2   M   s    MDEr   r2   )zX-Repolib-Name:zX-Repolib-ID:zX-Repolib-Default-Mirror:zX-Repolib-CommentX-Repolib-PrefszEnabled:zTypes:zURIs:zSuites:zComponents:zArchitectures:z
Languages:zTargets:zPDiffs:zBy-Hash:zAllow-Insecure:zAllow-Weak:zAllow-Downgrade-To-Insecure:zTrusted:z
Signed-By:zCheck-Valid-Until:zValid-Until-Min:zValid-Until-Max:r9   X-Repolib-IDArchitectures	LanguagesTargetsPDiffsBy-HashAllow-Insecure
Allow-WeakAllow-Downgrade-To-InsecureTrusted	Signed-ByCheck-Valid-UntilValid-Until-MinValid-Until-Max)archlangtargetpdiffsby-hashallow-insecure
allow-weakallow-downgrade-to-insecuretrusted	signed-bycheck-valid-untilvalid-until-minvalid-until-maxrH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   )r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   TTruetrueYesr3   YESyYr   zName: zIdent: z
Comments: zDefault Mirror: )zX-Repolib-Name: zX-Repolib-ID: zX-Repolib-Comments: zX-Repolib-Default-Mirror: 
    z[^@.+]\[([^[]+.+)\]\ z\w+:(\/?\/?)[^\s]+!   @   -   #   $   %   ^   &   *   )   (   +   =   [   \   ]   {   }   _   )|   ?   /   .   <   >   ,   `   ~       :   ;   r   fileskeyserrorsnamer'   c                 ,    | j                  t              S )z Clean up a string intended for a filename.
    
    Arguments:
        name (str): The prospective name to scrub.
    
    Returns: str
        The cleaned-up name.
    )	translateCLEAN_CHARS)r~   s    r   scrub_filenamer      s     >>+&&r   testingc                     t        j                         }| sdadayt	        |j
                        }|dz  dz  dz  a|dz  dz  d	z  ay)
zSets Repolib in testing mode where changes will not be saved.
    
    Arguments:
        testing(bool): Whether testing mode should be enabled or disabled
            (Defaul: True)
    z/usr/share/keyringsr   Nusrsharekeyringsetcaptzsources.list.d)tempfileTemporaryDirectoryKEYS_DIRSOURCES_DIRr   r~   )r   testing_tempdirtesting_roots      r   set_testingr      s[     113O(/,,-Le#g-
:H&.1AAKr   c                  ,    t         j                          y)zClean up our tempdirN)_KEYS_TEMPDIRcleanupr"   r   r   _cleanup_temspsr      s    r   c                  p    t        j                         } | j                  dd      }|j                          y )Nzorg.pop_os.repolibz/Repo)dbus	SystemBus
get_objectexit)busprivileged_objects     r   	dbus_quitr     s,    
..
C';WEr   	excl_keysc                     | D ]  }||v r||v r| |   ||   k7  r y y |D ]  }||v r|| v r| |   ||   k7  r y y y)a  Compare two sources based on arbitrary criteria.
    
    This looks at a given list of keys, and if the given keys between the two
    given sources are identical, returns True.

    Arguments:
        source1, source2(Source): The two sources to compare
        excl_keys([str]): Any keys to exclude from the comparison
    
    Returns: bool
        `True` if the sources are identical, otherwise `False`.
    FTr"   )source1source2r   keys       r   compare_sourcesr   	  s      	)'>s|ws|+	  	)'>s|ws|+	 r   c                     i }| D ].  }||v r||v r| |   ||   k(  r| |   ||   f||<   | |   df||<   0 |D ]!  }||v r|| v r| |   ||   k(  rd||   f||<   # |S )a]  Find key-value pairs which differ between two sources.
    
    Arguments:
        source1, source2(Source): The two sources to compare
        excl_keys([str]): Any keys to exclude from the comparison
    
    Returns: dict{'key': ('source1[key]','source2[key]')}
        The dictionary of different keys, with the key values from each source.
     r"   )r   r   r   differing_keysr   s        r   find_differences_sourcesr   ,  s     N 1)'>s|ws|+#*3<">N3&s|R0s1  1)'>s|ws|+!73<0s1 r   c                     | D ]  }|dv r||v s| |xx   d||    z  cc<   ! |D ]  }|dv r|| v s| |xx   d||    z  cc<   ! | D ]V  }| |   j                         j                         }g }|D ]  }||vs|j                  |        dj                  |      | |<   X |D ]V  }||   j                         j                         }g }|D ]  }||vs|j                  |        dj                  |      ||<   X y)zCombine the data in two sources into one.
    
    Arguments:
        source1(Source): The source to be merged into
        source2(Source): The source to merge from
    )zX-Repolib-Namer:   EnabledTypes N)stripsplitappendjoin)r   r   r   valsnewvalsvals         r   combine_sourcesr   J  sL     /HH'>CLa~..L	/
  /HH'>CLa~..L	/  )s|!!#))+ 	$C'!s#	$ xx()  )s|!!#))+ 	$C'!s#	$ xx()r   enabledc                     | rda yda y)zEasy helper to enable/disable pretty-printing for object reprs.
    
    Can also be used as an easy way to reset to defaults.

    Arguments:
        enabled(bool): Whether or not Pretty Printing should be enabled
    r[   r   N)PRETTY_PRINT)r   s    r   prettyprint_enabler   m  s     r   c                    	 t        |       }|j                  sy|j                  dk(  ry|j                  r!t        |j                  |j                  g      S |j                  r!t        |j                  |j                  g      S y#  Y yxY w)z Validate a url and tell if it's good or not.

    Arguments:
        url (str): The URL to validate.

    Returns:
        `True` if `url` is not malformed, otherwise `False`.
    Fzx-repolib-name)r   schemenetlocallpath)urlresults     r   url_validatorr   {  sw     #}}==,,==v}}566[[v{{344s   B B ,B ,B B	c                    d}| j                  d      r$d}| j                  dd      } | j                         } | j                  d      r%| j                         }|D ]  }t	        |      s y y
| j                  d      rd| v ryy
| j                  d      ryt        | j                               d	k(  r|st	        |       S y)z Basic checks to see if a given debline is valid or not.

    Arguments:
        valid (str): The line to validate.

    Returns:
        True if the line is valid, False otherwise.
    F#Tr   r%   zppa:/z.flatpakrepor   N)
startswithr,   r   r   r   endswithlen)validcommentwordswords       r   validate_debliner     s     Gc2& 	DT"	 
		&	!%<  >>.)u{{}"7 ''r   linec                 p    	 | j                  d      } | j                         } | j                  d      s	 | S 6)z Strips the leading #'s from the given line.
    
    Arguments:
        line (str): The line to strip.
    
    Returns:
        (str): The input line without any leading/trailing hashes or 
            leading/trailing whitespace.
    r   )r   r   )r   s    r   strip_hashesr     s8     zz#zz|s#K r   )r   )T)r'   N)Dr   atexitloggingrer   enumr   pathlibr   urllib.parser   urllibr   r   r   r   r   TESTINGKEYSERVER_QUERY_URL	getLoggerr   log	Exceptionr
   distrocodenameDISTRO_CODENAMEImportErrorr   r$   r2   
valid_keysoutput_skip_keysoptions_inmapoptions_outmapr7   true_valueskeys_mapr   r   r   r~   TEMP_DIRcompile
options_reuri_rer   r   dict__annotations__r{   r|   r}   r0   r   boolr   r   registerr   r   r   r   r   r   r   r   r   r"   r   r   <module>r      s}  (   	    ! ! ,-$%
O g!
	 
%foo'O4 

 
t 
6   &#@,((" &#@,((" 	

			 !("4	 +++-""#RZZ01
	)	*  	
             !" #$ 





	
;@  dTt 	' 	'c 	'B Bd B,
   
! ! !F $ < )F  8@c c }  Os   $G G*)G*