from setuptools.command.build_py import build_py
ENCODING = 'utf-8' # locale.getpreferredencoding()
COMMIT_HASH = subprocess.check_output(['git', 'describe', '--always']).strip().decode()
GIT_BRANCH = subprocess.check_output(['git', 'symbolic-ref', '--short', 'HEAD'])
except subprocess.CalledProcessError:
"""Yield items from any nested iterable"""
if isinstance(x, collections.Iterable) and not isinstance(x, (str, bytes)):
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."""
def finalize_options(self):
# get the path to this file
root_dir = os.path.dirname(os.path.realpath(__file__))
build_py_cmd = self.get_finalized_command('build_py')
# get the path to the build directory
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)
'pipeline_common.min.js': [
'jquery-3.3.1.js', # dependency of pipeline.js and its plugins,
'holder.js', # placeholders for missing images
'lazyload.js', # only load images when they become visible
'jquery.fancybox.js', # fancybox
'plotcmd.js', # pipeline plugin for fancybox
'purl.js', # URL inspection
'bootstrap.js', # bootstrap
'pipeline.js', # pipeline
'pipeline_plots.min.js': [
'select2.js', # filters on detail plots pages
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)
def minify(self, inputs, output):
self.announce('Creating minified JS: {}'.format(os.path.basename(output)),
level=distutils.log.INFO)
'-jar', 'closure-compiler.jar',
'--js_output_file', output,
'--language_in', 'ECMASCRIPT6',
'--language_out', 'ECMASCRIPT6',
'--generate_exports', 'false',
'--warning_level', 'verbose',
'--compilation_level', 'BUNDLE']
input_files = [['--js', i] for i in inputs]
input_args = flatten([command, input_files])
subprocess.check_output([str(i) for i in input_args], cwd=self.root_dir)
class MinifyCSSCommand(distutils.cmd.Command):
description = 'Minify the pipeline CSS'
user_options = [('inplace', 'i', 'Generate minified CSS 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."""
def finalize_options(self):
# get the path to this file
root_dir = os.path.dirname(os.path.realpath(__file__))
build_py_cmd = self.get_finalized_command('build_py')
# get the path to the build directory
build_path = os.path.join(root_dir, build_py_cmd.build_lib)
# get the path to the source CSS directory
css_path = build_py_cmd.get_package_dir('pipeline.infrastructure.renderer.templates.resources.css')
self.css_input_dir = os.path.join(root_dir, css_path)
self.css_output_dir = os.path.join(build_path, css_path)
if not os.path.exists(self.css_output_dir):
os.mkdir(self.css_output_dir)
shutil.copymode(self.css_input_dir, self.css_output_dir)
for output_file, input_files in to_minify.items():
output_path = os.path.join(self.css_output_dir, output_file)
input_paths = [os.path.join(self.css_input_dir, f) for f in input_files]
self.minify(input_paths, output_path)
def minify(self, inputs, output_path):
self.announce('Creating minified CSS: {}'.format(os.path.basename(output_path)),
level=distutils.log.INFO)
with open(name, 'r', encoding=ENCODING) as f:
buffer = '\n\n'.join(buffer)
output = csscompressor.compress(buffer)
with open(output_path, 'w', encoding=ENCODING) as f:
class BuildMyTasksCommand(distutils.cmd.Command):
description = 'Generate the CASA CLI bindings'
user_options = [('inplace', 'i', 'Generate CLI bindings 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."""
def finalize_options(self):
# get the path to this file
dir_path = os.path.dirname(os.path.realpath(__file__))
self.build_path = dir_path
# get the path to the build directory
build_py_cmd = self.get_finalized_command('build_py')
self.build_path = os.path.join(dir_path, build_py_cmd.build_lib)
for d in ['h', 'hif', 'hifa', 'hifv', 'hsd', 'hsdn']:
cli_dir = os.path.join('pipeline', d, 'cli')
cli_module = '.'.join(['pipeline', d, 'cli'])
src_dir = os.path.join(self.build_path, cli_dir)
cli_init_py = os.path.join(src_dir, '__init__.py')
if not os.path.exists(src_dir):
for xml_file in [f for f in os.listdir(src_dir) if f.endswith('.xml')]:
self.announce('Building task from XML: {}'.format(xml_file), level=distutils.log.INFO)
subprocess.check_output([
root, _ = os.path.splitext(xml_file)
import_statement = 'from .{} import {}'.format(root, root)
with open(cli_init_py, 'r+', encoding=ENCODING) as init_file:
import_exists = any(import_statement in line for line in init_file)
init_file.seek(0, os.SEEK_END)
init_file.write('{}\n'.format(import_statement))
gotasks_dir = os.path.join(src_dir, 'gotasks')
gotasks_init_py = os.path.join(gotasks_dir, '__init__.py')
import_statement = 'from .{} import {}'.format(root, root)
with open(gotasks_init_py, 'a+', encoding=ENCODING) as init_file:
import_exists = any(import_statement in line for line in init_file)
init_file.seek(0, os.SEEK_END)
init_file.write('{}\n'.format(import_statement))
class PipelineBuildPyCommand(build_py):
self.run_command('minify_css')
self.run_command('minify_js')
self.run_command('buildmytasks')
packages = setuptools.find_packages()
'pipeline.infrastructure.renderer.templates.resources.css',
'pipeline.infrastructure.renderer.templates.resources.fonts',
'pipeline.infrastructure.renderer.templates.resources.img',
'pipeline.infrastructure.renderer.templates.resources.js'
description='CASA pipeline',
'buildmytasks': BuildMyTasksCommand,
'build_py': PipelineBuildPyCommand,
'minify_js': MinifyJSCommand,
'minify_css': MinifyCSSCommand,
# 'cachetools', # memo decorator to cache expensive calls
# 'intervaltree', # callibrary
# 'pyparsing', # parse user input using CASA syntax
'csscompressor' # minify CSS
options=dict(egg_info=dict(tag_build='{}-{}'.format(GIT_BRANCH, COMMIT_HASH))),
package_data={'': ['*.egg',
from setuptools import setup, find_packages
description="Pipeline package",
casaarch = os.getenv('CASAARCH')
pyversion = os.getenv('PYVERSION')
casasrc = os.getenv('CASASRC')
for i in ['h', 'hif', 'hsd', 'hifa', 'hifv', 'hsdn']:
srcdir = casasrc + '/' + i + '/cli'
destdir = casaarch + '/pipeline/' + i + '/cli'
command = '(cd '+casasrc+'/'+i+'/cli; buildmytasks *xml)'
command = '(cd '+casasrc+'/'+i+'/cli; buildmytasks --output-dir '+casaarch+'/pipeline/'+i+'/cli *xml)'
if (casasrc+'/extern') != (casaarch+'/pipeline/extern'):
command = 'cp '+casasrc+'/extern/Mako-0.7.0-py' + pyversion + '.egg '+casaarch+'/pipeline/extern'