【问题标题】:Embed git hash into python file when installing安装时将 git hash 嵌入到 python 文件中
【发布时间】:2013-07-23 13:50:14
【问题描述】:

如果python模块是使用./setup.py install从git存储库安装的,我想将git哈希嵌入到python模块的版本号中。我怎么做?

我的想法是在setup.py 中定义一个函数来插入哈希并安排在安装程序将模块复制到其build/lib/ 目录时调用它,但在将其安装到最终目的地之前。有没有办法在那个时候挂钩到构建过程?

编辑:我知道如何从命令行获取当前版本的哈希值,我正在询问如何在构建/安装期间让这样的命令在正确的时间运行。

【问题讨论】:

  • 即兴发挥:git log -n 1 | grep commit 将是在项目根目录中执行的有用命令。
  • @SamStudio8:不是重复的,该问题询问如何在提交时获取哈希。我想在构建包时这样做,所以我问的是setup.py机械中的一个钩子。
  • @Droogans 我知道那部分,现在我该如何在正确的时间运行它?
  • @Somejan 问题中的第二个答案链接到相关的 setup.py?

标签: python git distutils


【解决方案1】:

另一种可能更简单的方法,使用gitpython,如dd/setup.py

from pkg_resources import parse_version  # part of `setuptools`


def git_version(version):
    """Return version with local version identifier."""
    import git
    repo = git.Repo('.git')
    repo.git.status()
    # assert versions are increasing
    latest_tag = repo.git.describe(
        match='v[0-9]*', tags=True, abbrev=0)
    assert parse_version(latest_tag) <= parse_version(version), (
        latest_tag, version)
    sha = repo.head.commit.hexsha
    if repo.is_dirty():
        return f'{version}.dev0+{sha}.dirty'
    # commit is clean
    # is it release of `version` ?
    try:
        tag = repo.git.describe(
            match='v[0-9]*', exact_match=True,
            tags=True, dirty=True)
    except git.GitCommandError:
        return f'{version}.dev0+{sha}'
    assert tag == f'v{version}', (tag, version)
    return version

参见https://github.com/tulip-control/tulip-control/pull/145的讨论

【讨论】:

    猜你喜欢
    • 2012-11-05
    • 2022-01-13
    • 2017-09-21
    • 2012-12-21
    • 2016-04-26
    • 2020-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多