Example #1
0
def show_fcompilers(dist=None):
    """Print list of available compilers (used by the "--help-fcompiler"
    option to "config_fc").
    """
    if dist is None:
        from distutils.dist import Distribution
        from numpy.distutils.command.config_compiler import config_fc
        dist = Distribution()
        dist.script_name = os.path.basename(sys.argv[0])
        dist.script_args = ['config_fc'] + sys.argv[1:]
        try:
            dist.script_args.remove('--help-fcompiler')
        except ValueError:
            pass
        dist.cmdclass['config_fc'] = config_fc
        dist.parse_config_files()
        dist.parse_command_line()
    compilers = []
    compilers_na = []
    compilers_ni = []
    if not fcompiler_class:
        load_all_fcompiler_classes()
    platform_compilers = available_fcompilers_for_platform()
    for compiler in platform_compilers:
        v = None
        log.set_verbosity(-2)
        try:
            c = new_fcompiler(compiler=compiler, verbose=dist.verbose)
            c.customize(dist)
            v = c.get_version()
        except (DistutilsModuleError, CompilerNotFound):
            e = get_exception()
            log.debug("show_fcompilers: %s not found" % (compiler,))
            log.debug(repr(e))

        if v is None:
            compilers_na.append(("fcompiler="+compiler, None,
                              fcompiler_class[compiler][2]))
        else:
            c.dump_properties()
            compilers.append(("fcompiler="+compiler, None,
                              fcompiler_class[compiler][2] + ' (%s)' % v))

    compilers_ni = list(set(fcompiler_class.keys()) - set(platform_compilers))
    compilers_ni = [("fcompiler="+fc, None, fcompiler_class[fc][2])
                    for fc in compilers_ni]

    compilers.sort()
    compilers_na.sort()
    compilers_ni.sort()
    pretty_printer = FancyGetopt(compilers)
    pretty_printer.print_help("Fortran compilers found:")
    pretty_printer = FancyGetopt(compilers_na)
    pretty_printer.print_help("Compilers available for this "
                              "platform, but not found:")
    if compilers_ni:
        pretty_printer = FancyGetopt(compilers_ni)
        pretty_printer.print_help("Compilers not available on this platform:")
    print("For compiler details, run 'config_fc --verbose' setup command.")
Example #2
0
def show_fcompilers(dist=None):
    """Print list of available compilers (used by the "--help-fcompiler"
    option to "config_fc").
    """
    if dist is None:
        from distutils.dist import Distribution
        from numpy.distutils.command.config_compiler import config_fc
        dist = Distribution()
        dist.script_name = os.path.basename(sys.argv[0])
        dist.script_args = ['config_fc'] + sys.argv[1:]
        try:
            dist.script_args.remove('--help-fcompiler')
        except ValueError:
            pass
        dist.cmdclass['config_fc'] = config_fc
        dist.parse_config_files()
        dist.parse_command_line()
    compilers = []
    compilers_na = []
    compilers_ni = []
    if not fcompiler_class:
        load_all_fcompiler_classes()
    platform_compilers = available_fcompilers_for_platform()
    for compiler in platform_compilers:
        v = None
        log.set_verbosity(-2)
        try:
            c = new_fcompiler(compiler=compiler, verbose=dist.verbose)
            c.customize(dist)
            v = c.get_version()
        except (DistutilsModuleError, CompilerNotFound):
            e = get_exception()
            log.debug("show_fcompilers: %s not found" % (compiler, ))
            log.debug(repr(e))

        if v is None:
            compilers_na.append(
                ("fcompiler=" + compiler, None, fcompiler_class[compiler][2]))
        else:
            c.dump_properties()
            compilers.append(("fcompiler=" + compiler, None,
                              fcompiler_class[compiler][2] + ' (%s)' % v))

    compilers_ni = list(set(fcompiler_class.keys()) - set(platform_compilers))
    compilers_ni = [("fcompiler=" + fc, None, fcompiler_class[fc][2])
                    for fc in compilers_ni]

    compilers.sort()
    compilers_na.sort()
    compilers_ni.sort()
    pretty_printer = FancyGetopt(compilers)
    pretty_printer.print_help("Fortran compilers found:")
    pretty_printer = FancyGetopt(compilers_na)
    pretty_printer.print_help("Compilers available for this "
                              "platform, but not found:")
    if compilers_ni:
        pretty_printer = FancyGetopt(compilers_ni)
        pretty_printer.print_help("Compilers not available on this platform:")
    print("For compiler details, run 'config_fc --verbose' setup command.")
Example #3
0
def update_extension(extension, requires_math=True):
    import numpy  # noqa: F811
    from numpy.distutils.log import set_verbosity
    from numpy.distutils.misc_util import get_info

    set_verbosity(1)

    numpy_includes = [numpy.get_include()]
    extra_incl = pkg_resources.resource_filename("numpy", "core/include")
    numpy_includes += [extra_incl]
    numpy_includes = list(set(numpy_includes))
    numpy_math_libs = get_info("npymath")

    if not hasattr(extension, "include_dirs"):
        return
    extension.include_dirs = list(set(extension.include_dirs + numpy_includes))
    if requires_math:
        extension.include_dirs += numpy_math_libs["include_dirs"]
        extension.libraries += numpy_math_libs["libraries"]
        extension.library_dirs += numpy_math_libs["library_dirs"]
