???<!-- GIF89;a -->
123123123123
.....................................................................................................................................???<!-- GIF89;a -->
123123123123
.....................................................................................................................................3
fZv                 @   s  d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlZd dlmZmZ d dlZd dlmZ d dlmZ d dlZy$d dlmZmZmZmZ dd ZW nL ek
r   d	d
 Zyd dlmZ W n ek
r   dd ZY nX Y nX dZeededgZeededdgZerdd dl m!Z!m"Z"m#Z# d dl$m%Z%m&Z&m'Z' n(d dl!m!Z!m"Z"m#Z# d dl(m%Z%m&Z&m'Z' ej)j*G dd dej+j,Z-ej)j*G dd de-Z.dS )    )print_functionN)_logger)PY3)ucd)nameversioncodenameos_release_attrc               C   s   t  t t fS )N)r   r   r	    r   r   /usr/lib/python3.6/copr.pylinux_distribution.   s    r   c             C   s   dS )N r   )r   r   r   r   r
   1   s    r
   )r   c              C   sr   t d`} i }xF| D ]>}y$|j jd\}}|jd||< W q tk
rP   Y qX qW |d |d d fS Q R X d S )Nz/etc/os-release="NAMEZ
VERSION_ID)openrstripsplitstrip
ValueError)Zos_release_fileZos_release_datalineZos_release_keyZos_release_valuer   r   r   r   7   s    


copryesynonr   )ConfigParserNoOptionErrorNoSectionError)urlopen	HTTPErrorURLErrorc               @   s  e Zd ZdZdZdZdZdZdZed e Z	d8Z
ed	Zd
ZedZedd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zed$d% Zd&d' Zd(d) Zd*d+ Z d,d- Z!d.d/ Z"d0d1 Z#ed2d3 Z$ed4d5 Z%ed6d7 Z&dS )9CoprCommandz Copr plugin for DNF Nzcopr.fedorainfracloud.orgZfedoraZhttpsi  z://r   z 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          	   C   s   | j ddddddddgd	 | j }|j d
dtdd |j ddtdd |j ddtdd |j ddtdd | j dtdd | j ddd d S )N
subcommand   helpenabledisableremovelistsearch)nargschoicesz--installed
store_truez.List all installed Copr repositories (default))actionr&   z	--enabledzList enabled Copr repositoriesz
--disabledzList disabled Copr repositoriesz--available-by-userr   z-List available Copr repositories by user NAME)metavarr&   z--hubz(Specify an instance of Copr to work with)r&   arg*)r,   )add_argumentZadd_mutually_exclusive_groupr   )parserZlist_optionr   r   r   set_argparserp   s    zCoprCommand.set_argparserc             C   s  | j jjjdkrd S d }t }g }| jjjd }tjj	|t
d }tjj|r|j| |j| |jddr|jddr|jdd}|jdd}||g| _n
ddg| _xHtjtjj	|t
d D ],}|jdrtjj	|t
d |}	|j|	 qW g }
t| jjr| jjd jd	}
t|
d
krV| jjrVtjtdtd  tj jtdnL| jj rt|
d
kr| j| _| j| _n t|
d
kr|
d }n| jj}|rH|rHd | _|jt |dd | j!||dd }|rH| j!||d| j"}| j!||d| j#}|| _|d | | _t$|| j#krH|  jd| 7  _|  jd| 7  _| jsd|krr|| _| j"d | | _n|jddd | _|| _d S )Nr   r   z.confmaindistribution
releaseverFz.d/   zError: z^specify Copr hub either with `--hub` or using `copr_hub/copr_username/copr_projectname` formatzmultiple hubs specifiedT)reversehostnameprotocolportz://:r%   )%cliZcommandoptsr   baseconfZpluginconfpathospathjoinPLUGIN_CONFisfileappendreadZ
has_optiongetchroot_configlistdirendswithlenr1   r   hubr   criticalr   dnfCliErrordefault_hostnamecopr_hostnamedefault_urlcopr_urlsorted_read_config_itemdefault_protocoldefault_portint)selfZcopr_hubZcopr_plugin_configZconfig_filesZconfig_pathZdefault_config_filer7   r8   filenameZconfig_fileprojectr<   r=   r>   r   r   r   	configure   sl    








zCoprCommand.configurec             C   s*   y|j ||S  ttfk
r$   |S X d S )N)rK   r   r   )r]   configrP   Zsectiondefaultr   r   r   rY      s    zCoprCommand._read_config_itemc             C   s   t jjdj|j  d S )Nz{0}
)sysstderrwriteformatr   )r]   textr   r   r   _user_warning_before_prompt   s    z'CoprCommand._user_warning_before_promptc       
      C   s  | j jd }|dkr&| jjj|  dS |dkrl| j jrH| j| j j d S | j| jj	j
d | j j| j j d S y| j jd }W nL ttfk
r   tjtdtd  | jjj|  tjjtdY nX y\| j jd }t| j jdkrtjjtd|jd	| _t| jd
k r$tjjtdW n* tk
rP   | j }|jd	| _Y nX |dkrj| j| d S |jd}t|dkrtjtdtd  tjjtdn<t|dkr|d }|d }n|d }|d }|d | }dj| jj	j| j| j||}|dkrn| j   td}dj!| j||g}dj|}	| j"||	 | j#|| tj$td | j%|| nr|dkr| j   | j&|| tj$td nD|dkr| j   | j'|| tj$td ntjjtdj|d S )Nr   r&   r*   zError: z>exactly two additional parameters to copr command are requiredr%      zToo many arguments.-r:   zOBad format of optional chroot. The format is distribution-version-architecture.r+   r9   zEuse format `copr_username/copr_projectname` to reference copr projectzbad 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
<https://docs.pagure.org/copr.copr/user_documentation.html#what-i-can-build-in-copr>,
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.zUnknown subcommand {}.)ri   r:   )(rA   r$   r@   Z	optparserZ
print_helpZavailable_by_user_list_user_projects_list_installed_repositoriesrB   rC   ZreposdirenabledZdisabledr1   r   
IndexErrorr   rQ   r   rR   rS   rO   
exceptionsErrorr   chroot_parts_guess_chroot_searchrf   get_reposdirrU   _sanitize_username
_need_rootrF   	_ask_user_download_repoinfo_runtime_deps_warning_disable_repo_remove_repo)
r]   r$   project_namechrootr_   copr_usernamecopr_projectnamerepo_filenamery   msgr   r   r   run   s    








zCoprCommand.runc             C   s  |j jdd }tjd| j |}| j| jko8tjd|}tjd|}| jjr`| rh| rhd S n|shd S tjd|rxd S tjd|rd S |j	}	|	r|s|	 r|rd S d}
tjd	|r|jd
d\}}}}|d | d | }ntjd|r2|jdd}|j
d
dd jd
dd }|d |d  d |d  }n.|jdd}| jd |d  d |d  }d}
|	sn|d7 }|
r||d7 }t| |
S )Nr9   r%   z_copr:Z_copr_z_copr:|^_copr_zcopr:.*:.*:.*:mlz
coprdep:.*Fzcopr:r?   r:   rj   ri   r   Tz (disabled)z *)repofiler   rematchrU   rW   rV   rA   rP   rm   rsplitrT   print)r]   repo_idrepoenabled_onlydisabled_only	file_nameZ	match_newZ	match_oldZ	match_anyrm   old_repor   rU   Z
copr_ownerZcopr_dirr   Z	copr_namer   r   r   _list_repo_file8  sB    zCoprCommand._list_repo_filec             C   sF   d}x,| j jj D ]\}}| j||||rd}qW |rBttd d S )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.)rB   repositemsr   r   r   )r]   Z	directoryr   r   r   r   r   r   r   r   rl   i  s    z(CoprCommand._list_installed_repositoriesc       
      C   s   dj |}| j| }| jj|dd}ytj|j }W n* tk
