Sith/core/management/commands/install_xapian.py

263 lines
7.9 KiB
Python
Raw Permalink Normal View History

#
# Copyright 2024 © AE UTBM
# ae@utbm.fr / ae.info@utbm.fr
#
# This file is part of the website of the UTBM Student Association (AE UTBM),
# https://ae.utbm.fr.
#
# You can find the source code of the website at https://github.com/ae-utbm/sith
#
# LICENSED UNDER THE GNU GENERAL PUBLIC LICENSE VERSION 3 (GPLv3)
2024-09-23 08:25:27 +00:00
# SEE : https://raw.githubusercontent.com/ae-utbm/sith/master/LICENSE
# OR WITHIN THE LOCAL FILE "LICENSE"
#
#
import hashlib
import multiprocessing
import os
2024-10-17 23:40:13 +00:00
import platform
import shutil
import subprocess
import sys
import tarfile
from dataclasses import dataclass
from pathlib import Path
from typing import Self
2024-06-24 11:07:36 +00:00
import tomli
import urllib3
from django.core.management.base import BaseCommand, CommandParser, OutputWrapper
from urllib3.response import HTTPException
@dataclass
class XapianSpec:
version: str
core_sha1: str
bindings_sha1: str
@classmethod
def from_pyproject(cls) -> Self:
with open(
Path(__file__).parent.parent.parent.parent / "pyproject.toml", "rb"
) as f:
pyproject = tomli.load(f)
spec = pyproject["tool"]["xapian"]
return cls(
version=spec["version"],
core_sha1=spec["core-sha1"],
bindings_sha1=spec["bindings-sha1"],
)
2024-06-24 11:07:36 +00:00
class Command(BaseCommand):
help = "Install xapian"
def add_arguments(self, parser: CommandParser):
parser.add_argument(
"-f",
"--force",
action="store_true",
help="Force installation even if already installed",
)
def _current_version(self) -> str | None:
try:
import xapian
except ImportError:
return None
return xapian.version_string()
2024-06-27 12:57:40 +00:00
def handle(self, *args, force: bool, **options):
if not os.environ.get("VIRTUAL_ENV", None):
2024-08-06 09:42:10 +00:00
self.stdout.write(
"No virtual environment detected, this command can't be used"
)
return
desired = XapianSpec.from_pyproject()
if desired.version == self._current_version():
if not force:
2024-08-06 09:42:10 +00:00
self.stdout.write(
f"Version {desired.version} is already installed, use --force to re-install"
)
return
self.stdout.write(
f"Version {desired.version} is already installed, re-installing"
)
XapianInstaller(desired, self.stdout, self.stderr).run()
self.stdout.write("Installation success")
class XapianInstaller:
def __init__(
self,
spec: XapianSpec,
stdout: OutputWrapper,
stderr: OutputWrapper,
):
self._version = spec.version
self._core_sha1 = spec.core_sha1
self._bindings_sha1 = spec.bindings_sha1
self._stdout = stdout
self._stderr = stderr
self._virtual_env = os.environ.get("VIRTUAL_ENV", None)
if not self._virtual_env:
raise RuntimeError("You are not inside a virtual environment")
self._virtual_env = Path(self._virtual_env)
self._dest_dir = Path(self._virtual_env) / "packages"
self._core = f"xapian-core-{self._version}"
self._bindings = f"xapian-bindings-{self._version}"
2024-10-17 23:40:13 +00:00
def _util_download(self, url: str, dest: Path, sha1_hash: str) -> None:
resp = urllib3.request("GET", url)
if resp.status != 200:
raise HTTPException(f"Could not download {url}")
if hashlib.sha1(resp.data).hexdigest() != sha1_hash:
raise ValueError(f"File downloaded from {url} is compromised")
with open(dest, "wb") as f:
f.write(resp.data)
def _setup_env(self):
os.environ.update(
{
"CPATH": "",
"LIBRARY_PATH": "",
"CFLAGS": "",
"LDFLAGS": "",
"CCFLAGS": "",
"CXXFLAGS": "",
"CPPFLAGS": "",
}
)
def _prepare_dest_folder(self):
shutil.rmtree(self._dest_dir, ignore_errors=True)
self._dest_dir.mkdir(parents=True)
2024-10-17 23:40:13 +00:00
def _setup_windows(self):
if "64bit" not in platform.architecture():
raise OSError("Only windows 64bit is supported")
extractor = self._dest_dir / ""
installer = self._dest_dir / "w64devkit-x64-2.0.0.exe"
self._util_download(
"https://github.com/ip7z/7zip/releases/download/24.08/7zr.exe",
extractor,
"d99de792fd08db53bb552cd28f0080137274f897",
)
self._util_download(
"https://github.com/skeeto/w64devkit/releases/download/v2.0.0/w64devkit-x64-2.0.0.exe",
installer,
"b5190c3ca9b06abe2b5cf329d99255a0be3a61ee",
)
2024-10-17 23:40:13 +00:00
subprocess.run(
[str(extractor), "x", str(installer), f"-o{self._dest_dir}"], check=False
).check_returncode()
sys.path.insert(0, str(self._dest_dir / "w64devkit" / "bin"))
def _download(self):
self._stdout.write("Downloading source…")
core = self._dest_dir / f"{self._core}.tar.xz"
bindings = self._dest_dir / f"{self._bindings}.tar.xz"
2024-10-17 23:40:13 +00:00
self._util_download(
f"https://oligarchy.co.uk/xapian/{self._version}/{self._core}.tar.xz",
core,
2024-10-17 23:40:13 +00:00
self._core_sha1,
)
2024-10-17 23:40:13 +00:00
self._util_download(
f"https://oligarchy.co.uk/xapian/{self._version}/{self._bindings}.tar.xz",
bindings,
2024-10-17 23:40:13 +00:00
self._bindings_sha1,
2024-08-06 09:42:10 +00:00
)
self._stdout.write("Extracting source …")
with tarfile.open(core) as tar:
tar.extractall(self._dest_dir)
with tarfile.open(bindings) as tar:
tar.extractall(self._dest_dir)
os.remove(core)
os.remove(bindings)
def _install(self):
self._stdout.write("Installing Xapian-core…")
subprocess.run(
["./configure", "--prefix", str(self._virtual_env)],
env=dict(os.environ),
cwd=self._dest_dir / self._core,
2024-10-17 23:40:13 +00:00
check=False,
).check_returncode()
subprocess.run(
[
"make",
"-j",
str(multiprocessing.cpu_count()),
],
env=dict(os.environ),
cwd=self._dest_dir / self._core,
2024-10-17 23:40:13 +00:00
check=False,
).check_returncode()
subprocess.run(
["make", "install"],
env=dict(os.environ),
cwd=self._dest_dir / self._core,
2024-10-17 23:40:13 +00:00
check=False,
).check_returncode()
self._stdout.write("Installing Xapian-bindings")
subprocess.run(
[
"./configure",
"--prefix",
str(self._virtual_env),
"--with-python3",
f"XAPIAN_CONFIG={self._virtual_env / 'bin'/'xapian-config'}",
],
env=dict(os.environ),
cwd=self._dest_dir / self._bindings,
2024-10-17 23:40:13 +00:00
check=False,
).check_returncode()
subprocess.run(
[
"make",
"-j",
str(multiprocessing.cpu_count()),
],
env=dict(os.environ),
cwd=self._dest_dir / self._bindings,
2024-10-17 23:40:13 +00:00
check=False,
).check_returncode()
subprocess.run(
["make", "install"],
env=dict(os.environ),
cwd=self._dest_dir / self._bindings,
2024-10-17 23:40:13 +00:00
check=False,
).check_returncode()
def _post_clean(self):
shutil.rmtree(self._dest_dir, ignore_errors=True)
def _test(self):
2024-10-17 23:40:13 +00:00
subprocess.run(
[sys.executable, "-c", "import xapian"], check=False
).check_returncode()
def run(self):
self._setup_env()
self._prepare_dest_folder()
2024-10-17 23:40:13 +00:00
if platform.system() == "Windows":
self._setup_windows()
self._download()
self._install()
self._post_clean()
self._test()