forked from MDAnalysis/mdadash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
51 lines (42 loc) · 1.58 KB
/
Copy pathsetup.py
File metadata and controls
51 lines (42 loc) · 1.58 KB
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
import shutil
import subprocess
import sys
from pathlib import Path
from setuptools import setup
from setuptools.command.build_py import build_py
from setuptools.command.sdist import sdist
def _build_frontend():
frontend_dir = Path(__file__).parent / "mdadash" / "frontend"
npm_cmd = "npm.cmd" if sys.platform == "win32" else "npm"
subprocess.run([npm_cmd, "ci"], cwd=frontend_dir, check=True)
subprocess.run([npm_cmd, "run", "build"], cwd=frontend_dir, check=True)
class _sdist(sdist):
def run(self):
root_dir = Path(__file__).parent
node_modules = root_dir / "mdadash" / "frontend" / "node_modules"
# Move the node_modules temporarily if it exists to prevent it from
# becoming part of the final source distribution
tmp_node_modules = root_dir / ".tmp.frontend.node_modules"
node_modules_exists = node_modules.exists()
if node_modules_exists:
shutil.move(str(node_modules), str(tmp_node_modules))
try:
_build_frontend()
super().run()
finally:
# Restore any existing node_modules moved earlier
if node_modules_exists and tmp_node_modules.exists():
shutil.move(str(tmp_node_modules), str(node_modules))
class _build_py(build_py):
def run(self):
dist = Path(__file__).parent / "mdadash" / "frontend" / "dist"
if not dist.exists():
# Don't build frontend again after sdist
_build_frontend()
super().run()
setup(
cmdclass={
"sdist": _sdist,
"build_py": _build_py,
},
)