2015-08-06 15:28:29 +00:00
|
|
|
# Copyright 2015 The go-python Authors. All rights reserved.
|
|
|
|
# Use of this source code is governed by a BSD-style
|
|
|
|
# license that can be found in the LICENSE file.
|
|
|
|
|
2015-08-07 11:11:23 +00:00
|
|
|
### py2/py3 compat
|
|
|
|
from __future__ import print_function
|
|
|
|
|
2015-08-06 15:28:29 +00:00
|
|
|
__doc__ = """gopy is a convenience module to wrap and bind a Go package"""
|
|
|
|
__author__ = "The go-python authors"
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
"load",
|
|
|
|
]
|
|
|
|
|
|
|
|
### stdlib imports ---
|
|
|
|
import imp
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
def load(pkg, output=""):
|
|
|
|
"""
|
|
|
|
`load` takes a fully qualified Go package name and runs `gopy bind` on it.
|
|
|
|
@returns the C-extension module object
|
|
|
|
"""
|
2015-08-07 11:11:23 +00:00
|
|
|
|
|
|
|
from subprocess import check_call, check_output
|
2015-08-06 15:28:29 +00:00
|
|
|
if output == "":
|
|
|
|
output = os.getcwd()
|
|
|
|
pass
|
|
|
|
|
2015-08-07 11:11:23 +00:00
|
|
|
print("gopy> inferring package name...")
|
|
|
|
pkg = check_output(["go", "list", pkg]).strip()
|
2015-08-12 09:44:27 +00:00
|
|
|
if pkg in sys.modules:
|
|
|
|
print("gopy> package '%s' already wrapped and loaded!" % (pkg,))
|
|
|
|
print("gopy> NOT recompiling it again (see issue #27)")
|
|
|
|
return sys.modules[pkg]
|
2015-08-07 11:11:23 +00:00
|
|
|
print("gopy> loading '%s'..." % pkg)
|
|
|
|
|
2015-08-06 15:28:29 +00:00
|
|
|
check_call(["gopy","bind", "-output=%s" % output, pkg])
|
|
|
|
|
|
|
|
n = os.path.basename(pkg)
|
2015-08-07 11:11:23 +00:00
|
|
|
print("gopy> importing '%s'" % (pkg,))
|
2015-08-06 15:28:29 +00:00
|
|
|
|
|
|
|
ok = imp.find_module(n, [output])
|
|
|
|
if not ok:
|
2015-08-07 11:11:23 +00:00
|
|
|
raise RuntimeError("could not find module '%s'" % pkg)
|
2015-08-06 15:28:29 +00:00
|
|
|
fname, path, descr = ok
|
2015-08-07 11:11:23 +00:00
|
|
|
mod = imp.load_module('__gopy__.'+n, fname, path, descr)
|
|
|
|
mod.__name__ = pkg
|
|
|
|
sys.modules[pkg] = mod
|
|
|
|
del sys.modules['__gopy__.'+n]
|
|
|
|
return mod
|
2015-08-06 15:28:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|