<commit_before>##############################################################################
# Copyright (c) 2013-2017, Lawrence Livermore National Security, LLC.
# Produced at the Lawrence Livermore National Laboratory.
#
# This file is part of Spack.
# Created by Todd Gamblin, tgamblin@llnl.gov, All rights reserved.
# LLNL-CODE-647188
#
# For details, see https://github.com/llnl/spack
# Please also see the NOTICE and LICENSE files for our notice and the LGPL.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License (as
# published by the Free Software Foundation) version 2.1, February 1999.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and
# conditions of the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##############################################################################
from spack import *

class Paraview(CMakePackage):
    """ParaView is an open-source, multi-platform data analysis and
    visualization application."""

    homepage = 'http://www.paraview.org'
    url      = "http://www.paraview.org/files/v5.3/ParaView-v5.3.0.tar.gz"
    _urlfmt  = 'http://www.paraview.org/files/v{0}/ParaView-v{1}{2}.tar.gz'

    version('5.4.0', 'b92847605bac9036414b644f33cb7163')
    version('5.3.0', '68fbbbe733aa607ec13d1db1ab5eba71')
    version('5.2.0', '4570d1a2a183026adb65b73c7125b8b0')
    version('5.1.2', '44fb32fc8988fcdfbc216c9e40c3e925')
    version('5.0.1', 'fdf206113369746e2276b95b257d2c9b')
    version('4.4.0', 'fa1569857dd680ebb4d7ff89c2227378')

    variant('plugins', default=True,
            description='Install include files for plugins support')
    variant('python', default=False, description='Enable Python support')
    variant('mpi', default=True, description='Enable MPI support')
    variant('osmesa', default=False, description='Enable OSMesa support')
    variant('qt', default=False, description='Enable Qt (gui) support')
    variant('opengl2', default=True, description='Enable OpenGL2 backend')

    depends_on('python@2:2.8', when='+python')
    depends_on('py-numpy', when='+python', type='run')
    depends_on('py-matplotlib', when='+python', type='run')
    depends_on('mpi', when='+mpi')
    depends_on('qt', when='@5.3.0:+qt')
    depends_on('qt@:4', when='@:5.2.0+qt')

    depends_on('bzip2')
    depends_on('freetype')
    # depends_on('hdf5+mpi', when='+mpi')
    # depends_on('hdf5~mpi', when='~mpi')
    depends_on('jpeg')
    depends_on('libpng')
    depends_on('libtiff')
    depends_on('libxml2')
    # depends_on('netcdf')
    # depends_on('netcdf-cxx')
    # depends_on('protobuf') # version mismatches?
    # depends_on('sqlite') # external version not supported
    depends_on('zlib')
    depends_on('cmake@3.3:', type='build')

    patch('stl-reader-pv440.patch', when='@4.4.0')

    # Broken gcc-detection - improved in 5.1.0, redundant later
    patch('gcc-compiler-pv501.patch', when='@:5.0.1')

    # Broken installation (ui_pqExportStateWizard.h) - fixed in 5.2.0
    patch('ui_pqExportStateWizard.patch', when='@:5.1.2')

    def url_for_version(self, version):
        """Handle ParaView version-based custom URLs."""
        if version < Version('5.1.0'):
            return self._urlfmt.format(version.up_to(2), version, '-source')
        else:
            return self._urlfmt.format(version.up_to(2), version, '')

    def cmake_args(self):
        """Populate cmake arguments for ParaView."""
        spec = self.spec

        def variant_bool(feature, on='ON', off='OFF'):
            """Ternary for spec variant to ON/OFF string"""
            if feature in spec:
                return on
            return off

        def nvariant_bool(feature):
            """Negated ternary for spec variant to OFF/ON string"""
            return variant_bool(feature, on='OFF', off='ON')

        rendering = variant_bool('+opengl2', 'OpenGL2', 'OpenGL')
        includes  = variant_bool('+plugins')

        cmake_args = [
            '-DPARAVIEW_BUILD_QT_GUI:BOOL=%s' % variant_bool('+qt'),
            '-DVTK_OPENGL_HAS_OSMESA:BOOL=%s' % variant_bool('+osmesa'),
            '-DVTK_USE_X:BOOL=%s' % nvariant_bool('+osmesa'),
            '-DVTK_RENDERING_BACKEND:STRING=%s' % rendering,
            '-DPARAVIEW_INSTALL_DEVELOPMENT_FILES:BOOL=%s' % includes,
            '-DBUILD_TESTING:BOOL=OFF',
            '-DVTK_USE_SYSTEM_FREETYPE:BOOL=ON',
            '-DVTK_USE_SYSTEM_HDF5:BOOL=OFF',
            '-DVTK_USE_SYSTEM_JPEG:BOOL=ON',
            '-DVTK_USE_SYSTEM_LIBXML2:BOOL=ON',
            '-DVTK_USE_SYSTEM_NETCDF:BOOL=OFF',
            '-DVTK_USE_SYSTEM_TIFF:BOOL=ON',
            '-DVTK_USE_SYSTEM_ZLIB:BOOL=ON',
        ]

        # The assumed qt version changed to QT5 (as of paraview 5.2.1),
        # so explicitly specify which QT major version is actually being used
        if '+qt' in spec:
            cmake_args.extend([
                '-DPARAVIEW_QT_VERSION=%s' % spec['qt'].version[0],
            ])

        if '+python' in spec:
            cmake_args.extend([
                '-DPARAVIEW_ENABLE_PYTHON:BOOL=ON',
                '-DPYTHON_EXECUTABLE:FILEPATH=%s' % spec['python'].command.path
            ])

        if '+mpi' in spec:
            cmake_args.extend([
                '-DPARAVIEW_USE_MPI:BOOL=ON',
                '-DMPIEXEC:FILEPATH=%s/bin/mpiexec' % spec['mpi'].prefix
            ])

        if 'darwin' in self.spec.architecture:
            cmake_args.extend([
                '-DVTK_USE_X:BOOL=OFF',
                '-DPARAVIEW_DO_UNIX_STYLE_INSTALLS:BOOL=ON',
            ])

        return cmake_args
