Skip to content

Commit ef1a64d

Browse files
committed
Simplify setup.py by removing regex search for version, moving out some functions to new file tools.py (not setuptools.py because module named this)
1 parent 1437040 commit ef1a64d

File tree

3 files changed

+48
-68
lines changed

3 files changed

+48
-68
lines changed

fidimag/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,3 @@
4848
Processor: {}
4949
"""
5050
print(message.format(FIDIMAG_DIR, e, platform.machine(), platform.platform(), platform.processor()))
51-
52-
53-
__version__ = '0.2'

setup.py

Lines changed: 14 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,21 @@
11
from distutils.core import setup
22
from distutils.extension import Extension
33
import multiprocessing
4-
# from Cython.Distutils import build_ext
4+
from tools import glob_files, BuildError, get_user_module_sources
55
from Cython.Build import cythonize
66
import numpy
7-
import os
87
import glob
9-
import re
10-
import sys
11-
#if sys.platform == 'darwin':
12-
# from distutils import sysconfig
13-
# vars = sysconfig.get_config_vars()
14-
# vars['LDSHARED'] = vars['LDSHARED'].replace('-bundle', '-dynamiclib')
15-
16-
class BuildError(Exception):
17-
pass
8+
import os
189

10+
version = '5.0alpha'
1911

20-
if 'CC' in os.environ:
21-
print("Using CC={}".format(os.environ['CC']))
22-
else:
23-
os.environ["CC"] = "gcc"
24-
print("Using CC={} (set by setup.py)".format(os.environ['CC']))
12+
#if 'CC' in os.environ:
13+
# print("Using CC={}".format(os.environ['CC']))
14+
#else:
15+
# os.environ["CC"] = "gcc"
16+
# print("Using CC={} (set by setup.py)".format(os.environ['CC']))
2517

2618
MODULE_DIR = os.path.dirname(os.path.abspath(__file__))
27-
print(MODULE_DIR)
2819
SRC_DIR = os.path.join(MODULE_DIR, "fidimag")
2920
SUNDIALS_DIR = os.path.join(SRC_DIR, "common", "sundials")
3021
NEB_DIR = os.path.join(SRC_DIR, "common", "neb")
@@ -35,43 +26,14 @@ class BuildError(Exception):
3526
BARYAKHTAR_DIR = os.path.join(MICRO_DIR, "baryakhtar")
3627
DEMAG_DIR = os.path.join(SRC_DIR, "common", "dipolar")
3728
USER_DIR = os.path.join(SRC_DIR, "user")
38-
print(USER_DIR)
39-
4029
LOCAL_DIR = os.path.join(MODULE_DIR, "local")
4130
INCLUDE_DIR = os.path.join(LOCAL_DIR, "include")
4231
LIB_DIR = os.path.join(LOCAL_DIR, "lib")
43-
print("LIB_DIR={}".format(LIB_DIR))
44-
45-
46-
pkg_init_path = os.path.join(
47-
os.path.dirname(__file__), 'fidimag', '__init__.py')
48-
49-
50-
def get_version():
51-
with open(pkg_init_path) as f:
52-
for line in f:
53-
m = re.match(r'''__version__\s*=\s*(['"])(.+)\1''', line.strip())
54-
if m:
55-
return m.group(2)
56-
raise Exception("Couldn't find __version__ in %s" % pkg_init_path)
57-
58-
version = get_version()
59-
60-
61-
def glob_files(path, excludes, extension="*.cpp"):
62-
files = []
63-
for srcfile in glob.glob(os.path.join(path, extension)):
64-
filename = os.path.basename(srcfile)
65-
if not filename in tuple(excludes):
66-
files.append(srcfile)
67-
return files
6832

6933
sources = []
7034
sources.append(os.path.join(ATOM_DIR, 'clib.pyx'))
7135
sources += glob_files(ATOM_DIR, excludes=["clib.cpp"])
7236

73-
74-
7537
common_sources = []
7638
common_sources.append(os.path.join(COMMON_DIR, 'common_clib.pyx'))
7739
common_sources += glob_files(COMMON_DIR, excludes=["common_clib.cpp"])
@@ -217,25 +179,9 @@ def glob_files(path, excludes, extension="*.cpp"):
217179
),
218180
]
219181

220-
221182
for folder in glob.glob(os.path.join(USER_DIR, '*/')):
222183
module_name = folder.split('/')[-2]
223-
print('Found User Module: {}'.format(module_name))
224-
user_sources = glob.glob(folder + '/*.pyx')
225-
print('\tFound Cython sources: {}'.format(user_sources))
226-
227-
if len(user_sources) != 1:
228-
raise BuildError("User Modules are only allowed one Cython .pyx file")
229-
230-
filename_string = user_sources[0].split('/')[-1][:-4]
231-
if filename_string != module_name:
232-
print(filename_string, module_name)
233-
raise BuildError("The Cython source file in {} must match the folder name - i.e. it must be {}.pyx".format(module_name, module_name))
234-
cfilename = filename_string + '.cpp'
235-
print(cfilename)
236-
user_sources += glob_files(folder, excludes=[cfilename])
237-
238-
print(user_sources)
184+
user_sources = get_user_module_sources(folder)
239185

240186
ext_modules.append(
241187
Extension("fidimag.extensions.user.{}".format(module_name),
@@ -248,8 +194,11 @@ def glob_files(path, excludes, extension="*.cpp"):
248194
),
249195
)
250196

197+
198+
199+
251200
nthreads = multiprocessing.cpu_count()
252-
print('Building with {} threads'.format(nthreads))
201+
253202
setup(
254203
name='fidimag',
255204
version=version,
@@ -259,7 +208,7 @@ def glob_files(path, excludes, extension="*.cpp"):
259208
'fidimag.micro',
260209
'fidimag.extensions',
261210
'fidimag.common',
262-
],
211+
],
263212
ext_modules=cythonize(ext_modules,
264213
nthreads=nthreads,
265214
compiler_directives={

tools.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import glob
2+
import os
3+
4+
def glob_files(path, excludes, extension="*.cpp"):
5+
files = []
6+
for srcfile in glob.glob(os.path.join(path, extension)):
7+
filename = os.path.basename(srcfile)
8+
if not filename in tuple(excludes):
9+
files.append(srcfile)
10+
return files
11+
12+
13+
class BuildError(Exception):
14+
pass
15+
16+
17+
18+
def get_user_module_sources(folder):
19+
print('Found User Module: {}'.format(folder))
20+
user_sources = glob.glob(folder + '/*.pyx')
21+
print('\tFound Cython sources: {}'.format(user_sources))
22+
23+
if len(user_sources) != 1:
24+
raise BuildError("User Modules are only allowed one Cython .pyx file")
25+
26+
filename_string = user_sources[0].split('/')[-1][:-4]
27+
if filename_string != module_name:
28+
print(filename_string, module_name)
29+
raise BuildError("The Cython source file in {} must match the folder name - i.e. it must be {}.pyx".format(module_name, module_name))
30+
cfilename = filename_string + '.cpp'
31+
print(cfilename)
32+
user_sources += glob_files(folder, excludes=[cfilename])
33+
34+
return user_sources

0 commit comments

Comments
 (0)