|
| 1 | +""" |
| 2 | +This module contains a number of utilities for use during |
| 3 | +setup/build/packaging that are useful to astropy as a whole. |
| 4 | +""" |
| 5 | + |
| 6 | +import os.path |
| 7 | +import sys |
| 8 | + |
| 9 | + |
| 10 | +def write_if_different(filename, data): |
| 11 | + """ |
| 12 | + Write *data* to *filename*, if the content of the file is |
| 13 | + different. |
| 14 | + """ |
| 15 | + if os.path.exists(filename): |
| 16 | + with open(filename, 'rb') as fd: |
| 17 | + original_data = fd.read() |
| 18 | + else: |
| 19 | + original_data = None |
| 20 | + |
| 21 | + if original_data != data: |
| 22 | + with open(filename, 'wb') as fd: |
| 23 | + fd.write(data) |
| 24 | + |
| 25 | + |
| 26 | +def check_numpy(): |
| 27 | + """ |
| 28 | + Check that Numpy is installed and it is of the minimum version we |
| 29 | + require. |
| 30 | + """ |
| 31 | + import numpy |
| 32 | + |
| 33 | + major, minor, rest = numpy.__version__.split(".", 2) |
| 34 | + if (int(major), int(minor)) < (1, 3): |
| 35 | + raise ImportError("numpy version 1.3 or later must be installed to build astropy") |
| 36 | + |
| 37 | + |
| 38 | +def get_numpy_include_path(): |
| 39 | + """ |
| 40 | + Gets the path to the numpy headers |
| 41 | + """ |
| 42 | + import numpy |
| 43 | + |
| 44 | + try: |
| 45 | + numpy_include = numpy.get_include() |
| 46 | + except AttributeError: |
| 47 | + numpy_include = numpy.get_numpy_include() |
| 48 | + return numpy_include |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +################################################################################ |
| 53 | +# Backport of importlib.import_module from 3.x. This backport was provided by |
| 54 | +# Brett Cannon and downloaded from here: |
| 55 | +# http://pypi.python.org/pypi/importlib/1.0.1 |
| 56 | +# While not critical (and in no way guaranteed!), it would be nice to keep this |
| 57 | +# code compatible with Python 2.3. |
| 58 | + |
| 59 | +try: |
| 60 | + from importlib import import_module |
| 61 | +except ImportError: |
| 62 | + |
| 63 | + def _resolve_name(name, package, level): |
| 64 | + """Return the absolute name of the module to be imported.""" |
| 65 | + if not hasattr(package, 'rindex'): |
| 66 | + raise ValueError("'package' not set to a string") |
| 67 | + dot = len(package) |
| 68 | + for x in xrange(level, 1, -1): |
| 69 | + try: |
| 70 | + dot = package.rindex('.', 0, dot) |
| 71 | + except ValueError: |
| 72 | + raise ValueError("attempted relative import beyond top-level " |
| 73 | + "package") |
| 74 | + return "%s.%s" % (package[:dot], name) |
| 75 | + |
| 76 | + |
| 77 | + def import_module(name, package=None): |
| 78 | + """Import a module. |
| 79 | +
|
| 80 | + The 'package' argument is required when performing a relative |
| 81 | + import. It specifies the package to use as the anchor point |
| 82 | + from which to resolve the relative import to an absolute |
| 83 | + import. |
| 84 | + """ |
| 85 | + if name.startswith('.'): |
| 86 | + if not package: |
| 87 | + raise TypeError( |
| 88 | + "relative imports require the 'package' argument") |
| 89 | + level = 0 |
| 90 | + for character in name: |
| 91 | + if character != '.': |
| 92 | + break |
| 93 | + level += 1 |
| 94 | + name = _resolve_name(name[level:], package, level) |
| 95 | + __import__(name) |
| 96 | + return sys.modules[name] |
0 commit comments