3 fZv@sddlmZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddlZddl m Z m Z ddlZddlmZddlmZddlZy$ddlmZmZmZmZddZWnLek rd d Zydd lmZWnek rd dZYnXYnXd Zee de dgZee de ddgZerdddl m!Z!m"Z"m#Z#ddl$m%Z%m&Z&m'Z'n(ddl!m!Z!m"Z"m#Z#ddl(m%Z%m&Z&m'Z'ej)j*Gdddej+j,Z-ej)j*Gddde-Z.dS))print_functionN)_logger)PY3)ucd)nameversioncodenameos_release_attrcCstttfS)N)rrr r r /usr/lib/python3.6/copr.pylinux_distribution.sr cCsdS)Nr )rr r r r 1sr )r cCsrtd`}i}xF|D]>}y$|jjd\}}|jd||<Wqtk rPYqXqW|d|ddfSQRXdS)Nz/etc/os-release="NAMEZ VERSION_ID)openrstripsplitstrip ValueError)Zos_release_fileZos_release_datalineZos_release_keyZos_release_valuer r r r 7s   copryesynonr) ConfigParser NoOptionErrorNoSectionError)urlopen HTTPErrorURLErrorc@s eZdZdZdZdZdZdZdZedeZ d8Z e d Z d Z e d Zed d ZddZddZddZddZddZddZddZddZddZd d!Zd"d#Zed$d%Zd&d'Zd(d)Zd*d+Z d,d-Z!d.d/Z"d0d1Z#ed2d3Z$ed4d5Z%ed6d7Z&dS)9 CoprCommandz Copr plugin for DNF Nzcopr.fedorainfracloud.orgZfedoraZhttpsiz://rz Interact with Copr repositories.Ta enable name/project [chroot] disable name/project remove name/project list --installed/enabled/disabled list --available-by-user=NAME search project Examples: copr enable rhscl/perl516 epel-6-x86_64 copr enable ignatenkobrain/ocltoys copr disable rhscl/perl516 copr remove rhscl/perl516 copr list --enabled copr list --available-by-user=ignatenkobrain copr search tests c Cs|jddddddddgd |j}|jd d td d |jdd tdd |jdd tdd |jddtdd|jdtdd|jddddS)N subcommandhelpenabledisableremovelistsearch)nargschoicesz --installed store_truez.List all installed Copr repositories (default))actionr&z --enabledzList enabled Copr repositoriesz --disabledzList disabled Copr repositoriesz--available-by-userrz-List available Copr repositories by user NAME)metavarr&z--hubz(Specify an instance of Copr to work with)r&arg*)r,) add_argumentZadd_mutually_exclusive_groupr)parserZ list_optionr r r set_argparserps    zCoprCommand.set_argparsercCs|jjjjdkrdSd}t}g}|jjjd}tjj |t d}tjj |r|j ||j ||jddr|jddr|jdd}|jdd}||g|_n ddg|_xHtjtjj |t dD],}|jdrtjj |t d|} |j | qWg} t|jjr|jjdjd } t| d krV|jjrVtjtd td tjjtd nL|jj rt| d kr|j|_|j|_n t| d kr| d}n|jj}|rH|rHd|_|j t |dd|j!||dd} | rH|j!||d|j"} |j!||d|j#} | |_| d| |_t$| |j#krH|jd| 7_|jd| 7_|jsd|krr||_|j"d||_n|jddd|_||_dS)Nrrz.confmain distribution releaseverFz.d/zError: z^specify Copr hub either with `--hub` or using `copr_hub/copr_username/copr_projectname` formatzmultiple hubs specifiedT)reversehostnameprotocolportz://:r%)%cliZcommandoptsrbaseconfZpluginconfpathospathjoin PLUGIN_CONFisfileappendreadZ has_optionget chroot_configlistdirendswithlenr1rhubrcriticalrdnfCliErrordefault_hostname copr_hostname default_urlcopr_urlsorted_read_config_itemdefault_protocol default_portint)selfZcopr_hubZcopr_plugin_configZ config_filesZ config_pathZdefault_config_filer7r8filenameZ config_fileprojectr<r=r>r r r configuresl                 zCoprCommand.configurec Cs*y |j||Sttfk r$|SXdS)N)rKrr)r]configrPZsectiondefaultr r r rYs zCoprCommand._read_config_itemcCstjjdj|jdS)Nz{0} )sysstderrwriteformatr)r]textr r r _user_warning_before_promptsz'CoprCommand._user_warning_before_promptc Cs|jjd}|dkr&|jjj|dS|dkrl|jjrH|j|jjdS|j|jj j d|jj |jj dSy|jj d}WnLttfk rtjtdtd|jjj|tjjtdYnXy\|jj d}t|jj dkrtjjtd|jd |_t|jd kr$tjjtd Wn*tk rP|j}|jd |_YnX|d krj|j|dS|jd }t|dkrtjtdtdtjjtdnexactly two additional parameters to copr command are requiredr%zToo many arguments.-r:zOBad format of optional chroot. The format is distribution-version-architecture.r+r9zEuse format `copr_username/copr_projectname` to reference copr projectzbad copr project formatz{0}/_copr:{1}:{2}:{3}.repor'a Enabling a Copr repository. Please note that this repository is not part of the main distribution, and quality may vary. The Fedora Project does not exercise any power over the contents of this repository beyond the rules outlined in the Copr FAQ at , and packages are not held to any quality or security level. Please do not file bug reports about these packages in Fedora Bugzilla. In case of problems, contact the owner of this repository. z!Do you really want to enable {0}?z Repository successfully enabled.r(z!Repository successfully disabled.r)z Repository successfully removed.zUnknown subcommand {}.)rir:)(rAr$r@Z optparserZ print_helpZavailable_by_user_list_user_projects_list_installed_repositoriesrBrCZreposdirenabledZdisabledr1r IndexErrorrrQrrRrSrO exceptionsErrorr chroot_parts _guess_chroot_searchrf get_reposdirrU_sanitize_username _need_rootrF _ask_user_download_repoinfo_runtime_deps_warning _disable_repo _remove_repo) r]r$ project_namechrootr_ copr_usernamecopr_projectname repo_filenamerymsgr r r runs                     zCoprCommand.runcCs|jjdd}tjd|j|}|j|jko8tjd|}tjd|}|jjr`| rh| rhdSn|shdStjd|rxdStjd|rdS|j } | r|s| r|rdSd} tjd |r|jd d \} } } }| d| d|}ntjd|r2|jd d}|j d d djd dd} | d|dd|d}n.|jd d}|j d|dd|d}d} | sn|d7}| r||d7}t || S)Nr9r%z_copr:Z_copr_z_copr:|^_copr_zcopr:.*:.*:.*:mlz coprdep:.*Fzcopr:r?r:rjrirTz (disabled)z *) repofilerrematchrUrWrVrArPrmrsplitrTprint)r]repo_idrepo enabled_only disabled_only file_nameZ match_newZ match_oldZ match_anyrmold_reporrUZ copr_ownerZcopr_dirrZ copr_namer r r _list_repo_file8sB       zCoprCommand._list_repo_filecCsFd}x,|jjjD]\}}|j||||rd}qW|rBttddS)NFTz* These coprs have repo file with an old format that contains no information about Copr hub - the default one was assumed. Re-enable the project to fix this.)rBrepositemsrrr)r]Z directoryrrrrrr r r rlis z(CoprCommand._list_installed_repositoriesc Csdj|}|j|}|jj|dd}ytj|j}Wn*tk r`tj j t dj|YnX|j |t dj|}|j |xL|dD]@}dj||d}|d pt d } |jjjt|| }t|qWdS) Nz!/api_3/project/list?ownername={0}zw+)modez+Can't parse repositories for username '{}'.zList of {} coprsrz {0}/{1} : r descriptionzNo description given)rfrWrBr jsonloadsrJrrRrorpr_check_json_output_print_match_sectionoutput fmtKeyValFillrr) r]Z user_nameapi_pathurlres json_parse section_textitemrdescr r r rkss"    zCoprCommand._list_user_projectsc Csdj|}|j|}|jj|dd}ytj|j}Wn*tk r`tj j t dj|YnX|j |t dj|}|j |xJ|dD]>}dj|d}|d pt d } |jjjt|| }t|qWdS) Nz/api_3/project/search?query={}zw+)rzCan't parse search for '{}'.z Matched: {}rz{0} : Z full_namerzNo description given.)rfrWrBr rrrJrrRrorprrrrrrr) r]Zqueryrrrrrrrrr r r rss     zCoprCommand._searchcCs|jjj|}t|dS)N)rBrZ fmtSectionr)r]rgZ formattedr r r rsz CoprCommand._print_match_sectioncCsj|jstjjdd|_tjjdj|j|jjrf|jjj sb|jj j dj|dj|d rfdSdS)N Fz{0} z {} [y/N]: z {} [Y/n]: )rZdefaultyes_msgT) first_warningrcrdrerfrrBZ _promptWantedrCZassumenorZ userconfirm)r]ryrr r r _ask_user_no_raises  zCoprCommand._ask_user_no_raisecCs |j||stjjtddS)NzSafe and good answer. Exiting.)rrRrorpr)r]ryrr r r rws zCoprCommand._ask_usercCs tjdkrtjjtddS)Nrz/This command has to be run under the root user.)rDgeteuidrRrorpr)clsr r r rvs zCoprCommand._need_rootcs|jdks&ddks&ddkr,t|jjjd}tfdddDrd krbd |}n&d td krxd |}ndjd|}ndkrtj d}dkrdj|}ndjd|}nPdkrtj d}dkrdj|}ndjd|}nddj ddd}|S)z2 Guess which chroot is equivalent to this machine NrFr%Zbasearchcsg|] }|kqSr r ).0r)distr r sz-CoprCommand._guess_chroot..Fedora Fedora LinuxZRawhidezfedora-rawhide-ZrawhideZredhat_support_product_versionzfedora-{0}-{1}ZMageiaz%{distro_arch}ZCauldronzmageia-cauldron-{}zmageia-{0}-{1}ZopenSUSEz%{_target_cpu}Z Tumbleweedzopensuse-tumbleweed-{}zopensuse-leap-{0}-{1}zepel-%s-x86_64.)rr) rLr rBrC substitutionsanyr rfrpmZ expandMacror)r]Zdistarchr~r )rr rrs,        zCoprCommand._guess_chrootcCsdj|jdd}|jd}dj|||}y*t|j|}tjj|rRtj|Wn^t k rl}z|j dkrt dj|j||j t |}t jj|t d}|jjd} | r>tj| jd} tj| } |t d jdj|j|7}| jd r0|t d d jd d| d D7}|t dj|7}t jj|n|t dj|7}t jj|WYdd}~XnJtk r}z,t dj|j||jj}t jj|WYdd}~XnX|j} | jd} tjd| rtjj|jjjd| ddd}|j|j krR|j!dddj!|j"dj!dddj!ddj!dd} tjj| rRtj| t#|d.}|j$| x|j%D]} |j$| qrWWdQRXtj&|t'j(t'j)Bt'j*Bt'j+BdS) Nrjr%z%/coprs/{0}/repo/{1}/dnf.repo?arch={2}iz Request to {0} failed: {1} - {2}z+It wasn't possible to enable this project. zCopr-Error-Datazutf-8z1Repository '{0}' does not exist in project '{1}'.zavailable chrootsz Available repositories: z, css|]}dj|VqdS)z'{}'N)rf)rxr r r sz-CoprCommand._download_repo..z If you want to enable a non-default repository, use the following command: 'dnf copr enable {0} ' But note that the installed repo file will likely need a manual modification.zProject {0} does not exist.zFailed to connect to {0}: {1}z\[copr:rriz.repoz_copr:_coprrr?Zgroup_@wbrr),rFrqrfr rWrDrEexistsr)r!coderstrrRrorpZheadersrKbase64Z b64decodedecoderrr"reasonstrerrorreadlinerrrBrCrtrVreplacerUrre readlineschmodstatS_IRUSRS_IWUSRS_IRGRPS_IROTH)r]r}rZ short_chrootZarchrZresponseeZ error_msgZ error_dataZerror_data_decodedZ first_linerZold_repo_filenamefr r r rxsX           $   zCoprCommand._download_repocs|jjdd|jj|j|j||g}x(jjD]}|jdrJq:|j|q:W|s`dSt d}t j d|j dj fdd |D}|j|t d sx,|D]$}|jjjj||jjjd d iqWdS) a, In addition to the main copr repo (that has repo ID prefixed with `copr:`), the repofile might contain additional repositories that serve as runtime dependencies. This method informs the user about the additional repos and provides an option to disable them. T)rzcopr:NaMaintainer of the enabled Copr repository decided to make it dependent on other repositories. Such repositories are usually necessary for successful installation of RPMs from the main Copr repository (they provide runtime dependencies). Be aware that the note about quality and bug-reporting above applies here too, Fedora Project doesn't control the content. Please review the list: {0} These repositories have been enabled automatically.r%z cs*g|]"}djt|jj|ddqS)z){num:2}. [{repoid}] baseurl={baseurl}baseurl)Znumrepoidr)rfnextcfgZgetValue)rr)counterrr r r9sz5CoprCommand._runtime_deps_warning..z!Do you want to keep them enabled?rm0)rBresetZread_all_repos_get_copr_reporursections startswithrIr itertoolscountrfrFrrCwrite_raw_configfilerr)r]rrZ runtime_depsrryZdepr )rrr rzs*      z!CoprCommand._runtime_deps_warningc Csdj|jjddd|j||}||jjkrdj|j||}}||jjkrd|jj|jkr|jj|jjdd }y.|jdddjddd}||jkrdSWqtk rYqXndS|jj|S) Nzcopr:{0}:{1}:{2}r?r%rz{0}-{1}rr9rir) rfrUrrurBrrrrn)r]rrrrrUr r r rFs     zCoprCommand._get_copr_repocCst|j||}|s,tjjtdj|j||ytj|j Wn2t k rn}ztjjt |WYdd}~XnXdS)Nz&Failed to remove copr repo {0}/{1}/{2}) rrRrorprrfrUrDr)rOSErrorr)r]rrrrr r r r|\s zCoprCommand._remove_repocCsd|j||}|dkr,tjjtdj||x2|jjD]$}|jj j |j ||jj j ddiq8WdS)Nz!Failed to disable copr repo {}/{}rmr) rrRrorprrfrrrBrCrrr)r]rrrrr r r r{hs  zCoprCommand._disable_repoc Cs<ytj|j}Wn$tk r6tjjtddSX|S)z Wrapper around response from server check data and print nice error in case of some error (and return None) otherwise return json object. zUnknown response from server.N)rrrJrrRr@rSr)rrrr r r _get_datats zCoprCommand._get_datacCs"d|krtjjdj|ddS)Nerrorz{})rRrorprf)rZjson_objr r r rszCoprCommand._check_json_outputcCs&|ddkrdj|ddS|SdS)Nrrzgroup_{}r%)rf)rrr r r rus zCoprCommand._sanitize_username)r)'__name__ __module__ __qualname____doc__rLrTZ default_hubrZr[rValiasesrsummaryrusage staticmethodr5r`rYrhrrrlrkrsrrrw classmethodrvrrrxrzrr|r{rrrur r r r r#PsD  L_1   %82   r#c@sDeZdZdZdZedZdZddZddZ e d d Z d d Z d S)PlaygroundCommandz Playground plugin for DNF playgroundz$Interact with Playground repository.z [enable|disable|upgrade]c Cs0|j|jtdtddj|j}|jj|dd}|j|}|j|ddkrft j j tdx|d D]}d j|d |d }d j|jj j |jdd}yj||dkrwpdj|j||}|jj|dd}|j|}|j|od|ko|ddkr |j||Wqpt jjk r&YqpXqpWdS)Nz!Enabling a Playground repository.zDo you want to continue?z{0}/api/playground/list/zw+)rrokzUnknown response from server.rz{0}/{1}ZusernameZcoprnamez{}/_playground_{}.repor9rjZchrootsz{0}/api/coprs/{1}/detail/{2}/)rvrwrrfrWrBr rcloserRr@rSrCrtrrxrorp) r]r~Zapi_urlrrrr}rZoutput2r r r _cmd_enables8         zPlaygroundCommand._cmd_enablecCs6|jx(tjdj|jjjD]}|j|q WdS)Nz{}/_playground_*.repo)rvglobrfrBrCrtr|)r]rr r r _cmd_disableszPlaygroundCommand._cmd_disablecCs|jdddddgddS)Nr$r%r'r(upgrade)r,r-)r3)r4r r r r5szPlaygroundCommand.set_argparsercCstjjd|jjd}|j}|dkrB|j|tjt dn`|dkrb|j tjt dn@|dkr|j |j|tjt dntjjt d j |dS) Nz%Playground is temporarily unsupportedrr'z-Playground repositories successfully enabled.r(z.Playground repositories successfully disabled.rz-Playground repositories successfully updated.zUnknown subcommand {}.) rRrorprAr$rrrrryrrrf)r]r$r~r r r rs    zPlaygroundCommand.runN)r) rrrrrrrrrrrr5rr r r r rs  r)/Z __future__rrrrrDrZshutilrrcrZdnfpluginscorerrrRZ dnf.pycomprZdnf.i18nrrZdistrorrr r r ImportErrorplatformrGsetZYESZNOZ configparserrrrZurllib.requestr r!r"Zurllib2ZpluginZregister_commandr@ZCommandr#rr r r r sP      B