B ÔÂ_2ã@sÖddlZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl m Z ddl m Z ddlmZddlmZddlZddlmZedƒdZed ƒ\ZZe d ¡Zej d ¡ZGd d „d eƒZdS)éN)Úruné)ÚPYOMO_CONFIG_DIR)Ú deprecated)ÚDeveloperError)Úattempt_importzsix.moves.urllib.requestÚ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)Úselfr r ©rú9/tmp/pip-unpacked-wheel-d4p3hk07/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*)rr-r+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)rr&Úversion)rrrrÚ_get_distver_from_distroisz'FileDownloader._get_distver_from_distrocCs<| ¡}dddddddœ}x|D]}||kr ||Sq W|S)NÚcentosÚrhelÚfedoraÚdebianÚubuntu)r=Úredhatzred hatr?r@rA)r)rr+Ú_mapr/rrrr*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-releaser7z/etc/os-release)r"r"ÚdarwinÚmacosZwindowsÚwin) r!Údistro_availabler<rrÚexistsr6rr9r1rÚ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=r>>rArFrGr"Ú.ér)r Ú _os_versionrMÚjoinr)rÚ normalizerLÚ_verrCrrrÚ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)rrVrr ÚurlrrrrUÁs   zFileDownloader.get_platform_urlcCsR|dkrt ¡}|jdddddd|jddd dd d|jd d dd ddd|S)Nz --insecureÚ store_truer FzDisable all SSL verification)ÚactionÚdestÚdefaultÚhelpz--cacertÚstorer zBUse 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]r^z---cacert='%s' does not refer to a valid file.)rercÚ parse_argsr rrrr)rÚargvrdrrrrhós  zFileDownloader.parse_argscCs||jdk r|j|_n t|_tj |j¡s4t |j¡tj |j¡rTtj |j|¡|_tj |j¡}tj |¡sxt |¡dS)N) r r rrrÚisdirÚmakedirsrQÚ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)ÚsslÚcreate_default_contextr Úload_verify_locationsr Úcheck_hostnameÚ CERT_NONEÚ verify_modeÚrequestÚurlopenÚAttributeErrorÚreadÚloggerÚinfoÚlen)rrYÚ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})rrYÚbinaryr-Úraw_filerrrÚget_file$s   zFileDownloader.get_filecCs|j|ddS)z5Retrieve the specified url and write as a binary fileT)r…)r‡)rrYrrrÚget_binary_file1szFileDownloader.get_binary_filecCs|j|ddS)z3Retrieve the specified url and write as a text fileF)r…)r‡)rrYrrrÚ 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_filenamerz ...wrote %s bytes) r rr(ÚioÚBytesIOr€ÚzipfileÚZipFilerzrƒr{r|r})rrYÚsrcnamer-Z 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   |  |¡¡¡}g}xº|  ¡D]®}|j }|ddks€d|kr’t d|f¡qb| |¡}t|ƒ|krÆ|ddkrbt d |f¡qb|ddkrÞ|ddn|d|_ | |tjj|jft||d…ƒžŽ¡qbWdS) 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 rrrrIrjrrŒrrŠr‹r€ÚinfolistÚfilenamer{ÚerrorÚ _splitpathr}ÚwarningÚextractrQÚtuple)rrYZ dirOffsetÚzip_fileÚnamesr|Úfr rrrÚ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Šr‹r€ÚgzipÚGzipFilerzrƒr{r|r})rrYr-Z gzipped_filer†rrrÚget_gzipped_binary_fileas  z&FileDownloader.get_gzipped_binary_filecCsfg}tj tj |¡¡\}}x.||krJ|r4| |¡|}tj |¡\}}qW|rZ| |¡| ¡|S)N)rrrÚnormpathÚappendÚreverse)rrÚ componentsÚheadÚtailrrrr”ls   zFileDownloader._splitpath)FN)T)N)r)Ú__name__Ú __module__Ú __qualname__rPrÚ classmethodr!r1r6r9r<r*rMrTrrWrUrerhrmrnr€r‡rˆr‰rr›rŸr”rrrrr %s4       %     r ) rarrŠÚloggingrrr3rqrrŒZpyutilib.subprocessrÚconfigrÚ deprecationrÚerrorsrZ pyomo.commonZpyomoZpyomo.common.dependenciesrrwrrHÚ getLoggerr{ÚcommonZFactoryZDownloadFactoryÚobjectr rrrrÚ s(