-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
setup.py
180 lines (149 loc) · 5.97 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
168
169
170
171
172
173
174
175
176
177
178
179
180
from __future__ import annotations, print_function
import shutil
import subprocess
import sys
import traceback
from logging import getLogger
from pathlib import Path
from setuptools import find_namespace_packages, setup
from setuptools.command.develop import develop
from setuptools.command.sdist import sdist
log = getLogger(__name__)
# -----------------------------------------------------------------------------
# Basic Constants
# -----------------------------------------------------------------------------
name = "reactpy_django"
root_dir = Path(__file__).parent
src_dir = root_dir / "src"
js_dir = src_dir / "js"
package_dir = src_dir / name
static_dir = package_dir / "static" / name
# -----------------------------------------------------------------------------
# Package Definition
# -----------------------------------------------------------------------------
package = {
"name": name,
"python_requires": ">=3.9",
"packages": find_namespace_packages(src_dir),
"package_dir": {"": "src"},
"description": "It's React, but in Python. Now with Django integration.",
"author": "Mark Bakhit",
"author_email": "[email protected]",
"url": "https://github.com/reactive-python/reactpy-django",
"license": "MIT",
"platforms": "Linux, Mac OS X, Windows",
"keywords": [
"interactive",
"reactive",
"widgets",
"DOM",
"React",
"ReactJS",
"ReactPy",
],
"include_package_data": True,
"zip_safe": False,
"classifiers": [
"Framework :: Django",
"Framework :: Django :: 4.0",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Operating System :: OS Independent",
"Intended Audience :: Developers",
"Intended Audience :: Science/Research",
"Topic :: Multimedia :: Graphics",
"Environment :: Web Environment",
],
}
# -----------------------------------------------------------------------------
# Library Version
# -----------------------------------------------------------------------------
for line in (package_dir / "__init__.py").read_text().split("\n"):
if line.startswith("__version__ = "):
package["version"] = eval(line.split("=", 1)[1])
break
else:
print(f"No version found in {package_dir}/__init__.py")
sys.exit(1)
# -----------------------------------------------------------------------------
# Requirements
# -----------------------------------------------------------------------------
requirements: list[str] = []
with (root_dir / "requirements" / "pkg-deps.txt").open() as f:
requirements.extend(line for line in map(str.strip, f) if not line.startswith("#"))
package["install_requires"] = requirements
# -----------------------------------------------------------------------------
# Library Description
# -----------------------------------------------------------------------------
with (root_dir / "README.md").open() as f:
long_description = f.read()
package["long_description"] = long_description
package["long_description_content_type"] = "text/markdown"
# ----------------------------------------------------------------------------
# Build Javascript
# ----------------------------------------------------------------------------
def copy_js_files(source_dir: Path, destination: Path) -> None:
if destination.exists():
shutil.rmtree(destination)
destination.mkdir()
for file in source_dir.iterdir():
if file.is_file():
shutil.copy(file, destination / file.name)
else:
copy_js_files(file, destination / file.name)
def build_javascript_first(build_cls: type):
class Command(build_cls):
def run(self):
log.info("Installing Javascript...")
result = subprocess.run(
["bun", "install"], cwd=str(js_dir), check=True
).returncode
if result != 0:
log.error(traceback.format_exc())
log.error("Failed to install Javascript")
raise RuntimeError("Failed to install Javascript")
log.info("Building Javascript...")
result = subprocess.run(
[
"bun",
"build",
"./src/index.tsx",
"--outfile",
str(static_dir / "client.js"),
"--minify",
],
cwd=str(js_dir),
check=True,
).returncode
if result != 0:
log.error(traceback.format_exc())
log.error("Failed to build Javascript")
raise RuntimeError("Failed to build Javascript")
log.info("Copying @pyscript/core distribution")
pyscript_dist = js_dir / "node_modules" / "@pyscript" / "core" / "dist"
pyscript_static_dir = static_dir / "pyscript"
copy_js_files(pyscript_dist, pyscript_static_dir)
log.info("Copying Morphdom distribution")
morphdom_dist = js_dir / "node_modules" / "morphdom" / "dist"
morphdom_static_dir = static_dir / "morphdom"
copy_js_files(morphdom_dist, morphdom_static_dir)
log.info("Successfully built Javascript")
super().run()
return Command
package["cmdclass"] = {
"sdist": build_javascript_first(sdist),
"develop": build_javascript_first(develop),
}
if sys.version_info < (3, 10, 6):
from distutils.command.build import build
package["cmdclass"]["build"] = build_javascript_first(build)
else:
from setuptools.command.build_py import build_py
package["cmdclass"]["build_py"] = build_javascript_first(build_py)
# -----------------------------------------------------------------------------
# Installation
# -----------------------------------------------------------------------------
if __name__ == "__main__":
setup(**package)