source: sasview/setup.py @ 952ea1f

magnetic_scattrelease-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249
Last change on this file since 952ea1f was 952ea1f, checked in by Paul Kienzle <pkienzle@…>, 6 years ago

move c extensions from core/module.so to _module.so

  • Property mode set to 100755
File size: 15.7 KB
Line 
1# -*- coding: utf-8 -*-
2#!/usr/bin/env python
3
4"""
5    Setup for SasView
6    TODO: Add checks to see that all the dependencies are on the system
7"""
8from __future__ import print_function
9
10import os
11import subprocess
12import shutil
13import sys
14
15import numpy as np
16
17from setuptools import Extension, setup
18from setuptools import Command
19from setuptools.command.build_ext import build_ext
20
21try:
22    import tinycc.distutils
23except ImportError:
24    pass
25
26# Convert "test" argument to "pytest" so 'python setup.py test' works
27sys.argv = [("pytest" if s == "test" else s) for s in sys.argv]
28
29# Manage version number ######################################
30with open(os.path.join("src", "sas", "sasview", "__init__.py")) as fid:
31    for line in fid:
32        if line.startswith('__version__'):
33            VERSION = line.split('"')[1]
34            break
35    else:
36        raise ValueError("Could not find version in src/sas/sasview/__init__.py")
37##############################################################
38
39package_dir = {}
40package_data = {}
41packages = []
42ext_modules = []
43
44# Remove all files that should be updated by this setup
45# We do this here because application updates these files from .sasview
46# except when there is no such file
47# Todo : make this list generic
48# plugin_model_list = ['polynominal5.py', 'sph_bessel_jn.py',
49#                      'sum_Ap1_1_Ap2.py', 'sum_p1_p2.py',
50#                      'testmodel_2.py', 'testmodel.py',
51#                      'polynominal5.pyc', 'sph_bessel_jn.pyc',
52#                      'sum_Ap1_1_Ap2.pyc', 'sum_p1_p2.pyc',
53#                      'testmodel_2.pyc', 'testmodel.pyc', 'plugins.log']
54
55CURRENT_SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
56SASVIEW_BUILD = os.path.join(CURRENT_SCRIPT_DIR, "build")
57
58# TODO: build step should not be messing with existing installation!!
59sas_dir = os.path.join(os.path.expanduser("~"), '.sasview')
60if os.path.isdir(sas_dir):
61    f_path = os.path.join(sas_dir, "sasview.log")
62    if os.path.isfile(f_path):
63        os.remove(f_path)
64    f_path = os.path.join(sas_dir, "categories.json")
65    if os.path.isfile(f_path):
66        os.remove(f_path)
67    f_path = os.path.join(sas_dir, 'config', "custom_config.py")
68    if os.path.isfile(f_path):
69        os.remove(f_path)
70    #f_path = os.path.join(sas_dir, 'plugin_models')
71    # if os.path.isdir(f_path):
72    #     for f in os.listdir(f_path):
73    #         if f in plugin_model_list:
74    #             file_path =  os.path.join(f_path, f)
75    #             os.remove(file_path)
76
77
78# Optionally clean before build.
79dont_clean = 'update' in sys.argv
80if dont_clean:
81    sys.argv.remove('update')
82elif os.path.exists(SASVIEW_BUILD):
83    print("Removing existing build directory", SASVIEW_BUILD, "for a clean build")
84    shutil.rmtree(SASVIEW_BUILD)
85
86# 'sys.maxsize' and 64bit: Not supported for python2.5
87is_64bits = sys.maxsize > 2**32
88
89enable_openmp = False
90if sys.platform == 'darwin':
91    if not is_64bits:
92        # Disable OpenMP
93        enable_openmp = False
94    else:
95        # Newer versions of Darwin don't support openmp
96        try:
97            darwin_ver = int(os.uname()[2].split('.')[0])
98            if darwin_ver >= 12:
99                enable_openmp = False
100        except:
101            print("PROBLEM determining Darwin version")
102
103# Options to enable OpenMP
104copt = {'msvc': ['/openmp'],
105        'mingw32': ['-fopenmp'],
106        'unix': ['-fopenmp']}
107lopt = {'msvc': ['/MANIFEST'],
108        'mingw32': ['-fopenmp'],
109        'unix': ['-lgomp']}
110
111# Platform-specific link options
112platform_lopt = {'msvc': ['/MANIFEST']}
113platform_copt = {}
114
115# Set copts to get compile working on OS X >= 10.9 using clang
116if sys.platform == 'darwin':
117    try:
118        darwin_ver = int(os.uname()[2].split('.')[0])
119        if darwin_ver >= 13 and darwin_ver < 14:
120            platform_copt = {
121                'unix': ['-Wno-error=unused-command-line-argument-hard-error-in-future']}
122    except:
123        print("PROBLEM determining Darwin version")
124
125
126class DisableOpenMPCommand(Command):
127    description = "The version of MinGW that comes with Anaconda does not come with OpenMP :( "\
128                  "This commands means we can turn off compiling with OpenMP for this or any "\
129                  "other reason."
130    user_options = []
131
132    def initialize_options(self):
133        self.cwd = None
134
135    def finalize_options(self):
136        self.cwd = os.getcwd()
137        global enable_openmp
138        enable_openmp = False
139
140    def run(self):
141        pass
142
143
144class build_ext_subclass(build_ext):
145    def build_extensions(self):
146        # Get 64-bitness
147        c = self.compiler.compiler_type
148        print("Compiling with %s (64bit=%s)" % (c, str(is_64bits)))
149        #print("=== compiler attributes ===")
150        #print("\n".join("%s: %s"%(k, v) for k, v in sorted(self.compiler.__dict__.items())))
151        #print("=== build_ext attributes ===")
152        #print("\n".join("%s: %s"%(k, v) for k, v in self.__dict__.items()))
153        #sys.exit(1)
154
155        # OpenMP build options
156        if enable_openmp:
157            if c in copt:
158                for e in self.extensions:
159                    e.extra_compile_args = copt[c]
160            if c in lopt:
161                for e in self.extensions:
162                    e.extra_link_args = lopt[c]
163
164        # Platform-specific build options
165        if c in platform_lopt:
166            for e in self.extensions:
167                e.extra_link_args = platform_lopt[c]
168
169        if c in platform_copt:
170            for e in self.extensions:
171                e.extra_compile_args = platform_copt[c]
172
173        build_ext.build_extensions(self)
174
175
176class BuildSphinxCommand(Command):
177    description = "Build Sphinx documentation."
178    user_options = []
179
180    def initialize_options(self):
181        self.cwd = None
182
183    def finalize_options(self):
184        self.cwd = os.getcwd()
185
186    def run(self):
187        ''' First builds the sasmodels documentation if the directory
188        is present. Then builds the sasview docs.
189        '''
190        ### AJJ - Add code for building sasmodels docs here:
191        # check for doc path
192        SASMODELS_DOCPATH = os.path.abspath(os.path.join(os.getcwd(), '..', 'sasmodels', 'doc'))
193        print("========= check for sasmodels at", SASMODELS_DOCPATH, "============")
194        if os.path.exists(SASMODELS_DOCPATH):
195            if os.path.isdir(SASMODELS_DOCPATH):
196                # if available, build sasmodels docs
197                print("============= Building sasmodels model documentation ===============")
198                smdocbuild = subprocess.call(["make", "-C", SASMODELS_DOCPATH, "html"])
199        else:
200            # if not available warning message
201            print("== !!WARNING!! sasmodels directory not found. Cannot build model docs. ==")
202
203        #Now build sasview (+sasmodels) docs
204        sys.path.append("docs/sphinx-docs")
205        import build_sphinx
206        build_sphinx.rebuild()
207
208
209# sas module
210package_dir["sas"] = os.path.join("src", "sas")
211packages.append("sas")
212
213# sas module
214package_dir["sas.sasgui"] = os.path.join("src", "sas", "sasgui")
215packages.append("sas.sasgui")
216
217# sas module
218package_dir["sas.sascalc"] = os.path.join("src", "sas", "sascalc")
219packages.append("sas.sascalc")
220
221# sas.sascalc.invariant
222package_dir["sas.sascalc.invariant"] = os.path.join(
223    "src", "sas", "sascalc", "invariant")
224packages.extend(["sas.sascalc.invariant"])
225
226# sas.sasgui.guiframe
227guiframe_path = os.path.join("src", "sas", "sasgui", "guiframe")
228package_dir["sas.sasgui.guiframe"] = guiframe_path
229package_dir["sas.sasgui.guiframe.local_perspectives"] = os.path.join(
230    os.path.join(guiframe_path, "local_perspectives"))
231package_data["sas.sasgui.guiframe"] = ['images/*', 'media/*']
232packages.extend(
233    ["sas.sasgui.guiframe", "sas.sasgui.guiframe.local_perspectives"])
234# build local plugin
235for d in os.listdir(os.path.join(guiframe_path, "local_perspectives")):
236    if d not in ['.svn', '__init__.py', '__init__.pyc']:
237        package_name = "sas.sasgui.guiframe.local_perspectives." + d
238        packages.append(package_name)
239        package_dir[package_name] = os.path.join(
240            guiframe_path, "local_perspectives", d)
241
242# sas.sascalc.dataloader
243package_dir["sas.sascalc.dataloader"] = os.path.join(
244    "src", "sas", "sascalc", "dataloader")
245package_data["sas.sascalc.dataloader.readers"] = ['schema/*.xsd']
246packages.extend(["sas.sascalc.dataloader", "sas.sascalc.dataloader.readers",
247                 "sas.sascalc.dataloader.readers.schema"])
248
249
250# sas.sascalc.calculator
251gen_dir = os.path.join("src", "sas", "sascalc", "calculator", "c_extensions")
252package_dir["sas.sascalc.calculator"] = os.path.join(
253    "src", "sas", "sascalc", "calculator")
254packages.append("sas.sascalc.calculator")
255ext_modules.append(Extension("sas.sascalc.calculator._sld2i",
256                             sources=[
257                                 os.path.join(gen_dir, "sld2i_module.c"),
258                                 os.path.join(gen_dir, "sld2i.c"),
259                                 os.path.join(gen_dir, "libfunc.c"),
260                                 os.path.join(gen_dir, "librefl.c"),
261                             ],
262                             include_dirs=[gen_dir],
263                             ))
264
265# sas.sascalc.pr
266srcdir = os.path.join("src", "sas", "sascalc", "pr", "c_extensions")
267package_dir["sas.sascalc.pr"] = os.path.join("src", "sas", "sascalc", "pr")
268packages.append("sas.sascalc.pr")
269ext_modules.append(Extension("sas.sascalc.pr._pr_inversion",
270                             sources=[os.path.join(srcdir, "Cinvertor.c"),
271                                      os.path.join(srcdir, "invertor.c"),
272                                      ],
273                             include_dirs=[],
274                             ))
275
276
277# sas.sascalc.file_converter
278mydir = os.path.join("src", "sas", "sascalc", "file_converter", "c_ext")
279package_dir["sas.sascalc.file_converter"] = os.path.join(
280    "src", "sas", "sascalc", "file_converter")
281packages.append("sas.sascalc.file_converter")
282ext_modules.append(Extension("sas.sascalc.file_converter._bsl_loader",
283                             sources=[os.path.join(mydir, "bsl_loader.c")],
284                             include_dirs=[np.get_include()],
285                             ))
286
287# sas.sascalc.corfunc
288package_dir["sas.sascalc.corfunc"] = os.path.join(
289    "src", "sas", "sascalc", "corfunc")
290
291packages.extend(["sas.sascalc.corfunc"])
292
293# sas.sascalc.fit
294package_dir["sas.sascalc.fit"] = os.path.join("src", "sas", "sascalc", "fit")
295packages.append("sas.sascalc.fit")
296
297# Perspectives
298package_dir["sas.sasgui.perspectives"] = os.path.join(
299    "src", "sas", "sasgui", "perspectives")
300package_dir["sas.sasgui.perspectives.pr"] = os.path.join(
301    "src", "sas", "sasgui", "perspectives", "pr")
302packages.extend(["sas.sasgui.perspectives", "sas.sasgui.perspectives.pr"])
303package_data["sas.sasgui.perspectives.pr"] = ['media/*']
304
305package_dir["sas.sasgui.perspectives.invariant"] = os.path.join(
306    "src", "sas", "sasgui", "perspectives", "invariant")
307packages.extend(["sas.sasgui.perspectives.invariant"])
308package_data['sas.sasgui.perspectives.invariant'] = [
309    os.path.join("media", '*')]
310
311package_dir["sas.sasgui.perspectives.fitting"] = os.path.join(
312    "src", "sas", "sasgui", "perspectives", "fitting")
313package_dir["sas.sasgui.perspectives.fitting.plugin_models"] = os.path.join(
314    "src", "sas", "sasgui", "perspectives", "fitting", "plugin_models")
315packages.extend(["sas.sasgui.perspectives.fitting",
316                 "sas.sasgui.perspectives.fitting.plugin_models"])
317package_data['sas.sasgui.perspectives.fitting'] = [
318    'media/*', 'plugin_models/*']
319
320packages.extend(["sas.sasgui.perspectives",
321                 "sas.sasgui.perspectives.calculator"])
322package_data['sas.sasgui.perspectives.calculator'] = ['images/*', 'media/*']
323
324package_dir["sas.sasgui.perspectives.corfunc"] = os.path.join(
325    "src", "sas", "sasgui", "perspectives", "corfunc")
326packages.extend(["sas.sasgui.perspectives.corfunc"])
327package_data['sas.sasgui.perspectives.corfunc'] = ['media/*']
328
329package_dir["sas.sasgui.perspectives.file_converter"] = os.path.join(
330    "src", "sas", "sasgui", "perspectives", "file_converter")
331packages.extend(["sas.sasgui.perspectives.file_converter"])
332package_data['sas.sasgui.perspectives.file_converter'] = ['media/*']
333
334# Data util
335package_dir["sas.sascalc.data_util"] = os.path.join(
336    "src", "sas", "sascalc", "data_util")
337packages.append("sas.sascalc.data_util")
338
339# Plottools
340package_dir["sas.sasgui.plottools"] = os.path.join(
341    "src", "sas", "sasgui", "plottools")
342packages.append("sas.sasgui.plottools")
343
344# # Last of the sas.models
345# package_dir["sas.models"] = os.path.join("src", "sas", "models")
346# packages.append("sas.models")
347
348EXTENSIONS = [".c", ".cpp"]
349
350
351def append_file(file_list, dir_path):
352    """
353    Add sources file to sources
354    """
355    for f in os.listdir(dir_path):
356        if os.path.isfile(os.path.join(dir_path, f)):
357            _, ext = os.path.splitext(f)
358            if ext.lower() in EXTENSIONS:
359                file_list.append(os.path.join(dir_path, f))
360        elif os.path.isdir(os.path.join(dir_path, f)) and \
361                not f.startswith("."):
362            sub_dir = os.path.join(dir_path, f)
363            for new_f in os.listdir(sub_dir):
364                if os.path.isfile(os.path.join(sub_dir, new_f)):
365                    _, ext = os.path.splitext(new_f)
366                    if ext.lower() in EXTENSIONS:
367                        file_list.append(os.path.join(sub_dir, new_f))
368
369
370# Comment out the following to avoid rebuilding all the models
371file_sources = []
372append_file(file_sources, gen_dir)
373
374# Wojtek's hacky way to add doc files while bundling egg
375# def add_doc_files(directory):
376#    paths = []
377#    for (path, directories, filenames) in os.walk(directory):
378#        for filename in filenames:
379#            paths.append(os.path.join(path, filename))
380#    return paths
381
382#doc_files = add_doc_files('doc')
383
384# SasView
385package_data['sas'] = ['logging.ini']
386package_data['sas.sasview'] = ['images/*',
387                               'media/*',
388                               'test/*.txt',
389                               'test/1d_data/*',
390                               'test/2d_data/*',
391                               'test/convertible_files/*',
392                               'test/coordinate_data/*',
393                               'test/image_data/*',
394                               'test/media/*',
395                               'test/other_files/*',
396                               'test/save_states/*',
397                               'test/sesans_data/*',
398                               'test/upcoming_formats/*',
399                               ]
400packages.append("sas.sasview")
401
402required = [
403    'bumps>=0.7.5.9', 'periodictable>=1.5.0', 'pyparsing>=2.0.0',
404
405    # 'lxml>=2.2.2',
406    'lxml', 'h5py',
407
408    # The following dependecies won't install automatically, so assume them
409    # The numbers should be bumped up for matplotlib and wxPython as well.
410    # 'numpy>=1.4.1', 'scipy>=0.7.2', 'matplotlib>=0.99.1.1',
411    # 'wxPython>=2.8.11', 'pil',
412]
413
414if os.name == 'nt':
415    required.extend(['html5lib', 'reportlab'])
416else:
417    # 'pil' is now called 'pillow'
418    required.extend(['pillow'])
419
420# Set up SasView
421setup(
422    name="sasview",
423    version=VERSION,
424    description="SasView application",
425    author="SasView Team",
426    author_email="developers@sasview.org",
427    url="http://sasview.org",
428    license="PSF",
429    keywords="small-angle x-ray and neutron scattering analysis",
430    download_url="https://github.com/SasView/sasview.git",
431    package_dir=package_dir,
432    packages=packages,
433    package_data=package_data,
434    ext_modules=ext_modules,
435    install_requires=required,
436    zip_safe=False,
437    entry_points={
438        'console_scripts': [
439            "sasview = sas.sasview.sasview:run_gui",
440        ]
441    },
442    cmdclass={'build_ext': build_ext_subclass,
443              'docs': BuildSphinxCommand,
444              'disable_openmp': DisableOpenMPCommand},
445    setup_requires=['pytest-runner'] if 'pytest' in sys.argv else [],
446    tests_require=['pytest'],
447)
Note: See TracBrowser for help on using the repository browser.