/astropy/_erfa/setup_package.py

https://github.com/crawfordsm/astropy
Python | 117 lines | 75 code | 31 blank | 11 comment | 14 complexity | fe5610ad30b7159950024a8e12827df0 MD5 | raw file
  1. # Licensed under a 3-clause BSD style license - see LICENSE.rst
  2. from __future__ import absolute_import
  3. import os
  4. import glob
  5. from distutils import log
  6. from distutils.extension import Extension
  7. from astropy_helpers import setup_helpers
  8. from astropy_helpers.version_helpers import get_pkg_version_module
  9. ERFAPKGDIR = os.path.relpath(os.path.dirname(__file__))
  10. ERFA_SRC = os.path.abspath(os.path.join(ERFAPKGDIR, '..', '..', 'cextern', 'erfa'))
  11. SRC_FILES = glob.glob(os.path.join(ERFA_SRC, '*'))
  12. SRC_FILES += [os.path.join(ERFAPKGDIR, filename)
  13. for filename in ['core.py.templ', 'core.c.templ', 'erfa_generator.py']]
  14. GEN_FILES = [os.path.join(ERFAPKGDIR, 'core.py'), os.path.join(ERFAPKGDIR, 'core.c')]
  15. def pre_build_py_hook(cmd_obj):
  16. preprocess_source()
  17. def pre_build_ext_hook(cmd_obj):
  18. preprocess_source()
  19. def pre_sdist_hook(cmd_obj):
  20. preprocess_source()
  21. def preprocess_source():
  22. # Generating the ERFA wrappers should only be done if needed. This also
  23. # ensures that it is not done for any release tarball since those will
  24. # include core.py and core.c.
  25. if all(os.path.exists(filename) for filename in GEN_FILES):
  26. # Determine modification times
  27. erfa_mtime = max(os.path.getmtime(filename) for filename in SRC_FILES)
  28. gen_mtime = min(os.path.getmtime(filename) for filename in GEN_FILES)
  29. version = get_pkg_version_module('astropy')
  30. if gen_mtime > erfa_mtime:
  31. # If generated source is recent enough, don't update
  32. return
  33. elif version.release:
  34. # or, if we're on a release, issue a warning, but go ahead and use
  35. # the wrappers anyway
  36. log.warn('WARNING: The autogenerated wrappers in astropy._erfa '
  37. 'seem to be older than the source templates used to '
  38. 'create them. Because this is a release version we will '
  39. 'use them anyway, but this might be a sign of some sort '
  40. 'of version mismatch or other tampering. Or it might just '
  41. 'mean you moved some files around or otherwise '
  42. 'accidentally changed timestamps.')
  43. return
  44. # otherwise rebuild the autogenerated files
  45. # If jinja2 isn't present, then print a warning and use existing files
  46. try:
  47. import jinja2
  48. except:
  49. log.warn("WARNING: jinja2 could not be imported, so the existing "
  50. "ERFA core.py and core.c files will be used")
  51. return
  52. name = 'erfa_generator'
  53. filename = os.path.join(ERFAPKGDIR, 'erfa_generator.py')
  54. try:
  55. from importlib import machinery as import_machinery
  56. loader = import_machinery.SourceFileLoader(name, filename)
  57. gen = loader.load_module()
  58. except ImportError:
  59. import imp
  60. gen = imp.load_source(name, filename)
  61. gen.main(gen.DEFAULT_ERFA_LOC,
  62. os.path.join(ERFAPKGDIR, 'core.py'),
  63. gen.DEFAULT_TEMPLATE_LOC,
  64. verbose=False)
  65. def get_extensions():
  66. sources = [os.path.join(ERFAPKGDIR, "core.c")]
  67. include_dirs = ['numpy']
  68. libraries = []
  69. if setup_helpers.use_system_library('erfa'):
  70. libraries.append('erfa')
  71. else:
  72. # get all of the .c files in the cextern/erfa directory
  73. erfafns = os.listdir(ERFA_SRC)
  74. sources.extend(['cextern/erfa/'+fn for fn in erfafns if fn.endswith('.c')])
  75. include_dirs.append('cextern/erfa')
  76. erfa_ext = Extension(
  77. name="astropy._erfa._core",
  78. sources=sources,
  79. include_dirs=include_dirs,
  80. libraries=libraries,
  81. language="c",)
  82. return [erfa_ext]
  83. def get_external_libraries():
  84. return ['erfa']
  85. def requires_2to3():
  86. return False