Example #4
0
def show_fcompilers(dist=None):
    """Print list of available compilers (used by the "--help-fcompiler"
    option to "config_fc").
    """
    if dist is None:
        from distutils.dist import Distribution
        from numpy.distutils.command.config_compiler import config_fc
        dist = Distribution()
        dist.script_name = os.path.basename(sys.argv[0])
        dist.script_args = ['config_fc'] + sys.argv[1:]
        try:
            dist.script_args.remove('--help-fcompiler')
        except ValueError:
            pass
        dist.cmdclass['config_fc'] = config_fc
        dist.parse_config_files()
        dist.parse_command_line()
    compilers = []
    compilers_na = []
    compilers_ni = []
    if not fcompiler_class:
        load_all_fcompiler_classes()
    platform_compilers = available_fcompilers_for_platform()
    for compiler in platform_compilers:
        v = None
        log.set_verbosity(-2)
        try:
            c = new_fcompiler(compiler=compiler, verbose=dist.verbose)
            c.customize(dist)
            v = c.get_version()
        except (DistutilsModuleError, CompilerNotFound), e:
            log.debug("show_fcompilers: %s not found" % (compiler,))
            log.debug(repr(e))

        if v is None:
            compilers_na.append(("fcompiler="+compiler, None,
                              fcompiler_class[compiler][2]))
        else:
            c.dump_properties()
            compilers.append(("fcompiler="+compiler, None,
                              fcompiler_class[compiler][2] + ' (%s)' % v))
Example #5
0
def show_fcompilers(dist=None):
    """Print list of available compilers (used by the "--help-fcompiler"
    option to "config_fc").
    """
    if dist is None:
        from distutils.dist import Distribution
        from numpy.distutils.command.config_compiler import config_fc
        dist = Distribution()
        dist.script_name = os.path.basename(sys.argv[0])
        dist.script_args = ['config_fc'] + sys.argv[1:]
        try:
            dist.script_args.remove('--help-fcompiler')
        except ValueError:
            pass
        dist.cmdclass['config_fc'] = config_fc
        dist.parse_config_files()
        dist.parse_command_line()
    compilers = []
    compilers_na = []
    compilers_ni = []
    if not fcompiler_class:
        load_all_fcompiler_classes()
    platform_compilers = available_fcompilers_for_platform()
    for compiler in platform_compilers:
        v = None
        log.set_verbosity(-2)
        try:
            c = new_fcompiler(compiler=compiler, verbose=dist.verbose)
            c.customize(dist)
            v = c.get_version()
        except (DistutilsModuleError, CompilerNotFound), e:
            log.debug("show_fcompilers: %s not found" % (compiler, ))
            log.debug(repr(e))

        if v is None:
            compilers_na.append(
                ("fcompiler=" + compiler, None, fcompiler_class[compiler][2]))
        else:
            c.dump_properties()
            compilers.append(("fcompiler=" + compiler, None,
                              fcompiler_class[compiler][2] + ' (%s)' % v))
Example #6
0
    def _update_extensions(self):
        import numpy
        from numpy.distutils.misc_util import get_info
        from numpy.distutils.log import set_verbosity
        set_verbosity(1)

        numpy_includes = [numpy.get_include()]
        extra_incl = pkg_resources.resource_filename('numpy', 'core/include')
        numpy_includes += [extra_incl]
        numpy_includes = list(set(numpy_includes))
        numpy_math_libs = get_info('npymath')

        for extension in self.extensions:
            if not hasattr(extension, 'include_dirs'):
                continue
            extension.include_dirs = list(set(extension.include_dirs +
                                              numpy_includes))
            if extension.name in EXT_REQUIRES_NUMPY_MATH_LIBS:
                extension.include_dirs += numpy_math_libs['include_dirs']
                extension.libraries += numpy_math_libs['libraries']
                extension.library_dirs += numpy_math_libs['library_dirs']
Example #7
0
def show_fcompilers(dist = None):
    """ Print list of available compilers (used by the "--help-fcompiler"
    option to "config_fc").
    """
    if dist is None:
        from distutils.dist import Distribution
        dist = Distribution()
        dist.script_name = os.path.basename(sys.argv[0])
        dist.script_args = ['config_fc'] + sys.argv[1:]
        dist.cmdclass['config_fc'] = config_fc
        dist.parse_config_files()
        dist.parse_command_line()
    compilers = []
    compilers_na = []
    compilers_ni = []
    for compiler in fcompiler_class.keys():
        v = 'N/A'
        log.set_verbosity(-2)
        try:
            c = new_fcompiler(compiler=compiler)
            c.customize(dist)
            v = c.get_version()
        except DistutilsModuleError:
            pass
        except Exception, msg:
            log.warn(msg)

        if v is None:
            compilers_na.append(("fcompiler="+compiler, None,
                              fcompiler_class[compiler][2]))
        elif v=='N/A':
            compilers_ni.append(("fcompiler="+compiler, None,
                                 fcompiler_class[compiler][2]))
        else:
            compilers.append(("fcompiler="+compiler, None,
                              fcompiler_class[compiler][2] + ' (%s)' % v))
Example #8
0
    def run (self):
        fn = os.path.join(self.config_path, "Make.cfg")
        if os.path.isfile(fn) and os.path.isfile(self.config_h):
            print '*'*70
            print 'Files\n%s\n%s\n exist.' % (fn,self.config_h)
            print 'Skipping pygist configuration'\
                  ' (remove %s to force reconfiguration).' % fn
            print '*'*70
            return

        from numpy.distutils.log import set_verbosity
        from numpy.distutils.ccompiler import new_compiler
        save_verbosity = set_verbosity(-1)
        self.compiler = new_compiler(compiler=self.compiler,
                                     verbose=0)
        old_spawn = self.compiler.spawn
        self.compiler.spawn = self.spawn
        from distutils.sysconfig import customize_compiler
        customize_compiler(self.compiler)

        self.configfile = open(fn,'w')
        self.configfile.write('# Make.cfg from setup.py script ' + time.ctime() + '\n')
        if sys.platform != 'win32':
            self.configfile.write('#')
            for item in os.uname():
                self.configfile.write(' '+item)
            self.configfile.write('\n')

        self.config_toplevel()
        self.config_unix()
        self.config_x11()
        print 'wrote',fn
        self.configfile.close()

        set_verbosity(save_verbosity)
        self.compiler.spawn = old_spawn
