Skip to content

Commit 8b93472

Browse files
committed
skpkg: package update command with no manual edits.
1 parent eae357e commit 8b93472

2 files changed

Lines changed: 12 additions & 216 deletions

File tree

setup.py

Lines changed: 12 additions & 185 deletions
Original file line numberDiff line numberDiff line change
@@ -1,207 +1,34 @@
11
#!/usr/bin/env python
22

33
# Extensions script for diffpy.pdffit2
4-
"""PDFfit2 - real space structure refinement engine
5-
6-
Packages: diffpy.pdffit2
7-
Scripts: pdffit2
8-
"""
94

105
import glob
11-
import os
12-
import re
13-
import shutil
14-
import sys
15-
import warnings
16-
from pathlib import Path
6+
import os # noqa
7+
import re # noqa
8+
import sys # noqa
179

1810
from setuptools import Extension, setup
19-
from setuptools.command.build_ext import build_ext
20-
21-
# Use this version when git data are not available, like in git zip archive.
22-
# Update when tagging a new release.
23-
FALLBACK_VERSION = "1.4.3"
24-
25-
MYDIR = str(Path(__file__).parent.resolve())
26-
27-
# Helper functions -----------------------------------------------------------
28-
29-
30-
def get_compiler_type():
31-
"""Return the compiler type used during the build."""
32-
cc_arg = [a for a in sys.argv if a.startswith("--compiler=")]
33-
if cc_arg:
34-
return cc_arg[-1].split("=", 1)[1]
35-
from distutils.ccompiler import new_compiler
36-
37-
return new_compiler().compiler_type
38-
39-
40-
def get_gsl_config():
41-
"""
42-
Determine the GSL include and library directories by trying in order:
43-
1) CONDA_PREFIX,
44-
2) GSL_PATH,
45-
3) gsl-config (for Unix-like systems).
46-
Raises EnvironmentError if none are found.
47-
"""
48-
rv = {"include_dirs": [], "library_dirs": []}
49-
50-
# 1. Check using CONDA_PREFIX.
51-
conda_prefix = os.environ.get("CONDA_PREFIX", "")
52-
if conda_prefix:
53-
if os.name == "nt":
54-
inc = Path(conda_prefix) / "Library" / "include"
55-
lib = Path(conda_prefix) / "Library" / "lib"
56-
else:
57-
inc = Path(conda_prefix) / "include"
58-
lib = Path(conda_prefix) / "lib"
59-
if inc.is_dir() and lib.is_dir():
60-
rv["include_dirs"].append(str(inc))
61-
rv["library_dirs"].append(str(lib))
62-
return rv
63-
else:
64-
warnings.warn(
65-
f"CONDA_PREFIX is set to {conda_prefix}, "
66-
"but GSL not found at those paths. Proceeding..."
67-
)
6811

69-
# 2. Check using GSL_PATH.
70-
gsl_path = os.environ.get("GSL_PATH", "")
71-
if gsl_path:
72-
inc = Path(gsl_path) / "include"
73-
lib = Path(gsl_path) / "lib"
74-
if inc.is_dir() and lib.is_dir():
75-
rv["include_dirs"].append(str(inc))
76-
rv["library_dirs"].append(str(lib))
77-
return rv
78-
else:
79-
raise EnvironmentError(
80-
f"GSL_PATH={gsl_path} is set, but {inc} or {lib} not found. "
81-
"Please verify your GSL_PATH."
82-
)
83-
84-
# 3. Try using the gsl-config executable (only on Unix-like systems).
85-
if os.name != "nt":
86-
path_dirs = os.environ.get("PATH", "").split(os.pathsep)
87-
gslcfg_paths = [Path(p) / "gsl-config" for p in path_dirs if p]
88-
gslcfg_paths = [p for p in gslcfg_paths if p.is_file()]
89-
if gslcfg_paths:
90-
gslcfg = gslcfg_paths[0]
91-
txt = gslcfg.read_text()
92-
prefix_match = re.search(r"(?m)^prefix=(.+)", txt)
93-
include_match = re.search(r"(?m)^[^#]*\s-I(\S+)", txt)
94-
lib_match = re.search(r"(?m)^[^#]*\s-L(\S+)", txt)
95-
if prefix_match:
96-
prefix_path = Path(prefix_match.group(1))
97-
inc_dir = (
98-
include_match.group(1)
99-
if include_match
100-
else (prefix_path / "include")
101-
)
102-
lib_dir = (
103-
lib_match.group(1) if lib_match else (prefix_path / "lib")
104-
)
105-
rv["include_dirs"].append(str(inc_dir))
106-
rv["library_dirs"].append(str(lib_dir))
107-
return rv
108-
else:
109-
raise RuntimeError(f"Cannot parse 'prefix=' from {gslcfg}.")
110-
else:
111-
warnings.warn(
112-
"No gsl-config found in PATH. GSL may not be installed or not in PATH. "
113-
"Proceeding without GSL configuration."
114-
)
115-
116-
# 4. Nothing found: raise error.
117-
raise EnvironmentError(
118-
"Unable to locate GSL:\n"
119-
"1) CONDA_PREFIX not set or no GSL there\n"
120-
"2) GSL_PATH not set or invalid\n"
121-
"3) gsl-config not available\n"
122-
"Please set GSL_PATH or use a conda environment with GSL."
123-
)
124-
125-
126-
class CustomBuildExt(build_ext):
127-
def run(self):
128-
# Retrieve the GSL library directories and append them to each extension.
129-
gsl_cfg = get_gsl_config()
130-
lib_dirs = gsl_cfg.get("library_dirs", [])
131-
for ext in self.extensions:
132-
# Add gsl lib for linking.
133-
ext.library_dirs.extend(lib_dirs)
134-
# Embed RPATH flags, runtime linking without LD_LIBRARY_PATH.
135-
ext.extra_link_args = ext.extra_link_args or []
136-
for lib in lib_dirs:
137-
ext.extra_link_args.append(f"-Wl,-rpath,{lib}")
138-
super().run()
139-
# Avoid dll error
140-
gsl_path = (
141-
Path(os.environ.get("GSL_PATH"))
142-
if os.environ.get("GSL_PATH")
143-
else Path(os.environ.get("CONDA_PREFIX", "")) / "Library"
144-
)
145-
bin_path = gsl_path / "bin"
146-
dest_path = Path(self.build_lib) / "diffpy" / "pdffit2"
147-
dest_path.mkdir(parents=True, exist_ok=True)
148-
for dll_file in bin_path.glob("gsl*.dll"):
149-
shutil.copy(str(dll_file), str(dest_path))
12+
# Define extension arguments here
13+
ext_kws = {
14+
"libraries": [],
15+
"extra_compile_args": [],
16+
"extra_link_args": [],
17+
"include_dirs": [],
18+
}
15019

