-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_ci.py
105 lines (87 loc) · 3 KB
/
setup_ci.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
import json
import os
import subprocess
import sys
import re
from distutils.core import Extension
from pathlib import Path
import setuptools
from setuptools.command.build_ext import build_ext
"""
NOTE: This project uses more than one version of a 'setup.py' file:
* 'setup.py', and
* 'setup_ci.py'
Based on:
https://github.com/popatam/gopy_build_wheel_example/blob/main/setup_ci.py
"""
def normalize(name): # https://peps.python.org/pep-0503/#normalized-names
return re.sub(r"[-_.]+", "-", name).lower()
PACKAGE_PATH = "gohpygossh"
PACKAGE_NAME = "ohpygossh"
if sys.platform == "darwin":
# PYTHON_BINARY_PATH is setting explicitly for 310 and 311, see build_wheel.yml
# on macos PYTHON_BINARY_PATH must be python bin installed from python.org or from brew
PYTHON_BINARY = os.getenv("PYTHON_BINARY_PATH", sys.executable)
if PYTHON_BINARY == sys.executable:
subprocess.check_call([sys.executable, "-m", "pip", "install", "pybindgen"])
else:
# linux & windows
PYTHON_BINARY = sys.executable
subprocess.check_call([sys.executable, "-m", "pip", "install", "pybindgen"])
def _generate_path_with_gopath() -> str:
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(
[
"gopy",
"build",
"-no-make",
"-dynamic-link=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_PATH} import *")
this_directory = Path(__file__).parent
long_description = (this_directory / "README.md").read_text()
setuptools.setup(
name=PACKAGE_NAME,
version="0.0.10",
author="b-long",
description="A project to create a new cross-platform SSH wheel for Python.",
long_description_content_type="text/markdown",
long_description=long_description,
url="https://github.com/b-long/ohpygossh",
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],
)
],
)