r`   tj	j
tdj |Y nX | j| tdj |}| j| xL|d D ]@}dj ||d }|d	 ptd
}	| jjjt||	}t| qW d S )Nz!/api_3/project/list?ownername={0}zw+)modez+Can't parse repositories for username '{}'.zList of {} coprsr   z
{0}/{1} : r   descriptionzNo description given)rf   rW   rB   r    jsonloadsrJ   r   rR   ro   rp   r   _check_json_output_print_match_sectionoutputfmtKeyValFillr   r   )
r]   Z	user_nameapi_pathurlres
json_parsesection_textitemr   descr   r   r   rk   s  s"    



zCoprCommand._list_user_projectsc       
      C   s   dj |}| j| }| jj|dd}ytj|j }W n* tk
r`   tj	j
tdj |Y nX | j| tdj |}| j| xJ|d D ]>}dj |d }|d	 ptd
}	| jjjt||	}t| qW d S )Nz/api_3/project/search?query={}zw+)r   zCan't parse search for '{}'.zMatched: {}r   z{0} : Z	full_namer   zNo description given.)rf   rW   rB   r    r   r   rJ   r   rR   ro   rp   r   r   r   r   r   r   r   )
r]   Zqueryr   r   r   r   r   r   r   r   r   r   r   rs     s     



