Skip to content

Commit

Permalink
Simplify setup.py by removing regex search for version, moving out so…
Browse files Browse the repository at this point in the history
…me functions to new file tools.py (not setuptools.py because module named this)
  • Loading branch information
rpep committed May 9, 2019
1 parent 1437040 commit ef1a64d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 68 deletions.
3 changes: 0 additions & 3 deletions fidimag/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,3 @@
Processor: {}
"""
print(message.format(FIDIMAG_DIR, e, platform.machine(), platform.platform(), platform.processor()))


__version__ = '0.2'
79 changes: 14 additions & 65 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,21 @@
from distutils.core import setup
from distutils.extension import Extension
import multiprocessing
# from Cython.Distutils import build_ext
from tools import glob_files, BuildError, get_user_module_sources
from Cython.Build import cythonize
import numpy
import os
import glob
import re
import sys
#if sys.platform == 'darwin':
# from distutils import sysconfig
# vars = sysconfig.get_config_vars()
# vars['LDSHARED'] = vars['LDSHARED'].replace('-bundle', '-dynamiclib')

class BuildError(Exception):
pass
import os

version = '5.0alpha'

if 'CC' in os.environ:
print("Using CC={}".format(os.environ['CC']))
else:
os.environ["CC"] = "gcc"
print("Using CC={} (set by setup.py)".format(os.environ['CC']))
#if 'CC' in os.environ:
# print("Using CC={}".format(os.environ['CC']))
#else:
# os.environ["CC"] = "gcc"
# print("Using CC={} (set by setup.py)".format(os.environ['CC']))

MODULE_DIR = os.path.dirname(os.path.abspath(__file__))
print(MODULE_DIR)
SRC_DIR = os.path.join(MODULE_DIR, "fidimag")
SUNDIALS_DIR = os.path.join(SRC_DIR, "common", "sundials")
NEB_DIR = os.path.join(SRC_DIR, "common", "neb")
Expand All @@ -35,43 +26,14 @@ class BuildError(Exception):
BARYAKHTAR_DIR = os.path.join(MICRO_DIR, "baryakhtar")
DEMAG_DIR = os.path.join(SRC_DIR, "common", "dipolar")
USER_DIR = os.path.join(SRC_DIR, "user")
print(USER_DIR)

LOCAL_DIR = os.path.join(MODULE_DIR, "local")
INCLUDE_DIR = os.path.join(LOCAL_DIR, "include")
LIB_DIR = os.path.join(LOCAL_DIR, "lib")
print("LIB_DIR={}".format(LIB_DIR))


pkg_init_path = os.path.join(
os.path.dirname(__file__), 'fidimag', '__init__.py')


def get_version():
with open(pkg_init_path) as f:
for line in f:
m = re.match(r'''__version__\s*=\s*(['"])(.+)\1''', line.strip())
if m:
return m.group(2)
raise Exception("Couldn't find __version__ in %s" % pkg_init_path)

version = get_version()


def glob_files(path, excludes, extension="*.cpp"):
files = []
for srcfile in glob.glob(os.path.join(path, extension)):
filename = os.path.basename(srcfile)
if not filename in tuple(excludes):
files.append(srcfile)
return files

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



common_sources = []
common_sources.append(os.path.join(COMMON_DIR, 'common_clib.pyx'))
common_sources += glob_files(COMMON_DIR, excludes=["common_clib.cpp"])
Expand Down Expand Up @@ -217,25 +179,9 @@ def glob_files(path, excludes, extension="*.cpp"):
),
]


for folder in glob.glob(os.path.join(USER_DIR, '*/')):
module_name = folder.split('/')[-2]
print('Found User Module: {}'.format(module_name))
user_sources = glob.glob(folder + '/*.pyx')
print('\tFound Cython sources: {}'.format(user_sources))

if len(user_sources) != 1:
raise BuildError("User Modules are only allowed one Cython .pyx file")

filename_string = user_sources[0].split('/')[-1][:-4]
if filename_string != module_name:
print(filename_string, module_name)
raise BuildError("The Cython source file in {} must match the folder name - i.e. it must be {}.pyx".format(module_name, module_name))
cfilename = filename_string + '.cpp'
print(cfilename)
user_sources += glob_files(folder, excludes=[cfilename])

print(user_sources)
user_sources = get_user_module_sources(folder)

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




nthreads = multiprocessing.cpu_count()
print('Building with {} threads'.format(nthreads))

setup(
name='fidimag',
version=version,
Expand All @@ -259,7 +208,7 @@ def glob_files(path, excludes, extension="*.cpp"):
'fidimag.micro',
'fidimag.extensions',
'fidimag.common',
],
],
ext_modules=cythonize(ext_modules,
nthreads=nthreads,
compiler_directives={
Expand Down
34 changes: 34 additions & 0 deletions tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import glob
import os

def glob_files(path, excludes, extension="*.cpp"):
files = []
for srcfile in glob.glob(os.path.join(path, extension)):
filename = os.path.basename(srcfile)
if not filename in tuple(excludes):
files.append(srcfile)
return files


class BuildError(Exception):
pass



def get_user_module_sources(folder):
print('Found User Module: {}'.format(folder))
user_sources = glob.glob(folder + '/*.pyx')
print('\tFound Cython sources: {}'.format(user_sources))

if len(user_sources) != 1:
raise BuildError("User Modules are only allowed one Cython .pyx file")

filename_string = user_sources[0].split('/')[-1][:-4]
if filename_string != module_name:
print(filename_string, module_name)
raise BuildError("The Cython source file in {} must match the folder name - i.e. it must be {}.pyx".format(module_name, module_name))
cfilename = filename_string + '.cpp'
print(cfilename)
user_sources += glob_files(folder, excludes=[cfilename])

return user_sources

0 comments on commit ef1a64d

Please sign in to comment.