Exemple #1
0
def _action(target, source, env):

    # prepare the line separator
    linesep = env['LINESEPARATOR']
    if linesep is None:
        linesep = LINESEP  # os.linesep
    elif is_String(linesep):
        pass
    elif isinstance(linesep, Value):
        linesep = linesep.get_text_contents()
    else:
        raise SCons.Errors.UserError(
            'unexpected type/class for LINESEPARATOR: %s' % repr(linesep),
            None)

    if 'b' in TEXTFILE_FILE_WRITE_MODE:
        linesep = to_bytes(linesep)

    # create a dictionary to use for the substitutions
    if 'SUBST_DICT' not in env:
        subs = None  # no substitutions
    else:
        subst_dict = env['SUBST_DICT']
        if is_Dict(subst_dict):
            subst_dict = list(subst_dict.items())
        elif is_Sequence(subst_dict):
            pass
        else:
            raise SCons.Errors.UserError('SUBST_DICT must be dict or sequence')
        subs = []
        for (k, value) in subst_dict:
            if callable(value):
                value = value()
            if is_String(value):
                value = env.subst(value)
            else:
                value = str(value)
            subs.append((k, value))

    # write the file
    try:
        if SCons.Util.PY3:
            target_file = open(target[0].get_path(),
                               TEXTFILE_FILE_WRITE_MODE,
                               newline='',
                               encoding="utf8")
        else:
            target_file = open(target[0].get_path(), TEXTFILE_FILE_WRITE_MODE)
    except (OSError, IOError):
        raise SCons.Errors.UserError("Can't write target file %s" % target[0])

    # separate lines by 'linesep' only if linesep is not empty
    lsep = None
    for line in source:
        if lsep:
            target_file.write(lsep)

        target_file.write(_do_subst(line, subs))
        lsep = linesep
    target_file.close()
Exemple #2
0
def _action(target, source, env):

    # prepare the line separator
    linesep = env['LINESEPARATOR']
    if linesep is None:
        linesep = LINESEP # os.linesep
    elif is_String(linesep):
        pass
    elif isinstance(linesep, Value):
        linesep = linesep.get_text_contents()
    else:
        raise SCons.Errors.UserError('unexpected type/class for LINESEPARATOR: %s'
                                     % repr(linesep), None)

    if 'b' in TEXTFILE_FILE_WRITE_MODE:
        linesep = to_bytes(linesep)

    # create a dictionary to use for the substitutions
    if 'SUBST_DICT' not in env:
        subs = None    # no substitutions
    else:
        subst_dict = env['SUBST_DICT']
        if is_Dict(subst_dict):
            subst_dict = list(subst_dict.items())
        elif is_Sequence(subst_dict):
            pass
        else:
            raise SCons.Errors.UserError('SUBST_DICT must be dict or sequence')
        subs = []
        for (k, value) in subst_dict:
            if callable(value):
                value = value()
            if is_String(value):
                value = env.subst(value)
            else:
                value = str(value)
            subs.append((k, value))

    # write the file
    try:
        if SCons.Util.PY3:
            target_file = open(target[0].get_path(), TEXTFILE_FILE_WRITE_MODE, newline='')
        else:
            target_file = open(target[0].get_path(), TEXTFILE_FILE_WRITE_MODE)
    except (OSError, IOError):
        raise SCons.Errors.UserError("Can't write target file %s" % target[0])

    # separate lines by 'linesep' only if linesep is not empty
    lsep = None
    for line in source:
        if lsep:
            target_file.write(lsep)

        target_file.write(_do_subst(line, subs))
        lsep = linesep
    target_file.close()
Exemple #3
0
 def _mix_kwargs(defaults, **kw):
     from SCons.Util import is_Dict
     if is_Dict(defaults):
         kw2 = defaults.copy()
         kw2.update(kw)
         return kw2
     elif defaults:
         return kw
     else:
         return None
