Get package version for conda meta.yaml from source file

前端 未结 3 432
遥遥无期
遥遥无期 2021-01-12 02:37

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 nee

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-12 03:10

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

    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.

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

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

    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 }}"
    

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

    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. 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

    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,
        }
    )
    

    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.


    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
    

    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
    

提交回复
热议问题