B
    Tò†[´A  ã               @   s6  d Z 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mZ ddl	m
Z
 ddlZddlmZ ddlmZ ddlmZmZmZmZmZ dd	lmZ dd
lmZ ddlmZmZ e d¡Zd*dd„Zd+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+d(d)„ Z,dS ),a,  

    anaconda upload CONDA_PACKAGE_1.bz2
    anaconda upload notebook.ipynb
    anaconda upload environment.yml

##### See Also

  * [Uploading a Conda Package](http://docs.anaconda.org/using.html#Uploading)
  * [Uploading a PyPI Package](http://docs.anaconda.org/using.html#UploadingPypiPackages)

é    )Úunicode_literalsN)Úglob)Úexists)Úinput)Úerrors)Ú
bool_inputÚDEFAULT_CONFIGÚ
get_configÚget_server_apiÚupload_print_callback)ÚPACKAGE_TYPES)Úupload_project)Údetect_package_typeÚ	get_attrszbinstar.uploadTc             C   s   t  | d¡}|r| ¡ }|S )NÚunknown)r   ÚgetÚlower)Zpkg_typeZ	lowercaseZverbose_type© r   ú]lib/python3.7/site-packages/anaconda_client-1.7.2-py3.7.egg/binstar_client/commands/upload.pyÚverbose_package_type'   s    r   c             C   s   |   |||g ||¡ d S )N)Úadd_release)Úaserver_apiÚusernameÚpackage_nameÚversionÚrelease_attrsÚannouncer   r   r   Úcreate_release.   s    r   c             C   sp   t  d|||¡ tdƒs*t  d¡ tdƒ‚tdƒ}t  d¡ tddƒ}|rTtd	ƒ}nd
}|  |||g ||¡ d S )Nz%The release "%s/%s/%s" does not existz Would you like to create it now?zgood-byeéÿÿÿÿz*Enter a short description of the release:
z4Announcements are emailed to your package followers.z@Would you like to make an announcement to the package followers?FzMarkdown Announcement:
Ú )ÚloggerÚinfor   Ú
SystemExitr   r   )r   r   r   r   r   ÚdescriptionZmake_announcementr   r   r   r   Úcreate_release_interactive2   s    



r$   c             C   sT   |j r|j }nBt d¡ t| ƒ}|dkrDd|  }t |¡ t |¡‚t d|¡ |S )zd
    return the file type from the inspected package or from the
    -t/--package-type argument
    zDetecting file type...Nz_Could not detect package type of file %r please specify package type with option --package-typezFile type is "%s")Úpackage_typer    r!   r   Úerrorr   ÚBinstarError)ÚfilenameÚargsr%   Úmessager   r   r   Údetermine_package_typeE   s    


r+   c             C   sŒ   | j rZd|krR|d  ¡ | j  ¡ krRd | j  ¡ |d  ¡ ¡}t |¡ t |¡‚| j }n.d|kr€d|f }t |¡ t |¡‚|d }|S )NÚnamezWPackage name on the command line " {}" does not match the package name in the file "{}"zRCould not detect package name for package type %s, please use the --package option)Úpackager   Úformatr    r&   r   r'   )r)   Úpackage_attrsr(   r%   Úmsgr   r*   r   r   r   Úget_package_name[   s    




r1   c             C   s@   | j r| j }n.d|kr4d|f }t |¡ t |¡‚|d }|S )Nr   zUCould not detect package version for package type %s, please use the --version option)r   r    r&   r   r'   )r)   r   r%   r   r*   r   r   r   Úget_versionn   s    


r2   c       	      C   sÂ   y|   ||¡S  tjk
r¼   |jsFd||f }t |¡ t |¡‚nr|jrT|j}n.d|krzd|f }t |¡ t |¡‚|d }|j	 }| j
|||| d¡||| d¡| d¡|d	S Y nX d S )Nz‡Anaconda repository package %s/%s does not exist. Please run "anaconda package --create" to create this package namespace in the cloud.ÚsummaryzUCould not detect package summary for package type %s, please use the --summary optionÚlicenseÚlicense_urlÚlicense_family)ÚpublicÚattrsr5   r6   r%   )r-   r   ÚNotFoundÚauto_registerr    r&   Z	UserErrorr3   r'   ZprivateÚadd_packager   )	r   r)   r   r   r/   r%   r*   r3   r7   r   r   r   r;   z   s4    