def build_extension(module_path,compiler_name='',build_dir=None,
                    temp_dir=None, verbose=0, **kw):
    """ Build the file given by module_path into a Python extension module.

        build_extensions uses distutils to build Python extension modules.
        kw arguments not used are passed on to the distutils extension
        module.  Directory settings can handle absoulte settings, but don't
        currently expand '~' or environment variables.

        module_path   -- the full path name to the c file to compile.
                         Something like:  /full/path/name/module_name.c
                         The name of the c/c++ file should be the same as the
                         name of the module (i.e. the initmodule() routine)
        compiler_name -- The name of the compiler to use.  On Windows if it
                         isn't given, MSVC is used if it exists (is found).
                         gcc is used as a second choice. If neither are found,
                         the default distutils compiler is used. Acceptable
                         names are 'gcc', 'msvc' or any of the compiler names
                         shown by distutils.ccompiler.show_compilers()
        build_dir     -- The location where the resulting extension module
                         should be placed. This location must be writable.  If
                         it isn't, several default locations are tried.  If the
                         build_dir is not in the current python path, a warning
                         is emitted, and it is added to the end of the path.
                         build_dir defaults to the current directory.
        temp_dir      -- The location where temporary files (*.o or *.obj)
                         from the build are placed. This location must be
                         writable.  If it isn't, several default locations are
                         tried.  It defaults to tempfile.gettempdir()
        verbose       -- 0, 1, or 2.  0 is as quiet as possible. 1 prints
                         minimal information.  2 is noisy.
        **kw          -- keyword arguments. These are passed on to the
                         distutils extension module.  Most of the keywords
                         are listed below.

        Distutils keywords.  These are cut and pasted from Greg Ward's
        distutils.extension.Extension class for convenience:

        sources : [string]
          list of source filenames, relative to the distribution root
          (where the setup script lives), in Unix form (slash-separated)
          for portability.  Source files may be C, C++, SWIG (.i),
          platform-specific resource files, or whatever else is recognized
          by the "build_ext" command as source for a Python extension.
          Note: The module_path file is always appended to the front of this
                list
        include_dirs : [string]
          list of directories to search for C/C++ header files (in Unix
          form for portability)
        define_macros : [(name : string, value : string|None)]
          list of macros to define; each macro is defined using a 2-tuple,
          where 'value' is either the string to define it to or None to
          define it without a particular value (equivalent of "#define
          FOO" in source or -DFOO on Unix C compiler command line)
        undef_macros : [string]
          list of macros to undefine explicitly
        library_dirs : [string]
          list of directories to search for C/C++ libraries at link time
        libraries : [string]
          list of library names (not filenames or paths) to link against
        runtime_library_dirs : [string]
          list of directories to search for C/C++ libraries at run time
          (for shared extensions, this is when the extension is loaded)
        extra_objects : [string]
          list of extra files to link with (eg. object files not implied
          by 'sources', static library that must be explicitly specified,
          binary resource files, etc.)
        extra_compile_args : [string]
          any extra platform- and compiler-specific information to use
          when compiling the source files in 'sources'.  For platforms and
          compilers where "command line" makes sense, this is typically a
          list of command-line arguments, but for other platforms it could
          be anything.
        extra_link_args : [string]
          any extra platform- and compiler-specific information to use
          when linking object files together to create the extension (or
          to create a new static Python interpreter).  Similar
          interpretation as for 'extra_compile_args'.
        export_symbols : [string]
          list of symbols to be exported from a shared extension.  Not
          used on all platforms, and not generally necessary for Python
          extensions, which typically export exactly one symbol: "init" +
          extension_name.
    """
    success = 0
    from numpy.distutils.core import setup
    from numpy.distutils.log import set_verbosity
    set_verbosity(-1)

    # this is a screwy trick to get rid of a ton of warnings on Unix
    import distutils.sysconfig
    distutils.sysconfig.get_config_vars()
    if 'OPT' in distutils.sysconfig._config_vars:
        flags = distutils.sysconfig._config_vars['OPT']
        flags = flags.replace('-Wall','')
        distutils.sysconfig._config_vars['OPT'] = flags

    # get the name of the module and the extension directory it lives in.
    module_dir,cpp_name = os.path.split(os.path.abspath(module_path))
    module_name,ext = os.path.splitext(cpp_name)

    # configure temp and build directories
    temp_dir = configure_temp_dir(temp_dir)
    build_dir = configure_build_dir(module_dir)

    # dag. We keep having to add directories to the path to keep
    # object files separated from each other.  gcc2.x and gcc3.x C++
    # object files are not compatible, so we'll stick them in a sub
    # dir based on their version.  This will add an md5 check sum
    # of the compiler binary to the directory name to keep objects
    # from different compilers in different locations.

    compiler_dir = platform_info.get_compiler_dir(compiler_name)
    temp_dir = os.path.join(temp_dir,compiler_dir)
    distutils.dir_util.mkpath(temp_dir)

    compiler_name = choose_compiler(compiler_name)

    configure_sys_argv(compiler_name,temp_dir,build_dir)

    # the business end of the function
    try:
        if verbose == 1:
            print('Compiling code...')

        # set compiler verboseness 2 or more makes it output results
        if verbose > 1:
            verb = 1
        else:
            verb = 0

        t1 = time.time()
        ext = create_extension(module_path,**kw)
        # the switcheroo on SystemExit here is meant to keep command line
        # sessions from exiting when compiles fail.
        builtin = sys.modules['__builtin__']
        old_SysExit = builtin.__dict__['SystemExit']
        builtin.__dict__['SystemExit'] = CompileError

        # distutils for MSVC messes with the environment, so we save the
        # current state and restore them afterward.
        import copy
        environ = copy.deepcopy(os.environ)
        try:
            setup(name=module_name, ext_modules=[ext],verbose=verb)
        finally:
            # restore state
            os.environ = environ
            # restore SystemExit
            builtin.__dict__['SystemExit'] = old_SysExit
        t2 = time.time()

        if verbose == 1:
            print('finished compiling (sec): ', t2 - t1)
        success = 1
        configure_python_path(build_dir)
    except SyntaxError:  # TypeError:
        success = 0

    # restore argv after our trick...
    restore_sys_argv()

    return success