<commit_msg>- build paraview without qt when osmesa is specified. (#5480)<commit_after>##############################################################################
# Copyright (c) 2013-2017, Lawrence Livermore National Security, LLC.
# Produced at the Lawrence Livermore National Laboratory.
#
# This file is part of Spack.
# Created by Todd Gamblin, tgamblin@llnl.gov, All rights reserved.
# LLNL-CODE-647188
#
# For details, see https://github.com/llnl/spack
# Please also see the NOTICE and LICENSE files for our notice and the LGPL.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License (as
# published by the Free Software Foundation) version 2.1, February 1999.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and
# conditions of the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##############################################################################
from spack import *

class Paraview(CMakePackage):
    """ParaView is an open-source, multi-platform data analysis and
    visualization application."""

    homepage = 'http://www.paraview.org'
    url      = "http://www.paraview.org/files/v5.3/ParaView-v5.3.0.tar.gz"
    _urlfmt  = 'http://www.paraview.org/files/v{0}/ParaView-v{1}{2}.tar.gz'

    version('5.4.1', '4030c70477ec5a85aa72d6fc86a30753')
    version('5.4.0', 'b92847605bac9036414b644f33cb7163')
    version('5.3.0', '68fbbbe733aa607ec13d1db1ab5eba71')
    version('5.2.0', '4570d1a2a183026adb65b73c7125b8b0')
    version('5.1.2', '44fb32fc8988fcdfbc216c9e40c3e925')
    version('5.0.1', 'fdf206113369746e2276b95b257d2c9b')
    version('4.4.0', 'fa1569857dd680ebb4d7ff89c2227378')

    variant('plugins', default=True,
            description='Install include files for plugins support')
    variant('python', default=False, description='Enable Python support')
    variant('mpi', default=True, description='Enable MPI support')
    variant('osmesa', default=False, description='Enable OSMesa support')
    variant('qt', default=False, description='Enable Qt (gui) support')
    variant('opengl2', default=True, description='Enable OpenGL2 backend')

    depends_on('python@2:2.8', when='+python')
    depends_on('py-numpy', when='+python', type='run')
    depends_on('py-matplotlib', when='+python', type='run')
    depends_on('mpi', when='+mpi')
    depends_on('qt', when='@5.3.0:+qt')
    depends_on('qt@:4', when='@:5.2.0+qt')

    depends_on('mesa+swrender', when='+osmesa')
    conflicts('+qt', when='+osmesa')

    depends_on('bzip2')
    depends_on('freetype')
    # depends_on('hdf5+mpi', when='+mpi')
    # depends_on('hdf5~mpi', when='~mpi')
    depends_on('jpeg')
    depends_on('libpng')
    depends_on('libtiff')
    depends_on('libxml2')
    # depends_on('netcdf')
    # depends_on('netcdf-cxx')
    # depends_on('protobuf') # version mismatches?
    # depends_on('sqlite') # external version not supported
    depends_on('zlib')
    depends_on('cmake@3.3:', type='build')

    patch('stl-reader-pv440.patch', when='@4.4.0')

    # Broken gcc-detection - improved in 5.1.0, redundant later
    patch('gcc-compiler-pv501.patch', when='@:5.0.1')

    # Broken installation (ui_pqExportStateWizard.h) - fixed in 5.2.0
    patch('ui_pqExportStateWizard.patch', when='@:5.1.2')

    def url_for_version(self, version):
        """Handle ParaView version-based custom URLs."""
        if version < Version('5.1.0'):
            return self._urlfmt.format(version.up_to(2), version, '-source')
        else:
            return self._urlfmt.format(version.up_to(2), version, '')

    def cmake_args(self):
        """Populate cmake arguments for ParaView."""
        spec = self.spec

        def variant_bool(feature, on='ON', off='OFF'):
            """Ternary for spec variant to ON/OFF string"""
            if feature in spec:
                return on
            return off

        def nvariant_bool(feature):
            """Negated ternary for spec variant to OFF/ON string"""
            return variant_bool(feature, on='OFF', off='ON')

        rendering = variant_bool('+opengl2', 'OpenGL2', 'OpenGL')
        includes  = variant_bool('+plugins')

        cmake_args = [
            '-DPARAVIEW_BUILD_QT_GUI:BOOL=%s' % variant_bool('+qt'),
            '-DVTK_OPENGL_HAS_OSMESA:BOOL=%s' % variant_bool('+osmesa'),
            '-DVTK_USE_X:BOOL=%s' % nvariant_bool('+osmesa'),
            '-DVTK_RENDERING_BACKEND:STRING=%s' % rendering,
            '-DPARAVIEW_INSTALL_DEVELOPMENT_FILES:BOOL=%s' % includes,
            '-DBUILD_TESTING:BOOL=OFF',
            '-DVTK_USE_SYSTEM_FREETYPE:BOOL=ON',
            '-DVTK_USE_SYSTEM_HDF5:BOOL=OFF',
            '-DVTK_USE_SYSTEM_JPEG:BOOL=ON',
            '-DVTK_USE_SYSTEM_LIBXML2:BOOL=ON',
            '-DVTK_USE_SYSTEM_NETCDF:BOOL=OFF',
            '-DVTK_USE_SYSTEM_TIFF:BOOL=ON',
            '-DVTK_USE_SYSTEM_ZLIB:BOOL=ON',
        ]

        # The assumed qt version changed to QT5 (as of paraview 5.2.1),
        # so explicitly specify which QT major version is actually being used
        if '+qt' in spec:
            cmake_args.extend([
                '-DPARAVIEW_QT_VERSION=%s' % spec['qt'].version[0],
            ])

        if '+python' in spec:
            cmake_args.extend([
                '-DPARAVIEW_ENABLE_PYTHON:BOOL=ON',
                '-DPYTHON_EXECUTABLE:FILEPATH=%s' % spec['python'].command.path
            ])

        if '+mpi' in spec:
            cmake_args.extend([
                '-DPARAVIEW_USE_MPI:BOOL=ON',
                '-DMPIEXEC:FILEPATH=%s/bin/mpiexec' % spec['mpi'].prefix
            ])

        if 'darwin' in self.spec.architecture:
            cmake_args.extend([
                '-DVTK_USE_X:BOOL=OFF',
                '-DPARAVIEW_DO_UNIX_STYLE_INSTALLS:BOOL=ON',
            ])

        return cmake_args
<|endoftext|>


<commit_before># Copyright 2013-present Barefoot Networks, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#!/usr/bin/env python

"""
p4c - P4 Compiler Driver

"""

from __future__ import absolute_import
import argparse
import glob
import itertools
import os
import subprocess
import shlex
import sys
import re

import p4c_src.util as util
import p4c_src.config as config
import p4c_src

commands = {}

p4c_version = p4c_src.__version__

def set_version(ver):
    global p4c_version
    p4c_version = ver

def get_version():
    return p4c_version

def display_supported_targets(cfg):
    print "Supported targets in \"target-arch-vendor\" triplet:"
    for target in cfg.target:
        print target

def add_developer_options(parser):
    parser.add_argument("-T", dest="log_levels",
                        action="append", default=[],
                        help="[Compiler debugging] Adjust logging level per file (see below)")
    parser.add_argument("--top4", dest="passes",
                        action="append", default=[],
                        help="[Compiler debugging] Dump the P4 representation after \
                               passes whose name contains one of `passX' substrings. \
                               When '-v' is used this will include the compiler IR.")
    parser.add_argument("--dump", dest="dump_dir", default=None,
                        help="[Compiler debugging] Folder where P4 programs are dumped.")
    parser.add_argument("--toJson", dest="json", default=None,
                        help="Dump IR to JSON in the specified file.")
    parser.add_argument("--pp", dest="pretty_print", default=None,
                        help="Pretty-print the program in the specified file.")

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("-V", "--version", dest="show_version",
                        help="show version and exit",
                        action="store_true", default=False)
    parser.add_argument("-v", "--debug", dest="debug",
                        help="verbose",
                        action="store_true", default=False)
    parser.add_argument("-###", "--test-only", dest="dry_run",
                        help="print (but do not run) the commands",
                        action="store_true", default=False)
    parser.add_argument("-Xpreprocessor", dest="preprocessor_options",
                        metavar="<arg>",
                        help="Pass <arg> to the preprocessor",
                        action="append", default=[])
    parser.add_argument("-Xp4c", dest="compiler_options",
                        metavar="<arg>",
                        help="Pass <arg> to the compiler",
                        action="append", default=[])
    parser.add_argument("-Xassembler", dest="assembler_options",
                        metavar="<arg>",
                        help="Pass <arg> to the assembler",
                        action="append", default=[])
    parser.add_argument("-Xlinker", dest="linker_options",
                        metavar="<arg>",
                        help="Pass <arg> to the linker",
                        action="append", default=[])
    parser.add_argument("-b", "--target", dest="backend",
                        help="specify target backend",
                        action="store", default="bmv2-ss-p4org")
    parser.add_argument("-D", dest="preprocessor_defines",
                        help="define a macro to be used by the preprocessor",
                        action="append", default=[])
    parser.add_argument("-E", dest="run_preprocessor_only",
                        help="Only run the preprocessor",
                        action="store_true", default=False)
    parser.add_argument("-e", dest="skip_preprocessor",
                        help="Skip the preprocessor",
                        action="store_true", default=False)
    parser.add_argument("-S", dest="run_till_assembler",
                        help="Only run the preprocess and compilation steps",
                        action="store_true", default=False)
    parser.add_argument("-c", dest="run_all",
                        help="Only run preprocess, compile, and assemble steps",
                        action="store_true", default=True)
    parser.add_argument("-x", dest="language",
                        choices = ["p4-14", "p4-16"],
                        help="Treat subsequent input files as having type language.",
                        action="store", default="p4-16")
    parser.add_argument("-I", dest="search_path",
                        help="Add directory to include search path",
                        action="append", default=[])
    parser.add_argument("-o", dest="output_directory",
                        help="Write output to the provided path",
                        action="store", metavar="PATH", default=".")
    parser.add_argument("--target-help", dest="show_target_help",
                        help="Display target specific command line options.",
                        action="store_true", default=False)
    parser.add_argument("--p4runtime-file",
                        help="Write a P4Runtime control plane API description "
                        "to the specified file.",
                        action="store", default=None)
    parser.add_argument("--p4runtime-format",
                        choices=["binary", "json", "text"],
                        help="Choose output format for the P4Runtime API "
                        "description (default is binary).",
                        action="store", default="binary")

    if (os.environ['P4C_BUILD_TYPE'] == "DEVELOPER"):
        add_developer_options(parser)

    parser.add_argument("source_file", nargs='?', help="Files to compile", default=None)

    # many more options
    opts = parser.parse_args()
    source = opts.source_file

    if opts.show_version:
        print("p4c %s" % (get_version()))
        sys.exit(0)

    # load supported configuration
    cfg_files = glob.glob("{}/*.cfg".format(os.environ['P4C_CFG_PATH']))
    cfg = config.Config(config_prefix = "p4c")
    for cf in cfg_files:
        if opts.debug:
            print 'loading config {}'.format(cf)
        cfg.load_from_config(cf, opts.output_directory, opts.source_file)

    if opts.show_target_help:
        display_supported_targets(cfg)
        sys.exit(0)

    # target-arch-vendor, e.g.
    # bmv2-ss-p4org
    # ebpf-psa-p4org
    triplet = opts.backend.split('-')
    if (len(triplet) != 3):
        print "Invalid target-arch-vendor triplet."
        display_supported_targets(cfg)
        sys.exit(1)

    if not source:
        parser.error('No input specified.')

    backend = None
    for triplet, _ in cfg.steps.iteritems():
        regex = triplet.replace('*', '[a-zA-Z0-9*]*')
        pattern = re.compile(regex)
        if (pattern.match(opts.backend)):
            backend = triplet
            break
    if backend == None:
        print "Unknown backend:", opts.backend
        sys.exit(1)

    commands['preprocessor'] = []
    commands['compiler'] = []
    commands['assembler'] = []
    commands['linker'] = []

    for option in opts.preprocessor_options:
        commands["preprocessor"] += shlex.split(option)

    for option in opts.compiler_options:
        commands["compiler"] += shlex.split(option)

    if (os.environ['P4C_BUILD_TYPE'] == "DEVELOPER"):
        for option in opts.log_levels:
            commands["compiler"].append("-T{}".format(option))
        if opts.passes:
            commands["compiler"].append("--top4 {}".format(",".join(opts.passes)))
        if opts.debug:
            commands["compiler"].append("-vvv")
        if opts.dump_dir:
            commands["compiler"].append("--dump {}".format(opts.dump_dir))
        if opts.json:
            commands["compiler"].append("--toJSON {}".format(opts.json))
        if opts.pretty_print:
            commands["compiler"].append("--pp {}".format(opts.pretty_print))

    for option in opts.assembler_options:
        commands["assembler"] += shlex.split(option)

    for option in opts.linker_options:
        commands["linker"] += shlex.split(option)

    # set output directory
    if not os.path.exists(opts.output_directory):
        os.makedirs(opts.output_directory)

    commands['preprocessor'].insert(0, cfg.get_preprocessor(backend))
    commands['compiler'].insert(0, cfg.get_compiler(backend))
    commands['assembler'].insert(0, cfg.get_assembler(backend))
    commands['linker'].insert(0, cfg.get_linker(backend))

    # handle mode flags
    step_enable = [False, False, False, False]
    if opts.run_preprocessor_only:
        step_enable = [True, False, False, False]
    elif opts.skip_preprocessor:
        step_enable = [False, True, True, True]
    elif opts.run_till_assembler:
        step_enable = [True, True, False, False]
    elif opts.run_all:
        step_enable = [True, True, True, True]

    # append to the list of defines
    for d in opts.preprocessor_defines:
        commands['preprocessor'].append("-D"+d)
        commands['compiler'].append("-D"+d)

    # default search path
    if opts.language == 'p4-16':
        commands['preprocessor'].append("-I {}".format(os.environ['P4C_16_INCLUDE_PATH']))
        commands['compiler'].append("-I {}".format(os.environ['P4C_16_INCLUDE_PATH']))
    else:
        commands['preprocessor'].append("-I {}".format(os.environ['P4C_14_INCLUDE_PATH']))
        commands['compiler'].append("-I {}".format(os.environ['P4C_14_INCLUDE_PATH']))

    # append search path
    for path in opts.search_path:
        commands['preprocessor'].append("-I")
        commands['preprocessor'].append(path)
        commands['compiler'].append("-I")
        commands['compiler'].append(path)

    # set p4 version
    if opts.language == 'p4-16':
        commands['compiler'].append("--p4v=16")
    else:
        commands['compiler'].append("--p4v=14")

    # P4Runtime options
    if opts.p4runtime_file:
        commands['compiler'].append("--p4runtime-file {}".format(opts.p4runtime_file))
        commands['compiler'].append("--p4runtime-format {}".format(opts.p4runtime_format))

    for idx, step in enumerate(cfg.steps[backend]):

        cmd = []
        for c in commands[step]:
            cmd = cmd + shlex.split(c)
        options = cfg.options[backend][step]
        for option in options:
            cmd = cmd + shlex.split(option)
        # check if cmd in PATH unless absolute path
        if cmd[0].find('/') != 0 and (util.find_bin(cmd[0]) == None):
            print "{}: command not found".format(cmd[0])
            sys.exit(1)
        # only dry-run
        if opts.dry_run:
            print "{}: {}".format(step, " ".join(cmd))
            continue
        # skip if not required
        if not step_enable[idx]:
            continue
        # run command
        try:
            p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        except:
            import traceback
            print "error invoking {}".format(" ".join(cmd))
            print traceback.format_exc()
            sys.exit(1)

        if opts.debug:
            print 'running {}'.format(' '.join(cmd))
        out, err = p.communicate() # now wait
        if len(out) > 0:
            print out
        if p.returncode != 0:
            sys.exit(p.returncode)
<commit_msg>add flag for producing debug information<commit_after># Copyright 2013-present Barefoot Networks, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#!/usr/bin/env python

"""
p4c - P4 Compiler Driver

"""

from __future__ import absolute_import
import argparse
import glob
import itertools
import os
import subprocess
import shlex
import sys
import re

import p4c_src.util as util
import p4c_src.config as config
import p4c_src

commands = {}

p4c_version = p4c_src.__version__

def set_version(ver):
    global p4c_version
    p4c_version = ver

def get_version():
    return p4c_version

def display_supported_targets(cfg):
    print "Supported targets in \"target-arch-vendor\" triplet:"
    for target in cfg.target:
        print target

def add_developer_options(parser):
    parser.add_argument("-T", dest="log_levels",
                        action="append", default=[],
                        help="[Compiler debugging] Adjust logging level per file (see below)")
    parser.add_argument("--top4", dest="passes",
                        action="append", default=[],
                        help="[Compiler debugging] Dump the P4 representation after \
                               passes whose name contains one of `passX' substrings. \
                               When '-v' is used this will include the compiler IR.")
    parser.add_argument("--dump", dest="dump_dir", default=None,
                        help="[Compiler debugging] Folder where P4 programs are dumped.")
    parser.add_argument("--toJson", dest="json", default=None,
                        help="Dump IR to JSON in the specified file.")
    parser.add_argument("--pp", dest="pretty_print", default=None,
                        help="Pretty-print the program in the specified file.")

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("-V", "--version", dest="show_version",
                        help="show version and exit",
                        action="store_true", default=False)
    parser.add_argument("-v", "--debug", dest="debug",
                        help="verbose",
                        action="store_true", default=False)
    parser.add_argument("-###", "--test-only", dest="dry_run",
                        help="print (but do not run) the commands",
                        action="store_true", default=False)
    parser.add_argument("-Xpreprocessor", dest="preprocessor_options",
                        metavar="<arg>",
                        help="Pass <arg> to the preprocessor",
                        action="append", default=[])
    parser.add_argument("-Xp4c", dest="compiler_options",
                        metavar="<arg>",
                        help="Pass <arg> to the compiler",
                        action="append", default=[])
    parser.add_argument("-Xassembler", dest="assembler_options",
                        metavar="<arg>",
                        help="Pass <arg> to the assembler",
                        action="append", default=[])
    parser.add_argument("-Xlinker", dest="linker_options",
                        metavar="<arg>",
                        help="Pass <arg> to the linker",
                        action="append", default=[])
    parser.add_argument("-b", "--target", dest="backend",
                        help="specify target backend",
                        action="store", default="bmv2-ss-p4org")
    parser.add_argument("-c", dest="run_all",
                        help="Only run preprocess, compile, and assemble steps",
                        action="store_true", default=True)
    parser.add_argument("-D", dest="preprocessor_defines",
                        help="define a macro to be used by the preprocessor",
                        action="append", default=[])
    parser.add_argument("-E", dest="run_preprocessor_only",
                        help="Only run the preprocessor",
                        action="store_true", default=False)
    parser.add_argument("-e", dest="skip_preprocessor",
                        help="Skip the preprocessor",
                        action="store_true", default=False)
    parser.add_argument("-g", dest="debug_info",
                        help="Generate debug information",
                        action="store_true", default=False)
    parser.add_argument("-I", dest="search_path",
                        help="Add directory to include search path",
                        action="append", default=[])
    parser.add_argument("-o", dest="output_directory",
                        help="Write output to the provided path",
                        action="store", metavar="PATH", default=".")
    parser.add_argument("--p4runtime-file",
                        help="Write a P4Runtime control plane API description "
                        "to the specified file.",
                        action="store", default=None)
    parser.add_argument("--p4runtime-format",
                        choices=["binary", "json", "text"],
                        help="Choose output format for the P4Runtime API "
                        "description (default is binary).",
                        action="store", default="binary")
    parser.add_argument("--target-help", dest="show_target_help",
                        help="Display target specific command line options.",
                        action="store_true", default=False)
    parser.add_argument("-S", dest="run_till_assembler",
                        help="Only run the preprocess and compilation steps",
                        action="store_true", default=False)
    parser.add_argument("-x", dest="language",
                        choices = ["p4-14", "p4-16"],
                        help="Treat subsequent input files as having type language.",
                        action="store", default="p4-16")

    if (os.environ['P4C_BUILD_TYPE'] == "DEVELOPER"):
        add_developer_options(parser)

    parser.add_argument("source_file", nargs='?', help="Files to compile", default=None)

    # many more options
    opts = parser.parse_args()
    source = opts.source_file

    if opts.show_version:
        print("p4c %s" % (get_version()))
        sys.exit(0)

    # load supported configuration
    cfg_files = glob.glob("{}/*.cfg".format(os.environ['P4C_CFG_PATH']))
    cfg = config.Config(config_prefix = "p4c")
    for cf in cfg_files:
        if opts.debug:
            print 'loading config {}'.format(cf)
        cfg.load_from_config(cf, opts.output_directory, opts.source_file)

    if opts.show_target_help:
        display_supported_targets(cfg)
        sys.exit(0)

    # target-arch-vendor, e.g.
    # bmv2-ss-p4org
    # ebpf-psa-p4org
    triplet = opts.backend.split('-')
    if (len(triplet) != 3):
        print "Invalid target-arch-vendor triplet."
        display_supported_targets(cfg)
        sys.exit(1)

    if not source:
        parser.error('No input specified.')

    backend = None
    for triplet, _ in cfg.steps.iteritems():
        regex = triplet.replace('*', '[a-zA-Z0-9*]*')
        pattern = re.compile(regex)
        if (pattern.match(opts.backend)):
            backend = triplet
            break
    if backend == None:
        print "Unknown backend:", opts.backend
        sys.exit(1)

    commands['preprocessor'] = []
    commands['compiler'] = []
    commands['assembler'] = []
    commands['linker'] = []

    for option in opts.preprocessor_options:
        commands["preprocessor"] += shlex.split(option)

    for option in opts.compiler_options:
        commands["compiler"] += shlex.split(option)

    if (os.environ['P4C_BUILD_TYPE'] == "DEVELOPER"):
        for option in opts.log_levels:
            commands["compiler"].append("-T{}".format(option))
        if opts.passes:
            commands["compiler"].append("--top4 {}".format(",".join(opts.passes)))
        if opts.debug:
            commands["assembler"].append("-vvv")
            commands["compiler"].append("-vvv")
        if opts.dump_dir:
            commands["compiler"].append("--dump {}".format(opts.dump_dir))
        if opts.json:
            commands["compiler"].append("--toJSON {}".format(opts.json))
        if opts.pretty_print:
            commands["compiler"].append("--pp {}".format(opts.pretty_print))

    if opts.debug_info:
        commands["assembler"].append("-g")
        commands["compiler"].append("-g")
        commands["linker"].append("-g")

    for option in opts.assembler_options:
        commands["assembler"] += shlex.split(option)

    for option in opts.linker_options:
        commands["linker"] += shlex.split(option)

    # set output directory
    if not os.path.exists(opts.output_directory):
        os.makedirs(opts.output_directory)

    commands['preprocessor'].insert(0, cfg.get_preprocessor(backend))
    commands['compiler'].insert(0, cfg.get_compiler(backend))
    commands['assembler'].insert(0, cfg.get_assembler(backend))
    commands['linker'].insert(0, cfg.get_linker(backend))

    # handle mode flags
    step_enable = [False, False, False, False]
    if opts.run_preprocessor_only:
        step_enable = [True, False, False, False]
    elif opts.skip_preprocessor:
        step_enable = [False, True, True, True]
    elif opts.run_till_assembler:
        step_enable = [True, True, False, False]
    elif opts.run_all:
        step_enable = [True, True, True, True]

    # append to the list of defines
    for d in opts.preprocessor_defines:
        commands['preprocessor'].append("-D"+d)
        commands['compiler'].append("-D"+d)

    # default search path
    if opts.language == 'p4-16':
        commands['preprocessor'].append("-I {}".format(os.environ['P4C_16_INCLUDE_PATH']))
        commands['compiler'].append("-I {}".format(os.environ['P4C_16_INCLUDE_PATH']))
    else:
        commands['preprocessor'].append("-I {}".format(os.environ['P4C_14_INCLUDE_PATH']))
        commands['compiler'].append("-I {}".format(os.environ['P4C_14_INCLUDE_PATH']))

    # append search path
    for path in opts.search_path:
        commands['preprocessor'].append("-I")
        commands['preprocessor'].append(path)
        commands['compiler'].append("-I")
        commands['compiler'].append(path)

    # set p4 version
    if opts.language == 'p4-16':
        commands['compiler'].append("--p4v=16")
    else:
        commands['compiler'].append("--p4v=14")

    # P4Runtime options
    if opts.p4runtime_file:
        commands['compiler'].append("--p4runtime-file {}".format(opts.p4runtime_file))
        commands['compiler'].append("--p4runtime-format {}".format(opts.p4runtime_format))

    for idx, step in enumerate(cfg.steps[backend]):

        cmd = []
        for c in commands[step]:
            cmd = cmd + shlex.split(c)
        options = cfg.options[backend][step]
        for option in options:
            cmd = cmd + shlex.split(option)
        # check if cmd in PATH unless absolute path
        if cmd[0].find('/') != 0 and (util.find_bin(cmd[0]) == None):
            print "{}: command not found".format(cmd[0])
            sys.exit(1)
        # only dry-run
        if opts.dry_run:
            print "{}: {}".format(step, " ".join(cmd))
            continue
        # skip if not required
        if not step_enable[idx]:
            continue
        # run command
        try:
            p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        except:
            import traceback
            print "error invoking {}".format(" ".join(cmd))
            print traceback.format_exc()
            sys.exit(1)

        if opts.debug:
            print 'running {}'.format(' '.join(cmd))
        out, err = p.communicate() # now wait
        if len(out) > 0:
            print out
        if p.returncode != 0:
            sys.exit(p.returncode)
<|endoftext|>


<commit_before>import py2js

import inspect

class JSVar(object):

    def __init__(self, *names):
        self.names = [x.split(".") for x in names]

    def __call__(self, obj):
        return obj

class JavaScript(object):
    """
    Decorator that you can use to convert methods to JavaScript.

    For example this code::

        @JavaScript
        class TestClass(object):
            def __init__(self):
                alert('TestClass created')
                self.reset()

            def reset(self):
                self.value = 0

            def inc(self):
                alert(self.value)
                self.value += 1

        print str(TestClass)

    prints::

        function TestClass() {
            return new _TestClass();
        }
        function _TestClass() {
            this.__init__();
        }
        _TestClass.__name__ = 'TestClass'
        _TestClass.prototype.__class__ = _TestClass
        _TestClass.prototype.__init__ = function() {
            alert("TestClass created");
            this.reset();
        }
        _TestClass.prototype.reset = function() {
            this.value = 0;
        }
        _TestClass.prototype.inc = function() {
            alert(this.value);
            this.value += 1;
        }

    Alternatively, an equivalent way is to use JavaScript() as a function:

        class TestClass(object):
            def __init__(self):
                alert('TestClass created')
                self.reset()

            def reset(self):
                self.value = 0

            def inc(self):
                alert(self.value)
                self.value += 1

        print str(JavaScript(TestClass))

    If you want to call the original function/class as Python, use the
    following syntax::

        js = JavaScript(TestClass)
        test_class = js() # Python instance of TestClass() will be created
        js_class = str(js) # A string with the JS code

    """

    def __init__(self, *args):
        self.jsvars = list(args)
        self._js = None

    def __call__(self, obj):
        lines = inspect.getsource(obj).split("\n")
        if lines[0].startswith("@"):
            lines.pop(0)
        self._js = py2js.compile_string("\n".join(lines), self.jsvars)
        return self._js

<commit_msg> + Added license header to decorator.py<commit_after>######################################################################
##
## Copyright 2011 Christian Iversen <ci@sikkerhed.org>
## Copyright 2011 Samuel Ytterbrink <neppord@gmail.com>
##
## Permission is hereby granted, free of charge, to any person
## obtaining a copy of this software and associated documentation
## files (the "Software"), to deal in the Software without
## restriction, including without limitation the rights to use,
## copy, modify, merge, publish, distribute, sublicense, and/or sell
## copies of the Software, and to permit persons to whom the
## Software is furnished to do so, subject to the following
## conditions:
##
## The above copyright notice and this permission notice shall be
## included in all copies or substantial portions of the Software.
##
## 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 THE AUTHORS OR COPYRIGHT
## HOLDERS 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 OR
## OTHER DEALINGS IN THE SOFTWARE.
##
######################################################################

import py2js

import inspect

class JSVar(object):

    def __init__(self, *names):
        self.names = [x.split(".") for x in names]

    def __call__(self, obj):
        return obj

class JavaScript(object):
    """
    Decorator that you can use to convert methods to JavaScript.

    For example this code::

        @JavaScript
        class TestClass(object):
            def __init__(self):
                alert('TestClass created')
                self.reset()

            def reset(self):
                self.value = 0

            def inc(self):
                alert(self.value)
                self.value += 1

        print str(TestClass)

    prints::

        function TestClass() {
            return new _TestClass();
        }
        function _TestClass() {
            this.__init__();
        }
        _TestClass.__name__ = 'TestClass'
        _TestClass.prototype.__class__ = _TestClass
        _TestClass.prototype.__init__ = function() {
            alert("TestClass created");
            this.reset();
        }
        _TestClass.prototype.reset = function() {
            this.value = 0;
        }
        _TestClass.prototype.inc = function() {
            alert(this.value);
            this.value += 1;
        }

    Alternatively, an equivalent way is to use JavaScript() as a function:

        class TestClass(object):
            def __init__(self):
                alert('TestClass created')
                self.reset()

            def reset(self):
                self.value = 0

            def inc(self):
                alert(self.value)
                self.value += 1

        print str(JavaScript(TestClass))

    If you want to call the original function/class as Python, use the
    following syntax::

        js = JavaScript(TestClass)
        test_class = js() # Python instance of TestClass() will be created
        js_class = str(js) # A string with the JS code

    """

    def __init__(self, *args):
        self.jsvars = list(args)
        self._js = None

    def __call__(self, obj):
        lines = inspect.getsource(obj).split("\n")
        if lines[0].startswith("@"):
            lines.pop(0)
        self._js = py2js.compile_string("\n".join(lines), self.jsvars)
        return self._js

<|endoftext|>


<commit_before>##############################################################################
# Copyright (c) 2013-2016, Lawrence Livermore National Security, LLC.
# Produced at the Lawrence Livermore National Laboratory.
#
# This file is part of Spack.
# Created by Todd Gamblin, tgamblin@llnl.gov, All rights reserved.
# LLNL-CODE-647188
#
# For details, see https://github.com/llnl/spack
# Please also see the LICENSE file for our notice and the LGPL.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License (as
# published by the Free Software Foundation) version 2.1, February 1999.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and
# conditions of the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##############################################################################
from spack import *
import platform

class PyNumpy(PythonPackage):
    """NumPy is the fundamental package for scientific computing with Python.
    It contains among other things: a powerful N-dimensional array object,
    sophisticated (broadcasting) functions, tools for integrating C/C++ and
    Fortran code, and useful linear algebra, Fourier transform, and random
    number capabilities"""

    homepage = "http://www.numpy.org/"
    url      = "https://pypi.io/packages/source/n/numpy/numpy-1.9.1.tar.gz"

    install_time_test_callbacks = ['install_test', 'import_module_test']

    import_modules = [
        'numpy', 'numpy.compat', 'numpy.core', 'numpy.distutils', 'numpy.doc',
        'numpy.f2py', 'numpy.fft', 'numpy.lib', 'numpy.linalg', 'numpy.ma',
        'numpy.matrixlib', 'numpy.polynomial', 'numpy.random', 'numpy.testing',
        'numpy.distutils.command', 'numpy.distutils.fcompiler'
    ]

    # FIXME: numpy._build_utils and numpy.core.code_generators failed to import
    # FIXME: Is this expected?

    version('1.12.0', '33e5a84579f31829bbbba084fe0a4300',
            url="https://pypi.io/packages/source/n/numpy/numpy-1.12.0.zip")
    version('1.11.2', '03bd7927c314c43780271bf1ab795ebc')
    version('1.11.1', '2f44a895a8104ffac140c3a70edbd450')
    version('1.11.0', 'bc56fb9fc2895aa4961802ffbdb31d0b')
    version('1.10.4', 'aed294de0aa1ac7bd3f9745f4f1968ad')
    version('1.9.2',  'a1ed53432dbcd256398898d35bc8e645')
    version('1.9.1',  '78842b73560ec378142665e712ae4ad9')

    variant('blas',   default=True, description='Build with BLAS support')
    variant('lapack', default=True, description='Build with LAPACK support')

    depends_on('python@2.7:2.8,3.4:')
    depends_on('py-setuptools', type='build')
    depends_on('blas',   when='+blas')
    depends_on('lapack', when='+lapack')

    # Tests require:
    # TODO: Add a 'test' deptype
    # depends_on('py-nose@1.0.0:', type='test')

    def setup_dependent_package(self, module, dependent_spec):
        python_version = self.spec['python'].version.up_to(2)
        arch = '{0}-{1}'.format(platform.system().lower(), platform.machine())

        self.spec.include = join_path(
            self.prefix.lib,
            'python{0}'.format(python_version),
            'site-packages',
            'numpy-{0}-py{1}-{2}.egg'.format(
                self.spec.version, python_version, arch),
            'numpy/core/include')

    def patch(self):
        spec = self.spec
        # for build notes see http://www.scipy.org/scipylib/building/linux.html
        lapackblas = LibraryList('')
        if '+lapack' in spec:
            lapackblas += spec['lapack'].libs

        if '+blas' in spec:
            lapackblas += spec['blas'].libs

        if '+blas' in spec or '+lapack' in spec:
            # note that one should not use [blas_opt] and [lapack_opt], see
            # https://github.com/numpy/numpy/commit/ffd4332262ee0295cb942c94ed124f043d801eb6
            with open('site.cfg', 'w') as f:
                # Unfortunately, numpy prefers to provide each BLAS/LAPACK
                # differently.
                names  = ','.join(lapackblas.names)
                dirs   = ':'.join(lapackblas.directories)

                # Special treatment for some (!) BLAS/LAPACK. Note that
                # in this case library_dirs can not be specified within [ALL].
                if '^openblas' in spec:
                    f.write('[openblas]\n')
                    f.write('libraries=%s\n'    % names)
                elif '^mkl' in spec:
                    # numpy does not expect system libraries needed for MKL
                    # here.
                    # names = [x for x in names if x.startswith('mkl')]
                    # FIXME: as of @1.11.2, numpy does not work with separately
                    # specified threading and interface layers. A workaround is
                    # a terribly bad idea to use mkl_rt. In this case Spack
                    # will no longer be able to guarantee that one and the
                    # same variant of Blas/Lapack (32/64bit, threaded/serial)
                    # is used within the DAG. This may lead to a lot of
                    # hard-to-debug segmentation faults on user's side. Users
                    # may also break working installation by (unconsciously)
                    # setting environment variable to switch between different
                    # interface and threading layers dynamically. From this
                    # perspective it is no different from throwing away RPATH's
                    # and using LD_LIBRARY_PATH throughout Spack.
                    f.write('[mkl]\n')
                    f.write('mkl_libs=%s\n'     % 'mkl_rt')
                elif '^atlas' in spec:
                    f.write('[atlas]\n')
                    f.write('atlas_libs=%s\n'   % names)
                else:
                    # The section title for the defaults changed in @1.10, see
                    # https://github.com/numpy/numpy/blob/master/site.cfg.example
                    if spec.satisfies('@:1.9.2'):
                        f.write('[DEFAULT]\n')
                    else:
                        f.write('[ALL]\n')
                    f.write('libraries=%s\n'    % names)

                f.write('library_dirs=%s\n' % dirs)
                if not ((platform.system() == "Darwin") and
                        (platform.mac_ver()[0] == '10.12')):
                    f.write('rpath=%s\n' % dirs)

    def build_args(self, spec, prefix):
        args = []

        # From NumPy 1.10.0 on it's possible to do a parallel build
        if self.version >= Version('1.10.0'):
            args = ['-j', str(make_jobs)]

        return args

    def test(self):
        # `setup.py test` is not supported.  Use one of the following
        # instead:
        #
        # - `python runtests.py`              (to build and test)
        # - `python runtests.py --no-build`   (to test installed numpy)
        # - `>>> numpy.test()`           (run tests for installed numpy
        #                                 from within an interpreter)
        pass

    def install_test(self):
        # Change directories due to the following error:
        #
        # ImportError: Error importing numpy: you should not try to import
        #       numpy from its source directory; please exit the numpy
        #       source tree, and relaunch your python interpreter from there.
        with working_dir('..'):
            python('-c', 'import numpy; numpy.test("full", verbose=2)')
<commit_msg>Add latest numpy release (#3869)<commit_after>##############################################################################
# Copyright (c) 2013-2016, Lawrence Livermore National Security, LLC.
# Produced at the Lawrence Livermore National Laboratory.
#
# This file is part of Spack.
# Created by Todd Gamblin, tgamblin@llnl.gov, All rights reserved.
# LLNL-CODE-647188
#
# For details, see https://github.com/llnl/spack
# Please also see the LICENSE file for our notice and the LGPL.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License (as
# published by the Free Software Foundation) version 2.1, February 1999.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and
# conditions of the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##############################################################################
from spack import *
import platform

class PyNumpy(PythonPackage):
    """NumPy is the fundamental package for scientific computing with Python.
    It contains among other things: a powerful N-dimensional array object,
    sophisticated (broadcasting) functions, tools for integrating C/C++ and
    Fortran code, and useful linear algebra, Fourier transform, and random
    number capabilities"""

    homepage = "http://www.numpy.org/"
    url      = "https://pypi.io/packages/source/n/numpy/numpy-1.9.1.tar.gz"

    install_time_test_callbacks = ['install_test', 'import_module_test']

    import_modules = [
        'numpy', 'numpy.compat', 'numpy.core', 'numpy.distutils', 'numpy.doc',
        'numpy.f2py', 'numpy.fft', 'numpy.lib', 'numpy.linalg', 'numpy.ma',
        'numpy.matrixlib', 'numpy.polynomial', 'numpy.random', 'numpy.testing',
        'numpy.distutils.command', 'numpy.distutils.fcompiler'
    ]

    # FIXME: numpy._build_utils and numpy.core.code_generators failed to import
    # FIXME: Is this expected?

    version('1.12.1', 'c75b072a984028ac746a6a332c209a91',
            url="https://pypi.io/packages/source/n/numpy/numpy-1.12.1.zip")
    version('1.12.0', '33e5a84579f31829bbbba084fe0a4300',
            url="https://pypi.io/packages/source/n/numpy/numpy-1.12.0.zip")
    version('1.11.2', '03bd7927c314c43780271bf1ab795ebc')
    version('1.11.1', '2f44a895a8104ffac140c3a70edbd450')
    version('1.11.0', 'bc56fb9fc2895aa4961802ffbdb31d0b')
    version('1.10.4', 'aed294de0aa1ac7bd3f9745f4f1968ad')
    version('1.9.2',  'a1ed53432dbcd256398898d35bc8e645')
    version('1.9.1',  '78842b73560ec378142665e712ae4ad9')

    variant('blas',   default=True, description='Build with BLAS support')
    variant('lapack', default=True, description='Build with LAPACK support')

    depends_on('python@2.7:2.8,3.4:')
    depends_on('py-setuptools', type='build')
    depends_on('blas',   when='+blas')
    depends_on('lapack', when='+lapack')

    # Tests require:
    # TODO: Add a 'test' deptype
    # depends_on('py-nose@1.0.0:', type='test')

    def setup_dependent_package(self, module, dependent_spec):
        python_version = self.spec['python'].version.up_to(2)
        arch = '{0}-{1}'.format(platform.system().lower(), platform.machine())

        self.spec.include = join_path(
            self.prefix.lib,
            'python{0}'.format(python_version),
            'site-packages',
            'numpy-{0}-py{1}-{2}.egg'.format(
                self.spec.version, python_version, arch),
            'numpy/core/include')

    def patch(self):
        spec = self.spec
        # for build notes see http://www.scipy.org/scipylib/building/linux.html
        lapackblas = LibraryList('')
        if '+lapack' in spec:
            lapackblas += spec['lapack'].libs

        if '+blas' in spec:
            lapackblas += spec['blas'].libs

        if '+blas' in spec or '+lapack' in spec:
            # note that one should not use [blas_opt] and [lapack_opt], see
            # https://github.com/numpy/numpy/commit/ffd4332262ee0295cb942c94ed124f043d801eb6
            with open('site.cfg', 'w') as f:
                # Unfortunately, numpy prefers to provide each BLAS/LAPACK
                # differently.
                names  = ','.join(lapackblas.names)
                dirs   = ':'.join(lapackblas.directories)

                # Special treatment for some (!) BLAS/LAPACK. Note that
                # in this case library_dirs can not be specified within [ALL].
                if '^openblas' in spec:
                    f.write('[openblas]\n')
                    f.write('libraries=%s\n'    % names)
                elif '^mkl' in spec:
                    # numpy does not expect system libraries needed for MKL
                    # here.
                    # names = [x for x in names if x.startswith('mkl')]
                    # FIXME: as of @1.11.2, numpy does not work with separately
                    # specified threading and interface layers. A workaround is
                    # a terribly bad idea to use mkl_rt. In this case Spack
                    # will no longer be able to guarantee that one and the
                    # same variant of Blas/Lapack (32/64bit, threaded/serial)
                    # is used within the DAG. This may lead to a lot of
                    # hard-to-debug segmentation faults on user's side. Users
                    # may also break working installation by (unconsciously)
                    # setting environment variable to switch between different
                    # interface and threading layers dynamically. From this
                    # perspective it is no different from throwing away RPATH's
                    # and using LD_LIBRARY_PATH throughout Spack.
                    f.write('[mkl]\n')
                    f.write('mkl_libs=%s\n'     % 'mkl_rt')
                elif '^atlas' in spec:
                    f.write('[atlas]\n')
                    f.write('atlas_libs=%s\n'   % names)
                else:
                    # The section title for the defaults changed in @1.10, see
                    # https://github.com/numpy/numpy/blob/master/site.cfg.example
                    if spec.satisfies('@:1.9.2'):
                        f.write('[DEFAULT]\n')
                    else:
                        f.write('[ALL]\n')
                    f.write('libraries=%s\n'    % names)

                f.write('library_dirs=%s\n' % dirs)
                if not ((platform.system() == "Darwin") and
                        (platform.mac_ver()[0] == '10.12')):
                    f.write('rpath=%s\n' % dirs)

    def build_args(self, spec, prefix):
        args = []

        # From NumPy 1.10.0 on it's possible to do a parallel build
        if self.version >= Version('1.10.0'):
            args = ['-j', str(make_jobs)]

        return args

    def test(self):
        # `setup.py test` is not supported.  Use one of the following
        # instead:
        #
        # - `python runtests.py`              (to build and test)
        # - `python runtests.py --no-build`   (to test installed numpy)
        # - `>>> numpy.test()`           (run tests for installed numpy
        #                                 from within an interpreter)
        pass

    def install_test(self):
        # Change directories due to the following error:
        #
        # ImportError: Error importing numpy: you should not try to import
        #       numpy from its source directory; please exit the numpy
        #       source tree, and relaunch your python interpreter from there.
        with working_dir('..'):
            python('-c', 'import numpy; numpy.test("full", verbose=2)')
<|endoftext|>


<commit_before>#!/usr/bin/env python2
#
# mailer.py: send email describing a commit
#
# USAGE: mailer.py REPOS-DIR REVISION [CONFIG-FILE]
#
#   Using CONFIG-FILE, deliver an email describing the changes between
#   REV and REV-1 for the repository REPOS.
#

import os
import sys
import string
import ConfigParser

from svn import fs, util, delta, _repos

def main(pool, config_fname, repos_dir, rev):
  cfg = Config(config_fname)

  repos = Repository(repos_dir, rev, pool)

  editor = ChangeCollector(repos.root_prev)

  e_ptr, e_baton = delta.make_editor(editor, pool)
  wrap_editor, wrap_baton = delta.svn_delta_compat_wrap(e_ptr, e_baton, pool)

  _repos.svn_repos_dir_delta(repos.root_prev, '', None, repos.root_this, '',
                             wrap_editor, wrap_baton,
                             0,  # text_deltas
                             1,  # recurse
                             0,  # entry_props
                             1,  # use_copy_history
                             pool)

  ### pipe it to sendmail rather than stdout
  generate_content(sys.stdout, repos, editor, pool)

def generate_content(output, repos, editor, pool):

  date = repos.get_rev_prop(util.SVN_PROP_REVISION_DATE)
  ### reformat the date

  output.write('Author: %s\nDate: %s\nNew Revision: %s\n\n'
               % (repos.get_rev_prop(util.SVN_PROP_REVISION_AUTHOR),
                  date,
                  repos.rev))

  generate_list(output, 'Added', editor.adds)
  generate_list(output, 'Removed', editor.deletes)
  generate_list(output, 'Modified', editor.changes)

  output.write('Log:\n%s\n'
               % (repos.get_rev_prop(util.SVN_PROP_REVISION_LOG) or ''))

  # build a complete list of affected dirs/files
  paths = editor.adds.keys() + editor.deletes.keys() + editor.changes.keys()
  paths.sort()

  for path in paths:
    if editor.adds.has_key(path):
      src = None
      dst = path
      change = editor.adds[path]
    elif editor.deletes.has_key(path):
      src = path
      dst = None
      change = editor.deletes[path]
    else:
      src = dst = path
      change = editor.changes[path]

    generate_diff(output, repos, src, dst, change, pool)

  ### print diffs. watch for binary files.

def generate_list(output, header, fnames):
  if fnames:
    output.write('%s:\n' % header)
    items = fnames.items()
    items.sort()
    for fname, change in items:
      ### should print prop_changes?, copy_info, and binary? here
      ### hmm. don't have binary right now.
      if change.item_type == ChangeCollector.DIR:
        output.write('   %s/\n' % fname)
      else:
        output.write('   %s\n' % fname)

def generate_diff(output, repos, src, dst, change, pool):
  if 0 and copy_info and copy_info[0]:
    assert (not src) and dst  # it was ADDED

    copyfrom_path = copy_info[0]
    if copyfrom_path[0] == '/':
      # remove the leading slash for consistency with other paths
      copyfrom_path = copyfrom_path[1:]

    output.write('Copied: %s (from rev %d, %s)\n'
                 % (dst, copy_info[1], copy_info[0]))

  if not dst:
    output.write('deleted: %s\n' % src)
    return
  if not src:
    if change.base_path:
      # this was copied. note that we strip the leading slash from the
      # base (copyfrom) path.
      output.write('Copied: %s (from rev %d, %s)\n'
                   % (dst, change.base_rev, change.base_path[1:]))
    else:
      output.write('Added: %s\n' % dst)
    return

  output.write('diff: %s@%d <- %s@%d  (%s; propmod=%d)\n'
               % (src, repos.rev, change.base_path[1:], change.base_rev,
                  change.item_type, change.prop_changes))

class Repository:
  "Hold roots and other information about the repository."

  def __init__(self, repos_dir, rev, pool):
    self.repos_dir = repos_dir
    self.rev = rev
    self.pool = pool

    db_path = os.path.join(repos_dir, 'db')
    if not os.path.exists(db_path):
      db_path = repos_dir

    self.fs_ptr = fs.new(pool)
    fs.open_berkeley(self.fs_ptr, db_path)

    self.roots = { }

    self.root_prev = self.get_root(rev-1)
    self.root_this = self.get_root(rev)

  def get_rev_prop(self, propname):
    return fs.revision_prop(self.fs_ptr, self.rev, propname, self.pool)

  def get_root(self, rev):
    try:
      return self.roots[rev]
    except KeyError:
      pass
    root = self.roots[rev] = fs.revision_root(self.fs_ptr, rev, self.pool)
    return root

class ChangeCollector(delta.Editor):
  DIR = 'DIR'
  FILE = 'FILE'

  def __init__(self, root_prev):
    self.root_prev = root_prev

    # path -> [ item-type, prop-changes?, (copyfrom_path, rev) ]
    self.adds = { }
    self.changes = { }
    self.deletes = { }

  def open_root(self, base_revision, dir_pool):
    return ('', '', base_revision)  # dir_baton

  def delete_entry(self, path, revision, parent_baton, pool):
    if fs.is_dir(self.root_prev, '/' + path, pool):
      item_type = ChangeCollector.DIR
    else:
      item_type = ChangeCollector.FILE
    # base_path is the specified path. revision is the parent's.
    self.deletes[path] = _change(item_type, False, path, parent_baton[2])

  def add_directory(self, path, parent_baton,
                    copyfrom_path, copyfrom_revision, dir_pool):
    self.adds[path] = _change(ChangeCollector.DIR,
                              False,
                              copyfrom_path,
                              copyfrom_revision,
                              )

    return (path, copyfrom_path, copyfrom_revision)  # dir_baton

  def open_directory(self, path, parent_baton, base_revision, dir_pool):
    assert parent_baton[2] == base_revision

    base_path = _svn_join(parent_baton[1], _svn_basename(path))
    return (path, base_path, base_revision)  # dir_baton

  def change_dir_prop(self, dir_baton, name, value, pool):
    dir_path = dir_baton[0]
    if self.changes.has_key(dir_path):
      self.changes[dir_path].prop_changes = True
    elif self.adds.has_key(dir_path):
      self.adds[dir_path].prop_changes = True
    else:
      # can't be added or deleted, so this must be CHANGED
      self.changes[dir_baton] = _change(ChangeCollector.DIR,
                                        True,
                                        dir_baton[1], # base_path
                                        dir_baton[2], # base_rev
                                        )

  def add_file(self, path, parent_baton,
               copyfrom_path, copyfrom_revision, file_pool):
    self.adds[path] = _change(ChangeCollector.FILE,
                              False,
                              copyfrom_path,
                              copyfrom_revision,
                              )

    return (path, copyfrom_path, copyfrom_revision)  # file_baton

  def open_file(self, path, parent_baton, base_revision, file_pool):
    assert parent_baton[2] == base_revision

    base_path = _svn_join(parent_baton[1], _svn_basename(path))
    return (path, base_path, base_revision)  # file_baton

  def apply_textdelta(self, file_baton):
    file_path = file_baton[0]
    if not self.changes.has_key(file_path) \
       and not self.adds.has_key(file_path):
      # it wasn't added, and it can't be deleted, so this must be CHANGED
      self.changes[file_path] = _change(ChangeCollector.FILE,
                                        False,
                                        file_baton[1], # base_path
                                        file_baton[2], # base_rev
                                        )

    # no handler
    return None

  def change_file_prop(self, file_baton, name, value, pool):
    file_path = file_baton[0]
    if self.changes.has_key(file_path):
      self.changes[file_path].prop_changes = True
    elif self.adds.has_key(file_path):
      self.adds[file_path].prop_changes = True
    else:
      # it wasn't added, and it can't be deleted, so this must be CHANGED
      self.changes[file_path] = _change(ChangeCollector.FILE,
                                        True,
                                        file_baton[1], # base_path
                                        file_baton[2], # base_rev
                                        )

class _change:
  __slots__ = [ 'item_type', 'prop_changes',
                'base_path', 'base_rev',
                ]
  def __init__(self, item_type, prop_changes, base_path, base_rev):
    self.item_type = item_type
    self.prop_changes = prop_changes
    self.base_path = base_path
    self.base_rev = base_rev

class Config:
  def __init__(self, fname):
    cp = ConfigParser.ConfigParser()
    cp.read(fname)

    for section in cp.sections():
      if not hasattr(self, section):
        setattr(self, section, _sub_section())
      section_ob = getattr(self, section)
      for option in cp.options(section):
        # get the raw value -- we use the same format for *our* interpolation
        value = cp.get(section, option, raw=1)
        setattr(section_ob, option, value)

class _sub_section:
  pass

class MissingConfig(Exception):
  pass

def _svn_basename(path):
  "Compute the basename of an SVN path ('/' separators)."
  idx = string.rfind(path, '/')
  if idx == -1:
    return path
  return path[idx+1:]

def _svn_join(base, relative):
  "Join a relative path onto a base path using the SVN separator ('/')."
  if relative[:1] == '/':
    return relative
  if base[-1:] == '/':
    return base + relative
  return base + '/' + relative

# enable True/False in older vsns of Python
try:
  _unused = True
except NameError:
  True = 1
  False = 0

if __name__ == '__main__':
  if len(sys.argv) < 3 or len(sys.argv) > 4:
    sys.stderr.write('USAGE: %s REPOS-DIR REVISION [CONFIG-FILE]\n'
                     % sys.argv[0])
    sys.exit(1)

  repos_dir = sys.argv[1]
  revision = int(sys.argv[2])

  if len(sys.argv) == 3:
    # default to REPOS-DIR/conf/mailer.conf
    config_fname = os.path.join(repos_dir, 'conf', 'mailer.conf')
    if not os.path.exists(config_fname):
      # okay. look for 'mailer.conf' as a sibling of this script
      config_fname = os.path.join(os.path.dirname(sys.argv[0]), 'mailer.conf')
  else:
    # the config file was explicitly provided
    config_fname = sys.argv[3]

  if not os.path.exists(config_fname):
    raise MissingConfig(config_fname)

  ### run some validation on these params
  util.run_app(main, config_fname, repos_dir, revision)
<commit_msg>Some simple mods to bring the code up to the current codebase.<commit_after>#!/usr/bin/env python2
#
# mailer.py: send email describing a commit
#
# USAGE: mailer.py REPOS-DIR REVISION [CONFIG-FILE]
#
#   Using CONFIG-FILE, deliver an email describing the changes between
#   REV and REV-1 for the repository REPOS.
#

import os
import sys
import string
import ConfigParser

import svn.fs
import svn.util
import svn.delta
import svn.repos

def main(pool, config_fname, repos_dir, rev):
  cfg = Config(config_fname)

  repos = Repository(repos_dir, rev, pool)

  editor = ChangeCollector(repos.root_prev)

  e_ptr, e_baton = svn.delta.make_editor(editor, pool)
  svn.repos.svn_repos_dir_delta(repos.root_prev, '', None, repos.root_this, '',
                                e_ptr, e_baton,
                                0,  # text_deltas
                                1,  # recurse
                                0,  # entry_props
                                1,  # use_copy_history
                                pool)

  ### pipe it to sendmail rather than stdout
  generate_content(sys.stdout, repos, editor, pool)

def generate_content(output, repos, editor, pool):

  date = repos.get_rev_prop(svn.util.SVN_PROP_REVISION_DATE)
  ### reformat the date

  output.write('Author: %s\nDate: %s\nNew Revision: %s\n\n'
               % (repos.get_rev_prop(svn.util.SVN_PROP_REVISION_AUTHOR),
                  date,
                  repos.rev))

  generate_list(output, 'Added', editor.adds)
  generate_list(output, 'Removed', editor.deletes)
  generate_list(output, 'Modified', editor.changes)

  output.write('Log:\n%s\n'
               % (repos.get_rev_prop(svn.util.SVN_PROP_REVISION_LOG) or ''))

  # build a complete list of affected dirs/files
  paths = editor.adds.keys() + editor.deletes.keys() + editor.changes.keys()
  paths.sort()

  for path in paths:
    if editor.adds.has_key(path):
      src = None
      dst = path
      change = editor.adds[path]
    elif editor.deletes.has_key(path):
      src = path
      dst = None
      change = editor.deletes[path]
    else:
      src = dst = path
      change = editor.changes[path]

    generate_diff(output, repos, src, dst, change, pool)

  ### print diffs. watch for binary files.

def generate_list(output, header, fnames):
  if fnames:
    output.write('%s:\n' % header)
    items = fnames.items()
    items.sort()
    for fname, change in items:
      ### should print prop_changes?, copy_info, and binary? here
      ### hmm. don't have binary right now.
      if change.item_type == ChangeCollector.DIR:
        output.write('   %s/\n' % fname)
      else:
        output.write('   %s\n' % fname)

def generate_diff(output, repos, src, dst, change, pool):
  if 0 and copy_info and copy_info[0]:
    assert (not src) and dst  # it was ADDED

    copyfrom_path = copy_info[0]
    if copyfrom_path[0] == '/':
      # remove the leading slash for consistency with other paths
      copyfrom_path = copyfrom_path[1:]

    output.write('Copied: %s (from rev %d, %s)\n'
                 % (dst, copy_info[1], copy_info[0]))

  if not dst:
    output.write('deleted: %s\n' % src)
    return
  if not src:
    if change.base_path:
      # this was copied. note that we strip the leading slash from the
      # base (copyfrom) path.
      output.write('Copied: %s (from rev %d, %s)\n'
                   % (dst, change.base_rev, change.base_path[1:]))
    else:
      output.write('Added: %s\n' % dst)
    return

  output.write('diff: %s@%d <- %s@%d  (%s; propmod=%d)\n'
               % (src, repos.rev, change.base_path[1:], change.base_rev,
                  change.item_type, change.prop_changes))

class Repository:
  "Hold roots and other information about the repository."

  def __init__(self, repos_dir, rev, pool):
    self.repos_dir = repos_dir
    self.rev = rev
    self.pool = pool

    db_path = os.path.join(repos_dir, 'db')
    if not os.path.exists(db_path):
      db_path = repos_dir

    self.fs_ptr = svn.fs.new(pool)
    svn.fs.open_berkeley(self.fs_ptr, db_path)

    self.roots = { }

    self.root_prev = self.get_root(rev-1)
    self.root_this = self.get_root(rev)

  def get_rev_prop(self, propname):
    return svn.fs.revision_prop(self.fs_ptr, self.rev, propname, self.pool)

  def get_root(self, rev):
    try:
      return self.roots[rev]
    except KeyError:
      pass
    root = self.roots[rev] = svn.fs.revision_root(self.fs_ptr, rev, self.pool)
    return root

class ChangeCollector(svn.delta.Editor):
  DIR = 'DIR'
  FILE = 'FILE'

  def __init__(self, root_prev):
    self.root_prev = root_prev

    # path -> [ item-type, prop-changes?, (copyfrom_path, rev) ]
    self.adds = { }
    self.changes = { }
    self.deletes = { }

  def open_root(self, base_revision, dir_pool):
    return ('', '', base_revision)  # dir_baton

  def delete_entry(self, path, revision, parent_baton, pool):
    if svn.fs.is_dir(self.root_prev, '/' + path, pool):
      item_type = ChangeCollector.DIR
    else:
      item_type = ChangeCollector.FILE
    # base_path is the specified path. revision is the parent's.
    self.deletes[path] = _change(item_type, False, path, parent_baton[2])

  def add_directory(self, path, parent_baton,
                    copyfrom_path, copyfrom_revision, dir_pool):
    self.adds[path] = _change(ChangeCollector.DIR,
                              False,
                              copyfrom_path,
                              copyfrom_revision,
                              )

    return (path, copyfrom_path, copyfrom_revision)  # dir_baton

  def open_directory(self, path, parent_baton, base_revision, dir_pool):
    assert parent_baton[2] == base_revision

    base_path = _svn_join(parent_baton[1], _svn_basename(path))
    return (path, base_path, base_revision)  # dir_baton

  def change_dir_prop(self, dir_baton, name, value, pool):
    dir_path = dir_baton[0]
    if self.changes.has_key(dir_path):
      self.changes[dir_path].prop_changes = True
    elif self.adds.has_key(dir_path):
      self.adds[dir_path].prop_changes = True
    else:
      # can't be added or deleted, so this must be CHANGED
      self.changes[dir_baton] = _change(ChangeCollector.DIR,
                                        True,
                                        dir_baton[1], # base_path
                                        dir_baton[2], # base_rev
                                        )

  def add_file(self, path, parent_baton,
               copyfrom_path, copyfrom_revision, file_pool):
    self.adds[path] = _change(ChangeCollector.FILE,
                              False,
                              copyfrom_path,
                              copyfrom_revision,
                              )

    return (path, copyfrom_path, copyfrom_revision)  # file_baton

  def open_file(self, path, parent_baton, base_revision, file_pool):
    assert parent_baton[2] == base_revision

    base_path = _svn_join(parent_baton[1], _svn_basename(path))
    return (path, base_path, base_revision)  # file_baton

  def apply_textdelta(self, file_baton):
    file_path = file_baton[0]
    if not self.changes.has_key(file_path) \
       and not self.adds.has_key(file_path):
      # it wasn't added, and it can't be deleted, so this must be CHANGED
      self.changes[file_path] = _change(ChangeCollector.FILE,
                                        False,
                                        file_baton[1], # base_path
                                        file_baton[2], # base_rev
                                        )

    # no handler
    return None

  def change_file_prop(self, file_baton, name, value, pool):
    file_path = file_baton[0]
    if self.changes.has_key(file_path):
      self.changes[file_path].prop_changes = True
    elif self.adds.has_key(file_path):
      self.adds[file_path].prop_changes = True
    else:
      # it wasn't added, and it can't be deleted, so this must be CHANGED
      self.changes[file_path] = _change(ChangeCollector.FILE,
                                        True,
                                        file_baton[1], # base_path
                                        file_baton[2], # base_rev
                                        )

class _change:
  __slots__ = [ 'item_type', 'prop_changes',
                'base_path', 'base_rev',
                ]
  def __init__(self, item_type, prop_changes, base_path, base_rev):
    self.item_type = item_type
    self.prop_changes = prop_changes
    self.base_path = base_path
    self.base_rev = base_rev

class Config:
  def __init__(self, fname):
    cp = ConfigParser.ConfigParser()
    cp.read(fname)

    for section in cp.sections():
      if not hasattr(self, section):
        setattr(self, section, _sub_section())
      section_ob = getattr(self, section)
      for option in cp.options(section):
        # get the raw value -- we use the same format for *our* interpolation
        value = cp.get(section, option, raw=1)
        setattr(section_ob, option, value)

class _sub_section:
  pass

class MissingConfig(Exception):
  pass

def _svn_basename(path):
  "Compute the basename of an SVN path ('/' separators)."
  idx = string.rfind(path, '/')
  if idx == -1:
    return path
  return path[idx+1:]

def _svn_join(base, relative):
  "Join a relative path onto a base path using the SVN separator ('/')."
  if relative[:1] == '/':
    return relative
  if base[-1:] == '/':
    return base + relative
  return base + '/' + relative

# enable True/False in older vsns of Python
try:
  _unused = True
except NameError:
  True = 1
  False = 0

if __name__ == '__main__':
  if len(sys.argv) < 3 or len(sys.argv) > 4:
    sys.stderr.write('USAGE: %s REPOS-DIR REVISION [CONFIG-FILE]\n'
                     % sys.argv[0])
    sys.exit(1)

  repos_dir = sys.argv[1]
  revision = int(sys.argv[2])

  if len(sys.argv) == 3:
    # default to REPOS-DIR/conf/mailer.conf
    config_fname = os.path.join(repos_dir, 'conf', 'mailer.conf')
    if not os.path.exists(config_fname):
      # okay. look for 'mailer.conf' as a sibling of this script
      config_fname = os.path.join(os.path.dirname(sys.argv[0]), 'mailer.conf')
  else:
    # the config file was explicitly provided
    config_fname = sys.argv[3]

  if not os.path.exists(config_fname):
    raise MissingConfig(config_fname)

  ### run some validation on these params
  svn.util.run_app(main, config_fname, repos_dir, revision)
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2010, Sascha Peilicke <saschpe@gmx.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program (see the file COPYING); if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA

import argparse, os, pwd, sys, urllib, xmlrpclib
from datetime import datetime
from jinja2 import Environment, FileSystemLoader, Template
from pprint import pprint

TEMPLATE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates') # absolute template path

pypi = xmlrpclib.ServerProxy('http://python.org/pypi')                      # XML RPC connection to PyPI
env = Environment(loader = FileSystemLoader(TEMPLATE_DIR))                  # Jinja2 template environment

def list(args):
    print('listing all PyPI packages...')
    for package in pypi.list_packages():                                    # nothing fancy
        print(package)

def search(args):
    print('searching for package {0}...'.format(args.name))
    for hit in pypi.search({'name': args.name}):
        print('found {0}-{1}'.format(hit['name'], hit['version']))

def show(args):
    check_or_set_version(args)
    print('showing package {0}...'.format(args.name))
    data = pypi.release_data(args.name, args.version)                       # fetch all meta data
    pprint(data)

def fetch(args):
    check_or_set_version(args)
    url = newest_download_url(args)
    print('downloading package {0}-{1}...'.format(args.name, args.version))
    print('from {0}'.format(url['url']))
    urllib.urlretrieve(url['url'], url['filename'])                         # download the object behind the URL

def generate(args):
    check_or_set_version(args)
    if not args.template:
        args.template = template_list()[0]
    if not args.filename:
        args.filename = args.name + '.' + args.template.rsplit('.', 1)[1]   # take template file ending
    print('generating spec file for {0}...'.format(args.name))
    data = pypi.release_data(args.name, args.version)                       # fetch all meta data
    url = newest_download_url(args)
    data['ending'] = url['filename'].rsplit(args.name + "-" + args.version)[1] # split of name-version to get ending
    data['year'] = datetime.now().year                                      # set current year
    data['user_name'] = pwd.getpwuid(os.getuid())[4]                        # set system user (packager)
    template = env.get_template(args.template)
    result = template.render(data)
    with open(args.filename, 'w') as outfile:                               # write result to spec file
        outfile.write(result)

def check_or_set_version(args):
    if not args.version:                                                    # take first version found
        releases = pypi.package_releases(args.name)
        if len(releases) == 0:
            print("unable to find a suitable release for {0}!".format(args.name))
            sys.exit(1)
        else:
            args.version = pypi.package_releases(args.name)[0]              # return first (current) release number

def newest_download_url(args):
    for url in pypi.package_urls(args.name, args.version):                  # fetch all download URLs
        if url['packagetype'] == 'sdist':                                   # found the source URL we care for
            return url
    print("unable to find a source release for {0}!".format(args.name))
    sys.exit(1)

def template_list():
    return os.listdir(TEMPLATE_DIR)

def main():
    parser = argparse.ArgumentParser(description = __doc__)
    parser.add_argument('--version', action='version', version='%(prog)s 0.1')
    subparsers = parser.add_subparsers(title='commands')

    parser_list = subparsers.add_parser('list', help='list all packages on PyPI')
    parser_list.set_defaults(func=list)

    parser_search = subparsers.add_parser('search', help='search for packages on PyPI')
    parser_search.add_argument('name', help='package name (with optional version)')
    parser_search.set_defaults(func=search)

    parser_show = subparsers.add_parser('show', help='show metadata for package')
    parser_show.add_argument('name', help='package name')
    parser_show.add_argument('version', nargs='?', help='package version (optional)')
    parser_show.set_defaults(func=show)

    parser_fetch = subparsers.add_parser('fetch', help='download package source tarball from PyPI')
    parser_fetch.add_argument('name', help='package name')
    parser_fetch.add_argument('version', nargs='?', help='package version (optional)')
    parser_fetch.set_defaults(func=fetch)

    parser_spec = subparsers.add_parser('generate', help='generate RPM spec or DEB dsc file for a package')
    parser_spec.add_argument('name', help='package name')
    parser_spec.add_argument('version', nargs='?', help='package version (optional)')
    parser_spec.add_argument('-t', '--template', choices=template_list(), help='spec file template')
    parser_spec.add_argument('-f', '--filename', help='spec filename (optional)')
    parser_spec.set_defaults(func=generate)

    parser_help = subparsers.add_parser('help', help='show this help')
    parser_help.set_defaults(func=lambda args: parser.print_help())

    args = parser.parse_args()
    args.func(args)

# fallback if run directly
if __name__ == '__main__':
    main()
<commit_msg>Be able to generate a SPEC file even if there is no upstream source release.<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2010, Sascha Peilicke <saschpe@gmx.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program (see the file COPYING); if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA

import argparse, os, pwd, sys, urllib, xmlrpclib
from datetime import datetime
from jinja2 import Environment, FileSystemLoader, Template
from pprint import pprint

TEMPLATE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates') # absolute template path

pypi = xmlrpclib.ServerProxy('http://python.org/pypi')                      # XML RPC connection to PyPI
env = Environment(loader = FileSystemLoader(TEMPLATE_DIR))                  # Jinja2 template environment

def list(args):
    print('listing all PyPI packages...')
    for package in pypi.list_packages():                                    # nothing fancy
        print(package)

def search(args):
    print('searching for package {0}...'.format(args.name))
    for hit in pypi.search({'name': args.name}):
        print('found {0}-{1}'.format(hit['name'], hit['version']))

def show(args):
    check_or_set_version(args)
    print('showing package {0}...'.format(args.name))
    data = pypi.release_data(args.name, args.version)                       # fetch all meta data
    pprint(data)

def fetch(args):
    check_or_set_version(args)
    url = newest_download_url(args)
    if not url:
      print("unable to find a source release for {0}!".format(args.name))
      sys.exit(1)
    print('downloading package {0}-{1}...'.format(args.name, args.version))
    print('from {0}'.format(url['url']))
    urllib.urlretrieve(url['url'], url['filename'])                         # download the object behind the URL

def generate(args):
    check_or_set_version(args)
    if not args.template:
        args.template = template_list()[0]
    if not args.filename:
        args.filename = args.name + '.' + args.template.rsplit('.', 1)[1]   # take template file ending
    print('generating spec file for {0}...'.format(args.name))
    data = pypi.release_data(args.name, args.version)                       # fetch all meta data
    url = newest_download_url(args)
    if url:
      data['ending'] = url['filename'].rsplit(args.name + "-" + args.version)[1] # split of name-version to get ending
    else:
      data['ending'] = '.zip'                                               # set sane default if no download available
    data['year'] = datetime.now().year                                      # set current year
    data['user_name'] = pwd.getpwuid(os.getuid())[4]                        # set system user (packager)
    template = env.get_template(args.template)
    result = template.render(data)
    with open(args.filename, 'w') as outfile:                               # write result to spec file
        outfile.write(result)

def check_or_set_version(args):
    if not args.version:                                                    # take first version found
        releases = pypi.package_releases(args.name)
        if len(releases) == 0:
            print("unable to find a suitable release for {0}!".format(args.name))
            sys.exit(1)
        else:
            args.version = pypi.package_releases(args.name)[0]              # return first (current) release number

def newest_download_url(args):
    for url in pypi.package_urls(args.name, args.version):                  # fetch all download URLs
        if url['packagetype'] == 'sdist':                                   # found the source URL we care for
            return url
    return []

def template_list():
    return os.listdir(TEMPLATE_DIR)

def main():
    parser = argparse.ArgumentParser(description = __doc__)
    parser.add_argument('--version', action='version', version='%(prog)s 0.1')
    subparsers = parser.add_subparsers(title='commands')

    parser_list = subparsers.add_parser('list', help='list all packages on PyPI')
    parser_list.set_defaults(func=list)

    parser_search = subparsers.add_parser('search', help='search for packages on PyPI')
    parser_search.add_argument('name', help='package name (with optional version)')
    parser_search.set_defaults(func=search)

    parser_show = subparsers.add_parser('show', help='show metadata for package')
    parser_show.add_argument('name', help='package name')
    parser_show.add_argument('version', nargs='?', help='package version (optional)')
    parser_show.set_defaults(func=show)

    parser_fetch = subparsers.add_parser('fetch', help='download package source tarball from PyPI')
    parser_fetch.add_argument('name', help='package name')
    parser_fetch.add_argument('version', nargs='?', help='package version (optional)')
    parser_fetch.set_defaults(func=fetch)

    parser_spec = subparsers.add_parser('generate', help='generate RPM spec or DEB dsc file for a package')
    parser_spec.add_argument('name', help='package name')
    parser_spec.add_argument('version', nargs='?', help='package version (optional)')
    parser_spec.add_argument('-t', '--template', choices=template_list(), help='spec file template')
    parser_spec.add_argument('-f', '--filename', help='spec filename (optional)')
    parser_spec.set_defaults(func=generate)

    parser_help = subparsers.add_parser('help', help='show this help')
    parser_help.set_defaults(func=lambda args: parser.print_help())

    args = parser.parse_args()
    args.func(args)

# fallback if run directly
if __name__ == '__main__':
    main()
<|endoftext|>


<commit_before># Copyright 2013-2020 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

from spack import *

class PyStorm(PythonPackage):
    """Storm is an object-relational mapper (ORM) for Python"""
    homepage = "https://storm.canonical.com/"
    url      = "https://launchpad.net/storm/trunk/0.20/+download/storm-0.20.tar.gz"

    version('0.20', sha256='0fa70043bb1a1c178c2f760db35f5956244cecf50dab7fb22d78be7507726603')

    depends_on('py-setuptools', type='build')
<commit_msg>Add py-storm@0.23 and update URL (#16991)<commit_after># Copyright 2013-2020 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

from spack import *

class PyStorm(PythonPackage):
    """Storm is an object-relational mapper (ORM) for Python"""
    homepage = "https://storm.canonical.com/"
    url      = "https://launchpad.net/storm/trunk/0.20/+download/storm-0.20.tar.bz2"

    version('0.23', sha256='01c59f1c898fb9891333abd65519ba2dd5f68623ac8e67b54932e99ce52593d3')
    version('0.20', sha256='1fe016c9ec40520eafc3cf359f1ec2b7fa86be91e45c9279bfb0ea3b06390a82')

    depends_on('py-setuptools', type='build')
    depends_on('py-six', when='@0.23:')
    depends_on('python@2.7:2.8', when='@:0.20')
    depends_on('python@2.7:2.8,3.5:', when='@0.21:')
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import urllib, logging

# module-level logger
ml = logging.getLogger('VWS')
ml.addHandler(logging.NullHandler())

class vws(object):
  def __init__(self,**kwargs):
    """
      keywords arguments
        vws, img_relpath, resolution, channel, dont_initialize
    """
    self.vws = kwargs.get('vws',"http://cameraserver")
    self.img_relpath = kwargs.get('img_relpath',"/Jpeg/CamImg.jpg")
    # ResType: 0: 176x144, 1: 352x288, 2: 320x240, 3: 640x80
    self.vws_res = "/ChangeResolution.cgi?ResType=" + str(kwargs.get('resolution',3))
    self.vws_cr = "/ChangeCompressRatio.cgi?Ratio=1" # High
    self.vws_chn = "/SetChannel.cgi?Channel="

    self.initialized = False
    if not kwargs.get('dont_initialize',False):
      self.setup()
    if self.initialized:
     self.set_channel(kwargs.get('channel',0))

  def setup(self):
    ml.info("Richte VWS ein")
    try:
      self._urlretrieve(self.vws + self.vws_res)
      self._urlretrieve(self.vws + self.vws_cr)
      self.initialized = True
    except:
      ml.warn("Konnte VWS nicht initialisieren")
      import traceback
      for line in traceback.format_exc().splitlines():
        ml.debug(line)

  def download_image(self,filename = "tmp.jpg"):
    if not self.initialized: raise RuntimeError
    http_path = self.vws + self.img_relpath
    ml.debug("Lade {0} nach {1} herunter".format(http_path,filename))
    (fname,headers) = self._urlretrieve(http_path, filename)
    ml.debug("Ladevorgang erfolgreich: {0}".format(fname))
    return fname

  def set_channel(self,channel):
    ml.info("Wechsle VWS Kanal zu {0}".format(channel))
    self._urlretrieve(self.vws + self.vws_chn + str(channel))

  def _urlretrieve(self,url):
    ml.debug("URL Retrieve: \"{}\"".format(url))
    return urllib.urlretrieve(url)

if __name__=='__main__':
  import argparse
  parser = argparse.ArgumentParser()
  parser.add_argument("-v","--verbose",type=int,default=1,help="Verbosity level. 0: Critical, 1: error, 2: warning, 3: info, 4: debug")
  args = parser.parse_args()

  ll = 10*(4-args.verbose)
  rootlogger = logging.getLogger('')
  rootlogger.setLevel(ll)
  ch = logging.StreamHandler()
  ch.setLevel(ll)
  formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  ch.setFormatter(formatter)
  rootlogger.addHandler(ch)

  e = vws()
  if not e.initialized:
    ml.info("Abbruch des Tests")
    raise SystemExit
  for channel in xrange(0,4):
    e.set_channel(channel)
    e.download_image("chan{0}.jpg".format(channel))
<commit_msg>vws: download to memory<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import urllib, logging

# module-level logger
ml = logging.getLogger('VWS')
ml.addHandler(logging.NullHandler())

class vws(object):
  def __init__(self,**kwargs):
    """
      keywords arguments
        vws, img_relpath, resolution, channel, dont_initialize
    """
    self.vws = kwargs.get('vws',"http://cameraserver")
    self.img_relpath = kwargs.get('img_relpath',"/Jpeg/CamImg.jpg")
    # ResType: 0: 176x144, 1: 352x288, 2: 320x240, 3: 640x80
    self.vws_res = "/ChangeResolution.cgi?ResType=" + str(kwargs.get('resolution',3))
    self.vws_cr = "/ChangeCompressRatio.cgi?Ratio=1" # High
    self.vws_chn = "/SetChannel.cgi?Channel="

    self.initialized = False
    if not kwargs.get('dont_initialize',False):
      self.setup()
    if self.initialized:
     self.set_channel(kwargs.get('channel',0))

  def setup(self):
    ml.info("Richte VWS ein")
    try:
      self._urlretrieve(self.vws + self.vws_res)
      self._urlretrieve(self.vws + self.vws_cr)
      self.initialized = True
    except:
      ml.warn("Konnte VWS nicht initialisieren")
      import traceback
      for line in traceback.format_exc().splitlines():
        ml.debug(line)

  def download_image(self):
    if not self.initialized: raise RuntimeError
    http_path = self.vws + self.img_relpath
    ml.debug("Lade {0}".format(http_path))
    imgfile = self._urlretrieve(http_path)
    ml.debug("Ladevorgang erfolgreich: {0}".format(len(imgfile)))
    return imgfile

  def set_channel(self,channel):
    ml.info("Wechsle VWS Kanal zu {0}".format(channel))
    self._urlretrieve(self.vws + self.vws_chn + str(channel))

  def _urlretrieve(self,url,*args):
    ml.debug("URL Retrieve: \"{}\"".format(url))
    return urllib.urlopen(url,*args).read()

if __name__=='__main__':
  import argparse
  parser = argparse.ArgumentParser()
  parser.add_argument("-v","--verbose",type=int,default=1,help="Verbosity level. 0: Critical, 1: error, 2: warning, 3: info, 4: debug")
  args = parser.parse_args()

  ll = 10*(4-args.verbose)
  rootlogger = logging.getLogger('')
  rootlogger.setLevel(ll)
  ch = logging.StreamHandler()
  ch.setLevel(ll)
  formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  ch.setFormatter(formatter)
  rootlogger.addHandler(ch)

  e = vws()
  if not e.initialized:
    ml.info("Abbruch des Tests")
    raise SystemExit
  for channel in xrange(0,4):
    e.set_channel(channel)
    with open("chan{0}.jpg".format(channel),'wb') as f:
      f.write(e.download_image())
<|endoftext|>