-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsetup.py
167 lines (136 loc) · 4.55 KB
/
setup.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"""
(c) Andriy Babak 2021
date: 28/05/2021
modified: 24/07/2024 10:15:50
Author: Andriy Babak
e-mail: [email protected]
------------------------------
description: CG C++ Support module
------------------------------
"""
from __future__ import print_function
import os
import re
import sys
import shutil
import subprocess
from setuptools.command.build_ext import build_ext
from setuptools.command.bdist_egg import bdist_egg as BuildEggCommand
import setuptools
# Define paths
PLUGIN_NAME = "cgcpp-{0}"
ROOT_PATH = os.path.dirname(os.path.realpath(__file__))
SOURCE_PATH = os.path.join(ROOT_PATH, "source")
README_PATH = os.path.join(ROOT_PATH, "README.md")
BUILD_PATH = os.path.join(ROOT_PATH, "build")
STAGING_PATH = os.path.join(BUILD_PATH, PLUGIN_NAME)
with open(os.path.join(SOURCE_PATH, "cgcpp", "_version.py")) as _version_file:
VERSION = re.match(
r".*__version__ = [\'\"](.*?)[\'\"]", _version_file.read(), re.DOTALL
).group(1)
STAGING_PATH = STAGING_PATH.format(VERSION)
class CMakeExtension(setuptools.Extension):
def __init__(self, name, sourcedir=""):
setuptools.Extension.__init__(self, name, sources=[])
self.sourcedir = os.path.abspath(sourcedir)
class CMakeBuild(build_ext):
"""
Build CMake using docker image
"""
DOCKER_APP = "docker"
DOCKER_IMAGE = "ababak/cgcpp:" + ".".join(VERSION.split(".")[:2])
def run(self):
try:
out = subprocess.check_output([self.DOCKER_APP, "--version"])
except OSError:
raise RuntimeError(
"Docker must be installed to build the following extensions: {extensions}".format(
extensions=", ".join(e.name for e in self.extensions)
)
)
out = subprocess.check_output(
[self.DOCKER_APP, "images", "-q", self.DOCKER_IMAGE]
)
if not out:
print('Building docker image "{}"...'.format(self.DOCKER_IMAGE))
docker_args = [
self.DOCKER_APP,
"build",
# "--rm",
"-t",
self.DOCKER_IMAGE,
".",
]
subprocess.check_call(docker_args)
for ext in self.extensions:
self.build_extension(ext)
def build_extension(self, ext):
extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
if not os.path.exists(extdir):
os.makedirs(extdir)
sourcedir = ext.sourcedir
print(
'Building extension "{}" using "{}"...'.format(ext.name, self.DOCKER_IMAGE)
)
docker_args = [
self.DOCKER_APP,
"run",
"--rm",
"-v",
"{}:c:/source:ro".format(sourcedir),
"-v",
"{}:c:/out".format(os.path.abspath(extdir)),
self.DOCKER_IMAGE,
]
subprocess.check_call(docker_args)
# Custom commands.
class BuildPlugin(setuptools.Command):
"""Build plugin."""
description = "Build plugin and create an archive"
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
"""Run the build step."""
# Clean staging path
shutil.rmtree(STAGING_PATH, ignore_errors=True)
subprocess.check_call(
[sys.executable, "-m", "pip", "install", ".", "--target", STAGING_PATH]
)
# Generate plugin zip
shutil.make_archive(
os.path.join(BUILD_PATH, PLUGIN_NAME.format(VERSION)), "zip", STAGING_PATH
)
class BuildEgg(BuildEggCommand):
"""Custom egg build to ensure resources built.
.. note::
Required because when this project is a dependency for another project,
only bdist_egg will be called and *not* build.
"""
def run(self):
"""Run egg build ensuring build_resources called first."""
self.run_command("build_ext")
BuildEggCommand.run(self)
# Configuration.
setuptools.setup(
name="cgcpp",
version=VERSION,
description="CG C++ Support module.",
long_description=open(README_PATH).read(),
keywords="",
url="https://github.com/ababak/cgcpp",
author="Andriy Babak",
author_email="[email protected]",
license="Apache License (2.0)",
packages=setuptools.find_packages(SOURCE_PATH),
package_dir={"": "source"},
ext_modules=[CMakeExtension("cgcpp/lib_loader", "source_lib_loader")],
cmdclass={
"build_ext": CMakeBuild,
"bdist_egg": BuildEgg,
"build_plugin": BuildPlugin,
},
zip_safe=False,
)