Created
September 15, 2023 12:33
-
-
Save grhkm21/895e4eee36e0a07854271b73cfc247bb to your computer and use it in GitHub Desktop.
Installs mpfr (for python-flint)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import ctypes | |
import requests | |
import subprocess | |
from tarfile import TarFile | |
# from logging import log | |
def get_env(env_name: str, error: bool = True) -> str: | |
res = os.environ[env_name] | |
if res is None: | |
res = "" | |
if res == "": | |
error_msg = f"ENV {env_name} is empty" | |
if error: | |
raise RuntimeError(error_msg) | |
else: | |
raise RuntimeWarning(error_msg) | |
return res | |
def run_command(args: list[str], error=False) -> str: | |
print(f"[*] Executing command {args}") | |
process = subprocess.Popen(args, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE) | |
stdout, stderr = process.communicate() | |
if process.returncode != 0 and error: | |
print(f"[-{process.returncode}] Error executing command: {args}") | |
print(stderr.decode()) | |
exit(1) | |
return stdout.decode() | |
def install_mpfr(): | |
MPFRVER = get_env("MPFRVER") | |
# https://stackoverflow.com/questions/46311212/pythons-requests-equivalent-of-curl-o | |
mpfr_fn = f"mpfr-{MPFRVER}" | |
mpfr_tar_fn = f"{mpfr_fn}.tar.gz" | |
url = f"https://ftp.gnu.org/gnu/mpfr/{mpfr_tar_fn}" | |
with open(mpfr_tar_fn, "wb") as fd, requests.get(url, stream=True) as r: | |
for chunk in r.iter_content(chunk_size=128): | |
fd.write(chunk) | |
with TarFile.open(mpfr_tar_fn, "r") as fd: | |
fd.extractall(".") | |
os.chdir(mpfr_fn) | |
PREFIX = get_env("PREFIX") | |
FLAGS = { | |
"--prefix": PREFIX, | |
"--with-gmp": PREFIX, | |
"--enable-shared": "yes", | |
"--enable-static": "no" | |
} | |
run_command(["./configure"] + [f"{key}={item}" for key, item in FLAGS.items()]) | |
run_command(["make", "-j3"]) | |
run_command(["make", "install"]) | |
# Ensure it has been installed at $PREFIX | |
_ = ctypes.CDLL(f"{PREFIX}/lib/libmpfr.so") | |
print("[+] Successfully installed mpfr") | |
if __name__ == "__main__": | |
install_mpfr() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment