o U7eea@sddlZddlZddlZddlZddlZddlmZddlmZm Z m Z ddl m Z ddl m Z ddlmZddlmZedZGd d d eZGd d d eZGd ddeZdS)N) get_distro) SourceEntry SourcesListCollapsedSourcesList)suppress)copy)gettext)urlparsez=gpg -q --no-options --no-default-keyring --batch --keyring %sc@seZdZdZdTddZeddZedd Zed d Zed d Z e ddZ e ddZ e ddZ e ddZe ddZe ddZdUddZe ddZe ddZd d!Zd"d#Zd$d%Zd&d'Ze d(d)Ze d*d+Ze d,d-Ze d.d/Zd0d1Zd2d3Ze d4d5Ze d6d7Ze d8d9Z e d:d;Z!e dd?Z#d@dAZ$e dBdCZ%e dDdEZ&e dFdGZ'e dHdIZ(e dJdKZ)dLdMZ*dUdNdOZ+dUdPdQZ,dRdSZ-dS)VShortcutHandleraCSuperclass for shortcut handler implementations. This provides a way to take a apt repository reference, in various forms, and write the specific apt configuration to local files. This also can remove previously written configuration from local files. This class and any subclasses should never modify any main apt configuration files, only specifically named files in '.d' subdirs (e.g. sources.list.d, etc) should be modified. The only exception to that rule is adding or removing sourceslist lines or components of existing source entries. NFcKsT||_|pg|_||_t|_|p|jj|_||_||_d|_d|_ d|_ d|_ dSN) shortcut components enable_sourcerdistrocodenamepocketdry_run _source_entry _filebase _username _password)selfr r rrrrkwargsrD/usr/lib/python3/dist-packages/softwareproperties/shortcuthandler.py__init__8s  zShortcutHandler.__init__cCst|}|jo |jS)z(Return if the uri is in valid uri format)r schemenetlocclsuriparsedrrr is_valid_uriJs zShortcutHandler.is_valid_uricCs$t|}|j|jdddS)z6Return the uri with the username and password stripped@r)r _replacer rpartitiongeturlrrrruri_strip_authPszShortcutHandler.uri_strip_authcCs.t||}d|||jf}|j|dS)z6Return the uri with the username and password includedz%s:%s@%sr%)r r)rr&r()rr usernamepasswordr!rrrruri_insert_authXszShortcutHandler.uri_insert_authc Csd}|d7}z5t'}|d|7}t|ts|}tj|d|tjdj }Wdn1s5wYWntj yX}zt t d|gWYd}~Sd}~wwz dd |DWStyvt t d |gYSw) zReturn an array of fingerprint(s) for provided key(s). The 'keys' parameter should be in text (str) or binary (bytes) format; it is converted to bytes if needed, and then passed to the 'gpg' program. z6gpg -q --no-options --no-keyring --batch --with-colonsz& --with-fingerprint --with-fingerprint --homedir T)checkinputstdoutNz,Warning: gpg error while processing keys: %scSs$g|]}|dr|ddqS)fpr: ) startswithsplit.0lrrr vs$z0ShortcutHandler.fingerprints..zWarning: invalid gpg output: %s)tempfileTemporaryDirectory isinstancebytesencode subprocessrunr5PIPEr0decodeCalledProcessErrorprint_ splitlinesKeyError)rkeyscmdhomedirr0errr fingerprints_s2   zShortcutHandler.fingerprintscCs"td|jd|jfS)Nz'Archive for codename: %s components: %s,)rErdistjoincompsrrrr description{s zShortcutHandler.descriptioncC|jSr ) archive_linkrQrrrweb_linkszShortcutHandler.web_linkcCs |jSr )rr rQrrrrTs zShortcutHandler.archive_linkcCs|jr d|j|jfS|jS)Nz%s-%s)rrrQrrrrNszShortcutHandler.distcC|jjS)z*Text indicating a binary-type SourceEntry.)r binary_typerQrrrrWzShortcutHandler.binary_typecCrV)z*Text indicating a source-type SourceEntry.)r source_typerQrrrrYrXzShortcutHandler.source_typecCs||jstdt|j}|s|S||jkr|d|j|_n||jkr/||j|j|_ntd|t t ||j dS)a"Get the SourceEntry representing this archive/shortcut. This should never include any authentication data; if required, the username and password should only be available from the username and password properties, as well as from the netrcparts_content property. If pkgtype is provided, it must be either binary_type or source_type, in which case this returns a SourceEntry with the requested type. If pkgtype is not specified, this returns a SourceEntry with an implementation-dependent type (in most cases, implementations should default to binary_type). Note that the default SourceEntry will be returned without modification, and the implementation will determine if it is enabled or disabled; while the source-type SourceEntry will be enabled or disabled based on self.enable_source. The binary-type SourceEntry will always be enabled. The SourceEntry 'file' field should always be set to the value of sourceparts_file. z3Implementation class did not set self._source_entryTzInvalid pkgtype: %sfile) rNotImplementedErrorrrW set_enabledtyperYr ValueErrorrstrr[)rpkgtyperKrrrrs        zShortcutHandler.SourceEntrycCrS)aWReturn the username used for authentication If authentication is used, return the username; otherwise return None. By default, this returns the private variable self._username, which defaults to None. Subclasses should override this method and/or set self._username if they have authentication data. )rrQrrrr* zShortcutHandler.usernamecCrS)aWReturn the password used for authentication If authentication is used, return the password; otherwise return None. By default, this returns the private variable self._password, which defaults to None. Subclasses should override this method and/or set self._password if they have authentication data. )rrQrrrr+rbzShortcutHandler.passwordcCs|||dS)zSave all data for this shortcut to file(s). This writes everything to the relevant files. By default, it calls add_source(), add_key(), and add_login(). Subclasses should override it if other actions are required. N) add_sourceadd_key add_loginrQrrradds zShortcutHandler.addcCs||dS)a<Remove all data for this shortcut from file(s). This removes everything from the relevant files. By default, it only calls remove_source() and remove_login(). Subclasses should override it if other actions are required. Note that by default is does not call remove_key(). N) remove_source remove_loginrQrrrremoves zShortcutHandler.removec Cs||j}||j}|j}t}t|}||}|r*ttd|j |j fn| |}|j |j kr?ttd|j n/|j rRtdj |_ ttd|j n|j rbttd|j |j fn ttd|j |j f|}|j |_ ||}|rttd|j |j fn|j ||d}|j |j krttd|j n|j rttd|j |j fn ttd|j |j f|}|jst|j |j gD]8}tj|s||jrtj|jst|jdt|d t||Wd n1swYq|d Sd S) aOAdd the apt SourceEntries. This uses SourcesList to add the binary-type and source-type SourceEntries. If the SourceEntry matches a known apt template, this will ignore the sourceparts_file and instead place the SourceEntries into the main/default sources.list file. Otherwise, this will add the SourceEntries into the sourceparts_file. If either the binary-type or source-type entry exist in the current SourcesList, the existing entries are updated instead of placing the entries in the sourceparts_file. zFound existing %s entry in %sz+Updating existing entry instead of using %sz!Archive has template, updating %szAdding disabled %s entry to %szAdding %s entry to %s)afterwN)rrWrYsourceparts_moderr get_entryrDrEr^r[ add_entrytemplatedisabledrsetospathexistsr4sourceparts_pathmkdiropenchmodsave)rbinentrysrcentrymode sourceslist collapsedlistnewentry entryfilerrrrcsT            zShortcutHandler.add_sourcecst}t|}j}j}|d||r2ttd|j |j f| |j dd|d||rQttd|j |j f| |j ddfdd|D}dd|Dsz|D]}|j stttd|j |j f||qcjs|jddd Sd S) a~Remove the apt SourceEntries. This uses SourcesList to remove the binary-type and source-type SourceEntries. This must disable the corresponding SourceEntries, from whatever file(s) they are located in. This must not disable more than matches, e.g. if the existing SourceEntry line contains more components this must edit the existing line to remove this SourceEntry's component(s). After disabling all matching SourceEntries, if the sourceparts_file is empty or contains only invalid and/or disabled SourceEntries, this may remove the sourceparts_file. TzDisabling %s entry in %s)rrcsg|] }|jjkr|qSr)r[sourceparts_file)r7srQrrr9Tz1ShortcutHandler.remove_source..cSsg|] }|js|js|qSr)invalidrr)r7rKrrrr9Urz"Removing disabled %s entry from %s)riN)rrrrWrYr] has_entryrDrEr^r[rpr&rrirr{)rrrr|r} file_entriesrKrrQrrg5s*       zShortcutHandler.remove_sourcecC tjdS)zAReturn result of apt_pkg.config.find_dir("Dir::Etc::sourceparts")zDir::Etc::sourcepartsapt_pkgconfigfind_dirrQrrrrw_ z ShortcutHandler.sourceparts_pathcCs|jd|jdS)a$Get the sources.list.d filename, without the leading path. By default, this combines the filebase with the codename, and uses a extension of 'list'. This is different than the trustedparts or netrcparts filenames, which use only the filebase plus extension. listsuffix)_filebase_to_filenamerrQrrrsourceparts_filenamedsz$ShortcutHandler.sourceparts_filenamecC||j|jS)aGet the sources.list.d absolute-path filename. Note that the add_source() function will not use this file if this shortcut's SourceEntry matches a known apt template; instead the entries will be placed in the main sources.list file. Also, if the SourceEntry already exists in the SourcesList, it will be edited in place, instead of using this file. See add_source() for more details. )_filename_to_filerwrrQrrrrns z ShortcutHandler.sourceparts_filecCdS)zMode of sourceparts file. Note that add_source() will only use this mode if it creates a new file for sourceparts_file; if the file already exists or if the SourceEntry is saved in a different file, this mode is not used. rrQrrrrnzsz ShortcutHandler.sourceparts_modec Cs^t|j|jfs dS|j}|j}t|ts|}||}ttd|d |ft |}d}|j st j |sn||jrOt j |jsOt j|jdd|jrnt|dt ||jWdn1siwYz-t}|d|d |7}tj|d |d WdWdS1swYWdStjy}zt|d}~wwdS) a[Add the GPG key(s) corresponding to this repo. By default, if self.trustedparts_content contains content, and self.trustedparts_file points to a file, the key(s) will be added to the file. If the file does not yet exist, and self.trustedparts_mode is set, the file will be created with that mode. Nz$Adding key to %s with fingerprint %srMz--importrlr~wbr- T)r.r/)alltrustedparts_filetrustedparts_contentr<r=r>rLrDrErOGPG_KEYRING_CMDrrtrurvr4trustedparts_pathrxtrustedparts_moderyrzr:r;r?r@r5rCShortcutException)rdestrHfprIactionrJrKrrrrds<        &zShortcutHandler.add_keyc Cs6t|j|jfs dS|j}||j}tj|sdSttd|d |ft |}dd |}|j sz)t }|d|d|7}tj|ddWdn1sYwYWntjyp}zt|d}~wwt|d }|| }Wdn1swY|rt|dSdSdS) aRemove the GPG key(s) corresponding to this repo. By default, if self.trustedparts_content contains content, and self.trustedparts_file points to a file, the key(s) will be removed from the file. If the file contains no more keys after removal, the file will be removed. This does not consider other files; multiple repositories may use the same signing key. This only modifies/removes self.trustedparts_file. Nz(Removing key from %s with fingerprint %srMz--delete-keys %srr-T)r.rb)rrrrLrtrurvrDrErOrrr:r;r?r@r5rCrryreadri) rrrrIrrJrKfemptyrrr remove_keys6     zShortcutHandler.remove_keycCr)zBReturn result of apt_pkg.config.find_dir("Dir::Etc::trustedparts")zDir::Etc::trustedpartsrrQrrrrrz!ShortcutHandler.trustedparts_pathcC |dS)z9Get the trusted.gpg.d filename, without the leading path.gpgrrQrrrtrustedparts_filename z%ShortcutHandler.trustedparts_filenamecCr)z-Get the trusted.gpg.d absolute-path filename.)rrrrQrrrrz!ShortcutHandler.trustedparts_filecCr)z&Content to put into trusted.gpg.d fileNrrQrrrrz$ShortcutHandler.trustedparts_contentcCr)zMode of trustedparts filerrrQrrrrrz!ShortcutHandler.trustedparts_modecst|j|jfs dS|j}|j}tj| }d}|sjt|d}dd|DWdn1s4wYtt dd}Wdn1sNwYtfdd| Drjt t d|dSt t d ||j s|r||jrtj|jstj|jd d |jrt|d t||jWdn1swYt|d }|dkr|d||jWddS1swYdSdS)zAdd the login credentials corresponding to this repo. By default, if self.netrcparts_content contains content, and self.netrcparts_file points to a file, the file will be created and content placed into it. N rcSg|]}|qSrstripr6rrrr9z-ShortcutHandler.add_login..csg|]}|vqSrrr6linesrrr9sz!Authentication data already in %sz Adding authentication data to %srlrrma)rnetrcparts_filenetrcparts_contentrtrurvry readlinesrrGrFrDrErr4netrcparts_pathrxnetrcparts_moderzwrite)rrcontentnewfile finalcharrrrrresB       "zShortcutHandler.add_logincCsHt|j|jfs dS|j}tdd|jD}tj|s!dSt|d}tdd| D}Wdn1s.rcSrrrr6rrrr9+rz'Authentication data not contained in %sz$Removing authentication data from %srmr)rrrrsrFrtrurvryrrDrErrrOrrri)rrrr filecontentrrrrrhs0     zShortcutHandler.remove_logincCr)z@Return result of apt_pkg.config.find_dir("Dir::Etc::netrcparts")zDir::Etc::netrcpartsrrQrrrr:rzShortcutHandler.netrcparts_pathcCr)z7Get the auth.conf.d filename, without the leading path.confrrQrrrnetrcparts_filename?rz#ShortcutHandler.netrcparts_filenamecCr)z+Get the auth.conf.d absolute-path filename.)rrrrQrrrrDrzShortcutHandler.netrcparts_filecCsRt|j|jfs dSt|jj}t|jj}d||d|jd|jS)zContent to put into auth.conf.d file By default, if both username and password are set, this will return a proper netrc-formatted line with the authentication information, including the hostname and path. Nzmachine z login z password )rr*r+r rr hostnameru)rrrurrrrIs z"ShortcutHandler.netrcparts_contentcCr)zMode of netrcparts fileirrQrrrrXrzShortcutHandler.netrcparts_modecCs:t|}tt|jt|jB|_tt||jd|_dS)zSet the SourceEntry. This should be called from subclasses to set the SourceEntry. The SourceEntry file will be set to the sourceparts_file value. The self.components, if any, will be added to the line's component(s). rZN)rrrsrPr r`rr)rlinerKrrr_set_source_entry]sz!ShortcutHandler._set_source_entrycCs0|j}|sdS|r|d|7}tdd|S)Nz-%sz [^a-z0-9_-]+rE)rresublower)rrbaserrr_encode_filebaseis  z ShortcutHandler._encode_filebasecCs |j|d}|s dSd||fS)Nrz%s.%s)r)rextrrrrrrqs  z%ShortcutHandler._filebase_to_filenamecCs|sdStj||Sr )rtrurO)rrunamerrrrwsz!ShortcutHandler._filename_to_file)NFNNFr ).__name__ __module__ __qualname____doc__r classmethodr"r)r,rLpropertyrRrUrTrNrWrYrr*r+rfrircrgrwrrrnrdrrrrrrrerhrrrrrrrrrrrrrr ,s            %     I*     '(     (&       r c@eZdZdZdS)rz-General Exception during shortcut processing.Nrrrrrrrrr}src@r)InvalidShortcutExceptionzInvalid shortcut. This should only be thrown from the constructor of a ShortcutHandler subclass, and only to indicate that the provided shortcut is invalid for that ShortcutHandler class. Nrrrrrrsr)rrtrr?r:aptsources.distror&softwareproperties.extendedsourceslistrrr contextlibrrrrE urllib.parser initrobjectr Exceptionrrrrrrs(     U