Exemple #4
0
def _action(target, source, env):
    # prepare the line separator
    linesep = env['LINESEPARATOR']
    if linesep is None:
        linesep = os.linesep
    elif is_String(linesep):
        pass
    elif isinstance(linesep, Value):
        linesep = linesep.get_text_contents()
    else:
        raise SCons.Errors.UserError(
                           'unexpected type/class for LINESEPARATOR: %s'
                                         % repr(linesep), None)

    # create a dictionary to use for the substitutions
    if 'SUBST_DICT' not in env:
        subs = None    # no substitutions
    else:
        d = env['SUBST_DICT']
        if is_Dict(d):
            d = list(d.items())
        elif is_Sequence(d):
            pass
        else:
            raise SCons.Errors.UserError('SUBST_DICT must be dict or sequence')
        subs = []
        for (k,v) in d:
            if callable(v):
                v = v()
            if is_String(v):
                v = env.subst(v)
            else:
                v = str(v)
            subs.append((k,v))

    # write the file
    try:
        fd = open(target[0].get_path(), "wb")
    except (OSError, IOError, e):
        raise SCons.Errors.UserError("Can't write target file %s" % target[0])
    # separate lines by 'linesep' only if linesep is not empty
    lsep = None
    for s in source:
        if lsep: fd.write(lsep)
        fd.write(_do_subst(s, subs))
        lsep = linesep
    fd.close()
Exemple #5
0
def _action(target, source, env):
    # prepare the line separator
    linesep = env['LINESEPARATOR']
    if linesep is None:
        linesep = os.linesep
    elif is_String(linesep):
        pass
    elif isinstance(linesep, Value):
        linesep = linesep.get_text_contents()
    else:
        raise SCons.Errors.UserError(
            'unexpected type/class for LINESEPARATOR: %s' % repr(linesep),
            None)

    # create a dictionary to use for the substitutions
    if 'SUBST_DICT' not in env:
        subs = None  # no substitutions
    else:
        d = env['SUBST_DICT']
        if is_Dict(d):
            d = list(d.items())
        elif is_Sequence(d):
            pass
        else:
            raise SCons.Errors.UserError('SUBST_DICT must be dict or sequence')
        subs = []
        for (k, v) in d:
            if callable(v):
                v = v()
            if is_String(v):
                v = env.subst(v)
            else:
                v = str(v)
            subs.append((k, v))

    # write the file
    try:
        fd = open(target[0].get_path(), "wb")
    except (OSError, IOError) as e:
        raise SCons.Errors.UserError("Can't write target file %s" % target[0])
    # separate lines by 'linesep' only if linesep is not empty
    lsep = None
    for s in source:
        if lsep: fd.write(lsep)
        fd.write(_do_subst(s, subs))
        lsep = linesep
    fd.close()
Exemple #6
0
 def test_is_Dict(self):
     assert is_Dict({})
     assert is_Dict(UserDict())
     try:
         class mydict(dict):
             pass
     except TypeError:
         pass
     else:
         assert is_Dict(mydict({}))
     assert not is_Dict([])
     assert not is_Dict(())
     assert not is_Dict("")
Exemple #7
0
def compute_exports(exports):
    """Compute a dictionary of exports given one of the parameters
    to the Export() function or the exports argument to SConscript()."""

    loc, glob = get_calling_namespaces()

    retval = {}
    try:
        for export in exports:
            if is_Dict(export):
                retval.update(export)
            else:
                try:
                    retval[export] = loc[export]
                except KeyError:
                    retval[export] = glob[export]
    except KeyError as x:
        raise SCons.Errors.UserError("Export of non-existent variable '%s'"%x)

    return retval