Example #10
0
def get_options(args=None, get_parser=False):
    import optparse
    import os
    from glob import glob
    import kmos

    parser = optparse.OptionParser(
        'Usage: %prog [help] ('
        + '|'.join(sorted(usage.keys()))
        + ') [options]',
        version=kmos.__version__)

    parser.add_option('-s', '--source-only',
                      dest='source_only',
                      action='store_true',
                      default=False)

    parser.add_option('-p', '--path-to-f2py',
                      dest='path_to_f2py',
                      default='f2py')

    parser.add_option('-b', '--backend',
                      dest='backend',
                      default='local_smart')

    parser.add_option('-v', '--steps-per-frame',
                      dest='steps_per_frame',
                      type='int',
                      default='50000')

    parser.add_option('-d', '--debug',
                      default=False,
                      dest='debug',
                      action='store_true')
    parser.add_option('-n', '--no-compiler-optimization',
                      default=False,
                      dest='no_optimize',
                      action='store_true')

    try:
        from numpy.distutils.fcompiler import get_default_fcompiler
        from numpy.distutils import log
        log.set_verbosity(-1, True)
        fcompiler = get_default_fcompiler()
    except:
        fcompiler = 'gfortran'

    parser.add_option('-f', '--fcompiler',
                      dest='fcompiler',
                      default=os.environ.get('F2PY_FCOMPILER', fcompiler))

    if args is not None:
        options, args = parser.parse_args(args.split())
    else:
        options, args = parser.parse_args()
    if len(args) < 1:
        parser.error('Command expected')
    if get_parser:
        return options, args, parser
    else:
        return options, args
Example #11
0
    libraries=[] if isWindows() else ['m'])
extensions = [grib_ext]

# build native executables - have to get hands a little dirty
grib_sources = [path.join(here, *x) for x in [
    ('src', 'wgrib.c'),
]]
grib_exe = 'wgrib'