zCoprCommand._searchc             C   s   | j jj|}t| d S )N)rB   r   Z
fmtSectionr   )r]   rg   Z	formattedr   r   r   r     s    z CoprCommand._print_match_sectionc             C   sj   | j stjjd d| _ tjjdj|j  | jj rf| jjj	sb| jj
jdj|dj|d rfdS dS )N
Fz{0}
z
{} [y/N]: z
{} [Y/n]: )r   Zdefaultyes_msgT)first_warningrc   rd   re   rf   r   rB   Z_promptWantedrC   Zassumenor   Zuserconfirm)r]   ry   r   r   r   r   _ask_user_no_raise  s    
zCoprCommand._ask_user_no_raisec             C   s    | j ||stjjtdd S )NzSafe and good answer. Exiting.)r   rR   ro   rp   r   )r]   ry   r   r   r   r   rw     s    zCoprCommand._ask_userc             C   s    t j dkrtjjtdd S )Nr   z/This command has to be run under the root user.)rD   geteuidrR   ro   rp   r   )clsr   r   r   rv     s    zCoprCommand._need_rootc                s  | j   dks& d dks& d dkr,t  | jjjd }t fdddD rd
 krbd| }n&dtdkrxd| }ndj d |}nd krtj	d}d krdj|}ndj d |}nPd krtj	d}d krdj|}ndj d |}nd d j
ddd  }|S )z2 Guess which chroot is equivalent to this machine Nr   Fr%   Zbasearchc                s   g | ]}| kqS r   r   ).0r   )distr   r   
<listcomp>  s    z-CoprCommand._guess_chroot.<locals>.<listcomp>FedoraFedora LinuxZRawhidezfedora-rawhide-ZrawhideZredhat_support_product_versionzfedora-{0}-{1}ZMageiaz%{distro_arch}ZCauldronzmageia-cauldron-{}zmageia-{0}-{1}ZopenSUSEz%{_target_cpu}Z
Tumbleweedzopensuse-tumbleweed-{}zopensuse-leap-{0}-{1}zepel-%s-x86_64.)r   r   )rL   r   rB   rC   substitutionsanyr
   rf   rpmZexpandMacror   )r]   Zdistarchr~   r   )r   r   rr     s,     



zCoprCommand._guess_chrootc             C   s  dj | jd d }| jd }dj|||}y*t| j| }tjj|rRtj| W n^ t	k
rl } z|j
dkrtdj| j| |j
t|}tjj|td}|jjd}	|	r>tj|	jd}
tj|
}
|td	jdj | j|7 }|
jd
r0|tddj dd |
d
 D  7 }|tdj|7 }tjj|n|tdj|7 }tjj|W Y d d }~X nJ tk
