def build_regex_win32():  # pragma: windows
    r"""Build the regex_win32 library using cmake."""
    # Configure project
    cmd = ['cmake', '-H.', '-Bbuild']
    comp_process = tools.popen_nobuffer(cmd, cwd=_regex_win32_dir)
    output, err = comp_process.communicate()
    exit_code = comp_process.returncode
    if exit_code != 0:  # pragma: debug
        print(' '.join(cmd))
        tools.print_encoded(output, end="")
        raise RuntimeError("Could not config regex_win32")
    # Build project
    cmd = ['cmake', '--build', 'build', '--clean-first']
    comp_process = tools.popen_nobuffer(cmd, cwd=_regex_win32_dir)
    output, err = comp_process.communicate()
    exit_code = comp_process.returncode
    if exit_code != 0:  # pragma: debug
        print(' '.join(cmd))
        tools.print_encoded(output, end="")
        raise RuntimeError("Could not build regex_win32")
    assert (os.path.isfile(_regex_win32_lib))
Example #2
0
def pprint_diff(x, y):  # pragma: no cover
    r"""Get the diff between the pprint.pformat string for two objects."""
    tools.print_encoded('\n'.join(
        difflib.ndiff(
            pprint.pformat(x).splitlines(),
            pprint.pformat(y).splitlines())))
Example #3
0
            # If the receive was not succesful, send the end-of-file message to
            # close the output file.
            print("End of file input (Python)")
            out_file.send_eof()

    # Read rows from ASCII table until end of file is reached.
    # As each row is received, it is then sent to the output ASCII table
    print('ascii_io(P): Receiving/sending ASCII table.')
    ret = True
    while ret:
        # Receive a single row
        (ret, line) = in_table.recv()
        if ret:
            # If the receive was succesful, send the values to output.
            # Formatting is taken care of on the output driver side.
            print_encoded("Table: %s, %d, %3.1f, %s" % tuple(line))
            # print("Table: %s, %d, %3.1f, %s" % line)
            ret = out_table.send(*line)
            if not ret:
                raise RuntimeError("ascii_io(P): ERROR SENDING ROW")
        else:
            # If the receive was not succesful, send the end-of-file message to
            # close the output file.
            print("End of table input (Python)")
            out_table.send_eof()

    # Read entire array from ASCII table into numpy array
    ret = True
    while ret:
        ret, arr = in_array.recv_array()
        if ret:
