Skip to content

Commit bef5e0a

Browse files
committed
cmake_setup: generalize install for Windows to allow multiple versions
1 parent a27a8f6 commit bef5e0a

File tree

3 files changed

+30
-24
lines changed

3 files changed

+30
-24
lines changed

cmake_setup.py

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import hashlib
1919
import platform
2020
import tarfile
21+
import zipfile
2122

2223
HEAD = "https://github.com/Kitware/CMake/releases/download/"
2324
PLATFORMS = ("amd64", "x86_64", "x64", "i86pc")
@@ -103,6 +104,7 @@ def check_cmake_version(min_version: str) -> bool:
103104
def 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

136147
def 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

164170
def download_cmake(outdir: Path, get_version: str) -> Path:

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = cmakeutils
3-
version = 1.7.1
3+
version = 1.7.3
44
author = Michael Hirsch, Ph.D.
55
author_email = scivision@users.noreply.github.com
66
description = Helper functions with CMake

test_module.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def test_files(tmp_path):
3737
if sys.platform == "linux":
3838
assert file.endswith(".tar.gz")
3939
elif sys.platform == "win32":
40-
assert file.endswith(".msi")
40+
assert file.endswith(".zip")
4141

4242

4343
if __name__ == "__main__":

0 commit comments

Comments
 (0)