Commits

Aleš Erjavec  committed 310b300

Added python c extension and setup.py script.

  • Participants
  • Parent commits ddf8f60

Comments (0)

Files changed (6)

+recursive-include orangekit/earth *.svg
+recursive-include src *.*

File README.txt

Empty file added.

File orangekit/__init__.py

+"""
+Namespace stub
+"""
+import pkg_resources
+pkg_resources.declare_namespace(__name__)

File orangekit/earth/__init__.py

Empty file added.
+#!/usr/bin/env python
+
+import os
+import glob
+from collections import namedtuple
+
+try:
+    from setuptools import setup
+except ImportError:
+    import distutils_setup
+    distutils_setup.use_setuptools()
+    from setuptools import setup
+
+from setuptools import find_packages
+
+from numpy.distutils.misc_util import Configuration
+
+from ConfigParser import SafeConfigParser
+
+
+NAME = "orangekit-earth"
+
+DESCRIPTION = "A Python interface for the earth package by Stephen Milborrow"
+
+LONG_DESCRIPTION = open("README.txt", "rb").read()
+
+PACKAGES = find_packages()
+
+
+lib_config = namedtuple("lib_config", ["libs", "linkflags", "library_dirs"])
+earth_config = namedtuple("earth_config", ["blas", "R"])
+
+
+def libs_parse(text):
+    return map(str.strip, text.split(","))
+
+
+def library_dirs_parse(text):
+    return text.strip()
+
+
+def parse_lib_opt(parser, section):
+    libs, library_dirs = None, None
+    if parser.has_option(section, "libs"):
+        libs = libs_parse(parser.get_option(section, "libs"))
+    if parser.has_option(section, "library_dirs"):
+        library_dirs = \
+            library_dirs_parse(parser.get_option(section, "library_dirs"))
+
+    if libs or library_dirs:
+        return lib_config(libs, [], library_dirs)
+    else:
+        return None
+
+
+def site_config():
+    blas = R = None
+    parser = SafeConfigParser()
+    parser.read(["setup-site.cfg",
+                 os.path.expanduser("~/.orangekit-earth.cfg")])
+
+    if parser.has_section("blas_opt"):
+        blas = parse_lib_opt(parser, "blas_opt")
+
+    if parser.has_section("R_opt"):
+        R = parse_lib_opt(parser, "R_opt")
+
+    return earth_config(blas, R)
+
+
+def sources(pattern):
+    return glob.glob(pattern)
+
+
+def configuration(parent_package="", top_path=None):
+    config = Configuration("earth", parent_package, top_path)
+    link_flags = []
+    libraries = []
+    library_dirs = []
+
+    site = site_config()
+    if site.blas:
+        # Link to provided blas library
+        libraries += site.blas.libs
+        link_flags += site.blas.linkflags
+        library_dirs += site.blas.library_dirs
+    else:
+        # Compile and link the included blas subset
+        config.add_library("blas", sources=sources("src/blas/*.c"))
+
+    if site.R:
+        # Link to the provided R library
+        libraries += site.R.libs
+        link_flags += site.R.linkflags
+        library_dirs += site.R.library_dirs
+    else:
+        config.add_library("R", sources=sources("src/rlib/*.c"))
+
+    config.add_extension(
+        "_earth",
+        sources=["src/_earth.c"],
+        define_macros=[("STANDALONE", 1),
+                       ("USE_BLAS", 1),
+                       ("MAIN", 1)],
+        libraries=libraries + ["m"],
+        extra_compile_args=["-std=c99"],
+    )
+
+    return config
+
+
+def setup_package():
+    setup(**configuration("orangekit").todict())
+
+if __name__ == "__main__":
+    setup_package()

File src/_earth.c

+#include <stdbool.h>
+
+#ifndef _MSC_VER
+	#define _finite isfinite
+	#define _isnan	isnan
+#endif
+
+#include "earth/earth.c"
+
+#include "Python.h"
+
+void init_earth(void) {
+	PyObject * mod;
+	mod = Py_InitModule("_earth", NULL);
+	if (mod == NULL) {
+		return;
+	}
+}