3 fLQ@sFddlmZddlmZddlmZmZddlZddlZddl Zddl Zddl Zddl Zddl ZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZdZ dZ!ej"ej#dZ$dej%e$ej&fZ'd e'e!fd e'e fd d Z(ej)d Z*ddZ+ddZ,d-ddZ-ddZ.Gddde/Z0Gddde1Z2Gdddej3j4Z5Gddde/Z6Gdd d ej7j8Z9Gd!d"d"ej3j:Z;Gd#d$d$e;Zej7j?j@ZAej7j?jBZCej7j?jDZEGd)d*d*ej7jFZGGd+d,d,ejHjIZ?dS).)absolute_import)unicode_literals)ucd_NpackagesZ mirrorlistz-_.:z(?P[%s]+)\-[%s]{16}z>^%s\/.*((xml|yaml)(\.gz|\.xz|\.bz2|.zck)?|asc|cachecookie|%s)$z^%s\/%s\/.+rpm$z^.+(solv|solvx)$)metadatarZdbcachednfcCstjjj|}|dkrdS|S)zAReturn index of an invalid character in the repo ID (if present).rN)libdnfrepoRepoZverifyId)Zrepo_idZ first_invalidr /usr/lib/python3.6/repo.pyrepo_id_invalidHsrcGs8x"|D]}|||}|dk r|SqWttd|dS)Nz"no matching payload factory for %s) ValueErrorr)pkgprogressZ factoriesfnploadr r r _pkg2payloadOs   rTc Csdd}|jjddt||dD}t}ytjjjtjj||Wn,t k rv}zt ||_ WYdd}~XnX|j |jj |_xj|D]b}|j}|dks|jdrq|j} | j} | j} |dkr|jj| q| jjj|g|j| <qW|S)NcSs t|d S)NZdelta)hasattr)payloadr r r _download_sort_keyYsz._download_payloads.._download_sort_keycSsg|] }|jqSr )_librepo_target).0rr r r ]sz&_download_payloads..)keyz Not finishedzAlready downloaded)errclearsorted_DownloadErrorsr r PackageTargetZdownloadPackagesZVectorPPackageTarget RuntimeErrorstr_fatalwaitcopy _recoverableZgetErr startswithZ getCallbacks package_ploadr_skippedadd_repoZexpire_pkg_irrecoverable) payloadsZdrpmZ fail_fastrZtargetserrseZtgtr callbacksrrr r r _download_payloadsWs0     r1cCsL|\}}x:|D]2}|j}||kr,||j7}q||j7}||j7}qW||fS)N)r download_size _full_size)Zsavingr-r.realZfullrrr r r _update_savingxs   r5c@s>eZdZddZddZeddZejddZdd Zd S) rcCsi|_i|_d|_t|_dS)N)r,_val_recoverabler#setr))selfr r r __init__sz_DownloadErrors.__init__cCs"|jr |jS|jrd|jgiSiS)N)r,r#)r8r r r _irrecoverables  z_DownloadErrors._irrecoverablecCs|jS)N)r6)r8r r r r&sz_DownloadErrors._recoverablecCs ||_dS)N)r6)r8Znew_dctr r r r&scCs|j|jkrdS|jS)Nr)rr)r2)r8rr r r _bandwidth_useds z_DownloadErrors._bandwidth_usedN) __name__ __module__ __qualname__r9r;propertyr&setterr<r r r r rs  rc@seZdZddZdS)_DetailedLibrepoErrorcCs,tj||jd|_|jd|_||_dS)Nr) Exceptionr9argsZ librepo_codeZ librepo_msg source_url)r8Z librepo_errrFr r r r9s   z_DetailedLibrepoError.__init__N)r=r>r?r9r r r r rBsrBc@seZdZddZdS)_NullKeyImportcCsdS)NTr )r8iduserid fingerprinturl timestampr r r _confirmsz_NullKeyImport._confirmN)r=r>r?rMr r r r rGsrGc@s eZdZddZeddZdS)MetadatacCs ||_dS)N)r+)r8r r r r r9szMetadata.__init__cCs |jjS)N)r+fresh)r8r r r rOszMetadata.freshN)r=r>r?r9r@rOr r r r rNsrNcs4eZdZfddZddZddZddZZS) PackageTargetCallbackscstt|j||_dS)N)superrPr9r()r8r() __class__r r r9szPackageTargetCallbacks.__init__cCs|jjd||dS)Nr)r(_end_cb)r8statusmsgr r r endszPackageTargetCallbacks.endcCs|jjd||dS)Nr)r( _progress_cb)r8totalToDownload downloadedr r r rszPackageTargetCallbacks.progresscCs|jjd||dS)Nr)r(_mirrorfail_cb)r8rUrKr r r mirrorFailuresz$PackageTargetCallbacks.mirrorFailure)r=r>r?r9rVrr[ __classcell__r r )rRr rPs rPcsHeZdZfddZddZddZddZed d Zd d Z Z S) PackagePayloadcs$tt|j|t||_||_dS)N)rQr]r9rPr0r)r8rr)rRr r r9s zPackagePayload.__init__cCsRtjj}|dkrtjj}n$|jdr(dS|tjjjkr>tjj }|j j |||dS)z"End callback to librepo operation.Nz Not finished) rcallbackZ STATUS_FAILEDZ STATUS_OKr'r r PackageTargetCBZTransferStatus_ALREADYEXISTSZSTATUS_ALREADY_EXISTSrrV)r8cbdataZ lr_statusrUrTr r r rSs  zPackagePayload._end_cbcCs|jj|tjj|dS)N)rrVrr^Z STATUS_MIRROR)r8r`rrKr r r rZszPackagePayload._mirrorfail_cbc CsXy|jj||Wn@tk rRtj\}}}tj|||}tjdj|YnXdS)Nr:) rrDsysexc_info tracebackformat_exceptionloggercriticaljoin)r8r`totaldoneexc_type exc_value exc_traceback except_listr r r rWs zPackagePayload._progress_cbcCs|jS)N)r2)r8r r r r3szPackagePayload._full_sizec Cs|j}|j}tjj||d||j|j|jd}|j|j t j j |j j |d|d|d|d|d|d|d d d |j S) NT)destresumer`Z progresscbZendcbZmirrorfailurecb relative_urlrn checksum_typechecksum expectedsizebase_urlror)rpkgdirrutil ensure_dirrWrSrZupdate_target_paramsr r r r+r0)r8rruZ target_dctr r r rs   zPackagePayload._librepo_target) r=r>r?r9rSrZrWr@r3rr\r r )rRr r]s    r]c@s(eZdZddZddZeddZdS) RPMPayloadcCstjj|jjS)N)ospathbasenamerlocation)r8r r r __str__szRPMPayload.__str__cCsT|j}|j\}}tjjj|}|tjjjkr>tjt d||j |||j |j dS)Nzunsupported checksum type: %s)rprqrrrsrt) rZ returnIdSumr r r Z checksumTypeZChecksumType_UNKNOWNrewarningrr~ downloadsizebaseurl)r8rZctypeZcsumZ ctype_coder r r rys zRPMPayload._target_paramscCs|jjS)zTotal size of the download.)rr)r8r r r r2szRPMPayload.download_sizeN)r=r>r?rryr@r2r r r r rzsrzcs@eZdZfddZddZddZddZed d ZZ S) RemoteRPMPayloadcstt|jd|||_d|_||_|jjp.d|jjjd}t j |j dj dd}d|}t jj|jj|d|_tjj|jt jj|j|jjd |_dS) NZ unused_objectrr:Zbasearchutf8z commandline-r/)rQrr9remote_location remote_sizeconfZ releasever substitutionsgethashlibZsha256encodeZ hexdigestr{r|rgZcachedirrurrvrwrlstripZ local_path)r8rrrsZdigestZrepodir)rRr r r9szRemoteRPMPayload.__init__cCstjj|jS)N)r{r|r}r)r8r r r r)szRemoteRPMPayload.__str__c Cs^||_y|jj||Wn@tk rXtj\}}}tj|||}tjdj |YnXdS)Nr:) rrrDrarbrcrdrerfrg)r8r`rhrirjrkrlrmr r r rW,szRemoteRPMPayload._progress_cbc Cs<tjj|jjtjj|j|j dddtjj |jddd|j S)NrT) r r r r_configr{r|r}rrudirnamer0)r8r r r r5sz RemoteRPMPayload._librepo_targetcCs|jS)zTotal size of the download.)r)r8r r r r2;szRemoteRPMPayload.download_size) r=r>r?r9rrWrr@r2r\r r )rRr rs   rcszeZdZfddZddZddZddZd d Zd d Ze d dZ e ddZ e j ddZ ddZ ddZZS) MDPayloadcs.tt|j|d|_d|_d|_t|_dS)Nr:rF)rQrr9_text_download_sizefastest_mirror_runningr7mirror_failures)r8r)rRr r r9Cs zMDPayload.__init__cCstjjr|jS|jjdSdS)Nzutf-8)rpycompZPY3rr)r8r r r rJszMDPayload.__str__cCs|jS)N)r)r8r r r __unicode__PszMDPayload.__unicode__cCs||_|jj||dS)N)rr)r8r`rhrir r r rWSszMDPayload._progress_cbcCs\|tjjjkr"td|}d|_n*|tjjjkrH|jrH|rBd|nd}ndS|jj|dS)Nz,determining the fastest mirror (%s hosts).. Tz error: %s zdone. ) r r RepoCBZFastestMirrorStage_DETECTIONrrZFastestMirrorStage_STATUSrmessage)r8r`stagedatarUr r r _fastestmirror_cbWs zMDPayload._fastestmirror_cbcCs&|jj|d||f}tj|dS)Nzerror: %s (%s).)rr*redebug)r8r`rUrKrr r r _mirror_failure_cbcs  zMDPayload._mirror_failure_cbcCs|jS)N)r)r8r r r r2hszMDPayload.download_sizecCs|jS)N) _progress)r8r r r rlszMDPayload.progresscCs|dkrtjj}||_dS)N)rr^NullDownloadProgressr)r8rr r r rps cCs||_|jjdddS)NrCr)rrstart)r8textr r r rvszMDPayload.startcCsd|_|jj|dddS)Nr)rrrV)r8r r r rVzsz MDPayload.end)r=r>r?r9rrrWrrr@r2rrArrVr\r r )rRr rAs    rcsLeZdZfddZddZddZddZd d Zd d Zd dZ Z S) RepoCallbackscs tt|j||_|j|_dS)N)rQrr9r+ _md_pload)r8r )rRr r r9szRepoCallbacks.__init__cCs|jj|dS)N)rr)r8Zwhatr r r rszRepoCallbacks.startcCs|jjdS)N)rrV)r8r r r rVszRepoCallbacks.endcCs|jjd||dS)Nr)rrW)r8rXrYr r r rszRepoCallbacks.progresscCs|jjd||dS)N)rr)r8rZptrr r r fastestMirrorszRepoCallbacks.fastestMirrorcCs|jjd|||dS)Nr)rr)r8rUrKrr r r handleMirrorFailuresz!RepoCallbacks.handleMirrorFailurecCs|jjj|||||S)N)r+ _key_importrM)r8rHrIrJrKrLr r r repokeyImportszRepoCallbacks.repokeyImport) r=r>r?r9rrVrrrrr\r r )rRr rs rcseZdZeZd7fdd ZeddZeddZej ddZed d Z d d Z e j d d Z eddZ eddZ e j ddZ ddZddZfddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd9d5d6ZZS):r Ncstt|j||d|jjjtjj|r.|nd|j|_t t j j |_ t||_|jjj|jj|jd|_t|_d|_|jj|r|jrtn|j|r|jj|jt jjj|_|dk r|jnd|_dS)N)Zsectionparentr:T) rQr r9rthisZdisownr r r+rrr^rrrZ _callbacksZ setCallbacks_pkgdirrGrrZsetSyncStrategyZ cacheonlySYNC_ONLY_CACHE DEFAULT_SYNCZsetSubstitutionsrrZ SubstitutionsZ_substitutionsZcheck_config_file_ageZ_check_config_file_age)r8nameZ parent_conf)rRr r r9s   z Repo.__init__cCs |jjS)N)r+ZgetId)r8r r r rHszRepo.idcCs |jjS)N)r+ZgetRepoFilePath)r8r r r repofilesz Repo.repofilecCs|jj|dS)N)r+ZsetRepoFilePath)r8valuer r r rscCs|jjr|jjS|jS)N)r+ZisLocalZgetLocalBaseurl cache_pkgdir)r8r r r rus  z Repo.pkgdircCs$|jdk r|jStjj|jjtS)N)rr{r|rgr+ getCachedir_PACKAGES_RELATIVE_DIR)r8r r r rs zRepo.cache_pkgdircCs ||_dS)N)r)r8valr r r ruscCstjj|jjdS)NZpubring)r{r|rgr+r)r8r r r _pubring_dirszRepo._pubring_dircCs |jjS)N)r+ZgetLoadMetadataOther)r8r r r load_metadata_otherszRepo.load_metadata_othercCs|jj|dS)N)r+ZsetLoadMetadataOther)r8rr r r rscCs |j|jkS)N)rH)r8otherr r r __lt__sz Repo.__lt__cCsd|jj|jfS)Nz<%s %s>)rRr=rH)r8r r r __repr__sz Repo.__repr__cstt|j||dS)N)rQr __setattr__)r8rr)rRr r rszRepo.__setattr__cCs|jjdS)N)r+disable)r8r r r rsz Repo.disablecCs|jjdS)N)r+enable)r8r r r rsz Repo.enablecCs|jj|dS)a/Ask for additional repository metadata type to download. Given metadata_type is appended to the default metadata set when repository is downloaded. Parameters ---------- metadata_type: string Example: add_metadata_type_to_download("productid") N)r+ZaddMetadataTypeToDownload)r8 metadata_typer r r add_metadata_type_to_downloads z"Repo.add_metadata_type_to_downloadcCs|jj|dS)aIStop asking for this additional repository metadata type in download. Given metadata_type is no longer downloaded by default when this repository is downloaded. Parameters ---------- metadata_type: string Example: remove_metadata_type_from_download("productid") N)r+ZremoveMetadataTypeFromDownload)r8rr r r "remove_metadata_type_from_downloadsz'Repo.remove_metadata_type_from_downloadcCs |jj|S)zReturn path to the file with downloaded repository metadata of given type. Parameters ---------- metadata_type: string )r+ZgetMetadataPath)r8rr r r get_metadata_pathszRepo.get_metadata_pathcCs |jj|S)zReturn content of the file with downloaded repository metadata of given type. Content of compressed metadata file is returned uncompressed. Parameters ---------- metadata_type: string )r+ZgetMetadataContent)r8rr r r get_metadata_content!s zRepo.get_metadata_contentcCsd}zy|jj}Wnttjjtfk r}zP|jjrhd|j}x|jjD]}|d|7}qJWt j |t j j t|WYdd}~XnXWdt|j_Xt|j|_|S)aLoad the metadata for this repo. Depending on the configuration and the age and consistence of data available on the disk cache, either loads the metadata from the cache or downloads them from the mirror, baseurl or metalink. This method will by default not try to refresh already loaded data if called repeatedly. Returns True if this call to load() caused a fresh metadata download. Fz7Errors during downloading metadata for repository '%s':z - %sN)r+loadr errorErrorr!rrrHrerr exceptionsZ RepoErrorr"r7rNr)r8retr/rUZfailurer r r r-s  &  z Repo.loadcCsP|js|jjd|jrL|jdkr&dS|jj}|jjrDtd|}d|fSdS) a)Get the number of seconds after which the cached metadata will expire. Returns a tuple, boolean whether there even is cached metadata and the number of seconds it will expire in. Negative number means the metadata has expired already, None that it never expires. FrCTNr)TN)Fr)rr+Z loadCacheZmetadata_expireZ getExpiresInZ isExpiredmin)r8Z expirationr r r _metadata_expire_inJs     zRepo._metadata_expire_incCs ||_dS)N)r)r8Z key_importr r r _set_key_import]szRepo._set_key_importcCs ||j_dS)N)rr)r8rr r r set_progress_bar`szRepo.set_progress_barcCs |jjS)zoReturns user defined http headers. Returns ------- headers : tuple of strings )r+ZgetHttpHeaders)r8r r r get_http_headersdszRepo.get_http_headerscCs|jj|dS)aSets http headers. Sets new http headers and rewrites existing ones. Parameters ---------- headers : tuple or list of strings Example: set_http_headers(["User-Agent: Agent007", "MyFieldName: MyFieldValue"]) N)r+ZsetHttpHeaders)r8Zheadersr r r set_http_headersns zRepo.set_http_headershttpftpfilehttpscs@fdd}sdS|jj}|r,||S|jr<||jSdS)z :param location: relative location inside the repo :param schemes: list of allowed protocols. Default is ('http', 'ftp', 'file', 'https') :return: absolute url (string) or None csZxT|D]L}r>tjjj|d}|krRtjj|jdSqtjj|jdSqWdS)Nrr)rrZurlparser{r|rgr)Zurl_listrKr)r~schemesr r schemes_filters z,Repo.remote_location..schemes_filterN)r+Z getMirrorsr)r8r~rrZmirrorsr )r~rr r{s  zRepo.remote_location)NNrrrr)r)r=r>r?SYNC_TRY_CACHErr9r@rHrrArurrrrrrrrrrrrrrrrrrrr\r r )rRr r s6          r )T)JZ __future__rrZdnf.i18nrrZ dnf.callbackrZdnf.confZdnf.conf.substitutionsZ dnf.constZ dnf.cryptoZdnf.exceptionsZ dnf.loggingZ dnf.pycompZdnf.utilZ dnf.yum.miscZ libdnf.errorr Z libdnf.repo functoolsrZhawkeyZloggingoperatorr{reZshutilstringraZtimercrZ_MIRRORLIST_FILENAMEZ ascii_lettersZdigitsZ _REPOID_CHARSescapeZ hexdigitsZ _CACHEDIR_REZ CACHE_FILESZ getLoggerrerrr1r5objectrrDrBr^Z KeyImportrGrNr r_rPZPayloadr]rzrrr ZSyncStrategy_LAZYZ SYNC_LAZYZSyncStrategy_ONLY_CACHErZSyncStrategy_TRY_CACHErrrrZRepoConfr r r r sl       !  8&?