if 'build_ext' in sys.argv:
    try:
        from numpy.distutils import ccompiler, fcompiler, log

        # get compilers
        cc = ccompiler.new_compiler()
        log.set_verbosity(1)  # show compilation commands

        # build sources
        print('\nBuilding wgrib...')
        if not isWindows():
            # clunky hack to force position independent code on *nix systems
            for var in ['CFLAGS', 'FFLAGS', 'LDFLAGS']:
                flags = os.environ.get(var, '-fPIC')
                flags += ' -fPIC' if '-fPIC' not in flags else ''
                os.environ[var] = flags

        try:
            grib_objs = cc.compile(list(map(fix_path, grib_sources)),
                                   output_dir=gettempdir())
            cc.link_executable(grib_objs,
                               grib_exe,
Example #12
0
def build_extension(module_path,
                    compiler_name='',
                    build_dir=None,
                    temp_dir=None,
                    verbose=0,
                    **kw):
    """ Build the file given by module_path into a Python extension module.

        build_extensions uses distutils to build Python extension modules.
        kw arguments not used are passed on to the distutils extension
        module.  Directory settings can handle absoulte settings, but don't
        currently expand '~' or environment variables.

        module_path   -- the full path name to the c file to compile.
                         Something like:  /full/path/name/module_name.c
                         The name of the c/c++ file should be the same as the
                         name of the module (i.e. the initmodule() routine)
        compiler_name -- The name of the compiler to use.  On Windows if it
                         isn't given, MSVC is used if it exists (is found).
                         gcc is used as a second choice. If neither are found,
                         the default distutils compiler is used. Acceptable
                         names are 'gcc', 'msvc' or any of the compiler names
                         shown by distutils.ccompiler.show_compilers()
        build_dir     -- The location where the resulting extension module
                         should be placed. This location must be writable.  If
                         it isn't, several default locations are tried.  If the
                         build_dir is not in the current python path, a warning
                         is emitted, and it is added to the end of the path.
                         build_dir defaults to the current directory.
        temp_dir      -- The location where temporary files (*.o or *.obj)
                         from the build are placed. This location must be
                         writable.  If it isn't, several default locations are
                         tried.  It defaults to tempfile.gettempdir()
        verbose       -- 0, 1, or 2.  0 is as quiet as possible. 1 prints
                         minimal information.  2 is noisy.
        **kw          -- keyword arguments. These are passed on to the
                         distutils extension module.  Most of the keywords
                         are listed below.

        Distutils keywords.  These are cut and pasted from Greg Ward's
        distutils.extension.Extension class for convenience:

        sources : [string]
          list of source filenames, relative to the distribution root
          (where the setup script lives), in Unix form (slash-separated)
          for portability.  Source files may be C, C++, SWIG (.i),
          platform-specific resource files, or whatever else is recognized
          by the "build_ext" command as source for a Python extension.
          Note: The module_path file is always appended to the front of this
                list
        include_dirs : [string]
          list of directories to search for C/C++ header files (in Unix
          form for portability)
        define_macros : [(name : string, value : string|None)]
          list of macros to define; each macro is defined using a 2-tuple,
          where 'value' is either the string to define it to or None to
          define it without a particular value (equivalent of "#define
          FOO" in source or -DFOO on Unix C compiler command line)
        undef_macros : [string]
          list of macros to undefine explicitly
        library_dirs : [string]
          list of directories to search for C/C++ libraries at link time
        libraries : [string]
          list of library names (not filenames or paths) to link against
        runtime_library_dirs : [string]
          list of directories to search for C/C++ libraries at run time
          (for shared extensions, this is when the extension is loaded)
        extra_objects : [string]
          list of extra files to link with (eg. object files not implied
          by 'sources', static library that must be explicitly specified,
          binary resource files, etc.)
        extra_compile_args : [string]
          any extra platform- and compiler-specific information to use
          when compiling the source files in 'sources'.  For platforms and
          compilers where "command line" makes sense, this is typically a
          list of command-line arguments, but for other platforms it could
          be anything.
        extra_link_args : [string]
          any extra platform- and compiler-specific information to use
          when linking object files together to create the extension (or
          to create a new static Python interpreter).  Similar
          interpretation as for 'extra_compile_args'.
        export_symbols : [string]
          list of symbols to be exported from a shared extension.  Not
          used on all platforms, and not generally necessary for Python
          extensions, which typically export exactly one symbol: "init" +
          extension_name.
    """
    success = 0
    from numpy.distutils.log import set_verbosity
    set_verbosity(-1)

    # get the name of the module and the extension directory it lives in.
    module_dir, cpp_name = os.path.split(os.path.abspath(module_path))
    module_name, ext = os.path.splitext(cpp_name)

    # configure temp and build directories
    temp_dir = configure_temp_dir(temp_dir)
    build_dir = configure_build_dir(build_dir or module_dir)

    # dag. We keep having to add directories to the path to keep
    # object files separated from each other.  gcc2.x and gcc3.x C++
    # object files are not compatible, so we'll stick them in a sub
    # dir based on their version. This will add a SHA-256 check sum
    # (truncated to 32 characters) of the compiler binary to the directory
    # name to keep objects from different compilers in different locations.

    compiler_dir = platform_info.get_compiler_dir(compiler_name)
    temp_dir = os.path.join(temp_dir, compiler_dir)
    distutils.dir_util.mkpath(temp_dir)

    compiler_name = choose_compiler(compiler_name)

    # the business end of the function
    try:
        if verbose == 1:
            print('Compiling code...')

        # set compiler verboseness 2 or more makes it output results
        from distutils import log
        log.set_verbosity(1 if verbose > 1 else 0)

        t1 = time.time()
        ext = create_extension(module_path, **kw)

        # change current working directory to 'build_dir' so compiler won't
        # pick up anything by mistake
        oldcwd = os.path.abspath(os.getcwd())
        os.chdir(build_dir)

        # distutils for MSVC messes with the environment, so we save the
        # current state and restore them afterward.
        import copy
        environ = copy.deepcopy(os.environ)
        try:
            attr = {
                'name': module_name,
                'ext_modules': [ext],
                'verbose': 1 if verbose > 1 else 0,
                'cmdclass': numpy_cmdclass.copy()
            }
            dist = NumpyDistribution(attr)
            dist.parse_config_files()
            options = dist.get_option_dict('build_ext')
            source = "weave.build_tools"
            options['build_lib'] = (source, build_dir)
            options['build_temp'] = (source, temp_dir)
            if compiler_name:
                options['compiler'] = (source, compiler_name)
            dist.run_command('build_ext')
        except Exception as exc:
            raise SystemExit("error: %s" % (exc, ))
        finally:
            # restore state
            os.environ = environ
            # restore working directory to one before setup
            os.chdir(oldcwd)
        t2 = time.time()

        if verbose == 1:
            print('finished compiling (sec): ', t2 - t1)
        success = 1
        configure_python_path(build_dir)
    except SyntaxError:  # TypeError:
        success = 0

    return success
Example #13
0
def setup_module():
    f = io.StringIO()  # changing verbosity also logs here, capture that
    with redirect_stdout(f):
        log.set_verbosity(2, force=True)  # i.e. DEBUG
Example #14
0
def setup_module():
    log.set_verbosity(2, force=True)  # i.e. DEBUG
Example #15
0
def get_options(args=None, get_parser=False):
    import optparse
    import os
    from glob import glob
    import kmos

    parser = optparse.OptionParser('Usage: %prog [help] (' +
                                   '|'.join(sorted(usage.keys())) +
                                   ') [options]',
                                   version=kmos.__version__)

    parser.add_option('-s',
                      '--source-only',
                      dest='source_only',
                      action='store_true',
                      default=False)

    parser.add_option('-p',
                      '--path-to-f2py',
                      dest='path_to_f2py',
                      default='f2py')

    parser.add_option('-b', '--backend', dest='backend', default='local_smart')
    parser.add_option(
        '-a',
        '--avoid-default-state',
        dest='avoid_default_state',
        action='store_true',
        default=False,
    )

    parser.add_option('-v',
                      '--steps-per-frame',
                      dest='steps_per_frame',
                      type='int',
                      default='50000')

    parser.add_option('-d',
                      '--debug',
                      default=False,
                      dest='debug',
                      action='store_true')

    parser.add_option('-n',
                      '--no-compiler-optimization',
                      default=False,
                      dest='no_optimize',
                      action='store_true')

    parser.add_option('-o', '--overwrite', default=False, action='store_true')

    parser.add_option('-l',
                      '--variable-length',
                      dest='variable_length',
                      default=95,
                      type='int')

    parser.add_option('-c', '--catmap', default=False, action='store_true')

    parser.add_option(
        '--acf',
        dest='acf',
        action='store_true',
        default=False,
    )

    try:
        from numpy.distutils.fcompiler import get_default_fcompiler
        from numpy.distutils import log
        log.set_verbosity(-1, True)
        fcompiler = get_default_fcompiler()
    except:
        fcompiler = 'gfortran'

    parser.add_option('-f',
                      '--fcompiler',
                      dest='fcompiler',
                      default=os.environ.get('F2PY_FCOMPILER', fcompiler))

    if args is not None:
        options, args = parser.parse_args(args.split())
    else:
        options, args = parser.parse_args()
    if len(args) < 1:
        parser.error('Command expected')
    if get_parser:
        return options, args, parser
    else:
        return options, args
Example #16
0
"""Ising: a Python package for exactly solving abritrary Ising model instances using exhaustive search."""

from setuptools import find_packages # pylint: disable=unused-import
from numpy.distutils.core import setup, Extension
from numpy.distutils.log import set_verbosity
from setup_helpers import BuildExtCommand, find_cuda_home

set_verbosity(1)

with open('README.rst') as readme:
    LONG_DESCRIPTION = readme.read()

try:
    find_cuda_home()
    CPP_EXT = '.cu'
except ValueError:
    CPP_EXT = '.cpp'

CPU_SEARCH_EXT = Extension('isingcpu',
                           extra_compile_args=[
                               '-fPIC',
                               '-fopenmp',
                               '-DTHRUST_DEVICE_SYSTEM=THRUST_DEVICE_SYSTEM_OMP',
                               '-lstdc++'],
                           sources=[
                               './ising/ext_sources/bucketSelectCPU' + CPP_EXT,
                               './ising/ext_sources/bucketselectcpu.f90',
                               './ising/ext_sources/cpucsort' + CPP_EXT,
                               './ising/ext_sources/cpu_thrust_sort.f90',
                               './ising/ext_sources/cpusearch.pyf',
                               './ising/ext_sources/cpusearch.f90'])
Example #17
0
def build_extensions(pkg):
    if not pkg.extensions:
        return {}

    # XXX: import here because numpy import time slows down everything
    # otherwise. This is ugly, but using numpy.distutils is temporary
    # anyway
    try:
        import numpy
        use_numpy_distutils = True
    except ImportError:
        use_numpy_distutils = False

    # FIXME: import done here to avoid clashing with monkey-patch as done by
    # the convert subcommand.
    if use_numpy_distutils:
        from numpy.distutils.numpy_distribution \
            import \
                NumpyDistribution as Distribution
        from numpy.distutils.command.build_ext \
            import \
                build_ext
        from numpy.distutils.command.build_src \
            import \
                build_src
        from numpy.distutils.command.scons \
            import \
                scons
        from numpy.distutils import log
        import distutils.core
    else:
        from distutils.dist \
            import \
                Distribution
        from distutils.command.build_ext \
            import \
                build_ext
        from distutils import log
    import distutils.errors

    log.set_verbosity(1)

    dist = Distribution()
    if use_numpy_distutils:
        dist.cmdclass['build_src'] = build_src
        dist.cmdclass['scons'] = scons
        distutils.core._setup_distribution = dist

    dist.ext_modules = [toyext_to_distext(e) for e in
                        pkg.extensions.values()]

    try:
        bld_cmd = build_ext(dist)
        bld_cmd.initialize_options()
        bld_cmd.finalize_options()
        bld_cmd.run()

        ret = {}
        for ext in bld_cmd.extensions:
            # FIXME: do package -> location translation correctly
            pkg_dir = os.path.dirname(ext.name.replace('.', os.path.sep))
            target = os.path.join('$sitedir', pkg_dir)
            fullname = bld_cmd.get_ext_fullname(ext.name)
            ext_target = os.path.join(bld_cmd.build_lib,
                                     bld_cmd.get_ext_filename(fullname))
            srcdir = os.path.dirname(ext_target)
            section = InstalledSection("extensions", fullname, srcdir,
                                        target, [os.path.basename(ext_target)])
            ret[fullname] = section
        return ret
    except distutils.errors.DistutilsError, e:
        raise CommandExecutionFailure(str(e))
Example #18
0
File: cli.py Project: lulzzz/kmos
def main(args=None):
    """The CLI main entry point function.

    The optional arguemnts args, can be used to
    directly supply command line argument like

    $ kmos <args>

    otherwise args will be taken from STDIN.

    """

    import optparse
    import os
    from glob import glob
    import kmos

    parser = optparse.OptionParser(
        'Usage: %prog [help] ('
        + '|'.join(sorted(usage.keys()))
        + ') [options]',
        version=kmos.__version__)

    parser.add_option('-s', '--source-only',
                      dest='source_only',
                      action='store_true',
                      default=False)

    parser.add_option('-p', '--path-to-f2py',
                      dest='path_to_f2py',
                      default='f2py')

    try:
        from numpy.distutils.fcompiler import get_default_fcompiler
        from numpy.distutils import log
        log.set_verbosity(-1, True)
        fcompiler = get_default_fcompiler()
    except:
        fcompiler = 'gfortran'

    parser.add_option('-f', '--fcompiler',
                     dest='fcompiler',
                     default=os.environ.get('F2PY_FCOMPILER', fcompiler ))

    if args is not None:
        options, args = parser.parse_args(args.split())
    else:
        options, args = parser.parse_args()

    if len(args) < 1:
        parser.error('Command expected')

    if args[0] == 'benchmark':
        from sys import path
        path.append(os.path.abspath(os.curdir))
        nsteps = 1000000
        from time import time
        from kmos.run import KMC_Model
        with KMC_Model(print_rates=False, banner=False) as model:
            time0 = time()
            model.do_steps(nsteps)
            needed_time = time() - time0
            print('%s steps took %.2f seconds' % (nsteps, needed_time))
            print('Or %.2e steps/s' % (1e6 / needed_time))
    elif args[0] == 'build':
        from kmos.utils import build
        build(options)
    elif args[0] == 'edit':
        from kmos import gui
        gui.main()
    elif args[0] == 'export-settings':
        import kmos.types
        import kmos.io
        from kmos.utils import build
        import shutil
        from kmos.io import ProcListWriter

        if len(args) < 2:
            parser.error('XML file and export path expected.')
        if len(args) < 3:
            out_dir = os.path.splitext(args[1])[0]
            print('No export path provided. Exporting to %s' % out_dir)
            args.append(out_dir)

        xml_file = args[1]
        export_dir = args[2]
        project = kmos.types.Project()
        project.import_xml_file(xml_file)

        writer = ProcListWriter(project, export_dir)
        writer.write_settings()

    elif args[0] == 'export':
        import kmos.types
        import kmos.io
        from kmos.utils import build
        import shutil
        if len(args) < 2:
            parser.error('XML file and export path expected.')
        if len(args) < 3:
            out_dir = os.path.splitext(args[1])[0]
            print('No export path provided. Exporting to %s' % out_dir)
            args.append(out_dir)

        xml_file = args[1]
        export_dir = os.path.join(args[2], 'src')

        project = kmos.types.Project()
        project.import_xml_file(xml_file)

        kmos.io.export_source(project, export_dir)

        if ((os.name == 'posix'
           and os.uname()[0] == 'Linux')
           or os.name == 'nt') \
           and not options.source_only:
            os.chdir(export_dir)
            build(options)
            for out in glob('kmc_*'):
                if os.path.exists('../%s' % out):
                    overwrite = raw_input(('Should I overwrite existing %s ?'
                                           '[y/N]  ') % out).lower()
                    if overwrite.startswith('y'):
                        os.remove('../%s' % out)
                        shutil.move(out, '..')
                else:
                    shutil.move(out, '..')

    elif args[0] == 'export-settings':
        import kmos.io
        pt = kmos.io.import_xml_file(args[1])
        if len(args) < 3:
            out_dir = os.path.splitext(args[1])[0]
            print('No export path provided. Exporting kmc_settings.py to %s'
                   % out_dir)
            args.append(out_dir)

        if not os.path.exists(args[2]):
            os.mkdir(args[2])
        elif not os.path.isdir(args[2]):
            raise UserWarning("Cannot overwrite %s; Exiting;" % args[2])
        writer = kmos.io.ProcListWriter(pt, args[2])
        writer.write_settings()

    elif args[0] == 'export-view':
        out_dir = os.path.splitext(args[1])[0]
        print('No export path provided. Exporting to %s' % out_dir)
        args.append(out_dir)
        os.system('kmos-export-program %s %s' % (args[1], args[2]))
        os.chdir(out_dir)
        main('view')

    elif args[0] == 'all':
        print('Interpreting: all -> help all\n\n')
        main('help all')
    elif args[0] == 'help':
        if len(args) < 2:
            parser.error('Which help do you  want?')
        if args[1] == 'all':
            for command in sorted(usage):
                print(usage[command])
        elif args[1] in usage:
            print('Usage: %s\n' % usage[args[1]])
        else:
            raise Exception('Command "%s" not known or not documented.'
                            % args[1])

    elif args[0] == 'import':
        import kmos.io
        if not len(args) >= 2:
            raise UserWarning('XML file name expected.')
        global pt
        pt = kmos.io.import_xml_file(args[1])
        sh(banner='Note: pt = kmos.io.import_xml(\'%s\')' % args[1])

    elif args[0] == 'rebuild':
        from sys import path
        path.append(os.path.abspath(os.curdir))
        from tempfile import mktemp
        if not os.path.exists('kmc_model.so') \
           and not os.path.exists('kmc_model.pyd'):
            raise Exception('No kmc_model.so found.')
        if not os.path.exists('kmc_settings.py'):
            raise Exception('No kmc_settings.py found.')

        from kmos.run import KMC_Model

        with KMC_Model(print_rates=False, banner=False) as model:
            tempfile = mktemp()
            f = file(tempfile, 'w')
            f.write(model.xml())
            f.close()

            for kmc_model in glob('kmc_model.*'):
                os.remove(kmc_model)
            os.remove('kmc_settings.py')
            main('export %s .' % tempfile)
            os.remove(tempfile)

    elif args[0] == 'run':
        from sys import path
        path.append(os.path.abspath(os.curdir))
        from kmos.run import KMC_Model

        with KMC_Model(print_rates=False) as model:
            global model
            sh(banner='Note: model = KMC_Model(print_rates=False)')

    elif args[0] == 'view':
        from sys import path
        path.append(os.path.abspath(os.curdir))
        from kmos import view
        view.main()

    else:
        parser.error('Command "%s" not understood.' % args[0])
Example #19
0
def teardown_module():
    log.set_verbosity(0, force=True)  # the default
Example #20
0
def main(args=None):
    """The CLI main entry point function.

    The optional arguemnts args, can be used to
    directly supply command line argument like

    $ kmos <args>

    otherwise args will be taken from STDIN.

    """

    import optparse
    import os
    from glob import glob
    import kmos

    parser = optparse.OptionParser('Usage: %prog [help] (' +
                                   '|'.join(sorted(usage.keys())) +
                                   ') [options]',
                                   version=kmos.__version__)

    parser.add_option('-s',
                      '--source-only',
                      dest='source_only',
                      action='store_true',
                      default=False)

    parser.add_option('-p',
                      '--path-to-f2py',
                      dest='path_to_f2py',
                      default='f2py')

    try:
        from numpy.distutils.fcompiler import get_default_fcompiler
        from numpy.distutils import log
        log.set_verbosity(-1, True)
        fcompiler = get_default_fcompiler()
    except:
        fcompiler = 'gfortran'

    parser.add_option('-f',
                      '--fcompiler',
                      dest='fcompiler',
                      default=os.environ.get('F2PY_FCOMPILER', fcompiler))

    if args is not None:
        options, args = parser.parse_args(args.split())
    else:
        options, args = parser.parse_args()

    if len(args) < 1:
        parser.error('Command expected')

    if args[0] == 'benchmark':
        from sys import path
        path.append(os.path.abspath(os.curdir))
        nsteps = 1000000
        from time import time
        from kmos.run import KMC_Model
        with KMC_Model(print_rates=False, banner=False) as model:
            time0 = time()
            model.do_steps(nsteps)
            needed_time = time() - time0
            print('%s steps took %.2f seconds' % (nsteps, needed_time))
            print('Or %.2e steps/s' % (1e6 / needed_time))
    elif args[0] == 'build':
        from kmos.utils import build
        build(options)
    elif args[0] == 'edit':
        from kmos import gui
        gui.main()
    elif args[0] == 'export-settings':
        import kmos.types
        import kmos.io
        from kmos.utils import build
        import shutil
        from kmos.io import ProcListWriter

        if len(args) < 2:
            parser.error('XML file and export path expected.')
        if len(args) < 3:
            out_dir = os.path.splitext(args[1])[0]
            print('No export path provided. Exporting to %s' % out_dir)
            args.append(out_dir)

        xml_file = args[1]
        export_dir = args[2]
        project = kmos.types.Project()
        project.import_xml_file(xml_file)

        writer = ProcListWriter(project, export_dir)
        writer.write_settings()

    elif args[0] == 'export':
        import kmos.types
        import kmos.io
        from kmos.utils import build
        import shutil
        if len(args) < 2:
            parser.error('XML file and export path expected.')
        if len(args) < 3:
            out_dir = os.path.splitext(args[1])[0]
            print('No export path provided. Exporting to %s' % out_dir)
            args.append(out_dir)

        xml_file = args[1]
        export_dir = os.path.join(args[2], 'src')

        project = kmos.types.Project()
        project.import_xml_file(xml_file)

        kmos.io.export_source(project, export_dir)

        if ((os.name == 'posix'
           and os.uname()[0] == 'Linux')
           or os.name == 'nt') \
           and not options.source_only:
            os.chdir(export_dir)
            build(options)
            for out in glob('kmc_*'):
                if os.path.exists('../%s' % out):
                    overwrite = raw_input(('Should I overwrite existing %s ?'
                                           '[y/N]  ') % out).lower()
                    if overwrite.startswith('y'):
                        os.remove('../%s' % out)
                        shutil.move(out, '..')
                else:
                    shutil.move(out, '..')

    elif args[0] == 'export-settings':
        import kmos.io
        pt = kmos.io.import_xml_file(args[1])
        if len(args) < 3:
            out_dir = os.path.splitext(args[1])[0]
            print('No export path provided. Exporting kmc_settings.py to %s' %
                  out_dir)
            args.append(out_dir)

        if not os.path.exists(args[2]):
            os.mkdir(args[2])
        elif not os.path.isdir(args[2]):
            raise UserWarning("Cannot overwrite %s; Exiting;" % args[2])
        writer = kmos.io.ProcListWriter(pt, args[2])
        writer.write_settings()

    elif args[0] == 'export-view':
        out_dir = os.path.splitext(args[1])[0]
        print('No export path provided. Exporting to %s' % out_dir)
        args.append(out_dir)
        os.system('kmos-export-program %s %s' % (args[1], args[2]))
        os.chdir(out_dir)
        main('view')

    elif args[0] == 'all':
        print('Interpreting: all -> help all\n\n')
        main('help all')
    elif args[0] == 'help':
        if len(args) < 2:
            parser.error('Which help do you  want?')
        if args[1] == 'all':
            for command in sorted(usage):
                print(usage[command])
        elif args[1] in usage:
            print('Usage: %s\n' % usage[args[1]])
        else:
            raise Exception('Command "%s" not known or not documented.' %
                            args[1])

    elif args[0] == 'import':
        import kmos.io
        if not len(args) >= 2:
            raise UserWarning('XML file name expected.')
        global pt
        pt = kmos.io.import_xml_file(args[1])
        sh(banner='Note: pt = kmos.io.import_xml(\'%s\')' % args[1])

    elif args[0] == 'rebuild':
        from sys import path
        path.append(os.path.abspath(os.curdir))
        from tempfile import mktemp
        if not os.path.exists('kmc_model.so') \
           and not os.path.exists('kmc_model.pyd'):
            raise Exception('No kmc_model.so found.')
        if not os.path.exists('kmc_settings.py'):
            raise Exception('No kmc_settings.py found.')

        from kmos.run import KMC_Model

        with KMC_Model(print_rates=False, banner=False) as model:
            tempfile = mktemp()
            f = file(tempfile, 'w')
            f.write(model.xml())
            f.close()

            for kmc_model in glob('kmc_model.*'):
                os.remove(kmc_model)
            os.remove('kmc_settings.py')
            main('export %s .' % tempfile)
            os.remove(tempfile)

    elif args[0] == 'run':
        from sys import path
        path.append(os.path.abspath(os.curdir))
        from kmos.run import KMC_Model

        with KMC_Model(print_rates=False) as model:
            global model
            sh(banner='Note: model = KMC_Model(print_rates=False)')

    elif args[0] == 'view':
        from sys import path
        path.append(os.path.abspath(os.curdir))
        from kmos import view
        view.main()

    else:
        parser.error('Command "%s" not understood.' % args[0])