从源文件获取conda meta.yaml的软件包版本 [英] Get package version for conda meta.yaml from source file

查看:201
本文介绍了从源文件获取conda meta.yaml的软件包版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试重新组织python软件包的版本控制,因此我只需要在一个地方(最好是python模块或文本文件)中更新版本即可。对于我需要我的版本的所有地方,似乎都有一种方法可以从mypkg import __version __ 的源加载它,或至少将其解析为文本。不过,我似乎找不到用conda meta.yaml文件执行此操作的方法。有没有办法从meta.yaml文件中的外部源加载版本?

I'm trying to reorganize my python package versioning so I only have to update the version in one place, preferably a python module or a text file. For all the places I need my version there seems to be a way to load it from the source from mypkg import __version__ or at least parse it out of the file as text. I can't seem to find a way to do it with my conda meta.yaml file though. Is there a way to load the version from an external source in the meta.yaml file?

我知道有git环境变量,但是我不想标记通过本地conda存储库进行测试的每个alpha / beta / rc提交。我可以在pyyaml中使用 !! python / object 加载python对象,但是conda不支持任意python执行。我看不到任何其他jinja2功能都可以做到的方法。我也可以编写一个脚本来在多个位置更新版本号,但是我真的希望只修改一个文件作为最终的版本号。谢谢您的帮助。

I know there are the git environment variables, but I don't want to tag every alpha/beta/rc commit that gets tested through out local conda repository. I could load the python object using !!python/object in pyyaml, but conda doesn't support arbitrary python execution. I don't see a way to do it with any other jinja2 features. I could also write a script to update the version number in more than one place, but I was really hoping to only modify one file as the definitive version number. Thanks for any help.

推荐答案

有很多方法可以到达端点。这就是conda本身的作用...

There are lots of ways to get to your endpoint. Here's what conda itself does...

conda版本信息的真实来源是<$ c中的 __ version __ $ c> conda / __ init __。py 。可以按照您的建议从conda import __version __ 中以编程方式在中以编程方式在python代码中加载它。它也硬连线到 setup.py 此处(请注意此代码),因此从命令行 python setup.py --version 是获取该信息的规范方法。

The source of truth for conda's version information is __version__ in conda/__init__.py. It can be loaded programmatically within python code as from conda import __version__ as you suggest. It's also hard-wired into setup.py here (note this code too), so from the command line python setup.py --version is the canonical way to get that information.

在1.x版本的conda-build中,放入一行

In 1.x versions of conda-build, putting a line

$PYTHON setup.py --version > __conda_version__.txt

build.sh 中使用我们的真实来源来设置内置软件包的版本。 已弃用 __ conda_version __。txt 文件,并且可能会在conda-build 2.0发行版中将其删除。在最新版本的conda-build中,首选的方法是在jinja2上下文中使用 load_setup_py_data(),这将使您可以访问<$中的所有元数据c $ c> setup.py 。具体来说,在 meta.yaml 文件中,我们会有类似的内容

in build.sh would set the version for the built package using our source of truth. The __conda_version__.txt file is deprecated, however, and it will likely be removed with the release of conda-build 2.0. In recent versions of conda-build, the preferred way to do this is to use load_setup_py_data() within a jinja2 context, which will give you access to all the metadata from setup.py. Specifically, in the meta.yaml file, we'd have something like this

package:
  name: conda
  version: "{{ load_setup_py_data().version }}"






现在,如何在 conda / __ init__中设置 __ version __ 变量.py ...


Now, how the __version__ variable is set in conda/__init__.py...

参见源代码是对 auxlib.packaging.get_version() 函数。此功能按顺序执行以下操作

What you see in the source code is a call to the auxlib.packaging.get_version() function. This function does the following in order


  1. 首先查找文件 conda / .version ,如果找到,则返回内容作为版本标识符

  2. 接下来查找 VERSION 环境变量,如果设置,则返回值作为版本标识符

  3. 最后查看 git describe --tags 输出,并在可能的情况下返回版本标识符(必须具有git已安装,必须是git repo等)

  4. 如果以上都不产生版本标识符,则返回 None

  1. look first for a file conda/.version, and if found return the contents as the version identifier
  2. look next for a VERSION environment variable, and if set return the value as the version identifier
  3. look last at the git describe --tags output, and return a version identifier if possible (must have git installed, must be a git repo, etc etc)
  4. if none of the above yield a version identifier, return None

现在只有最后一个技巧。在conda的 setup.py 文件,我们将 cmdclass 设置为 build_py sdist auxlib.packaging 提供的内容。基本上我们有

Now there's just one more final trick. In conda's setup.py file, we set cmdclass for build_py and sdist to those provided by auxlib.packaging. Basically we have

from auxlib import packaging
setup(
    cmdclass={
        'build_py': packaging.BuildPyCommand,
        'sdist': packaging.SDistCommand,
    }
)

这些特殊的命令类实际上修改了内置/安装包中的 conda / __ init __。py 文件,因此 __ version __ 变量被硬编码为字符串文字,并且不使用 auxlib.packaging.get_version()函数。

These special command classes actually modify the conda/__init__.py file in built/installed packages so the __version__ variable is hard-coded to a string literal, and doesn't use the auxlib.packaging.get_version() function.

在您的情况下,不想标记每个发行版,可以使用上述所有内容,并在命令行中使用<$设置版本c $ c> VERSION 环境变量。

In your case, with not wanting to tag every release, you could use all of the above, and from the command line set the version using a VERSION environment variable. Something like

VERSION=1.0.0alpha1 conda build conda.recipe

在您的构建部分meta.yaml配方中,您需要添加 script_env 键,告诉conda-build将 VERSION 环境变量一直传递到构建环境。

In your build section meta.yaml recipe, you'll need add a script_env key to tell conda-build to pass the VERSION environment variable all the way through to the build environment.

build:
  script_env:
    - VERSION

这篇关于从源文件获取conda meta.yaml的软件包版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