r } z,tdj| j| |jj}tjj|W Y d d }~X nX |j }|jd}tjd|rtjj | jjjd|dd  d }| j| j krR|j!dddj!| j"dj!dddj!ddj!dd}tjj|rRtj| t#|d.}|j$| x|j% D ]}|j$| qrW W d Q R X tj&|t'j(t'j)B t'j*B t'j+B  d S ) Nrj   r%   z%/coprs/{0}/repo/{1}/dnf.repo?arch={2}i  z Request to {0} failed: {1} - {2}z+It wasn't possible to enable this project.
zCopr-Error-Datazutf-8z1Repository '{0}' does not exist in project '{1}'.zavailable chrootsz
Available repositories: z, c             s   s   | ]}d j |V  qdS )z'{}'N)rf   )r   xr   r   r   	<genexpr>  s    z-CoprCommand._download_repo.<locals>.<genexpr>z

If you want to enable a non-default repository, use the following command:
  'dnf copr enable {0} <repository>'
But note that the installed repo file will likely need a manual modification.zProject {0} does not exist.zFailed to connect to {0}: {1}z\[copr:r   ri   z.repoz_copr:_coprr   r?   Zgroup_@wbr   r   ),rF   rq   rf   r    rW   rD   rE   existsr)   r!   coder   strrR   ro   rp   ZheadersrK   base64Z	b64decodedecoder   r   r"   reasonstrerrorreadliner   r   rB   rC   rt   rV   replacerU   r   re   	readlineschmodstatS_IRUSRS_IWUSRS_IRGRPS_IROTH)r]   r}   r   Zshort_chrootZarchr   ZresponseeZ	error_msgZ
error_dataZerror_data_decodedZ
first_liner   Zold_repo_filenamefr   r   r   rx     sX    





$

zCoprCommand._download_repoc                s   | j jdd | j j  | j| j||g }x(jj D ]}|jdrJq:|j| q:W |s`dS t	d}t
jd |jdj fdd	|D }| j|t	d
sx,|D ]$}| j jjj|| j jjddi qW dS )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)r   zcopr:Na  Maintainer 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

c                s*   g | ]"}d j t |jj|ddqS )z){num:2}. [{repoid}]
    baseurl={baseurl}baseurl)Znumrepoidr   )rf   nextcfgZgetValue)r   r   )counterr   r   r   r   9  s   z5CoprCommand._runtime_deps_warning.<locals>.<listcomp>z!Do you want to keep them enabled?rm   0)rB   resetZread_all_repos_get_copr_reporu   r   sections
startswithrI   r   	itertoolscountrf   rF   r   rC   write_raw_configfiler   r   )r]   r   r   Zruntime_depsr   ry   Zdepr   )r   r   r   rz     s*    



z!CoprCommand._runtime_deps_warningc             C   s   dj | jjddd | j||}|| jjkrdj | j|| }}|| jjkrd| jj| jkr| jj| jjdd	 }y.|jddd jddd }|| jkrd S W q tk
r   Y qX nd S | jj| S )
Nzcopr:{0}:{1}:{2}r?   r%   r   z{0}-{1}r   r9   ri   r   )	rf   rU   r   ru   rB   r   r   r   rn   )r]   r   r   r   r   rU   r   r   r   r   F  s     

zCoprCommand._get_copr_repoc             C   st   | j ||}|s,tjjtdj| j||ytj|j	 W n2 t
k
rn } ztjjt|W Y d d }~X nX d S )Nz&Failed to remove copr repo {0}/{1}/{2})r   rR   ro   rp   r   rf   rU   rD   r)   r   OSErrorr   )r]   r   r   r   r   r   r   r   r|   \  s    zCoprCommand._remove_repoc             C   sd   | j ||}|d kr,tjjtdj||x2|jj D ]$}| jj	j
|j|| jj	jddi q8W d S )Nz!Failed to disable copr repo {}/{}rm   r   )r   rR   ro   rp   r   rf   r   r   rB   rC   r   r   r   )r]   r   r   r   r   r   r   r   r{   h  s    zCoprCommand._disable_repoc             C   s<   yt j|j }W n$ tk
r6   tjjtd dS X |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.
        zUnknown response from server.N)r   r   rJ   r   rR   r@   rS   r   )r   r   r   r   r   r   	_get_datat  s    zCoprCommand._get_datac             C   s"   d|krt jjdj|d d S )Nerrorz{})rR   ro   rp   rf   )r   Zjson_objr   r   r   r     s    zCoprCommand._check_json_outputc             C   s&   |d dkrdj |dd  S |S d S )Nr   r   zgroup_{}r%   )rf   )r   r   r   r   r   ru     s    zCoprCommand._sanitize_username)r   )'__name__
__module____qualname____doc__rL   rT   Zdefault_hubrZ   r[   rV   aliasesr   summaryr   usagestaticmethodr5   r`   rY   rh   r   r   rl   rk   rs   r   r   rw   classmethodrv   rr   rx   rz   r   r|   r{   r   r   ru   r   r   r   r   r#   P   sD   L_1
%82r#   c               @   sD   e Zd ZdZdZedZdZdd Zdd Z	e
d	d
 Zdd ZdS )PlaygroundCommandz Playground plugin for DNF 
