-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsetup_local.py
106 lines (87 loc) · 2.92 KB
/
setup_local.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import json
import os
import subprocess
import sys
import re
from distutils.core import Extension
import setuptools
from setuptools.command.build_ext import build_ext
def normalize(name): # https://peps.python.org/pep-0503/#normalized-names
return re.sub(r"[-_.]+", "-", name).lower()
PACKAGE_PATH = "event_ruler"
PACKAGE_NAME = PACKAGE_PATH.split("/")[-1]
# https://pip.pypa.io/en/latest/user_guide/#using-pip-from-your-program
subprocess.check_call([sys.executable, "-m", "pip", "install", "pybindgen"])
if sys.platform == "darwin":
PYTHON_BINARY = os.getenv("PYTHON_BINARY_PATH")
if sys.platform == "linux":
PYTHON_BINARY = sys.executable
def _generate_path_with_gopath():
go_path = subprocess.check_output(["go", "env", "GOPATH"]).decode("utf-8").strip()
path_val = f'{os.getenv("PATH")}:{go_path}/bin'
return path_val
class CustomBuildExt(build_ext):
def build_extension(self, ext: Extension):
bin_path = _generate_path_with_gopath()
go_env = json.loads(
subprocess.check_output(["go", "env", "-json"]).decode("utf-8").strip()
)
destination = (
os.path.dirname(os.path.abspath(self.get_ext_fullpath(ext.name)))
+ f"/{PACKAGE_NAME}"
)
subprocess.check_call(
["go", "install", "golang.org/x/tools/cmd/goimports@latest"],
env={"PATH": bin_path, **go_env},
)
subprocess.check_call(
["go", "install", "github.com/go-python/gopy@v0.4.5"],
env={"PATH": bin_path, **go_env},
)
subprocess.check_call(
[
"gopy",
"build",
"-dynamic-link=True",
"-rename=True",
"-output",
destination,
"-vm",
PYTHON_BINARY,
*ext.sources,
],
env={"PATH": bin_path, **go_env, "CGO_LDFLAGS_ALLOW": ".*"},
)
# dirty hack to avoid "from pkg import pkg", remove if needed
with open(f"{destination}/__init__.py", "w") as f:
f.write(f"from .{PACKAGE_NAME} import *")
with open("README.md") as f:
readme = f.read()
with open("LICENSE") as f:
license = f.read()
setuptools.setup(
name=normalize(PACKAGE_NAME),
version="0.3.0",
author="Tuan Anh Tran",
author_email="me@tuananh.org",
description="Test EventBridge pattern with Python locally",
long_description=readme,
long_description_content_type="text/markdown",
license=license,
url="https://github.com/tuananh/py-event-ruler",
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
include_package_data=True,
cmdclass={
"build_ext": CustomBuildExt,
},
ext_modules=[
Extension(
PACKAGE_NAME,
[PACKAGE_PATH],
)
],
)