1818import hashlib
1919import platform
2020import tarfile
21+ import zipfile
2122
2223HEAD = "https://github.com/Kitware/CMake/releases/download/"
2324PLATFORMS = ("amd64" , "x86_64" , "x64" , "i86pc" )
@@ -103,6 +104,7 @@ def check_cmake_version(min_version: str) -> bool:
103104def install_cmake (
104105 cmake_version : str , outfile : Path , prefix : Path = None , quiet : bool = False ,
105106):
107+
106108 if sys .platform == "darwin" :
107109 brew = shutil .which ("brew" )
108110 if brew :
@@ -112,53 +114,57 @@ def install_cmake(
112114 elif sys .platform == "linux" :
113115 if platform .machine ().lower () not in PLATFORMS :
114116 raise ValueError ("This method is for Linux 64-bit x86_64 systems" )
115- prefix = Path (prefix ).expanduser ().resolve ()
116- prefix .mkdir (parents = True , exist_ok = True )
117- print ("Installing CMake to" , prefix )
118- with tarfile .open (str (outfile )) as tf :
119- tf .extractall (str (prefix ))
120117
121- stanza = f"export PATH={ prefix / outfile .stem } /bin:$PATH"
118+ prefix = Path (prefix ).expanduser ().resolve ()
119+ prefix .mkdir (parents = True , exist_ok = True )
120+ print ("Installing CMake to" , prefix )
121+
122+ if outfile .suffix == ".gz" :
123+ with tarfile .open (outfile ) as t :
124+ t .extractall (str (prefix ))
125+ stem = outfile .name .split (".tar.gz" )[0 ]
126+ # .stem doesn't work as intended for multiple suffixes: Path("foo.tar.gz").stem == "foo.tar"
127+ elif outfile .suffix == ".zip" :
128+ with zipfile .ZipFile (outfile ) as z :
129+ z .extractall (str (prefix ))
130+ stem = outfile .stem
131+ else :
132+ raise ValueError (f"Unsure how to extract { outfile } " )
133+
134+ if sys .platform == "linux" :
135+ stanza = f"export PATH={ prefix / stem } /bin:$PATH"
122136 for c in ("~/.bashrc" , "~/.profile" ):
123137 cfn = Path (c ).expanduser ()
124138 if cfn .is_file ():
125139 print ("\n \n add to" , cfn , stanza )
126140 break
127-
128141 elif sys .platform == "win32" :
129- passive = "/passive" if quiet else ""
130- cmd = ["msiexec" , passive , "/package" , str (outfile )]
131- print (" " .join (cmd ))
132- # without shell=True, install will fail
133- subprocess .run (" " .join (cmd ), shell = True )
142+ print (f"add to PATH: { prefix / stem } /bin" )
143+ else :
144+ raise ValueError (f"Unsure how to install CMake for { sys .platform } " )
134145
135146
136147def cmake_files (cmake_version : str , odir : Path ) -> T .Tuple [Path , str ]:
137148 """
138149 this relies on the per-OS naming scheme used by Kitware in their GitHub Releases
139150 """
140151
141- stem = ""
142152 if sys .platform == "cygwin" :
143153 raise ValueError ("use Cygwin setup.exe to install CMake, or manual compile" )
144154 elif sys .platform == "darwin" :
145- ofn = f"cmake-{ cmake_version } -Darwin-x86_64.dmg"
146- url = HEAD + f"v{ cmake_version } /{ ofn } "
155+ tail = "Darwin-x86_64.dmg"
147156 elif sys .platform == "linux" :
148157 if platform .machine ().lower () not in PLATFORMS :
149158 raise ValueError ("This method is for Linux 64-bit x86_64 systems" )
150- stem = f"cmake-{ cmake_version } -Linux-x86_64"
151- ofn = f"{ stem } .tar.gz"
152- url = HEAD + f"v{ cmake_version } /{ ofn } "
159+ tail = "Linux-x86_64.tar.gz"
153160 elif sys .platform == "win32" :
154- ofn = f"cmake-{ cmake_version } -win64-x64.msi"
155- url = HEAD + f"v{ cmake_version } /{ ofn } "
161+ tail = "win64-x64.zip"
156162 else :
157163 raise ValueError (f"unknown platform { sys .platform } " )
158164
159- outfile = odir / ofn
165+ ofn = f"cmake- { cmake_version } - { tail } "
160166
161- return outfile , url
167+ return odir / ofn , HEAD + f"v { cmake_version } / { ofn } "
162168
163169
164170def download_cmake (outdir : Path , get_version : str ) -> Path :
0 commit comments