a &c*@sddlmZddlmZddlmZmZmZddlZddlZddl Zddl Zddl Z ddl Z ddl Z ddlZddlZejjGdddejjZddZed Zed Zed Zed Zd dZdS))absolute_import)unicode_literals)_loggerP_Nc@sReZdZdgZedjejjdZ e ddZ ddZ dd Z d d Zd d ZdS)ConfigManagerCommandzconfig-managerz4manage {prog} configuration options and repositories)progcCs|jdddtdd|jdddtd d |jd gd d tdd|jdddtdd |jdddtdd |}|jddddtdd|jddddtdddS)Ncrepo*repozrepo to modify)nargsmetavarhelp--saveF store_truez/save the current options (useful with --setopt))defaultactionr --add-repoappendZURLz8add (and enable) the repo from the specified file or url)rrr r--dumpz,print current configuration values to stdout--dump-variableszprint variable values to stdout --set-enabled set_enabledz"enable repos (automatically saves))rdestrr--set-disabled set_disabledz#disable repos (automatically saves)) add_argumentradd_mutually_exclusive_group)parserZ enable_groupr>/usr/lib/python3.9/site-packages/dnf-plugins/config_manager.py set_argparser)s:z"ConfigManagerCommand.set_argparsercCs|jj}d|_|jjgksd|jjsd|jjsd|jjsd|jjsd|jj sd|jj t d dgd|jjgkr~tt d|jjs|jj s|jjs|jjrd|_dd|jjD}dd|D|j_dS) NTz.one of the following arguments is required: {} )rrrrrz--enablerz --disablez{Warning: --enablerepo/--disablerepo arguments have no meaningwith config manager. Use --set-enabled/--set-disabled instead.cSsg|]}|dkr|dqS),)split).0xrrr _z2ConfigManagerCommand.configure..cSs"g|]}|D]}|dkr |q qS)r)r%Zsublistitemrrr r'as)clidemandsZavailable_reposoptsadd_reposavedumpdump_variablesrrZ optparsererrorrformatjoinZrepos_edrZwarningZ root_userr )selfr,Z temp_listrrr configureBs4   zConfigManagerCommand.configurecCs|jjr|n|dS)zExecute the util action here.N)r-r. modify_repo)r5rrr runds zConfigManagerCommand.runc srgtfdd}jjrfjjD]|dq,tjdrjjD]|dqTn(tjdrjjD]|dq~rtjt dd j j }i}tjdrԈjj rԈjj }jjrj j jD]\}td |fqjjrd jjvrvjjrJ|rJj j j j jd |j|jjrvtj jd tj j sd Sjjsjjrdj_tD]}i}jjrd |d <njjrd|d <tjdrjjD]$\}}t|j|r||qjjr<|r<j j |j|j|j|jjrtj jd|jt|qd S)z< process --set-enabled, --set-disabled and --setopt options cs0jj|}|sn|r,|dS)N)baseZreposZ get_matchingaddextend)keyZadd_matching_reposZmatchingZmatching_reposnameZnot_matching_repos_idr5rr match_reposqs  z5ConfigManagerCommand.modify_repo..match_reposT repo_setoptsFzNo matching repo to modify: %s.z, main_setoptsz%s = %smainN1Zenabled0zrepo: )setr-r hasattrr@keysdnf exceptionsErrorrr4r9confrAr1Z substitutionsitemsprintr/Zwrite_raw_configfileZconfig_file_pathr0outputZ fmtSectionrrsortedfnmatchidupdateZrepofile) r5r?ZsbcZmodifyvalr Z repo_modifyrepoidZsetoptsrr=r r7ksd             z ConfigManagerCommand.modify_repoc CsT|jjj}d}|jjD]}tjj|jdkr@dt j |}t td||drt j |}t j ||}z6|jj|dd}t|j|t |d|WnBty}z*|d 7}t |WYd }~qWYd }~n d }~00qt|}d tjj|}t j |d |}d |||f} t|| sqq|rPtj t!dd|d S)z process --add-repo option rr)zfile://zAdding repo from: %sz.repow+)modeNz$created by {} config-manager from {}z%s.repoz"[%s] name=%s baseurl=%s enabled=1 zConfiguration of repo failedzConfiguration of repos failed)"r9rKZ get_reposdirr-r.rHpycompurlparseschemeospathabspathrinforendswithbasenamer4Zurlopenshutilcopy2r>chmodcloseIOErrorr2sanitize_url_to_fsr3util MAIN_PROG save_to_filerIrJr) r5Z myrepodirZ errors_counturlZdestnameferTZreponamecontentrrr r.s<      $  zConfigManagerCommand.add_repoN)__name__ __module__ __qualname__aliasesrr3rHrhriZsummary staticmethodr!r6r8r7r.rrrr r"s "Brc CszHt|d*}tj||t|dWdn1s<0YWn<ttfy}z t t d||WYd}~dSd}~00dS)NrUrWz&Could not save repo to repofile %s: %sFT) openrHrYZ write_to_filer\rdrfOSErrorrr2r)filenamernfdrmrrr rjs . rjz^\w+:/*(\w+:|www\.)?z[?/:&#|~\*\[\]\(\)\'\\]+z^[,.]*z[,.]*$c Cs(zbt|r`tjjr&|dd}n:t|trB|dd}n |d}t|t r`|d}Wnt t t t fy|Yn0td|}td|}td|}td|}t|dkr|ddd}dt|d}t}|||dd|d|d|}d }t|d|S) zReturn a filename suitable for the filesystem and for repo id Strips dangerous and common characters to create a filename we can use to store the cache in. Zidnazutf-8r)rNzE[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.:-]) RE_SCHEMEmatchrHrYZPY3encodedecode isinstancestrZunicodeUnicodeDecodeErrorUnicodeEncodeError UnicodeError TypeErrorsubRE_SLASHRE_BEGINRE_FINALlenr$hashlibZsha256rRZ hexdigestre)rkparts lastindexZcsumZ allowed_regexrrr rgs.        rg)Z __future__rrZdnfpluginscorerrrrHZdnf.cliZ dnf.pycompZdnf.utilrPrr\rrbZpluginZregister_commandr+ZCommandrrjcompiler{rrrrgrrrr s(  1