【问题标题】:How can I add a version/build information to my project and setup.py?如何向我的项目和 setup.py 添加版本/构建信息?
【发布时间】:2014-12-09 15:12:24
【问题描述】:

severalquestions关于将版本字符串放入setup.py。我的要求略有不同:

  1. 我需要版本(例如1.2.3)字符串和内部版本号($ git describe --tags --always HEAD 的结果)
  2. 我需要在我的 Flask 应用程序中提供这些版本(例如 app.config['VERSION'] = '1.2.3'app.config['BUILD'] = '1.2.3-33-g93abc32'
  3. 我需要setup.py 中可用的版本字符串

我真的不在乎版本/构建字符串是否为under version control,尽管版本似乎在那里是有意义的。

所以我想问题可能是如何将一些构建信息从setup.py 注入到我的项目中?

【问题讨论】:

    标签: python version versioning setup.py


    【解决方案1】:

    我决定做的是滚动以下内容:

    setup.py:

    # Stuff
    
    import subprocess
    
    __version__ = '1.2.3'
    __build__ = subprocess.check_output('git describe --tags --always HEAD'
                                        .split()).decode().strip()
    
    with open('mypkg/_version.py', 'w') as f:
        f.write('''\
    # I will destroy any changes you make to this file.
    # Sincerely,
    # setup.py ;)
    
    __version__ = '{}'
    __build__ = '{}'
    '''.format(__version__, __build__))
    
    # other stuff
    
    settings.update(
        version=__version__,
    # more stuff
    )
    

    我可能会使用re 模块来简单地查找/替换__version____build__ 行,但现在这似乎工作得很好。

    【讨论】:

    • 我将有条件地生成 mypkg/__version__.py 作为创建源代码分发的一部分(在 sdist 命令之前的某处)。确保python setup.py develop 也运行它。数字本身可以转到 setup.cfg 以避免仅将 setup.py 更改为颠簸版本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-16
    • 1970-01-01
    相关资源
    最近更新 更多