B u `M2ã@sâddlZddlZddlZddlZddlZddlZddlZddlmZddl m Z ddl m Z ddl mZddlZddlmZedƒdZed ƒdZed ƒdZed ƒdZed ƒ\ZZe d ¡Zej d¡ZGdd„deƒZdS)éN)Úruné)ÚPYOMO_CONFIG_DIR)Ú deprecated)ÚDeveloperError)Úattempt_importzsix.moves.urllib.requestÚsslÚzipfileÚgzipÚdistrozpyomo.common.downloadzlibrary downloadersc@süeZdZdZd6dd„Zedd„ƒZedd„ƒZed d „ƒZed d „ƒZ ed d„ƒZ edd„ƒZ edd„ƒZ ed7dd„ƒZ eddddd„ƒZdd„Zd8dd„Zdd „Zd!d"„Zd#d$„Zd%d&„Zd'd(„Zd)d*„Zd+d,„Zd-d.„Zd9d0d1„Zd2d3„Zd4d5„ZdS):ÚFileDownloaderNFcCsHd|_d|_||_||_|dk rD|jr4tj |j¡sDtd|jfƒ‚dS)Nz+cacert='%s' does not refer to a valid file.)Ú_fnameÚtargetÚinsecureÚcacertÚosÚpathÚisfileÚ RuntimeError)Úselfrr©rú9/tmp/pip-unpacked-wheel-n62dbgi3/pyomo/common/download.pyÚ__init__(szFileDownloader.__init__cCsBt ¡ ¡}xdD]}| |¡d}qWtjdkr6dnd}||fS)aReturn a tuple (platform_name, bits) for the current system Returns ------- platform_name (str): lower case, usually in {linux, windows, cygwin, darwin}. bits (int): OS address width in {32, 64} z.-_rlé@é )ÚplatformÚsystemÚlowerÚsplitÚsysÚmaxsize)ÚclsrÚcÚbitsrrrÚ get_sysinfo4s  zFileDownloader.get_sysinfoc Csœd}d}tddƒv}xn|D]f}| ¡}|s,q| ¡ d¡\}}|d|dkrf|ddkrf|dd…}|d krt|}q|d kr|}qWWdQRX| |¡|fS) NÚz/etc/os-releaseÚrtú=réÿÿÿÿz"'rÚidÚ version_id)ÚopenÚstriprrÚ _map_dist)r!ÚdistÚverÚFILEÚlineÚkeyÚvalrrrÚ_get_distver_from_os_releaseEs   z+FileDownloader._get_distver_from_os_releasec Cs\tddƒ>}| ¡ ¡ ¡}d}x"| ¡D]}t d|¡r*|}Pq*WWdQRX| |¡|fS)Nz/etc/redhat-releaser&r%z ^[0-9\.]+)r+Úreadlinerr,rÚreÚmatchr-)r!r0r.r/ÚwordrrrÚ _get_distver_from_redhat_releaseWs  z/FileDownloader._get_distver_from_redhat_releasecCs:tddgƒ\}}tddgƒ\}}| | ¡ ¡¡| ¡fS)NÚ lsb_releasez-siz-sr)rr-rr,)r!Úrcr.r/rrrÚ_get_distver_from_lsb_releasecsz,FileDownloader._get_distver_from_lsb_releasecCst ¡tjddfS)NT)Úbest)r r)Úversion)r!rrrÚ_get_distver_from_distroisz'FileDownloader._get_distver_from_distrocCs<| ¡}dddddddœ}x|D]}||kr ||Sq W|S)NÚcentosÚrhelÚfedoraÚdebianÚubuntu)r@Úredhatzred hatrBrCrD)r)r!r.Ú_mapr2rrrr-ms  zFileDownloader._map_distcCsÂ| ¡d}|dkrŠtr&| ¡\}}n\tj d¡r@| ¡\}}nBtdgƒddkr`| ¡\}}n"tj d¡rz|  ¡\}}nd\}}||fS|dkr¢dt   ¡dfS|d krºd t   ¡dfSdSdS) NrÚlinuxz/etc/redhat-releaser:z/etc/os-release)r%r%ÚdarwinÚmacosZwindowsÚwin) r$Údistro_availabler?rrÚexistsr9rr<r4rÚmac_verÚ win32_ver)r!Ú_osr.r/rrrÚ_get_os_version}s"   zFileDownloader._get_os_versionTcCsztjdkr| ¡t_|stjStj\}}ddi}||kr@||}|dkrd|d | d¡dd…¡S|| d¡dSdS) a!Return a standardized representation of the OS version This method was designed to help identify compatible binaries, and will return strings similar to: - rhel6 - fedora24 - ubuntu18.04 - macos10.13 - win10 Parameters ---------- normalize : bool, optional If True (the default) returns a simplified normalized string (e.g., `'rhel7'`) instead of the raw (os, version) tuple (e.g., `('centos', '7.7.1908')`) Nr@rA>rDrIrJr%Ú.ér)r Ú _os_versionrPÚjoinr)r!Ú normalizerOÚ_verrFrrrÚget_os_version•s   zFileDownloader.get_os_versionz/get_url() is deprecated. Use get_platform_url()z5.6.9)r>cCs | |¡S)N)Úget_platform_url)rÚurlmaprrrÚget_url»szFileDownloader.get_urlcCs2| ¡\}}| |d¡}|dkr.tdtfƒ‚|S)aˆSelect the url for this platform Given a `urlmap` dict that maps the platform name (from `FileDownloader.get_sysinfo()`) to a platform-specific URL, return the URL that matches the current platform. Parameters ---------- urlmap: dict Map of platform name (e.g., `linux`, `windows`, `cygwin`, `darwin`) to URL Nz.cannot infer the correct url for platform '%s')r$Úgetrr)rrYrr#ÚurlrrrrXÁs   zFileDownloader.get_platform_urlcCsR|dkrt ¡}|jdddddd|jddd dd d|jd d dd ddd|S)Nz --insecureÚ store_truerFzDisable all SSL verification)ÚactionÚdestÚdefaultÚhelpz--cacertÚstorerzBUse CACERT as the file of certificate authorities to verify peers.z-vz --verboseÚverbosez"Verbose output when download fails)ÚargparseÚArgumentParserÚ add_argument)rÚparserrrrÚ create_parserØs*zFileDownloader.create_parsercCsX| ¡}|jddddd| ||¡|jdk rT|jrDtj |j¡sTtd|jfƒ‚dS)Nrú?z(Target destination directory or filename)Únargsr`raz---cacert='%s' does not refer to a valid file.)rhrfÚ parse_argsrrrrr)rÚargvrgrrrrkós  zFileDownloader.parse_argscCs||jdk r|j|_n t|_tj |j¡s4t |j¡tj |j¡rTtj |j|¡|_tj |j¡}tj |¡sxt |¡dS)N) rr rrrÚisdirÚmakedirsrTÚdirname)rr`Z targetDirrrrÚset_destination_filenames    z'FileDownloader.set_destination_filenamecCs|jS)N)r )rrrrÚ destinationszFileDownloader.destinationcCs‚yBt ¡}|jr|j|jd|jr2d|_tj|_tj ||d}Wnt k r`t  |¡}YnX|  ¡}t   dt|ƒf¡|S)z4Return the contents of a URL as an io.BytesIO object)ÚcafileF)Úcontextz ...downloaded %s bytes)rÚcreate_default_contextrÚload_verify_locationsrÚcheck_hostnameÚ CERT_NONEÚ verify_modeÚrequestÚurlopenÚAttributeErrorÚreadÚloggerÚinfoÚlen)rr\ÚctxÚfetchZansrrrÚ retrieve_urlszFileDownloader.retrieve_urlc Csr|jdkrtdƒ‚t|j|r dndƒB}| |¡}|rB| |¡n| | ¡¡t dt|ƒf¡WdQRXdS)NzGtarget file name has not been initialized with set_destination_filenameÚwbÚwtz ...wrote %s bytes) r rr+r‚ÚwriteÚdecoder}r~r)rr\Úbinaryr0Úraw_filerrrÚget_file$s   zFileDownloader.get_filecCs|j|ddS)z5Retrieve the specified url and write as a binary fileT)r‡)r‰)rr\rrrÚget_binary_file1szFileDownloader.get_binary_filecCs|j|ddS)z3Retrieve the specified url and write as a text fileF)r‡)r‰)rr\rrrÚ get_text_file6szFileDownloader.get_text_filec Csp|jdkrtdƒ‚t|jdƒH}t | |¡¡}t |¡ |¡ ¡}|  |¡t   dt |ƒf¡WdQRXdS)NzGtarget file name has not been initialized with set_destination_filenamerƒz ...wrote %s bytes) r rr+ÚioÚBytesIOr‚r ÚZipFiler|r…r}r~r)rr\Úsrcnamer0Z zipped_filerˆrrrÚ get_binary_file_from_zip_archive;s  z/FileDownloader.get_binary_file_from_zip_archiverc Cs|jdkrtdƒ‚tj |j¡r>tj |j¡s>td|jfƒ‚t t   |  |¡¡¡}xº|  ¡D]®}|j }|ddks|d|krŽt d|f¡q^| |¡}t|ƒ|krÂ|ddkr^t d |f¡q^|ddkrÚ|ddn|d|_ | |tjj|jft||d…ƒžŽ¡q^WdS) NzGtarget file name has not been initialized with set_destination_filenamez4Target directory (%s) exists, but is not a directoryrz\/z..zTmalformed (potentially insecure) filename (%s) found in zip archive. Skipping file.r(ú/z2Skipping file (%s) in zip archive due to dirOffset)r rrrrLrmrr rŽrŒrr‚ÚinfolistÚfilenamer}ÚerrorÚ _splitpathrÚwarningÚextractrTÚtuple)rr\Z dirOffsetÚzip_filer~ÚfrrrrÚget_zip_archiveFs,       "zFileDownloader.get_zip_archivec Csl|jdkrtdƒ‚t|jdƒD}t | |¡¡}tj|d ¡}|  |¡t   dt |ƒf¡WdQRXdS)NzGtarget file name has not been initialized with set_destination_filenamerƒ)Úfileobjz ...wrote %s bytes) r rr+rŒrr‚r ÚGzipFiler|r…r}r~r)rr\r0Z gzipped_filerˆrrrÚget_gzipped_binary_file`s  z&FileDownloader.get_gzipped_binary_filecCsfg}tj tj |¡¡\}}x.||krJ|r4| |¡|}tj |¡\}}qW|rZ| |¡| ¡|S)N)rrrÚnormpathÚappendÚreverse)rrÚ componentsÚheadÚtailrrrr•ks   zFileDownloader._splitpath)FN)T)N)r)Ú__name__Ú __module__Ú __qualname__rSrÚ classmethodr$r4r9r<r?r-rPrWrrZrXrhrkrprqr‚r‰rŠr‹rr›ržr•rrrrr %s4       %     r ) rdrŒÚloggingrrr6rZpyutilib.subprocessrÚconfigrÚ deprecationrÚerrorsrZ pyomo.commonZpyomoZpyomo.common.dependenciesrryrr r r rKÚ getLoggerr}ÚcommonZFactoryZDownloadFactoryÚobjectr rrrrÚ s(