Exemple #8
0
def _InjectObjectEmitters(env, **overrides):
    """Inject object emitters

    :Parameters:
        env : SCons.Environment.Environment
            SCons environment object
        overrides : dict
            Used to override environment construction variables

    Injects our `_GCovAwareObjectEmitter` to all the ``Object`` builders listed
    in ``GCCCOV_OBJECT_BUILDERS``.
    """
    from SCons.Util import is_Dict
    env = env.Override(overrides)
    if env.get('GCCCOV_DISABLE'):
        return
    org2new = {}
    builders = _get_object_builders(env)
    for builder in builders:
        if is_Dict(builder.emitter):
            emitters = builder.emitter
            suffixes = env.get('GCCCOV_SOURCE_SUFFIXES', emitters.keys())
            for sfx in suffixes:
                org_emitter = builder.emitter.get(sfx)
                if org_emitter and not isinstance(org_emitter,
                                                  _GCovAwareObjectEmitter):
                    if org_emitter in org2new:
                        emitters[sfx] = org2new[org_emitter]
                    else:
                        emitters[sfx] = _GCovAwareObjectEmitter(org_emitter)
                        org2new[org_emitter] = emitters[sfx]
        else:
            org_emitter = builder.emitter
            if not isinstance(org_emitter, _GCovAwareObjectEmitter):
                if org_emitter in org2new:
                    builder.emitter = org2new[org_emitter]
                else:
                    builder.emitter = _GCovAwareObjectEmitter(org_emitter)
                    org2new[org_emitter] = builder.emitter
def _InjectObjectEmitters(env, **overrides):
    """Inject object emitters

    :Parameters:
        env : SCons.Environment.Environment
            SCons environment object
        overrides : dict
            Used to override environment construction variables

    Injects our `_GCovAwareObjectEmitter` to all the ``Object`` builders listed
    in ``GCCCOV_OBJECT_BUILDERS``.
    """
    from SCons.Util import is_Dict
    env = env.Override(overrides)
    if env.get('GCCCOV_DISABLE'):
        return
    org2new = {}
    builders = _get_object_builders(env)
    for builder in builders:
        if is_Dict(builder.emitter):
            emitters = builder.emitter
            suffixes = env.get('GCCCOV_SOURCE_SUFFIXES', emitters.keys())
            for sfx in suffixes:
                org_emitter = builder.emitter.get(sfx)
                if org_emitter and not isinstance(org_emitter, _GCovAwareObjectEmitter):
                    if org_emitter in org2new:
                        emitters[sfx] = org2new[org_emitter]
                    else:
                        emitters[sfx] = _GCovAwareObjectEmitter(org_emitter)
                        org2new[org_emitter] = emitters[sfx]
        else:
            org_emitter = builder.emitter
            if not isinstance(org_emitter, _GCovAwareObjectEmitter):
                if org_emitter in org2new:
                    builder.emitter = org2new[org_emitter]
                else:
                    builder.emitter = _GCovAwareObjectEmitter(org_emitter)
                    org2new[org_emitter] = builder.emitter
Exemple #10
0
def _action(target, source, env):
    # prepare the line separator
    linesep = env["LINESEPARATOR"]
    if linesep is None:
        linesep = os.linesep
    elif is_String(linesep):
        pass
    elif isinstance(linesep, Value):
        linesep = linesep.get_text_contents()
    else:
        raise SCons.Errors.UserError("unexpected type/class for LINESEPARATOR: %s" % repr(linesep), None)

    # create a dictionary to use for the substitutions
    if "SUBST_DICT" not in env:
        subs = None  # no substitutions
    else:
        d = env["SUBST_DICT"]
        if is_Dict(d):
            d = list(d.items())
        elif is_Sequence(d):
            pass
        else:
            raise SCons.Errors.UserError("SUBST_DICT must be dict or sequence")
        subs = []
        for (k, v) in d:
            if callable(v):
                v = v()
            if is_String(v):
                v = env.subst(v)
            else:
                v = str(v)
            subs.append((k, v))

    # write the file
    try:
        fd = open(target[0].get_path(), "wb")
    except (OSError, IOError), e:
        raise SCons.Errors.UserError("Can't write target file %s" % target[0])