Example #4
0
    if args.yggdrasil:
        from yggdrasil.interface import YggInput, YggOutput
        from yggdrasil.units import add_units
        from yggdrasil.tools import print_encoded
        input = YggInput('photosynthesis_rate')
        output = YggOutput('growth_rate', '%f\n')

        while True:
            flag, prate = input.recv()
            if not flag:
                print('growth: No more input.')
                break
            grate = calculate_growth(*prate)
            grate *= add_units(1.0, "cm*m**2/umol")
            print_encoded(
                f'growth: photosynthesis rate = {prate[0]} ---> '
                f'growth rate = {grate}')
            flag = output.send(grate)
            if not flag:
                print('growth: Error sending growth rate.')
                sys.exit(-1)
    else:
        parser.add_argument('input_file', help='Input file.', nargs=1)
        parser.add_argument('output_file', help='Output file.', nargs=1)
        args = parser.parse_known_args()[0]

        input_fd = open(args.input_file[0], newline='')
        output_fd = open(args.output_file[0], 'w', newline='')

        output = []
        input_reader = csv.reader(filter(lambda row: row[0] != '#', input_fd),
Example #5
0
def call_link(obj,
              out=None,
              flags=[],
              overwrite=False,
              verbose=False,
              cpp=False,
              shared=False,
              static=False,
              working_dir=None):
    r"""Compile a source file, checking for errors.

    Args:
        obj (list): Object files that should be linked.
        out (str, optional): Full path to output file that should be created.
            If None, the path will be determined from the path to the first
            object file provided. Defaults to False.
        flags (list, optional): Compilation flags. Defaults to [].
        overwrite (bool, optional): If True, the existing compile file will be
            overwritten. Otherwise, it will be kept and this function will
            return without recompiling the source file.
        verbose (bool, optional): If True, the linking command and any output
            produced by the command will be displayed on success. Defaults to
            False.
        cpp (bool, optional): If True, value is returned assuming the source
            is written in C++. Defaults to False.
        shared (bool, optional): If True, the object files are combined into a
            shared library. Defaults to False.
        static (bool, optional): If True, the object files are combined into a
            static library. Defaults to False.
        working_dir (str, optional): Working directory that input file paths are
            relative to. Defaults to current working directory.

    Returns:
        str: Full path to compiled source.

    """
    # Set defaults
    if working_dir is None:
        working_dir = os.getcwd()
    flags = copy.deepcopy(flags)
    if not isinstance(obj, list):
        obj = [obj]
    # Set path if not provided
    if out is None:
        obase = os.path.splitext(obj[0])[0]
        if platform._is_win:  # pragma: windows
            oext = '.exe'
        else:
            oext = '.out'
        out = obase + oext
    if not os.path.isabs(out):
        out = os.path.normpath(os.path.join(working_dir, out))
    # Check for file
    if os.path.isfile(out):
        if overwrite:
            os.remove(out)
        else:
            return out
    # Check extension for information about the result
    if out.endswith('.so') or out.endswith('.dll') or out.endswith('.dylib'):
        shared = True
    elif out.endswith('.a') or out.endswith('.lib'):
        static = True
    # Get compiler
    cc = get_cc(shared=shared, static=static, cpp=cpp, linking=True)
    # Construct arguments
    args = [cc]
    if shared:
        if platform._is_win:  # pragma: windows
            flags.append('/DLL')
        elif platform._is_mac:
            flags.append('-dynamiclib')
        else:
            flags.append('-shared')
        args += flags
    elif static:
        if platform._is_win:  # pragma: windows
            pass
        elif platform._is_mac:
            flags += ['-static']
        else:
            flags += ['-rcs', out]
        args += flags
    if platform._is_win:  # pragma: windows
        args += ['/OUT:%s' % out]
    elif platform._is_mac:
        if shared:
            args += ["-o", out]
        else:
            args += ["-o", out]
    else:
        if static:
            args += [out]
        else:
            args += ["-o", out]
    args += obj
    if not (shared or static):
        args += flags
    # Call linker
    comp_process = tools.popen_nobuffer(args)
    output, err = comp_process.communicate()
    exit_code = comp_process.returncode
    if exit_code != 0:  # pragma: debug
        print(' '.join(args))
        tools.print_encoded(output, end="")
        raise RuntimeError("Linking of %s failed with code %d." %
                           (out, exit_code))
    if not os.path.isfile(out):  # pragma: debug
        print(' '.join(args))
        raise RuntimeError("Linking failed to produce result '%s'" % out)
    logging.info("Linked %s" % out)
    if verbose:  # pragma: debug
        print(' '.join(args))
        tools.print_encoded(output, end="")
    return out
Example #6
0
def call_compile(src,
                 out=None,
                 flags=[],
                 overwrite=False,
                 verbose=False,
                 cpp=None,
                 working_dir=None):
    r"""Compile a source file, checking for errors.

    Args:
        src (str): Full path to source file.
        out (str, optional): Full path to the output object file that should
            be created. Defaults to None and is created from the provided source
            file.
        flags (list, optional): Compilation flags. Defaults to [].
        overwrite (bool, optional): If True, the existing compile file will be
            overwritten. Otherwise, it will be kept and this function will
            return without recompiling the source file.
        verbose (bool, optional): If True, the compilation command and any output
            produced by the command will be displayed on success. Defaults to
            False.
        cpp (bool, optional): If True, value is returned assuming the source
            is written in C++. Defaults to False.
        working_dir (str, optional): Working directory that input file paths are
            relative to. Defaults to current working directory.

    Returns:
        str: Full path to compiled source.

    """
    # Set defaults
    if working_dir is None:
        working_dir = os.getcwd()
    flags = copy.deepcopy(flags)
    if platform._is_win:  # pragma: windows
        flags = ['/W4', '/Zi', "/EHsc"] + flags
    else:
        flags = ['-g', '-Wall'] + flags
    src_base, src_ext = os.path.splitext(src)
    if cpp is None:
        cpp = False
        if src_ext in ['.hpp', '.cpp']:
            cpp = True
    if platform._is_win:  # pragma: windows
        if cpp:
            flags.insert(2, '/TP')
        else:
            flags.insert(2, '/TP')
            # TODO: Currently everything compiled as C++ on windows to allow use of
            # complex types
            # flags.insert(2, '/TC')
    # Add standard library flag
    std_flag = None
    for i, a in enumerate(flags):
        if a.startswith('-std='):
            std_flag = i
            break
    if cpp and (not platform._is_win):
        if std_flag is None:
            flags.append('-std=c++11')
    else:
        if std_flag is not None:
            flags.pop(i)
    # Get compiler command
    cc = get_cc(cpp=cpp)
    # Get output if not provided
    if out is None:
        if platform._is_win:  # pragma: windows
            out_ext = '.obj'
        else:
            out_ext = '.o'
        out = src_base + '_' + src_ext[1:] + out_ext
    if not os.path.isabs(out):
        out = os.path.normpath(os.path.join(working_dir, out))
    # Construct arguments
    args = [cc, "-c"] + flags + [src]
    if not platform._is_win:
        args += ["-o", out]
    else:  # pragma: windows
        args.insert(1, '/Fo%s' % out)
    # Check for file
    if os.path.isfile(out):
        if overwrite:
            os.remove(out)
        else:
            return out
    # Call compiler
    comp_process = tools.popen_nobuffer(args)
    output, err = comp_process.communicate()
    exit_code = comp_process.returncode
    if exit_code != 0:  # pragma: debug
        print(' '.join(args))
        tools.print_encoded(output, end="")
        raise RuntimeError("Compilation of %s failed with code %d." %
                           (out, exit_code))
    if not os.path.isfile(out):  # pragma: debug
        print(' '.join(args))
        raise RuntimeError("Compilation failed to produce result '%s'" % out)
    logging.info("Compiled %s" % out)
    if verbose:  # pragma: debug
        print(' '.join(args))
        tools.print_encoded(output, end="")
    return out