r;   c          	   C   sZ   y|   |||¡ W nB tjk
rT   |jdkr@t| ||||ƒ nt| ||||ƒ Y nX d S )NÚinteractive)Úreleaser   r9   Úmoder$   r   )r   r)   r   r   r   r   r   r   r   r   Ÿ   s    
r   c             C   s¤   y|   ||||d ¡ W n tjk
r.   dS X |jdkr^t d|d ¡ |  ||||d ¡ |jdkr td|d ƒrŒ|  ||||d ¡ nt d|d ¡ dS d S )	NÚbasenameFÚforcez+Distribution "%s" already exists. Removing.r<   z?Distribution "%s" already exists. Would you like to replace it?zNot replacing distribution "%s"T)	Zdistributionr   r9   r>   r    ÚwarningZremove_distr   r!   )r   r)   r   r   r   Ú
file_attrsr   r   r   Úremove_existing_fileª   s    

rC   c             C   s  t  d t|ƒ¡¡ yt|| |d\}}}W n@ tk
rl   d | t|ƒ¡}t  |¡ |jr^‚ t 	|¡‚Y nX |j
r‚|j
|d d< t||| |ƒ}	t|||ƒ}
t  d|	¡ t||||	||ƒ}| dg ¡}|r||krd t|rè|d	 nd
ƒ|	t|ƒ¡}t  |¡ t 	|¡‚t  d|
¡ t||||	|
|ƒ | d|¡}t| dƒÒ}t  d||	|
|d ¡ t||||	|
|ƒrpd S y:|j||	|
|d |||j| d¡|d |jt|ƒd}W nL tjk
rö   i }|jdkrèt  d||	|
|d ¡ ‚ n
t  d¡ Y nX |r
t  d¡ n |	|gS Q R X d S )Nz#Extracting {} attributes for upload)Zparser_argsz=Trouble reading metadata from {}. Is this a valid {} package?r8   Zbinstar_buildzCreating package "%s"Úpackage_typeszCYou already have a {} named '{}'. Use a different name for this {}.r   r   zCreating release "%s"Úbinstar_package_typeÚrbzUploading file "%s/%s/%s/%s"r?   Údependencies)rG   r8   ZchannelsÚcallbackÚskipzyDistribution already exists. Please use the -i/--interactive or --force or --skip options or `anaconda remove %s/%s/%s/%sz.Distribution already exists. Skipping upload.
zUpload complete
)r    r!   r.   r   r   Ú	Exceptionr&   Zshow_tracebackr   r'   Zbuild_idr1   r2   r;   r   r   ÚpopÚopenrC   Úuploadr#   Úlabelsr   ZConflictr>   )r(   r%   r   r   r)   r/   r   rB   r*   r   r   r-   rD   rE   ÚfdÚupload_infor   r   r   Úupload_package¼   sZ    



rQ   c       
      C   s¤   t  ¡ }xV| D ]N}t d|¡ tjddd|d|gtjtjd}| ¡ \}}|rt d||¡ qW g }x:t	 
|¡D ],\}}} x | D ]}	| t	j ||	¡¡ q€W qpW |S )NzRunning conda convert on "%s"zconda-convertz-pÚallz-o)ÚstdoutÚstderrz.Couldn't generate platform packages for %s: %s)ÚtempfileZmkdtempr    r!   Ú
subprocessÚPopenÚPIPEZcommunicaterA   ÚosÚwalkÚappendÚpathÚjoin)
ÚfilesZtmpdirÚfilepathZprocessrS   rT   Úresultr\   Údirsr(   r   r   r   Úget_convert_files   s    

rb   c             C   s\  t | jd}t| j| j|d}| ¡  d}| jr8| j}n&d|krJ|d }nd}| ¡ }|d }t d|¡ |r²y| |¡ W n4 tj	k
r°   d 
|¡}t |¡ t |¡‚Y nX g }g }d	d
„ | jD ƒ}	| jrÜ|	t|	ƒ7 }	x|	D ]
}
t|
ƒsd 
|
¡}t |¡ t |¡‚nt d|
¡ t|
| ƒ}|dkrH| t|
| |ƒ¡ qä|dkr´| jdks´yt t|
ƒtj¡ W n> tk
r² } zt d|
|¡ t d¡ wäW d d }~X Y nX t|
|||| d}|d k	rät|ƒdkrä|\}}|rä| |¡ qäW x<|D ]4\}}| dd||f ¡}t d 
t|ƒ|¡¡ qúW x$|D ]\}}t d 
||¡¡ q8W d S )N)Úsite)Útokenrc   ÚconfigTZupload_userFZloginzUsing "%s" as upload usernamezUser "{}" does not existc             S   s   g | ]}|D ]}|‘qqS r   r   )Ú.0ZfglobÚfr   r   r   ú
<listcomp>5  s    zmain.<locals>.<listcomp>zFile "{}" does not existzProcessing '%s'ZprojectZipynbr@   zInvalid notebook file '%s': %sz&Use --force to upload the file anyways)r%   r   r   r)   é   Úurlzhttps://anaconda.org/%s/%sz{} located at:
{}
zProject {} uploaded to {}.
)r	   rc   r
   rd   Zcheck_serverÚuserr    r!   r   r9   r.   r&   r'   r^   rR   rb   r   r+   r[   r   r>   ÚnbformatÚreadrL   Z
