Sith/core/management/commands/install_xapian.py

73 lines
2.2 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 os
import subprocess
from pathlib import Path
2024-06-24 11:07:36 +00:00
import tomli
from django.core.management.base import BaseCommand, CommandParser
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()
def _desired_version(self) -> str:
with open(
Path(__file__).parent.parent.parent.parent / "pyproject.toml", "rb"
) as f:
pyproject = tomli.load(f)
return pyproject["tool"]["xapian"]["version"]
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 = self._desired_version()
if desired == self._current_version():
if not force:
2024-08-06 09:42:10 +00:00
self.stdout.write(
f"Version {desired} is already installed, use --force to re-install"
)
return
2024-08-06 09:42:10 +00:00
self.stdout.write(f"Version {desired} is already installed, re-installing")
self.stdout.write(
f"Installing xapian version {desired} at {os.environ['VIRTUAL_ENV']}"
)
subprocess.run(
[str(Path(__file__).parent / "install_xapian.sh"), desired],
env=dict(os.environ),
check=True,
)
2024-08-06 09:42:10 +00:00
self.stdout.write("Installation success")