Skip to content

Commit d92c1e4

Browse files
authored
(Setup) Refactor Codebase for Pathlib Compatibility (#2740)
2 parents aa00bdf + 0648f4d commit d92c1e4

File tree

1 file changed

+28
-29
lines changed

1 file changed

+28
-29
lines changed

setup.py

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
# SPDX - License - Identifier: GPL-3.0-or-later
44
from __future__ import annotations
55

6-
import fnmatch
76
import importlib
8-
import os
97
import platform
108
import subprocess
119
from distutils.core import Command
@@ -17,9 +15,8 @@
1715

1816
from setuptools import find_packages, setup
1917

20-
THIS_PATH = os.path.dirname(__file__)
21-
22-
versions = SourceFileLoader('versions', 'mantidimaging/__init__.py').load_module()
18+
THIS_PATH = Path(__file__).parent
19+
versions = SourceFileLoader('versions', str(THIS_PATH / 'mantidimaging' / '__init__.py')).load_module()
2320

2421

2522
class GenerateReleaseNotes(Command):
@@ -33,7 +30,8 @@ def finalize_options(self):
3330
pass
3431

3532
def run(self):
36-
spec = importlib.util.spec_from_file_location('release_notes', 'docs/ext/release_notes.py')
33+
spec = importlib.util.spec_from_file_location('release_notes',
34+
str(THIS_PATH / 'docs' / 'ext' / 'release_notes.py'))
3735
release_notes = importlib.util.module_from_spec(spec)
3836
spec.loader.exec_module(release_notes)
3937

@@ -59,20 +57,16 @@ def finalize_options(self):
5957
pass
6058

6159
@staticmethod
62-
def compile_single_file(ui_filename):
60+
def compile_single_file(ui_filename: Path):
6361
from PyQt5 import uic
6462

65-
py_filename = os.path.splitext(ui_filename)[0] + ".py"
66-
with open(py_filename, "w") as py_file:
67-
uic.compileUi(ui_filename, py_file)
63+
py_filename = ui_filename.with_suffix(".py")
64+
with py_filename.open("w") as py_file:
65+
uic.compileUi(str(ui_filename), py_file)
6866

6967
@staticmethod
70-
def find_ui_files():
71-
matches = []
72-
for root, _, filenames in os.walk("./mantidimaging/"):
73-
for filename in fnmatch.filter(filenames, "*.ui"):
74-
matches.append(os.path.join(root, filename))
75-
return matches
68+
def find_ui_files() -> list[Path]:
69+
return list(Path("./mantidimaging").rglob("*.ui"))
7670

7771
def run(self):
7872
ui_files = self.find_ui_files()
@@ -98,13 +92,12 @@ def finalize_options(self):
9892
if self.conda is None:
9993
raise ValueError("Could not find conda or mamba")
10094

101-
def count_indent(self, line):
95+
def count_indent(self, line: str) -> int:
10296
leading_spaces = len(line) - len(line.lstrip(" "))
10397
return leading_spaces // 2
10498

105-
def get_package_depends(self):
106-
# Parse the metafile manually, do avoid needing a nonstandard library
107-
meta_file = Path(__file__).parent / "conda" / "meta.yaml"
99+
def get_package_depends(self) -> list[str]:
100+
meta_file = THIS_PATH / "conda" / "meta.yaml"
108101
with meta_file.open() as meta_file_fh:
109102
section = []
110103
parsed_values = defaultdict(list)
@@ -122,9 +115,10 @@ def get_package_depends(self):
122115
parsed_values[".".join(section)].append(line.strip(" -\n"))
123116
return parsed_values["requirements.run"]
124117

125-
def make_environment_file(self, extra_deps):
126-
dev_env_file = Path(__file__).parent / "environment-dev.yml"
118+
def make_environment_file(self, extra_deps: list[str]) -> str:
119+
dev_env_file = THIS_PATH / "environment-dev.yml"
127120
output_env_file = tempfile.NamedTemporaryFile("wt", delete=False, suffix=".yaml")
121+
128122
in_dependencies_section = False
129123
with dev_env_file.open() as dev_env_file_fh:
130124
for line in dev_env_file_fh:
@@ -142,17 +136,21 @@ def make_environment_file(self, extra_deps):
142136

143137
def run(self):
144138
existing_envs_output = subprocess.check_output([self.conda, "env", "list"], encoding="utf8")
145-
if any(line.startswith("mantidimaging-dev ") for line in existing_envs_output.split("\n")):
139+
if any(line.startswith("mantidimaging-dev ") for line in existing_envs_output.splitlines()):
146140
print("Removing existing mantidimaging-dev environment")
147-
command_conda_env_remove = [self.conda, "env", "remove", "-n", "mantidimaging-dev"]
148-
subprocess.check_call(command_conda_env_remove)
141+
subprocess.check_call([self.conda, "env", "remove", "-n", "mantidimaging-dev"])
149142
extra_deps = self.get_package_depends()
150143
env_file_path = self.make_environment_file(extra_deps)
151144
print("Creating conda environment for development")
152-
command_conda_env = [self.conda, "env", "create", "-f", env_file_path]
153-
subprocess.check_call(command_conda_env)
154-
os.remove(env_file_path)
145+
subprocess.check_call([self.conda, "env", "create", "-f", env_file_path])
146+
Path(env_file_path).unlink()
147+
155148

149+
try:
150+
long_description = (THIS_PATH / "README.md").read_text(encoding="utf-8")
151+
except FileNotFoundError:
152+
long_description = ""
153+
print("Warning: README.md not found. Using default description for long_description")
156154

157155
setup(
158156
name="mantidimaging",
@@ -169,7 +167,8 @@ def run(self):
169167
url="https://github.com/mantidproject/mantidimaging",
170168
license="GPL-3.0",
171169
description="Graphical toolkit for neutron imaging",
172-
long_description=open("README.md").read(),
170+
long_description=long_description,
171+
long_description_content_type="text/markdown",
173172
classifiers=[
174173
"Programming Language :: Python :: 3.10",
175174
"Natural Language :: English",

0 commit comments

Comments
 (0)