-
Notifications
You must be signed in to change notification settings - Fork 1
/
build_tasks.py
101 lines (82 loc) · 3.32 KB
/
build_tasks.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
# Copyright 2021. Corning Incorporated. All rights reserved.
#
# This software may only be used in accordance with the identified license(s).
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# CORNING BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OF THE SOFTWARE.
# ==========================================================================
import logging
import os
import shutil
import subprocess
import sys
import warnings
from distutils.core import Command
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
NODE_ROOT = os.path.join(BASE_DIR, "citamjs")
NODE_OUTPUT_DIR = os.path.join(NODE_ROOT, "dist")
CITAM_STATIC_DIR = os.path.join(BASE_DIR, "citam", "api", "static", "dash")
LOG = logging.getLogger(__name__)
class NodeJSBuild(Command):
"""distutils/setuptools command for building NodeJS modules
example::
# setup.py
import setuptools
from build_tasks import NodeJSBuild
setuptools.setup(
# ...
cmdclass={'build_js': NodeJSBuild}
)
"""
user_options = []
def initialize_options(self):
"""There are currently no options for this command"""
def finalize_options(self):
"""There are currently no options for this command"""
def run(self):
"""Perform a nodejs build for the given path"""
env = os.environ.copy()
# shell=True is required for subprocess in windows
shell = True if sys.platform == "win32" else False
try:
subprocess.check_call(["npm", "-v"], env=env, shell=shell)
except (subprocess.SubprocessError, FileNotFoundError):
warnings.warn(
"Unable to detect NodeJS installation. Skipping "
"NodeJS build step"
)
return
try:
LOG.info("Building NodeJS artifacts")
original_dir = os.getcwd()
os.chdir(NODE_ROOT)
# NODE_ENV=development is set to install dev_dependencies
env["NODE_ENV"] = "development"
subprocess.run(
["npm", "install", "--progress=false"],
check=True,
env=env,
shell=shell,
)
# NODE_ENV=production triggers build-time optimizations
env["NODE_ENV"] = "production"
subprocess.run(
["npm", "run", "build"], check=True, env=env, shell=shell
)
# TODO: Make JS compile directly to CITAM_STATIC_DIR
LOG.info("Cleaning previously generated NodeJS artifacts")
if os.path.exists(CITAM_STATIC_DIR):
shutil.rmtree(CITAM_STATIC_DIR, ignore_errors=True)
LOG.info("Copying NodeJS artifacts into package")
shutil.copytree(NODE_OUTPUT_DIR, CITAM_STATIC_DIR)
os.chdir(original_dir)
except (subprocess.SubprocessError, FileNotFoundError):
warnings.warn(
"NodeJS build Failed. "
"The CITAM dashboard will not be available"
)
return