import collections
import distutils.cmd
import distutils.log
import os
import shutil
import subprocess
import sys
import setuptools
from setuptools.command.build_py import build_py
ENCODING = 'utf-8'
PIPELINE_PACKAGES = ['h', 'hif', 'hifa', 'hifas', 'hifv', 'hsd', 'hsdn']
def flatten(items):
"""Yield items from any nested iterable"""
for x in items:
if isinstance(x, collections.Iterable) and not isinstance(x, (str, bytes)):
for sub_x in flatten(x):
yield sub_x
else:
yield x
def str_encode(s):
return bytes(s,sys.getdefaultencoding())
def str_decode(bs):
return bs.decode(sys.getdefaultencoding(),"strict")
def pipe_decode(output):
if isinstance(output,bytes) or isinstance(output,bytearray):
return str_decode(output)
elif isinstance(output,tuple):
return (str_decode(output[0]),str_decode(output[1]))
else:
return ("","")
class MinifyJSCommand(distutils.cmd.Command):
description = 'Minify the pipeline javascript'
user_options = [('inplace', 'i', 'Generate minified JS in src directory')]
boolean_options = ['inplace']
def __init__(self, dist):
distutils.cmd.Command.__init__(self, dist)
def initialize_options(self):
"""Set default values for options."""
self.inplace = None
def finalize_options(self):
root_dir = os.path.dirname(os.path.realpath(__file__))
build_py_cmd = self.get_finalized_command('build_py')
if self.inplace:
build_path = root_dir
else:
build_path = os.path.join(root_dir, build_py_cmd.build_lib)
js_path = build_py_cmd.get_package_dir('pipeline.infrastructure.renderer.templates.resources.js')
self.js_input_dir = os.path.join(root_dir, js_path)
self.js_output_dir = os.path.join(build_path, js_path)
self.root_dir = root_dir
def run(self):
to_minify = {
'pipeline_common.min.js': [
'jquery-3.3.1.js',
'holder.js',
'lazyload.js',
'jquery.fancybox.js',
'plotcmd.js',
'tcleancmd.js',
'purl.js',
'bootstrap.js',
'pipeline.js',
],
'pipeline_plots.min.js': [
'select2.js',
'd3.v3.js',
]
}
if not os.path.exists(self.js_output_dir):
os.mkdir(self.js_output_dir)
shutil.copymode(self.js_input_dir, self.js_output_dir)
for output_file, input_files in to_minify.items():
output_path = os.path.join(self.js_output_dir, output_file)
input_paths = [os.path.join(self.js_input_dir, f) for f in input_files]
self.minify(input_paths, output_path)