NO_CONVERTrJ   rQ   Úlenr   r   )r)   re   r   Zvalidate_usernamer   rk   r*   Zuploaded_packagesZuploaded_projectsr^   r(   r%   r&   Zpackage_infoZ_packageZ_upload_infor-   rP   Zpackage_urlZproject_namerj   r   r   r   Úmain  sn    








ro   c             C   s$   t jdkrd| krt| ƒS | gS d S )NÚntÚ*)rY   r,   r   )Úitemr   r   r   Úwindows_globc  s    rs   c       	   
   C   s  d}| j dtj||td}|jdddg td d}|jd	d
dg d|jddddd |jdddd|jdddd |jdddd |jdddd |jdd dd | d!¡}|jd"d#d$d |jd%d&d'd |jd(d)d*d t 	¡ }| 
d+¡ | 
d,¡ d- t| ¡ ƒ¡}|jd.d/d0 |¡d |jd1d2d3d |jd4d5d |jd6d7dd | ¡ }|jd8d9d:d;d< |jd=d9dd>d< |jt d9d?¡d@ |jdAdBd | ¡ }|jdCdDdEdFdGdHdI |jdJdKdLdEdGdMdN |jdOdPdEdGdQdN |jdRdSdEdGdTdN |jtdU d S )VNz+Upload packages to your Anaconda repositoryrM   )Zformatter_classÚhelpr#   Zepilogr^   ú+zDistributions to upload)Únargsrt   ÚdefaultÚtypez•{deprecation}Add this file to a specific {label}. Warning: if the file {label}s do not include "main", the file will not show up in your user {label}z-cz	--channelr[   rN   z[DEPRECATED]
Zchannel)ZdeprecationÚlabelZCHANNELS)Úactionrw   Údestrt   Úmetavarz-lz--labelr   ry   )rz   r{   rt   z--no-progresszDon't show upload progressÚ
store_true)rt   rz   z-uz--userz:User account or Organization, defaults to the current user)rt   z--allzHUse conda convert to generate packages for all platforms and upload themzmetadata optionsz-pz	--packagez1Defaults to the package name in the uploaded filez-vz	--versionz4Defaults to the package version in the uploaded filez-sz	--summaryzSet the summary of the packageZcondaZpypiz, z-tz--package-typez2Set the package type [{0}]. Defaults to autodetectz-dz--descriptionzdescription of the file(s)z--thumbnailzNotebook's thumbnail imagez	--privatez&Create the package with private accessz--no-registerr:   Zstore_falsez9Don't create a new package namespace if it does not exist)r{   rz   rt   z
--registerz3Create a new package namespace if it does not existT)r:   z
--build-idz,Anaconda repository Build ID (internal only)z-iz--interactiveZstore_constz5Run an interactive prompt if any packages are missingr>   r<   )rz   rt   r{   Úconstz-fz--failz5Fail if a package or release does not exist (default)Zfail)rt   rz   r{   r~   z--forcez+Force a package upload regardless of errorsr@   z--skip-existingz8Skip errors on package batch upload if it already existsrI   )ro   )Ú
add_parserÚargparseZRawDescriptionHelpFormatterÚ__doc__Úadd_argumentrs   r.   Zadd_argument_groupr   ÚcopyrK   r]   ÚlistÚkeysZadd_mutually_exclusive_groupZset_defaultsr   r   ro   )	Z
subparsersr#   ÚparserZ
label_helpZmgroupZpkgsZ	pkg_typesZregister_groupÚgroupr   r   r   r   j  sZ    






r   )T)N)-r   Z
__future__r   r€   rU   ZloggingrY   rV   r   Úos.pathr   rl   Z	six.movesr   Zbinstar_clientr   Zbinstar_client.utilsr   r   r	   r
   r   Zbinstar_client.utils.configr   Zbinstar_client.utils.projectsr   Zbinstar_client.utils.detectr   r   Z	getLoggerr    r   r   r$   r+   r1   r2   r;   r   rC   rQ   rb   ro   rs   r   r   r   r   r   Ú<module>   s<   


%DM