Skip to content

Commit 5718140

Browse files
committed
setup.py: Clean up implementation.
Also use pathlib.Path to better handle path manipulations. Signed-off-by: Chris PeBenito <chpebeni@linux.microsoft.com>
1 parent 2661b1a commit 5718140

1 file changed

Lines changed: 28 additions & 27 deletions

File tree

setup.py

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,34 @@
11
#!/usr/bin/env python3
22

3-
import glob
4-
from setuptools import Extension, setup
53
import sys
64
import os
7-
from os.path import join
8-
from contextlib import suppress
5+
import glob
6+
from pathlib import Path
7+
8+
from setuptools import Extension, setup
99
from Cython.Build import cythonize
10-
import os.path
1110

1211

1312
# Library linkage
14-
lib_dirs = ['.', '/usr/lib64', '/usr/lib', '/usr/local/lib']
15-
include_dirs = []
13+
lib_dirs: list[str] = ['.', '/usr/lib64', '/usr/lib', '/usr/local/lib']
14+
include_dirs: list[str] = []
1615

17-
with suppress(KeyError):
18-
userspace_src = os.environ["USERSPACE_SRC"]
19-
include_dirs.insert(0, userspace_src + "/libsepol/include")
20-
include_dirs.insert(1, userspace_src + "/libselinux/include")
21-
lib_dirs.insert(0, userspace_src + "/libsepol/src")
22-
lib_dirs.insert(1, userspace_src + "/libselinux/src")
16+
userspace_src = os.getenv("USERSPACE_SRC", "")
17+
if userspace_src:
18+
userspace_path = Path(userspace_src)
19+
include_dirs.insert(0, str(userspace_path / "libsepol/include"))
20+
include_dirs.insert(1, str(userspace_path / "libselinux/include"))
21+
lib_dirs.insert(0, str(userspace_path / "libsepol/src"))
22+
lib_dirs.insert(1, str(userspace_path / "libselinux/src"))
2323

24-
if sys.platform.startswith('darwin'):
25-
macros=[('DARWIN',1)]
26-
else:
27-
macros=[]
24+
macros: list[tuple[str, str | int]] = [('DARWIN',1)] if sys.platform.startswith('darwin') else []
2825

2926
# Code coverage. Enable this to get coverage in the cython code.
30-
enable_coverage = bool(os.environ.get("SETOOLS_COVERAGE", False))
27+
enable_coverage = bool(os.getenv("SETOOLS_COVERAGE", ""))
3128
if enable_coverage:
3229
macros.append(("CYTHON_TRACE", 1))
3330

34-
cython_annotate = bool(os.environ.get("SETOOLS_ANNOTATE", False))
31+
cython_annotate = bool(os.getenv("SETOOLS_ANNOTATE", ""))
3532

3633
ext_py_mods = [Extension('setools.policyrep', ['setools/policyrep.pyx'],
3734
include_dirs=include_dirs,
@@ -53,16 +50,20 @@
5350
'-Wwrite-strings',
5451
'-fno-exceptions'])]
5552

56-
installed_data = [('share/man/man1', glob.glob("man/*.1"))]
57-
58-
linguas = ["ru"]
59-
60-
with suppress(KeyError):
61-
linguas = os.environ["LINGUAS"].split(" ")
53+
linguas: set[Path] = set(Path(p) for p in os.getenv("LINGUAS", "").split(" ") if p)
54+
if not linguas:
55+
linguas.add(Path("ru"))
56+
linguas.add(Path("."))
6257

58+
base_source_path = Path("man") # below source root
59+
base_target_path = Path("share/man") # below prefixdir, usually /usr or /usr/local
60+
installed_data = list[tuple]()
6361
for lang in linguas:
64-
if lang and os.path.exists(join("man", lang)):
65-
installed_data.append((join('share/man', lang, 'man1'), glob.glob(join("man", lang, "*.1"))))
62+
source_path = base_source_path / lang
63+
if source_path.exists():
64+
for i in range(1, 9):
65+
installed_data.append((base_target_path / lang / f"man{i}",
66+
glob.glob(str(source_path / f"*.{i}"))))
6667

6768
# see pyproject.toml for most package options.
6869
setup(data_files=installed_data,

0 commit comments

Comments
 (0)