setup.py: Clean up implementation.

Also use pathlib.Path to better handle path manipulations.

Signed-off-by: Chris PeBenito <chpebeni@linux.microsoft.com>
This commit is contained in:
Chris PeBenito 2024-04-23 08:25:23 -04:00
parent 2661b1af6d
commit 5718140bfe

View File

@ -1,37 +1,34 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import glob
from setuptools import Extension, setup
import sys import sys
import os import os
from os.path import join import glob
from contextlib import suppress from pathlib import Path
from setuptools import Extension, setup
from Cython.Build import cythonize from Cython.Build import cythonize
import os.path
# Library linkage # Library linkage
lib_dirs = ['.', '/usr/lib64', '/usr/lib', '/usr/local/lib'] lib_dirs: list[str] = ['.', '/usr/lib64', '/usr/lib', '/usr/local/lib']
include_dirs = [] include_dirs: list[str] = []
with suppress(KeyError): userspace_src = os.getenv("USERSPACE_SRC", "")
userspace_src = os.environ["USERSPACE_SRC"] if userspace_src:
include_dirs.insert(0, userspace_src + "/libsepol/include") userspace_path = Path(userspace_src)
include_dirs.insert(1, userspace_src + "/libselinux/include") include_dirs.insert(0, str(userspace_path / "libsepol/include"))
lib_dirs.insert(0, userspace_src + "/libsepol/src") include_dirs.insert(1, str(userspace_path / "libselinux/include"))
lib_dirs.insert(1, userspace_src + "/libselinux/src") lib_dirs.insert(0, str(userspace_path / "libsepol/src"))
lib_dirs.insert(1, str(userspace_path / "libselinux/src"))
if sys.platform.startswith('darwin'): macros: list[tuple[str, str | int]] = [('DARWIN',1)] if sys.platform.startswith('darwin') else []
macros=[('DARWIN',1)]
else:
macros=[]
# Code coverage. Enable this to get coverage in the cython code. # Code coverage. Enable this to get coverage in the cython code.
enable_coverage = bool(os.environ.get("SETOOLS_COVERAGE", False)) enable_coverage = bool(os.getenv("SETOOLS_COVERAGE", ""))
if enable_coverage: if enable_coverage:
macros.append(("CYTHON_TRACE", 1)) macros.append(("CYTHON_TRACE", 1))
cython_annotate = bool(os.environ.get("SETOOLS_ANNOTATE", False)) cython_annotate = bool(os.getenv("SETOOLS_ANNOTATE", ""))
ext_py_mods = [Extension('setools.policyrep', ['setools/policyrep.pyx'], ext_py_mods = [Extension('setools.policyrep', ['setools/policyrep.pyx'],
include_dirs=include_dirs, include_dirs=include_dirs,
@ -53,16 +50,20 @@ ext_py_mods = [Extension('setools.policyrep', ['setools/policyrep.pyx'],
'-Wwrite-strings', '-Wwrite-strings',
'-fno-exceptions'])] '-fno-exceptions'])]
installed_data = [('share/man/man1', glob.glob("man/*.1"))] linguas: set[Path] = set(Path(p) for p in os.getenv("LINGUAS", "").split(" ") if p)
if not linguas:
linguas = ["ru"] linguas.add(Path("ru"))
linguas.add(Path("."))
with suppress(KeyError):
linguas = os.environ["LINGUAS"].split(" ")
base_source_path = Path("man") # below source root
base_target_path = Path("share/man") # below prefixdir, usually /usr or /usr/local
installed_data = list[tuple]()
for lang in linguas: for lang in linguas:
if lang and os.path.exists(join("man", lang)): source_path = base_source_path / lang
installed_data.append((join('share/man', lang, 'man1'), glob.glob(join("man", lang, "*.1")))) if source_path.exists():
for i in range(1, 9):
installed_data.append((base_target_path / lang / f"man{i}",
glob.glob(str(source_path / f"*.{i}"))))
# see pyproject.toml for most package options. # see pyproject.toml for most package options.
setup(data_files=installed_data, setup(data_files=installed_data,