playgroundz$Interact with Playground repository.z [enable|disable|upgrade]c       	      C   s0  | j   | jtdtd dj| j}| jj|dd}| j|}|j  |d dkrft	j
jtdx|d	 D ]}d
j|d |d }dj| jjj|jdd}yj||d krwpdj| j||}| jj|dd}| j|}|j  |od|ko|d dkr
| j|| W qp t	jjk
r&   Y qpX qpW d S )Nz!Enabling a Playground repository.zDo you want to continue?z{0}/api/playground/list/zw+)r   r   okzUnknown response from server.r   z{0}/{1}ZusernameZcoprnamez{}/_playground_{}.repor9   rj   Zchrootsz{0}/api/coprs/{1}/detail/{2}/)rv   rw   r   rf   rW   rB   r    r   closerR   r@   rS   rC   rt   r   rx   ro   rp   )	r]   r~   Zapi_urlr   r   r   r}   r   Zoutput2r   r   r   _cmd_enable  s8    




zPlaygroundCommand._cmd_enablec             C   s6   | j   x(tjdj| jjjD ]}| j| q W d S )Nz{}/_playground_*.repo)rv   globrf   rB   rC   rt   r|   )r]   r   r   r   r   _cmd_disable  s    zPlaygroundCommand._cmd_disablec             C   s   | j dddddgd d S )Nr$   r%   r'   r(   upgrade)r,   r-   )r3   )r4   r   r   r   r5     s    zPlaygroundCommand.set_argparserc             C   s   t jjd| jjd }| j }|dkrB| j| tjt	d n`|dkrb| j
  tjt	d n@|dkr| j
  | j| tjt	d nt jjt	d	j|d S )
Nz%Playground is temporarily unsupportedr   r'   z-Playground repositories successfully enabled.r(   z.Playground repositories successfully disabled.r   z-Playground repositories successfully updated.zUnknown subcommand {}.)rR   ro   rp   rA   r$   rr   r   r   ry   r   r   rf   )r]   r$   r~   r   r   r   r     s    

zPlaygroundCommand.runN)r   )r   r   r   r   r   r   r   r   r   r   r   r5   r   r   r   r   r   r     s    r   )/Z
__future__r   r   r   r   rD   r   Zshutilr   rc   r   Zdnfpluginscorer   r   rR   Z
dnf.pycompr   Zdnf.i18nr   r   Zdistror   r   r	   r
   r   ImportErrorplatformrG   setZYESZNOZconfigparserr   r   r   Zurllib.requestr    r!   r"   Zurllib2ZpluginZregister_commandr@   ZCommandr#   r   r   r   r   r   <module>   sP       B