15120

15221
def create_extensions():
153-
"""Create the list of Extension objects for the build."""
154-
# lazy evaluation prevents build sdist failure
155-
try:
156-
gcfg = get_gsl_config()
157-
except EnvironmentError:
158-
return []
159-
160-
libraries = ["gsl"]
161-
162-
include_dirs = [MYDIR] + gcfg["include_dirs"]
163-
library_dirs = gcfg["library_dirs"]
164-
define_macros = []
165-
extra_objects = []
166-
extra_compile_args = []
167-
extra_link_args = []
168-
169-
compiler_type = get_compiler_type()
170-
if compiler_type in ("unix", "cygwin", "mingw32"):
171-
extra_compile_args = [
172-
"-std=c++11",
173-
"-Wall",
174-
"-Wno-write-strings",
175-
"-O3",
176-
"-funroll-loops",
177-
"-ffast-math",
178-
]
179-
elif compiler_type == "msvc":
180-
define_macros += [("_USE_MATH_DEFINES", None)]
181-
extra_compile_args = ["/EHs"]
182-
183-
# Extension keyword arguments.
184-
ext_kws = {
185-
"include_dirs": include_dirs,
186-
"libraries": libraries,
187-
"library_dirs": library_dirs,
188-
"define_macros": define_macros,
189-
"extra_compile_args": extra_compile_args,
190-
"extra_link_args": extra_link_args,
191-
"extra_objects": extra_objects,
192-
}
22+
"Initialize Extension objects for the setup function."
19323
ext = Extension(
194-
"diffpy.pdffit2.pdffit2",
195-
glob.glob("src/extensions/**/*.cc"),
196-
**ext_kws,
24+
"diffpy.pdffit2.pdffit2", glob.glob("src/extensions/*.cpp"), **ext_kws
19725
)
19826
return [ext]
19927

20028

20129
# Extensions not included in pyproject.toml
20230
setup_args = dict(
20331
ext_modules=[],
204-
cmdclass={"build_ext": CustomBuildExt},
20532
)
20633

20734

tests/conftest.py

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
import io
21
import json
32
from pathlib import Path
43

54
import pytest
65

7-
import diffpy.pdffit2
8-
import diffpy.pdffit2.output # assuming this is the correct import path
9-
106

117
@pytest.fixture
128
def user_filesystem(tmp_path):
@@ -21,30 +17,3 @@ def user_filesystem(tmp_path):
2117
json.dump(home_config_data, f)
2218

2319
yield tmp_path
24-
25-
26-
@pytest.fixture
27-
def datafile():
28-
"""Fixture to dynamically load any test file."""
29-
30-
def _load(filename):
31-
return "tests/testdata/" + filename
32-
33-
return _load
34-
35-
36-
@pytest.fixture
37-
def capture_output():
38-
"""Capture output from pdffit2 engine produced in function call."""
39-
40-
def _capture(f, *args, **kwargs):
41-
savestdout = diffpy.pdffit2.output.stdout
42-
fp = io.StringIO()
43-
diffpy.pdffit2.redirect_stdout(fp)
44-
try:
45-
f(*args, **kwargs)
46-
finally:
47-
diffpy.pdffit2.redirect_stdout(savestdout)
48-
return fp.getvalue()
49-
50-
return _capture

0 commit comments

Comments
 (0)