您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

SCons:如何在scons脚本中调用自定义的python函数并进行正确的依赖

5b51 2022/1/14 8:21:14 python 字数 3294 阅读 520 来源 www.jb51.cc/python

我写了一个python函数,比如替换字符串,并在scons脚本中调用.def Replace(env, filename, old, new): with open(filename,'r+') as f: d = f.read() d = d.replace(old, new) f.truncate(0) f.see

概述

我写了一个python函数,比如替换字符串,并在scons脚本中调用.

def Replace(env,filename,old,new):
    with open(filename,"r+") as f:
    d = f.read()
    d = d.replace(old,new)
    f.truncate(0)
    f.seek(0)
    f.write(d)
    f.close()
env.AddMethod(Replace,'Replace')
@H_301_7@

在SConscript中

lib = env.SharedLibrary('lib',object,extra_libs)
tmp = env.Command([],[],[env.Replace(somefile,'A','b')] )
env.Depends(tmp,lib )
@H_301_7@

我期望在lib构建之后运行Replace()方法.
但是scons总是在第一轮脚本解析短语中运行Replace().
似乎我错过了一些依赖.

棘手的一点是,SCons并不真的想以你强迫它的方式工作.构建操作应该是可重复且非破坏性的,在您的代码中,您实际上正在破坏某些文件的原始内容.相反,您可以使用目标/源范例和某种模板文件来实现相同的结果.

import os
import re

def replace_action(target,source,env):
    # NB. this is a pretty sloppy way to write a builder,but
    #     for things that are used internally or infrequently
    #     it should be more than sufficient
    assert( len(target) == 1 )
    assert( len(source) == 1 )
    srcf = str(source[0])
    dstf = str(target[0])
    with open(srcf,"r") as f:
        contents = f.read()
        # In cases where this builder fails,check to make sure you
        # have correctly added REPLST to your environment
        for old,new in env['REPLST']:
            contents = re.sub( old,new,contents )
        with open( dstf,"w") as outf:
            outf.write(contents)

replace_builder = Builder(action = replace_action)

env = Environment( ENV = os.environ )
env.Append( BUILDERS = {'Replace' : replace_builder } )
b = env.Replace( 'somefile',['somefile.tmpl'],REPLST=[('A','b')] )
lib = env.SharedLibrary('lib',object + [b],extra_libs )
@H_301_7@

请注意,在我的测试中,替换函数与多行数据不能很好地协作,所以我只是交换使用完整的正则表达式(re.sub).这可能比较慢,但提供了更大的灵活性.

总结

以上是编程之家为你收集整理的SCons:如何在scons脚本中调用自定义的python函数并进行正确的依赖全部内容,希望文章能够帮你解决SCons:如何在scons脚本中调用自定义的python函数并进行正确的依赖所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