├── .gitattributes ├── .gitignore ├── CONTRIBUTING.md ├── LICENSE.txt ├── MANIFEST.in ├── README.md ├── azure-pipelines.yml ├── bors.toml ├── ci └── azure-pipelines-steps.yml ├── code_of_conduct.md ├── codecov.yml ├── doc-requirements.txt ├── docs ├── Makefile ├── _static │ └── .empty ├── changelog.rst ├── conf.py ├── conftest.py ├── contributing.rst ├── fixtures.rst ├── index.rst ├── make.bat ├── markers.rst └── usage.rst ├── known_broken_constraints.txt ├── pylintrc ├── pypi-intro.rst ├── setup.cfg ├── setup.py ├── src └── pytest_mpi │ ├── __init__.py │ ├── _helpers.py │ └── _version.py ├── tests ├── conftest.py ├── test_fixtures.py └── test_markers.py ├── tox.ini └── versioneer.py /.gitattributes: -------------------------------------------------------------------------------- 1 | src/pytest_mpi/_version.py export-subst 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .cache 2 | .tox 3 | *.egg-info 4 | *.swp 5 | *.swo 6 | _build 7 | .coverage 8 | dist 9 | build 10 | __pycache__ 11 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to pytest-mpi 2 | 3 | We welcome contributions to pytest-mpi, subject to our [code of conduct](code_of_conduct.md), 4 | whether it is improvements to the documentation or examples, bug reports or code 5 | improvements. 6 | 7 | Bugs should be reported to https://github.org/aragilar/pytest-mpi, and pull 8 | requests should be made against master on 9 | https://github.org/aragilar/pytest-mpi. 10 | 11 | Further details can be found at https://pytest-mpi.readthedocs.io/en/latest/Contributing.html 12 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016, James Tocknell 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | * Redistributions of source code must retain the above copyright 7 | notice, this list of conditions and the following disclaimer. 8 | * Redistributions in binary form must reproduce the above copyright 9 | notice, this list of conditions and the following disclaimer in the 10 | documentation and/or other materials provided with the distribution. 11 | * Neither the name of the nor the 12 | names of its contributors may be used to endorse or promote products 13 | derived from this software without specific prior written permission. 14 | 15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 19 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include versioneer.py src/pytest_mpi/_version.py 2 | include README.md 3 | include pypi-intro.rst 4 | include LICENSE.txt CONTRIBUTING.md code_of_conduct.md 5 | exclude known_broken_constraints.txt old_pytest.txt 6 | recursive-include docs * 7 | recursive-exclude docs/_build * 8 | include doc-requirements.txt 9 | include test-requirements.txt 10 | recursive-include tests *.py 11 | include pylintrc 12 | include tox.ini 13 | exclude TODO 14 | prune **/__pycache__ 15 | prune **/*.pyc 16 | prune **/*.swp 17 | prune **/*.swo 18 | exclude bors.toml azure-pipelines.yml codecov.yml 19 | recursive-exclude ci * 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Documentation Status](https://readthedocs.org/projects/pytest-mpi/badge/?version=latest)](http://pytest-mpi.readthedocs.org/en/latest/?badge=latest) 2 | [![Coverage Status](https://codecov.io/github/aragilar/pytest-mpi/coverage.svg?branch=master)](https://codecov.io/github/aragilar/pytest-mpi?branch=master) 3 | [![Version](https://img.shields.io/pypi/v/pytest-mpi.svg)](https://pypi.python.org/pypi/pytest-mpi/) 4 | [![License](https://img.shields.io/pypi/l/pytest-mpi.svg)](https://pypi.python.org/pypi/pytest-mpi/) 5 | [![Wheel](https://img.shields.io/pypi/wheel/pytest-mpi.svg)](https://pypi.python.org/pypi/pytest-mpi/) 6 | [![Format](https://img.shields.io/pypi/format/pytest-mpi.svg)](https://pypi.python.org/pypi/pytest-mpi/) 7 | [![Supported versions](https://img.shields.io/pypi/pyversions/pytest-mpi.svg)](https://pypi.python.org/pypi/pytest-mpi/) 8 | [![Supported implemntations](https://img.shields.io/pypi/implementation/pytest-mpi.svg)](https://pypi.python.org/pypi/pytest-mpi/) 9 | [![PyPI](https://img.shields.io/pypi/status/pytest-mpi.svg)](https://pypi.python.org/pypi/pytest-mpi/) 10 | 11 | `pytest_mpi` is a plugin for pytest providing some useful tools when running 12 | tests under MPI, and testing MPI-related code. 13 | 14 | To run a test only when using MPI, use the `pytest.mark.mpi` marker like: 15 | ```python 16 | 17 | @pytest.mark.mpi 18 | def test_mpi(): 19 | pass 20 | ``` 21 | 22 | Further documentation can be found at [https://pytest-mpi.readthedocs.io](https://pytest-mpi.readthedocs.io). 23 | 24 | Bug reports and suggestions should be filed at 25 | [https://github.com/aragilar/pytest-mpi/issues](https://github.com/aragilar/pytest-mpi/issues). 26 | -------------------------------------------------------------------------------- /azure-pipelines.yml: -------------------------------------------------------------------------------- 1 | # from matplotlib's azure setup 2 | 3 | schedules: 4 | - cron: "0 0 * * 4" 5 | displayName: Weekly build 6 | branches: 7 | include: 8 | - releases/* 9 | always: true 10 | 11 | trigger: 12 | tags: 13 | include: 14 | - "*" 15 | branches: 16 | include: 17 | - "*" 18 | 19 | variables: 20 | PIP_CACHE_DIR: $(Pipeline.Workspace)/cache/pip 21 | 22 | jobs: 23 | - job: "static_checks" 24 | pool: 25 | vmImage: ubuntu-22.04 26 | variables: 27 | TOXENV: flake8,pylint,docs,check-manifest,checkreadme 28 | steps: 29 | - task: UsePythonVersion@0 30 | inputs: 31 | versionSpec: "3.8" 32 | architecture: "x64" 33 | - script: | 34 | pip install tox 35 | displayName: Install tox 36 | - script: | 37 | tox 38 | displayName: tox 39 | 40 | - job: "ubuntu2204" 41 | pool: 42 | vmImage: ubuntu-22.04 43 | strategy: 44 | matrix: 45 | py37: 46 | python.version: "3.7" 47 | TOXENV: py37 48 | py38: 49 | python.version: "3.8" 50 | TOXENV: py38 51 | py39: 52 | python.version: "3.9" 53 | TOXENV: py39 54 | py310: 55 | python.version: "3.10" 56 | TOXENV: py310 57 | py37-mpi: 58 | python.version: "3.7" 59 | TOXENV: py37-mpi 60 | py38-mpi: 61 | python.version: "3.8" 62 | TOXENV: py38-mpi 63 | py39-mpi: 64 | python.version: "3.9" 65 | TOXENV: py39-mpi 66 | py310-mpi: 67 | python.version: "3.10" 68 | TOXENV: py310-mpi 69 | 70 | steps: 71 | - template: ci/azure-pipelines-steps.yml 72 | parameters: 73 | platform: linux 74 | installer: apt 75 | 76 | # - job: "macOS1015" 77 | # pool: 78 | # vmImage: macOS-10.15 79 | # strategy: 80 | # matrix: 81 | # py37: 82 | # python.version: "3.7" 83 | # TOXENV: py37 84 | # py38: 85 | # python.version: "3.8" 86 | # TOXENV: py38 87 | # py39: 88 | # python.version: "3.9" 89 | # TOXENV: py39 90 | # py37-mpi: 91 | # python.version: "3.7" 92 | # TOXENV: py37-mpi 93 | # py38-mpi: 94 | # python.version: "3.8" 95 | # TOXENV: py38-mpi 96 | # py39-mpi: 97 | # python.version: "3.9" 98 | # TOXENV: py39-mpi 99 | # maxParallel: 4 100 | # 101 | # steps: 102 | # - template: ci/azure-pipelines-steps.yml 103 | # parameters: 104 | # platform: macos 105 | # installer: brew 106 | -------------------------------------------------------------------------------- /bors.toml: -------------------------------------------------------------------------------- 1 | status = [ 2 | "aragilar.pytest-mpi", 3 | "codecov/patch", 4 | "codecov/project", 5 | ] 6 | -------------------------------------------------------------------------------- /ci/azure-pipelines-steps.yml: -------------------------------------------------------------------------------- 1 | parameters: 2 | platform: none 3 | installer: none 4 | 5 | steps: 6 | - task: UsePythonVersion@0 7 | inputs: 8 | versionSpec: "$(python.version)" 9 | architecture: "x64" 10 | displayName: "Use Python $(python.version)" 11 | condition: and(succeeded(), ne(variables['python.version'], 'Pre')) 12 | 13 | - ${{ if eq(parameters.installer, 'brew') }}: 14 | - script: | 15 | brew install open-mpi 16 | displayName: 'Install brew dependencies' 17 | - ${{ if eq(parameters.installer, 'apt') }}: 18 | - script: | 19 | sudo apt-get update 20 | sudo apt-get install openmpi-bin libopenmpi-dev 21 | displayName: 'Install apt dependencies' 22 | 23 | - script: | 24 | python -m pip install --upgrade pip 25 | pip install tox codecov twine wheel 26 | displayName: "Install pip dependencies" 27 | 28 | - task: TwineAuthenticate@0 29 | inputs: 30 | externalFeeds: "pypi" 31 | condition: startsWith(variables['Build.SourceBranch'], 'refs/tags/') 32 | 33 | - script: env 34 | displayName: "print env" 35 | 36 | - script: | 37 | tox 38 | displayName: "tox" 39 | 40 | #- script: | 41 | # codecov 42 | # displayName: 'codecov' 43 | # hopefully the bash uploader will work 44 | - script: | 45 | bash <(curl -s https://codecov.io/bash) 46 | displayName: "Upload to codecov.io" 47 | 48 | - script: | 49 | python setup.py sdist bdist_wheel 50 | twine upload --skip-existing -r pypi --config-file $(PYPIRC_PATH) dist/* 51 | displayName: "Upload to PyPI" 52 | condition: startsWith(variables['Build.SourceBranch'], 'refs/tags/') 53 | -------------------------------------------------------------------------------- /code_of_conduct.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, gender identity and expression, level of experience, 9 | nationality, personal appearance, race, religion, or sexual identity and 10 | orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at [INSERT EMAIL ADDRESS]. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at [https://contributor-covenant.org/version/1/4][version] 72 | 73 | [homepage]: https://contributor-covenant.org 74 | [version]: https://contributor-covenant.org/version/1/4/ 75 | -------------------------------------------------------------------------------- /codecov.yml: -------------------------------------------------------------------------------- 1 | codecov: 2 | notify: 3 | after_n_builds: 10 4 | # do not notify until at least 5 builds have been uploaded from the CI pipeline 5 | # you can also set after_n_builds on comments independently 6 | comment: 7 | after_n_builds: 10 8 | -------------------------------------------------------------------------------- /doc-requirements.txt: -------------------------------------------------------------------------------- 1 | sphinx>=1.3 2 | sphinx_rtd_theme==0.5.2 3 | -------------------------------------------------------------------------------- /docs/Makefile: -------------------------------------------------------------------------------- 1 | # Minimal makefile for Sphinx documentation 2 | # 3 | 4 | # You can set these variables from the command line. 5 | SPHINXOPTS = 6 | SPHINXBUILD = sphinx-build 7 | SOURCEDIR = . 8 | BUILDDIR = _build 9 | 10 | # Put it first so that "make" without argument is like "make help". 11 | help: 12 | @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) 13 | 14 | .PHONY: help Makefile 15 | 16 | # Catch-all target: route all unknown targets to Sphinx using the new 17 | # "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). 18 | %: Makefile 19 | @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) -------------------------------------------------------------------------------- /docs/_static/.empty: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aragilar/pytest-mpi/6729d76fa8a785d34ccdaec881c1980b6f4aa1ad/docs/_static/.empty -------------------------------------------------------------------------------- /docs/changelog.rst: -------------------------------------------------------------------------------- 1 | Changelog 2 | ========= 3 | 4 | 0.6 5 | --- 6 | * Again no codebase changes, testing/CI and packaging fixes only. 7 | * Update Azure Pipelines to use latest images (so CI runs again) 8 | * Update versioneer to work on future Python versions. 9 | * Fix doctests infrastructure 10 | 11 | 0.5 12 | --- 13 | * No codebase changes, only testing/CI changes needed to support pytest 6. 14 | * We use Azure Pipelines now for CI, rather than Travis 15 | * Autouploads to PyPI are done via Azure Pipelines 16 | * We test on both pytest<6 and pytest>=6, due to the need to support both for 17 | now. 18 | 19 | 0.4 20 | --- 21 | * Added license and contributing details 22 | * Added fixtures to enable sharing code across files 23 | * Numerous testing fixes/improvements 24 | 25 | 0.3 26 | --- 27 | * Fixed pylint failures 28 | * Added testing of examples in documentation 29 | * Added proper tests 30 | * Fix bugs found via tests 31 | 32 | 0.2 33 | --- 34 | * Add proper documentation of features 35 | * Display more MPI related information on test run 36 | * Add `mpi_skip` and `mpi_xfail` markers 37 | * Add `mpi_tmpdir` and `mpi_tmp_path` 38 | 39 | 0.1.1 40 | ----- 41 | * Fix plugin as the pytest command line parsing logic needs to be outside main 42 | plugin class 43 | 44 | 0.1 45 | --- 46 | Initial version 47 | -------------------------------------------------------------------------------- /docs/conf.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # Configuration file for the Sphinx documentation builder. 4 | # 5 | # This file does only contain a selection of the most common options. For a 6 | # full list see the documentation: 7 | # http://www.sphinx-doc.org/en/master/config 8 | 9 | # -- Path setup -------------------------------------------------------------- 10 | 11 | # If extensions (or modules to document with autodoc) are in another directory, 12 | # add these directories to sys.path here. If the directory is relative to the 13 | # documentation root, use os.path.abspath to make it absolute, like shown here. 14 | # 15 | # import os 16 | # import sys 17 | # sys.path.insert(0, os.path.abspath('.')) 18 | 19 | 20 | # -- Project information ----------------------------------------------------- 21 | 22 | project = u'pytest-mpi' 23 | copyright = u'2019, James Tocknell' 24 | author = u'James Tocknell' 25 | 26 | import sys 27 | import os 28 | sys.path.insert(0, os.path.abspath('..')) 29 | import pytest_mpi 30 | # The short X.Y version 31 | version = '.'.join(pytest_mpi.__version__.split(".")[0:2]) 32 | # The full version, including alpha/beta/rc tags. 33 | release = pytest_mpi.__version__ 34 | 35 | 36 | # -- General configuration --------------------------------------------------- 37 | 38 | # If your documentation needs a minimal Sphinx version, state it here. 39 | # 40 | # needs_sphinx = '1.0' 41 | 42 | # Add any Sphinx extension module names here, as strings. They can be 43 | # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom 44 | # ones. 45 | extensions = [ 46 | 'sphinx.ext.intersphinx', 47 | 'sphinx.ext.coverage', 48 | 'sphinx.ext.autodoc', 49 | ] 50 | 51 | # Add any paths that contain templates here, relative to this directory. 52 | templates_path = ['_templates'] 53 | 54 | # The suffix(es) of source filenames. 55 | # You can specify multiple suffix as a list of string: 56 | # 57 | # source_suffix = ['.rst', '.md'] 58 | source_suffix = '.rst' 59 | 60 | # The master toctree document. 61 | master_doc = 'index' 62 | 63 | # The language for content autogenerated by Sphinx. Refer to documentation 64 | # for a list of supported languages. 65 | # 66 | # This is also used if you do content translation via gettext catalogs. 67 | # Usually you set "language" from the command line for these cases. 68 | language = "en" 69 | 70 | # List of patterns, relative to source directory, that match files and 71 | # directories to ignore when looking for source files. 72 | # This pattern also affects html_static_path and html_extra_path. 73 | exclude_patterns = [u'_build', 'Thumbs.db', '.DS_Store'] 74 | 75 | # The name of the Pygments (syntax highlighting) style to use. 76 | pygments_style = None 77 | 78 | 79 | # -- Options for HTML output ------------------------------------------------- 80 | 81 | # The theme to use for HTML and HTML Help pages. See the documentation for 82 | # a list of builtin themes. 83 | # 84 | html_theme = 'alabaster' 85 | 86 | # Theme options are theme-specific and customize the look and feel of a theme 87 | # further. For a list of options available for each theme, see the 88 | # documentation. 89 | # 90 | # html_theme_options = {} 91 | 92 | # Add any paths that contain custom static files (such as style sheets) here, 93 | # relative to this directory. They are copied after the builtin static files, 94 | # so a file named "default.css" will overwrite the builtin "default.css". 95 | html_static_path = ['_static'] 96 | 97 | # Custom sidebar templates, must be a dictionary that maps document names 98 | # to template names. 99 | # 100 | # The default sidebars (for documents that don't match any pattern) are 101 | # defined by theme itself. Builtin themes are using these templates by 102 | # default: ``['localtoc.html', 'relations.html', 'sourcelink.html', 103 | # 'searchbox.html']``. 104 | # 105 | # html_sidebars = {} 106 | 107 | 108 | # -- Options for HTMLHelp output --------------------------------------------- 109 | 110 | # Output file base name for HTML help builder. 111 | htmlhelp_basename = 'pytest-mpidoc' 112 | 113 | 114 | # -- Options for LaTeX output ------------------------------------------------ 115 | 116 | latex_elements = { 117 | # The paper size ('letterpaper' or 'a4paper'). 118 | # 119 | # 'papersize': 'letterpaper', 120 | 121 | # The font size ('10pt', '11pt' or '12pt'). 122 | # 123 | # 'pointsize': '10pt', 124 | 125 | # Additional stuff for the LaTeX preamble. 126 | # 127 | # 'preamble': '', 128 | 129 | # Latex figure (float) alignment 130 | # 131 | # 'figure_align': 'htbp', 132 | } 133 | 134 | # Grouping the document tree into LaTeX files. List of tuples 135 | # (source start file, target name, title, 136 | # author, documentclass [howto, manual, or own class]). 137 | latex_documents = [ 138 | (master_doc, 'pytest-mpi.tex', u'pytest-mpi Documentation', 139 | u'James Tocknell', 'manual'), 140 | ] 141 | 142 | 143 | # -- Options for manual page output ------------------------------------------ 144 | 145 | # One entry per manual page. List of tuples 146 | # (source start file, name, description, authors, manual section). 147 | man_pages = [ 148 | (master_doc, 'pytest-mpi', u'pytest-mpi Documentation', 149 | [author], 1) 150 | ] 151 | 152 | 153 | # -- Options for Texinfo output ---------------------------------------------- 154 | 155 | # Grouping the document tree into Texinfo files. List of tuples 156 | # (source start file, target name, title, author, 157 | # dir menu entry, description, category) 158 | texinfo_documents = [ 159 | (master_doc, 'pytest-mpi', u'pytest-mpi Documentation', 160 | author, 'pytest-mpi', 'One line description of project.', 161 | 'Miscellaneous'), 162 | ] 163 | 164 | 165 | # -- Options for Epub output ------------------------------------------------- 166 | 167 | # Bibliographic Dublin Core info. 168 | epub_title = project 169 | 170 | # The unique identifier of the text. This can be a ISBN number 171 | # or the project homepage. 172 | # 173 | # epub_identifier = '' 174 | 175 | # A unique identification for the text. 176 | # 177 | # epub_uid = '' 178 | 179 | # A list of files that should not be packed into the epub file. 180 | epub_exclude_files = ['search.html'] 181 | 182 | 183 | # -- Extension configuration ------------------------------------------------- 184 | 185 | # -- Options for intersphinx extension --------------------------------------- 186 | 187 | # Example configuration for intersphinx: refer to the Python standard library. 188 | intersphinx_mapping = {'python': ('https://docs.python.org/', None)} 189 | -------------------------------------------------------------------------------- /docs/conftest.py: -------------------------------------------------------------------------------- 1 | from doctest import ELLIPSIS 2 | 3 | import pytest 4 | 5 | from sybil import Sybil 6 | from sybil.parsers.codeblock import PythonCodeBlockParser 7 | from sybil.parsers.doctest import DocTestParser 8 | from sybil.parsers.skip import skip 9 | 10 | pytest_collect_file = Sybil( 11 | parsers=[ 12 | DocTestParser(optionflags=ELLIPSIS), 13 | PythonCodeBlockParser(), 14 | skip, 15 | ], 16 | pattern='*.rst', 17 | ).pytest() 18 | -------------------------------------------------------------------------------- /docs/contributing.rst: -------------------------------------------------------------------------------- 1 | .. _contributing: 2 | 3 | Contributing to pytest-mpi 4 | ########################## 5 | We welcome contributions to pytest-mpi, subject to our 6 | `code of conduct `_ 7 | whether it is improvements to the documentation or examples, bug reports or code 8 | improvements. 9 | 10 | Reporting Bugs 11 | -------------- 12 | Bugs should be reported to https://github.com/aragilar/pytest-mpi. Please 13 | include what version of Python this occurs on, as well as which operating 14 | system. Information about your h5py and HDF5 configuration is also helpful. 15 | 16 | Patches and Pull Requests 17 | ------------------------- 18 | The main repository is https://github.com/aragilar/pytest-mpi, please make pull 19 | requests against that repository, and the branch that pull requests should be 20 | made on is master (backporting fixes will be done separately if necessary). 21 | 22 | Running the tests 23 | ----------------- 24 | pytest-mpi uses tox_ to run its tests. See https://tox.readthedocs.io/en/latest/ 25 | for more information about tox, but the simplest method is to run:: 26 | 27 | tox 28 | 29 | in the top level of the git repository. 30 | 31 | .. note:: 32 | If you want to run pytest directly, remember to include ``-p pytester``, as 33 | pytester needs to be manually activated. 34 | 35 | .. _tox: https://tox.readthedocs.io/en/latest/ 36 | 37 | Making a release 38 | ---------------- 39 | Current minimal working method (this doesn't produce a release commit, deal 40 | with DOIs needing to be preregistered, not automated, not signed etc.): 41 | 42 | #. Checkout the latest commit on the ``master`` branch on the main repository 43 | locally. Ensure the work directory is clean 44 | (``git purge``/``git clean -xfd``). 45 | #. Tag this commit with an annotated tag, with the format being ``v*.*.*`` 46 | (``git tag -a v*.*.*``; I should sign these...). The tag should mention the 47 | changes in this release. 48 | #. Push tag to github. 49 | #. Create a release on github using the web interface, copying the content of 50 | the tag. 51 | #. Build sdist and wheel (``python setup.py sdist bdist_wheel``), and upload to 52 | PyPI (``twine upload dist/*``). 53 | -------------------------------------------------------------------------------- /docs/fixtures.rst: -------------------------------------------------------------------------------- 1 | Fixtures 2 | ======== 3 | 4 | .. autofunction:: pytest_mpi.mpi_file_name 5 | 6 | .. autofunction:: pytest_mpi.mpi_tmp_path 7 | 8 | .. autofunction:: pytest_mpi.mpi_tmpdir 9 | -------------------------------------------------------------------------------- /docs/index.rst: -------------------------------------------------------------------------------- 1 | .. pytest-mpi documentation master file, created by 2 | sphinx-quickstart on Wed Jun 26 22:34:19 2019. 3 | You can adapt this file completely to your liking, but it should at least 4 | contain the root `toctree` directive. 5 | 6 | Welcome to pytest-mpi's documentation! 7 | ====================================== 8 | 9 | `pytest-mpi` provides a number of things to assist with using pytest with 10 | MPI-using code, specifically: 11 | 12 | * Displaying of the current MPI configuration (e.g. the MPI version, the 13 | number of processes) 14 | * Sharing temporary files/folders across the MPI processes 15 | * Markers which allow for skipping or xfailing tests based on whether the 16 | tests are being run under MPI 17 | 18 | Further features will be added in the future, and contribution of features is 19 | very much welcomed. 20 | 21 | .. toctree:: 22 | :maxdepth: 2 23 | :caption: Contents: 24 | 25 | usage 26 | markers 27 | fixtures 28 | changelog 29 | contributing 30 | 31 | 32 | 33 | Indices and tables 34 | ================== 35 | 36 | * :ref:`genindex` 37 | * :ref:`modindex` 38 | * :ref:`search` 39 | -------------------------------------------------------------------------------- /docs/make.bat: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | 3 | pushd %~dp0 4 | 5 | REM Command file for Sphinx documentation 6 | 7 | if "%SPHINXBUILD%" == "" ( 8 | set SPHINXBUILD=sphinx-build 9 | ) 10 | set SOURCEDIR=. 11 | set BUILDDIR=_build 12 | 13 | if "%1" == "" goto help 14 | 15 | %SPHINXBUILD% >NUL 2>NUL 16 | if errorlevel 9009 ( 17 | echo. 18 | echo.The 'sphinx-build' command was not found. Make sure you have Sphinx 19 | echo.installed, then set the SPHINXBUILD environment variable to point 20 | echo.to the full path of the 'sphinx-build' executable. Alternatively you 21 | echo.may add the Sphinx directory to PATH. 22 | echo. 23 | echo.If you don't have Sphinx installed, grab it from 24 | echo.http://sphinx-doc.org/ 25 | exit /b 1 26 | ) 27 | 28 | %SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% 29 | goto end 30 | 31 | :help 32 | %SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% 33 | 34 | :end 35 | popd 36 | -------------------------------------------------------------------------------- /docs/markers.rst: -------------------------------------------------------------------------------- 1 | Markers 2 | ======= 3 | 4 | .. py:function:: pytest.mark.mpi(min_size=None) 5 | 6 | Mark that this test must be run under MPI. 7 | 8 | :keyword int min_size: 9 | Specify that this test requires at least `min_size` processes to run. If 10 | there are insufficient processes, skip this test. 11 | 12 | For example: 13 | 14 | .. code-block:: python 15 | 16 | import pytest 17 | 18 | @pytest.mark.mpi(minsize=4) 19 | def test_mpi_feature(): 20 | ... 21 | 22 | 23 | .. py:function:: pytest.mark.mpi_skip 24 | 25 | Mark that this test should be skipped when run under MPI. 26 | 27 | 28 | .. py:function:: pytest.mark.mpi_xfail 29 | 30 | Mark that this test should be xfailed when run under MPI. 31 | -------------------------------------------------------------------------------- /docs/usage.rst: -------------------------------------------------------------------------------- 1 | Usage 2 | ===== 3 | 4 | The important thing to remember is that `pytest-mpi` assists with running tests 5 | when `pytest` is run under MPI, rather than launching `pytest` under MPI. To 6 | actually run the tests under MPI, you will want to run something like:: 7 | 8 | $ mpirun -n 2 python -m pytest --with-mpi 9 | 10 | Note that by default the MPI tests are not run—this makes it easy to run the 11 | non-MPI parts of a test suite without having to worry about installing MPI and 12 | mpi4py. 13 | 14 | An simple test using the `mpi` marker managed by `pytest-mpi` is: 15 | 16 | .. code-block:: python 17 | 18 | import pytest 19 | @pytest.mark.mpi 20 | def test_size(): 21 | from mpi4py import MPI 22 | comm = MPI.COMM_WORLD 23 | assert comm.size > 0 24 | 25 | This test will be automatically be skipped unless `--with-mpi` is used. We can 26 | also specify a minimum number of processes required to run the test: 27 | 28 | .. code-block:: python 29 | 30 | import pytest 31 | @pytest.mark.mpi(min_size=2) 32 | def test_size(): 33 | from mpi4py import MPI 34 | comm = MPI.COMM_WORLD 35 | assert comm.size >= 2 36 | 37 | There are also `mpi_skip`, for when a test should not be run under MPI (e.g. it 38 | causes a lockup or segmentation fault), and `mpi_xfail`, for when a test should 39 | succeed when run normally, but fail when run under MPI. 40 | -------------------------------------------------------------------------------- /known_broken_constraints.txt: -------------------------------------------------------------------------------- 1 | pytest!=5.3.0 2 | -------------------------------------------------------------------------------- /pylintrc: -------------------------------------------------------------------------------- 1 | [MASTER] 2 | 3 | # Specify a configuration file. 4 | #rcfile= 5 | 6 | # Python code to execute, usually for sys.path manipulation such as 7 | # pygtk.require(). 8 | #init-hook= 9 | 10 | # Add files or directories to the blacklist. They should be base names, not 11 | # paths. 12 | ignore=CVS,_version.py 13 | 14 | # Pickle collected data for later comparisons. 15 | persistent=yes 16 | 17 | # List of plugins (as comma separated values of python modules names) to load, 18 | # usually to register additional checkers. 19 | load-plugins= 20 | 21 | [REPORTS] 22 | 23 | # Set the output format. Available formats are text, parseable, colorized, msvs 24 | # (visual studio) and html. You can also give a reporter class, eg 25 | # mypackage.mymodule.MyReporterClass. 26 | output-format=colorized 27 | 28 | # Tells whether to display a full report or only the messages 29 | reports=no 30 | 31 | # Python expression which should return a note less than 10 (10 is the highest 32 | # note). You have access to the variables errors warning, statement which 33 | # respectively contain the number of errors / warnings messages and the total 34 | # number of statements analyzed. This is used by the global evaluation report 35 | # (RP0004). 36 | evaluation=10.0 - ((float(5 * error + warning + refactor + convention) / statement) * 10) 37 | 38 | # Template used to display messages. This is a python new-style format string 39 | # used to format the message information. See doc for all details 40 | #msg-template= 41 | 42 | 43 | [MESSAGES CONTROL] 44 | 45 | # Enable the message, report, category or checker with the given id(s). You can 46 | # either give multiple identifier separated by comma (,) or put this option 47 | # multiple time. See also the "--disable" option for examples. 48 | #enable= 49 | 50 | # Disable the message, report, category or checker with the given id(s). You 51 | # can either give multiple identifiers separated by comma (,) or put this 52 | # option multiple times (only on the command line, not in the configuration 53 | # file where it should appear only once).You can also use "--disable=all" to 54 | # disable everything first and then reenable specific checks. For example, if 55 | # you want to run only the similarities checker, you can use "--disable=all 56 | # --enable=similarities". If you want to run only the classes checker, but have 57 | # no Warning level messages displayed, use"--disable=all --enable=classes 58 | # --disable=W" 59 | disable=useless-object-inheritance,import-outside-toplevel,no-member,invalid-name,consider-using-f-string 60 | 61 | 62 | [SIMILARITIES] 63 | 64 | # Minimum lines number of a similarity. 65 | min-similarity-lines=4 66 | 67 | # Ignore comments when computing similarities. 68 | ignore-comments=yes 69 | 70 | # Ignore docstrings when computing similarities. 71 | ignore-docstrings=yes 72 | 73 | # Ignore imports when computing similarities. 74 | ignore-imports=no 75 | 76 | 77 | [BASIC] 78 | # Good variable names which should always be accepted, separated by a comma 79 | good-names=i,j,k 80 | 81 | # Bad variable names which should always be refused, separated by a comma 82 | bad-names=foo,bar,baz,toto,tutu,tata 83 | 84 | # Colon-delimited sets of names that determine each other's naming style when 85 | # the name regexes allow several styles. 86 | name-group= 87 | 88 | # Include a hint for the correct naming format with invalid-name 89 | include-naming-hint=no 90 | 91 | # Regular expression matching correct module names 92 | module-rgx=(([a-z_][a-z0-9_]*)|([A-Z][a-zA-Z0-9]+))$ 93 | 94 | # Regular expression matching correct method names 95 | method-rgx=[a-z_][a-z0-9_]{2,30}$ 96 | 97 | # Regular expression matching correct variable names 98 | variable-rgx=[a-z_][a-z0-9_]{2,30}$ 99 | 100 | # Regular expression matching correct constant names 101 | const-rgx=(([A-Z_][A-Z0-9_]*)|(__.*__))$ 102 | 103 | # Regular expression matching correct argument names 104 | argument-rgx=[a-z_][a-z0-9_]{2,30}$ 105 | 106 | # Regular expression matching correct inline iteration names 107 | inlinevar-rgx=[A-Za-z_][A-Za-z0-9_]*$ 108 | 109 | # Regular expression matching correct attribute names 110 | attr-rgx=[a-z_][a-z0-9_]{2,30}$ 111 | 112 | # Regular expression matching correct class names 113 | class-rgx=[A-Z_][a-zA-Z0-9]+$ 114 | 115 | # Regular expression matching correct function names 116 | function-rgx=[a-z_][a-z0-9_]{2,30}$ 117 | 118 | # Regular expression matching correct class attribute names 119 | class-attribute-rgx=([A-Za-z_][A-Za-z0-9_]{2,30}|(__.*__))$ 120 | 121 | # Regular expression which should only match function or class names that do 122 | # not require a docstring. 123 | no-docstring-rgx=__.*__ 124 | 125 | # Minimum line length for functions/classes that require docstrings, shorter 126 | # ones are exempt. 127 | docstring-min-length=-1 128 | 129 | 130 | [LOGGING] 131 | 132 | # Logging modules to check that the string format arguments are in logging 133 | # function parameter format 134 | logging-modules=logging 135 | 136 | 137 | [MISCELLANEOUS] 138 | 139 | # List of note tags to take in consideration, separated by a comma. 140 | notes=FIXME,XXX,TODO 141 | 142 | 143 | [TYPECHECK] 144 | 145 | # Tells whether missing members accessed in mixin class should be ignored. A 146 | # mixin class is detected if its name ends with "mixin" (case insensitive). 147 | ignore-mixin-members=yes 148 | 149 | # List of module names for which member attributes should not be checked 150 | # (useful for modules/projects where namespaces are manipulated during runtime 151 | # and thus existing member attributes cannot be deduced by static analysis 152 | #ignored-modules=numpy,scikits.odes.sundials.cvode 153 | 154 | # List of classes names for which member attributes should not be checked 155 | # (useful for classes with attributes dynamically set). 156 | #ignored-classes=SQLObject 157 | 158 | # List of members which are set dynamically and missed by pylint inference 159 | # system, and so shouldn't trigger E0201 when accessed. Python regular 160 | # expressions are accepted. 161 | generated-members=REQUEST,acl_users,aq_parent 162 | 163 | 164 | [FORMAT] 165 | 166 | # Maximum number of characters on a single line. 167 | max-line-length=80 168 | 169 | # Regexp for a line that is allowed to be longer than the limit. 170 | ignore-long-lines=^\s*(# )??$ 171 | 172 | # Allow the body of an if to be on the same line as the test if there is no 173 | # else. 174 | single-line-if-stmt=no 175 | 176 | # Maximum number of lines in a module 177 | max-module-lines=1000 178 | 179 | # String used as indentation unit. This is usually " " (4 spaces) or "\t" (1 180 | # tab). 181 | indent-string=' ' 182 | 183 | # Number of spaces of indent required inside a hanging or continued line. 184 | indent-after-paren=4 185 | 186 | 187 | [VARIABLES] 188 | 189 | # Tells whether we should check for unused import in __init__ files. 190 | init-import=no 191 | 192 | # A regular expression matching the name of dummy variables (i.e. expectedly 193 | # not used). 194 | dummy-variables-rgx=_$|dummy 195 | 196 | # List of additional names supposed to be defined in builtins. Remember that 197 | # you should avoid to define new builtins when possible. 198 | additional-builtins= 199 | 200 | 201 | [DESIGN] 202 | 203 | # Maximum number of arguments for function / method 204 | max-args=5 205 | 206 | # Argument names that match this expression will be ignored. Default to name 207 | # with leading underscore 208 | ignored-argument-names=_.* 209 | 210 | # Maximum number of locals for function / method body 211 | max-locals=15 212 | 213 | # Maximum number of return / yield for function / method body 214 | max-returns=6 215 | 216 | # Maximum number of branch for function / method body 217 | max-branches=12 218 | 219 | # Maximum number of statements in function / method body 220 | max-statements=50 221 | 222 | # Maximum number of parents for a class (see R0901). 223 | max-parents=7 224 | 225 | # Maximum number of attributes for a class (see R0902). 226 | max-attributes=7 227 | 228 | # Minimum number of public methods for a class (see R0903). 229 | min-public-methods=2 230 | 231 | # Maximum number of public methods for a class (see R0904). 232 | max-public-methods=20 233 | 234 | 235 | [CLASSES] 236 | # List of method names used to declare (i.e. assign) instance attributes. 237 | defining-attr-methods=__init__,__new__,setUp 238 | 239 | # List of valid names for the first argument in a class method. 240 | valid-classmethod-first-arg=cls 241 | 242 | # List of valid names for the first argument in a metaclass class method. 243 | valid-metaclass-classmethod-first-arg=mcs 244 | 245 | 246 | [IMPORTS] 247 | 248 | # Deprecated modules which should not be used, separated by a comma 249 | deprecated-modules=stringprep,optparse 250 | 251 | # Create a graph of every (i.e. internal and external) dependencies in the 252 | # given file (report RP0402 must not be disabled) 253 | import-graph= 254 | 255 | # Create a graph of external dependencies in the given file (report RP0402 must 256 | # not be disabled) 257 | ext-import-graph= 258 | 259 | # Create a graph of internal dependencies in the given file (report RP0402 must 260 | # not be disabled) 261 | int-import-graph= 262 | 263 | 264 | [EXCEPTIONS] 265 | 266 | # Exceptions that will emit a warning when being caught. Defaults to 267 | # "Exception" 268 | # overgeneral-exceptions=Exception 269 | -------------------------------------------------------------------------------- /pypi-intro.rst: -------------------------------------------------------------------------------- 1 | `pytest_mpi` is a plugin for pytest providing some useful tools when running 2 | tests under MPI, and testing MPI-related code. 3 | 4 | To run a test only when using MPI, use the `pytest.mark.mpi` marker like:: 5 | 6 | @pytest.mark.mpi 7 | def test_mpi(): 8 | pass 9 | 10 | 11 | Further documentation can be found at ``_. 12 | 13 | Bug reports and suggestions should be filed at 14 | ``_. 15 | 16 | 17 | |Documentation Status| |Build Status| |Coverage Status| 18 | 19 | .. |Documentation Status| image:: https://readthedocs.org/projects/pytest-mpi/badge/?version=latest 20 | :target: http://pytest-mpi.readthedocs.org/en/latest/?badge=latest 21 | .. |Build Status| image:: https://travis-ci.org/aragilar/pytest-mpi.svg?branch=master 22 | :target: https://travis-ci.org/aragilar/pytest-mpi 23 | .. |Coverage Status| image:: https://codecov.io/github/aragilar/pytest-mpi/coverage.svg?branch=master 24 | :target: https://codecov.io/github/aragilar/pytest-mpi?branch=master 25 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [wheel] 2 | universal=1 3 | 4 | [versioneer] 5 | VCS = git 6 | style = pep440 7 | versionfile_source = src/pytest_mpi/_version.py 8 | versionfile_build = pytest_mpi/_version.py 9 | tag_prefix = v 10 | 11 | [check-manifest] 12 | ignore = 13 | .travis.yml 14 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup, find_packages 2 | import versioneer 3 | 4 | DESCRIPTION_FILES = ["pypi-intro.rst"] 5 | 6 | long_description = [] 7 | import codecs 8 | for filename in DESCRIPTION_FILES: 9 | with codecs.open(filename, 'r', 'utf-8') as f: 10 | long_description.append(f.read()) 11 | long_description = "\n".join(long_description) 12 | 13 | setup( 14 | name="pytest-mpi", 15 | version=versioneer.get_version(), 16 | packages = find_packages('src'), 17 | package_dir = {'': 'src'}, 18 | install_requires = ["pytest"], 19 | author = "James Tocknell", 20 | author_email = "aragilar@gmail.com", 21 | description = "pytest plugin to collect information from tests", 22 | long_description = long_description, 23 | license = "3-clause BSD", 24 | keywords = "pytest testing", 25 | url = "https://pytest-mpi.readthedocs.io", 26 | entry_points = { 27 | 'pytest11': [ 28 | 'pytest_mpi = pytest_mpi', 29 | ] 30 | }, 31 | classifiers=[ 32 | 'Framework :: Pytest', 33 | 'Development Status :: 3 - Alpha', 34 | 'Intended Audience :: Developers', 35 | 'License :: OSI Approved :: BSD License', 36 | 'Programming Language :: Python :: 3', 37 | 'Programming Language :: Python :: 3.5', 38 | 'Programming Language :: Python :: 3.6', 39 | 'Programming Language :: Python :: 3.7', 40 | 'Programming Language :: Python :: 3.8', 41 | 'Programming Language :: Python :: 3.9', 42 | ], 43 | cmdclass=versioneer.get_cmdclass(), 44 | ) 45 | -------------------------------------------------------------------------------- /src/pytest_mpi/__init__.py: -------------------------------------------------------------------------------- 1 | """ 2 | Support for testing python code with MPI and pytest 3 | """ 4 | from enum import Enum 5 | from pathlib import Path 6 | 7 | import py 8 | import pytest 9 | 10 | from . import _version 11 | __version__ = _version.get_versions()['version'] 12 | 13 | 14 | WITH_MPI_ARG = "--with-mpi" 15 | ONLY_MPI_ARG = "--only-mpi" 16 | 17 | 18 | class MPIMarkerEnum(str, Enum): 19 | """ 20 | Enum containing all the markers used by pytest-mpi 21 | """ 22 | mpi = "mpi" 23 | mpi_skip = "mpi_skip" 24 | mpi_xfail = "mpi_xfail" 25 | mpi_break = "mpi_break" 26 | 27 | 28 | MPI_MARKERS = { 29 | MPIMarkerEnum.mpi_skip: pytest.mark.skip( 30 | reason="test does not work under mpi" 31 | ), 32 | MPIMarkerEnum.mpi_break: pytest.mark.skip( 33 | reason="test does not work under mpi" 34 | ), 35 | MPIMarkerEnum.mpi_xfail: pytest.mark.xfail( 36 | reason="test fails under mpi" 37 | ), 38 | } 39 | 40 | 41 | class MPIPlugin(object): 42 | """ 43 | pytest plugin to assist with testing MPI-using code 44 | """ 45 | 46 | _is_testing_mpi = False 47 | 48 | def _testing_mpi(self, config): 49 | """ 50 | Return if we're testing with MPI or not. 51 | """ 52 | with_mpi = config.getoption(WITH_MPI_ARG) 53 | only_mpi = config.getoption(ONLY_MPI_ARG) 54 | return with_mpi or only_mpi 55 | 56 | def _add_markers(self, item): 57 | """ 58 | Add markers to tests when run under MPI. 59 | """ 60 | for label, marker in MPI_MARKERS.items(): 61 | if label in item.keywords: 62 | item.add_marker(marker) 63 | 64 | def pytest_configure(self, config): 65 | """ 66 | Hook setting config object (always called at least once) 67 | """ 68 | self._is_testing_mpi = self._testing_mpi(config) 69 | 70 | def pytest_collection_modifyitems(self, config, items): 71 | """ 72 | Skip tests depending on what options are chosen 73 | """ 74 | with_mpi = config.getoption(WITH_MPI_ARG) 75 | only_mpi = config.getoption(ONLY_MPI_ARG) 76 | for item in items: 77 | if with_mpi: 78 | self._add_markers(item) 79 | elif only_mpi and MPIMarkerEnum.mpi not in item.keywords: 80 | item.add_marker( 81 | pytest.mark.skip(reason="test does not use mpi") 82 | ) 83 | elif not (with_mpi or only_mpi) and ( 84 | MPIMarkerEnum.mpi in item.keywords 85 | ): 86 | item.add_marker( 87 | pytest.mark.skip(reason="need --with-mpi option to run") 88 | ) 89 | 90 | def pytest_terminal_summary(self, terminalreporter, exitstatus, *args): 91 | """ 92 | Hook for printing MPI info at the end of the run 93 | """ 94 | # pylint: disable=unused-argument 95 | if self._is_testing_mpi: 96 | terminalreporter.section("MPI Information") 97 | try: 98 | from mpi4py import MPI, rc, get_config 99 | except ImportError: 100 | terminalreporter.write("Unable to import mpi4py") 101 | else: 102 | comm = MPI.COMM_WORLD 103 | terminalreporter.write("rank: {}\n".format(comm.rank)) 104 | terminalreporter.write("size: {}\n".format(comm.size)) 105 | 106 | terminalreporter.write("MPI version: {}\n".format( 107 | '.'.join([str(v) for v in MPI.Get_version()]) 108 | )) 109 | terminalreporter.write("MPI library version: {}\n".format( 110 | MPI.Get_library_version() 111 | )) 112 | 113 | vendor, vendor_version = MPI.get_vendor() 114 | terminalreporter.write("MPI vendor: {} {}\n".format( 115 | vendor, '.'.join([str(v) for v in vendor_version]) 116 | )) 117 | 118 | terminalreporter.write("mpi4py rc: \n") 119 | for name, value in vars(rc).items(): 120 | terminalreporter.write(" {}: {}\n".format(name, value)) 121 | 122 | terminalreporter.write("mpi4py config:\n") 123 | for name, value in get_config().items(): 124 | terminalreporter.write(" {}: {}\n".format(name, value)) 125 | 126 | def pytest_runtest_setup(self, item): 127 | """ 128 | Hook for doing additional MPI-related checks on mpi marked tests 129 | """ 130 | if self._testing_mpi(item.config): 131 | for mark in item.iter_markers(name="mpi"): 132 | if mark.args: 133 | raise ValueError("mpi mark does not take positional args") 134 | try: 135 | from mpi4py import MPI 136 | except ImportError: 137 | pytest.fail("MPI tests require that mpi4py be installed") 138 | comm = MPI.COMM_WORLD 139 | min_size = mark.kwargs.get('min_size') 140 | if min_size is not None and comm.size < min_size: 141 | pytest.skip( 142 | "Test requires {} MPI processes, only {} MPI " 143 | "processes specified, skipping " 144 | "test".format(min_size, comm.size) 145 | ) 146 | 147 | 148 | @pytest.fixture 149 | def mpi_file_name(tmpdir, request): 150 | """ 151 | Provides a temporary file name which can be used under MPI from all MPI 152 | processes. 153 | 154 | This function avoids the need to ensure that only one process handles the 155 | naming of temporary files. 156 | """ 157 | try: 158 | from mpi4py import MPI 159 | except ImportError: 160 | pytest.fail("mpi4py needs to be installed to run this test") 161 | 162 | comm = MPI.COMM_WORLD 163 | rank = comm.Get_rank() 164 | 165 | # we only want to put the file inside one tmpdir, this creates the name 166 | # under one process, and passes it on to the others 167 | name = str(tmpdir.join(str(request.node) + '.hdf5')) if rank == 0 else None 168 | name = comm.bcast(name, root=0) 169 | return name 170 | 171 | 172 | @pytest.fixture 173 | def mpi_tmpdir(tmpdir): 174 | """ 175 | Wraps `pytest.tmpdir` so that it can be used under MPI from all MPI 176 | processes. 177 | 178 | This function avoids the need to ensure that only one process handles the 179 | naming of temporary folders. 180 | """ 181 | try: 182 | from mpi4py import MPI 183 | except ImportError: 184 | pytest.fail("mpi4py needs to be installed to run this test") 185 | 186 | comm = MPI.COMM_WORLD 187 | rank = comm.Get_rank() 188 | 189 | # we only want to put the file inside one tmpdir, this creates the name 190 | # under one process, and passes it on to the others 191 | name = str(tmpdir) if rank == 0 else None 192 | name = comm.bcast(name, root=0) 193 | return py.path.local(name) 194 | 195 | 196 | @pytest.fixture 197 | def mpi_tmp_path(tmp_path): 198 | """ 199 | Wraps `pytest.tmp_path` so that it can be used under MPI from all MPI 200 | processes. 201 | 202 | This function avoids the need to ensure that only one process handles the 203 | naming of temporary folders. 204 | """ 205 | try: 206 | from mpi4py import MPI 207 | except ImportError: 208 | pytest.fail("mpi4py needs to be installed to run this test") 209 | 210 | comm = MPI.COMM_WORLD 211 | rank = comm.Get_rank() 212 | 213 | # we only want to put the file inside one tmpdir, this creates the name 214 | # under one process, and passes it on to the others 215 | name = str(tmp_path) if rank == 0 else None 216 | name = comm.bcast(name, root=0) 217 | return Path(name) 218 | 219 | 220 | def pytest_configure(config): 221 | """ 222 | Add pytest-mpi to pytest (see pytest docs for more info) 223 | """ 224 | config.addinivalue_line( 225 | "markers", "mpi: Tests that require being run with MPI/mpirun" 226 | ) 227 | config.addinivalue_line( 228 | "markers", "mpi_break: Tests that cannot run under MPI/mpirun " 229 | "(deprecated)" 230 | ) 231 | config.addinivalue_line( 232 | "markers", "mpi_skip: Tests to skip when running MPI/mpirun" 233 | ) 234 | config.addinivalue_line( 235 | "markers", "mpi_xfail: Tests that fail when run under MPI/mpirun" 236 | ) 237 | config.pluginmanager.register(MPIPlugin()) 238 | 239 | 240 | def pytest_addoption(parser): 241 | """ 242 | Add pytest-mpi options to pytest cli 243 | """ 244 | group = parser.getgroup("mpi", description="support for MPI-enabled code") 245 | group.addoption( 246 | WITH_MPI_ARG, action="store_true", default=False, 247 | help="Run MPI tests, this should be paired with mpirun." 248 | ) 249 | group.addoption( 250 | ONLY_MPI_ARG, action="store_true", default=False, 251 | help="Run *only* MPI tests, this should be paired with mpirun." 252 | ) 253 | -------------------------------------------------------------------------------- /src/pytest_mpi/_helpers.py: -------------------------------------------------------------------------------- 1 | """ 2 | Internal helpers for testing only, do not use in main code 3 | """ 4 | import pytest 5 | 6 | 7 | def _fix_plural(**kwargs): 8 | """ 9 | Work around error -> errors change in pytest 6 10 | """ 11 | if int(pytest.__version__[0]) >= 6: 12 | return kwargs 13 | if "errors" in kwargs: 14 | errors = kwargs.pop("errors") 15 | kwargs["error"] = errors 16 | return kwargs 17 | -------------------------------------------------------------------------------- /src/pytest_mpi/_version.py: -------------------------------------------------------------------------------- 1 | 2 | # This file helps to compute a version number in source trees obtained from 3 | # git-archive tarball (such as those provided by githubs download-from-tag 4 | # feature). Distribution tarballs (built by setup.py sdist) and build 5 | # directories (produced by setup.py build) will contain a much shorter file 6 | # that just contains the computed version number. 7 | 8 | # This file is released into the public domain. Generated by 9 | # versioneer-0.21 (https://github.com/python-versioneer/python-versioneer) 10 | 11 | """Git implementation of _version.py.""" 12 | 13 | import errno 14 | import os 15 | import re 16 | import subprocess 17 | import sys 18 | from typing import Callable, Dict 19 | 20 | 21 | def get_keywords(): 22 | """Get the keywords needed to look up the version information.""" 23 | # these strings will be replaced by git during git-archive. 24 | # setup.py/versioneer.py will grep for the variable names, so they must 25 | # each be defined on a line of their own. _version.py will just call 26 | # get_keywords(). 27 | git_refnames = " (HEAD -> master)" 28 | git_full = "6729d76fa8a785d34ccdaec881c1980b6f4aa1ad" 29 | git_date = "2024-12-22 13:54:11 +1100" 30 | keywords = {"refnames": git_refnames, "full": git_full, "date": git_date} 31 | return keywords 32 | 33 | 34 | class VersioneerConfig: 35 | """Container for Versioneer configuration parameters.""" 36 | 37 | 38 | def get_config(): 39 | """Create, populate and return the VersioneerConfig() object.""" 40 | # these strings are filled in when 'setup.py versioneer' creates 41 | # _version.py 42 | cfg = VersioneerConfig() 43 | cfg.VCS = "git" 44 | cfg.style = "pep440" 45 | cfg.tag_prefix = "v" 46 | cfg.parentdir_prefix = "None" 47 | cfg.versionfile_source = "src/pytest_mpi/_version.py" 48 | cfg.verbose = False 49 | return cfg 50 | 51 | 52 | class NotThisMethod(Exception): 53 | """Exception raised if a method is not valid for the current scenario.""" 54 | 55 | 56 | LONG_VERSION_PY: Dict[str, str] = {} 57 | HANDLERS: Dict[str, Dict[str, Callable]] = {} 58 | 59 | 60 | def register_vcs_handler(vcs, method): # decorator 61 | """Create decorator to mark a method as the handler of a VCS.""" 62 | def decorate(f): 63 | """Store f in HANDLERS[vcs][method].""" 64 | if vcs not in HANDLERS: 65 | HANDLERS[vcs] = {} 66 | HANDLERS[vcs][method] = f 67 | return f 68 | return decorate 69 | 70 | 71 | def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, 72 | env=None): 73 | """Call the given command(s).""" 74 | assert isinstance(commands, list) 75 | process = None 76 | for command in commands: 77 | try: 78 | dispcmd = str([command] + args) 79 | # remember shell=False, so use git.cmd on windows, not just git 80 | process = subprocess.Popen([command] + args, cwd=cwd, env=env, 81 | stdout=subprocess.PIPE, 82 | stderr=(subprocess.PIPE if hide_stderr 83 | else None)) 84 | break 85 | except OSError: 86 | e = sys.exc_info()[1] 87 | if e.errno == errno.ENOENT: 88 | continue 89 | if verbose: 90 | print("unable to run %s" % dispcmd) 91 | print(e) 92 | return None, None 93 | else: 94 | if verbose: 95 | print("unable to find command, tried %s" % (commands,)) 96 | return None, None 97 | stdout = process.communicate()[0].strip().decode() 98 | if process.returncode != 0: 99 | if verbose: 100 | print("unable to run %s (error)" % dispcmd) 101 | print("stdout was %s" % stdout) 102 | return None, process.returncode 103 | return stdout, process.returncode 104 | 105 | 106 | def versions_from_parentdir(parentdir_prefix, root, verbose): 107 | """Try to determine the version from the parent directory name. 108 | 109 | Source tarballs conventionally unpack into a directory that includes both 110 | the project name and a version string. We will also support searching up 111 | two directory levels for an appropriately named parent directory 112 | """ 113 | rootdirs = [] 114 | 115 | for _ in range(3): 116 | dirname = os.path.basename(root) 117 | if dirname.startswith(parentdir_prefix): 118 | return {"version": dirname[len(parentdir_prefix):], 119 | "full-revisionid": None, 120 | "dirty": False, "error": None, "date": None} 121 | rootdirs.append(root) 122 | root = os.path.dirname(root) # up a level 123 | 124 | if verbose: 125 | print("Tried directories %s but none started with prefix %s" % 126 | (str(rootdirs), parentdir_prefix)) 127 | raise NotThisMethod("rootdir doesn't start with parentdir_prefix") 128 | 129 | 130 | @register_vcs_handler("git", "get_keywords") 131 | def git_get_keywords(versionfile_abs): 132 | """Extract version information from the given file.""" 133 | # the code embedded in _version.py can just fetch the value of these 134 | # keywords. When used from setup.py, we don't want to import _version.py, 135 | # so we do it with a regexp instead. This function is not used from 136 | # _version.py. 137 | keywords = {} 138 | try: 139 | with open(versionfile_abs, "r") as fobj: 140 | for line in fobj: 141 | if line.strip().startswith("git_refnames ="): 142 | mo = re.search(r'=\s*"(.*)"', line) 143 | if mo: 144 | keywords["refnames"] = mo.group(1) 145 | if line.strip().startswith("git_full ="): 146 | mo = re.search(r'=\s*"(.*)"', line) 147 | if mo: 148 | keywords["full"] = mo.group(1) 149 | if line.strip().startswith("git_date ="): 150 | mo = re.search(r'=\s*"(.*)"', line) 151 | if mo: 152 | keywords["date"] = mo.group(1) 153 | except OSError: 154 | pass 155 | return keywords 156 | 157 | 158 | @register_vcs_handler("git", "keywords") 159 | def git_versions_from_keywords(keywords, tag_prefix, verbose): 160 | """Get version information from git keywords.""" 161 | if "refnames" not in keywords: 162 | raise NotThisMethod("Short version file found") 163 | date = keywords.get("date") 164 | if date is not None: 165 | # Use only the last line. Previous lines may contain GPG signature 166 | # information. 167 | date = date.splitlines()[-1] 168 | 169 | # git-2.2.0 added "%cI", which expands to an ISO-8601 -compliant 170 | # datestamp. However we prefer "%ci" (which expands to an "ISO-8601 171 | # -like" string, which we must then edit to make compliant), because 172 | # it's been around since git-1.5.3, and it's too difficult to 173 | # discover which version we're using, or to work around using an 174 | # older one. 175 | date = date.strip().replace(" ", "T", 1).replace(" ", "", 1) 176 | refnames = keywords["refnames"].strip() 177 | if refnames.startswith("$Format"): 178 | if verbose: 179 | print("keywords are unexpanded, not using") 180 | raise NotThisMethod("unexpanded keywords, not a git-archive tarball") 181 | refs = {r.strip() for r in refnames.strip("()").split(",")} 182 | # starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of 183 | # just "foo-1.0". If we see a "tag: " prefix, prefer those. 184 | TAG = "tag: " 185 | tags = {r[len(TAG):] for r in refs if r.startswith(TAG)} 186 | if not tags: 187 | # Either we're using git < 1.8.3, or there really are no tags. We use 188 | # a heuristic: assume all version tags have a digit. The old git %d 189 | # expansion behaves like git log --decorate=short and strips out the 190 | # refs/heads/ and refs/tags/ prefixes that would let us distinguish 191 | # between branches and tags. By ignoring refnames without digits, we 192 | # filter out many common branch names like "release" and 193 | # "stabilization", as well as "HEAD" and "master". 194 | tags = {r for r in refs if re.search(r'\d', r)} 195 | if verbose: 196 | print("discarding '%s', no digits" % ",".join(refs - tags)) 197 | if verbose: 198 | print("likely tags: %s" % ",".join(sorted(tags))) 199 | for ref in sorted(tags): 200 | # sorting will prefer e.g. "2.0" over "2.0rc1" 201 | if ref.startswith(tag_prefix): 202 | r = ref[len(tag_prefix):] 203 | # Filter out refs that exactly match prefix or that don't start 204 | # with a number once the prefix is stripped (mostly a concern 205 | # when prefix is '') 206 | if not re.match(r'\d', r): 207 | continue 208 | if verbose: 209 | print("picking %s" % r) 210 | return {"version": r, 211 | "full-revisionid": keywords["full"].strip(), 212 | "dirty": False, "error": None, 213 | "date": date} 214 | # no suitable tags, so version is "0+unknown", but full hex is still there 215 | if verbose: 216 | print("no suitable tags, using unknown + full revision id") 217 | return {"version": "0+unknown", 218 | "full-revisionid": keywords["full"].strip(), 219 | "dirty": False, "error": "no suitable tags", "date": None} 220 | 221 | 222 | @register_vcs_handler("git", "pieces_from_vcs") 223 | def git_pieces_from_vcs(tag_prefix, root, verbose, runner=run_command): 224 | """Get version from 'git describe' in the root of the source tree. 225 | 226 | This only gets called if the git-archive 'subst' keywords were *not* 227 | expanded, and _version.py hasn't already been rewritten with a short 228 | version string, meaning we're inside a checked out source tree. 229 | """ 230 | GITS = ["git"] 231 | TAG_PREFIX_REGEX = "*" 232 | if sys.platform == "win32": 233 | GITS = ["git.cmd", "git.exe"] 234 | TAG_PREFIX_REGEX = r"\*" 235 | 236 | _, rc = runner(GITS, ["rev-parse", "--git-dir"], cwd=root, 237 | hide_stderr=True) 238 | if rc != 0: 239 | if verbose: 240 | print("Directory %s not under git control" % root) 241 | raise NotThisMethod("'git rev-parse --git-dir' returned error") 242 | 243 | # if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty] 244 | # if there isn't one, this yields HEX[-dirty] (no NUM) 245 | describe_out, rc = runner(GITS, ["describe", "--tags", "--dirty", 246 | "--always", "--long", 247 | "--match", 248 | "%s%s" % (tag_prefix, TAG_PREFIX_REGEX)], 249 | cwd=root) 250 | # --long was added in git-1.5.5 251 | if describe_out is None: 252 | raise NotThisMethod("'git describe' failed") 253 | describe_out = describe_out.strip() 254 | full_out, rc = runner(GITS, ["rev-parse", "HEAD"], cwd=root) 255 | if full_out is None: 256 | raise NotThisMethod("'git rev-parse' failed") 257 | full_out = full_out.strip() 258 | 259 | pieces = {} 260 | pieces["long"] = full_out 261 | pieces["short"] = full_out[:7] # maybe improved later 262 | pieces["error"] = None 263 | 264 | branch_name, rc = runner(GITS, ["rev-parse", "--abbrev-ref", "HEAD"], 265 | cwd=root) 266 | # --abbrev-ref was added in git-1.6.3 267 | if rc != 0 or branch_name is None: 268 | raise NotThisMethod("'git rev-parse --abbrev-ref' returned error") 269 | branch_name = branch_name.strip() 270 | 271 | if branch_name == "HEAD": 272 | # If we aren't exactly on a branch, pick a branch which represents 273 | # the current commit. If all else fails, we are on a branchless 274 | # commit. 275 | branches, rc = runner(GITS, ["branch", "--contains"], cwd=root) 276 | # --contains was added in git-1.5.4 277 | if rc != 0 or branches is None: 278 | raise NotThisMethod("'git branch --contains' returned error") 279 | branches = branches.split("\n") 280 | 281 | # Remove the first line if we're running detached 282 | if "(" in branches[0]: 283 | branches.pop(0) 284 | 285 | # Strip off the leading "* " from the list of branches. 286 | branches = [branch[2:] for branch in branches] 287 | if "master" in branches: 288 | branch_name = "master" 289 | elif not branches: 290 | branch_name = None 291 | else: 292 | # Pick the first branch that is returned. Good or bad. 293 | branch_name = branches[0] 294 | 295 | pieces["branch"] = branch_name 296 | 297 | # parse describe_out. It will be like TAG-NUM-gHEX[-dirty] or HEX[-dirty] 298 | # TAG might have hyphens. 299 | git_describe = describe_out 300 | 301 | # look for -dirty suffix 302 | dirty = git_describe.endswith("-dirty") 303 | pieces["dirty"] = dirty 304 | if dirty: 305 | git_describe = git_describe[:git_describe.rindex("-dirty")] 306 | 307 | # now we have TAG-NUM-gHEX or HEX 308 | 309 | if "-" in git_describe: 310 | # TAG-NUM-gHEX 311 | mo = re.search(r'^(.+)-(\d+)-g([0-9a-f]+)$', git_describe) 312 | if not mo: 313 | # unparsable. Maybe git-describe is misbehaving? 314 | pieces["error"] = ("unable to parse git-describe output: '%s'" 315 | % describe_out) 316 | return pieces 317 | 318 | # tag 319 | full_tag = mo.group(1) 320 | if not full_tag.startswith(tag_prefix): 321 | if verbose: 322 | fmt = "tag '%s' doesn't start with prefix '%s'" 323 | print(fmt % (full_tag, tag_prefix)) 324 | pieces["error"] = ("tag '%s' doesn't start with prefix '%s'" 325 | % (full_tag, tag_prefix)) 326 | return pieces 327 | pieces["closest-tag"] = full_tag[len(tag_prefix):] 328 | 329 | # distance: number of commits since tag 330 | pieces["distance"] = int(mo.group(2)) 331 | 332 | # commit: short hex revision ID 333 | pieces["short"] = mo.group(3) 334 | 335 | else: 336 | # HEX: no tags 337 | pieces["closest-tag"] = None 338 | count_out, rc = runner(GITS, ["rev-list", "HEAD", "--count"], cwd=root) 339 | pieces["distance"] = int(count_out) # total number of commits 340 | 341 | # commit date: see ISO-8601 comment in git_versions_from_keywords() 342 | date = runner(GITS, ["show", "-s", "--format=%ci", "HEAD"], cwd=root)[0].strip() 343 | # Use only the last line. Previous lines may contain GPG signature 344 | # information. 345 | date = date.splitlines()[-1] 346 | pieces["date"] = date.strip().replace(" ", "T", 1).replace(" ", "", 1) 347 | 348 | return pieces 349 | 350 | 351 | def plus_or_dot(pieces): 352 | """Return a + if we don't already have one, else return a .""" 353 | if "+" in pieces.get("closest-tag", ""): 354 | return "." 355 | return "+" 356 | 357 | 358 | def render_pep440(pieces): 359 | """Build up version string, with post-release "local version identifier". 360 | 361 | Our goal: TAG[+DISTANCE.gHEX[.dirty]] . Note that if you 362 | get a tagged build and then dirty it, you'll get TAG+0.gHEX.dirty 363 | 364 | Exceptions: 365 | 1: no tags. git_describe was just HEX. 0+untagged.DISTANCE.gHEX[.dirty] 366 | """ 367 | if pieces["closest-tag"]: 368 | rendered = pieces["closest-tag"] 369 | if pieces["distance"] or pieces["dirty"]: 370 | rendered += plus_or_dot(pieces) 371 | rendered += "%d.g%s" % (pieces["distance"], pieces["short"]) 372 | if pieces["dirty"]: 373 | rendered += ".dirty" 374 | else: 375 | # exception #1 376 | rendered = "0+untagged.%d.g%s" % (pieces["distance"], 377 | pieces["short"]) 378 | if pieces["dirty"]: 379 | rendered += ".dirty" 380 | return rendered 381 | 382 | 383 | def render_pep440_branch(pieces): 384 | """TAG[[.dev0]+DISTANCE.gHEX[.dirty]] . 385 | 386 | The ".dev0" means not master branch. Note that .dev0 sorts backwards 387 | (a feature branch will appear "older" than the master branch). 388 | 389 | Exceptions: 390 | 1: no tags. 0[.dev0]+untagged.DISTANCE.gHEX[.dirty] 391 | """ 392 | if pieces["closest-tag"]: 393 | rendered = pieces["closest-tag"] 394 | if pieces["distance"] or pieces["dirty"]: 395 | if pieces["branch"] != "master": 396 | rendered += ".dev0" 397 | rendered += plus_or_dot(pieces) 398 | rendered += "%d.g%s" % (pieces["distance"], pieces["short"]) 399 | if pieces["dirty"]: 400 | rendered += ".dirty" 401 | else: 402 | # exception #1 403 | rendered = "0" 404 | if pieces["branch"] != "master": 405 | rendered += ".dev0" 406 | rendered += "+untagged.%d.g%s" % (pieces["distance"], 407 | pieces["short"]) 408 | if pieces["dirty"]: 409 | rendered += ".dirty" 410 | return rendered 411 | 412 | 413 | def pep440_split_post(ver): 414 | """Split pep440 version string at the post-release segment. 415 | 416 | Returns the release segments before the post-release and the 417 | post-release version number (or -1 if no post-release segment is present). 418 | """ 419 | vc = str.split(ver, ".post") 420 | return vc[0], int(vc[1] or 0) if len(vc) == 2 else None 421 | 422 | 423 | def render_pep440_pre(pieces): 424 | """TAG[.postN.devDISTANCE] -- No -dirty. 425 | 426 | Exceptions: 427 | 1: no tags. 0.post0.devDISTANCE 428 | """ 429 | if pieces["closest-tag"]: 430 | if pieces["distance"]: 431 | # update the post release segment 432 | tag_version, post_version = pep440_split_post(pieces["closest-tag"]) 433 | rendered = tag_version 434 | if post_version is not None: 435 | rendered += ".post%d.dev%d" % (post_version+1, pieces["distance"]) 436 | else: 437 | rendered += ".post0.dev%d" % (pieces["distance"]) 438 | else: 439 | # no commits, use the tag as the version 440 | rendered = pieces["closest-tag"] 441 | else: 442 | # exception #1 443 | rendered = "0.post0.dev%d" % pieces["distance"] 444 | return rendered 445 | 446 | 447 | def render_pep440_post(pieces): 448 | """TAG[.postDISTANCE[.dev0]+gHEX] . 449 | 450 | The ".dev0" means dirty. Note that .dev0 sorts backwards 451 | (a dirty tree will appear "older" than the corresponding clean one), 452 | but you shouldn't be releasing software with -dirty anyways. 453 | 454 | Exceptions: 455 | 1: no tags. 0.postDISTANCE[.dev0] 456 | """ 457 | if pieces["closest-tag"]: 458 | rendered = pieces["closest-tag"] 459 | if pieces["distance"] or pieces["dirty"]: 460 | rendered += ".post%d" % pieces["distance"] 461 | if pieces["dirty"]: 462 | rendered += ".dev0" 463 | rendered += plus_or_dot(pieces) 464 | rendered += "g%s" % pieces["short"] 465 | else: 466 | # exception #1 467 | rendered = "0.post%d" % pieces["distance"] 468 | if pieces["dirty"]: 469 | rendered += ".dev0" 470 | rendered += "+g%s" % pieces["short"] 471 | return rendered 472 | 473 | 474 | def render_pep440_post_branch(pieces): 475 | """TAG[.postDISTANCE[.dev0]+gHEX[.dirty]] . 476 | 477 | The ".dev0" means not master branch. 478 | 479 | Exceptions: 480 | 1: no tags. 0.postDISTANCE[.dev0]+gHEX[.dirty] 481 | """ 482 | if pieces["closest-tag"]: 483 | rendered = pieces["closest-tag"] 484 | if pieces["distance"] or pieces["dirty"]: 485 | rendered += ".post%d" % pieces["distance"] 486 | if pieces["branch"] != "master": 487 | rendered += ".dev0" 488 | rendered += plus_or_dot(pieces) 489 | rendered += "g%s" % pieces["short"] 490 | if pieces["dirty"]: 491 | rendered += ".dirty" 492 | else: 493 | # exception #1 494 | rendered = "0.post%d" % pieces["distance"] 495 | if pieces["branch"] != "master": 496 | rendered += ".dev0" 497 | rendered += "+g%s" % pieces["short"] 498 | if pieces["dirty"]: 499 | rendered += ".dirty" 500 | return rendered 501 | 502 | 503 | def render_pep440_old(pieces): 504 | """TAG[.postDISTANCE[.dev0]] . 505 | 506 | The ".dev0" means dirty. 507 | 508 | Exceptions: 509 | 1: no tags. 0.postDISTANCE[.dev0] 510 | """ 511 | if pieces["closest-tag"]: 512 | rendered = pieces["closest-tag"] 513 | if pieces["distance"] or pieces["dirty"]: 514 | rendered += ".post%d" % pieces["distance"] 515 | if pieces["dirty"]: 516 | rendered += ".dev0" 517 | else: 518 | # exception #1 519 | rendered = "0.post%d" % pieces["distance"] 520 | if pieces["dirty"]: 521 | rendered += ".dev0" 522 | return rendered 523 | 524 | 525 | def render_git_describe(pieces): 526 | """TAG[-DISTANCE-gHEX][-dirty]. 527 | 528 | Like 'git describe --tags --dirty --always'. 529 | 530 | Exceptions: 531 | 1: no tags. HEX[-dirty] (note: no 'g' prefix) 532 | """ 533 | if pieces["closest-tag"]: 534 | rendered = pieces["closest-tag"] 535 | if pieces["distance"]: 536 | rendered += "-%d-g%s" % (pieces["distance"], pieces["short"]) 537 | else: 538 | # exception #1 539 | rendered = pieces["short"] 540 | if pieces["dirty"]: 541 | rendered += "-dirty" 542 | return rendered 543 | 544 | 545 | def render_git_describe_long(pieces): 546 | """TAG-DISTANCE-gHEX[-dirty]. 547 | 548 | Like 'git describe --tags --dirty --always -long'. 549 | The distance/hash is unconditional. 550 | 551 | Exceptions: 552 | 1: no tags. HEX[-dirty] (note: no 'g' prefix) 553 | """ 554 | if pieces["closest-tag"]: 555 | rendered = pieces["closest-tag"] 556 | rendered += "-%d-g%s" % (pieces["distance"], pieces["short"]) 557 | else: 558 | # exception #1 559 | rendered = pieces["short"] 560 | if pieces["dirty"]: 561 | rendered += "-dirty" 562 | return rendered 563 | 564 | 565 | def render(pieces, style): 566 | """Render the given version pieces into the requested style.""" 567 | if pieces["error"]: 568 | return {"version": "unknown", 569 | "full-revisionid": pieces.get("long"), 570 | "dirty": None, 571 | "error": pieces["error"], 572 | "date": None} 573 | 574 | if not style or style == "default": 575 | style = "pep440" # the default 576 | 577 | if style == "pep440": 578 | rendered = render_pep440(pieces) 579 | elif style == "pep440-branch": 580 | rendered = render_pep440_branch(pieces) 581 | elif style == "pep440-pre": 582 | rendered = render_pep440_pre(pieces) 583 | elif style == "pep440-post": 584 | rendered = render_pep440_post(pieces) 585 | elif style == "pep440-post-branch": 586 | rendered = render_pep440_post_branch(pieces) 587 | elif style == "pep440-old": 588 | rendered = render_pep440_old(pieces) 589 | elif style == "git-describe": 590 | rendered = render_git_describe(pieces) 591 | elif style == "git-describe-long": 592 | rendered = render_git_describe_long(pieces) 593 | else: 594 | raise ValueError("unknown style '%s'" % style) 595 | 596 | return {"version": rendered, "full-revisionid": pieces["long"], 597 | "dirty": pieces["dirty"], "error": None, 598 | "date": pieces.get("date")} 599 | 600 | 601 | def get_versions(): 602 | """Get version information or return default if unable to do so.""" 603 | # I am in _version.py, which lives at ROOT/VERSIONFILE_SOURCE. If we have 604 | # __file__, we can work backwards from there to the root. Some 605 | # py2exe/bbfreeze/non-CPython implementations don't do __file__, in which 606 | # case we can only use expanded keywords. 607 | 608 | cfg = get_config() 609 | verbose = cfg.verbose 610 | 611 | try: 612 | return git_versions_from_keywords(get_keywords(), cfg.tag_prefix, 613 | verbose) 614 | except NotThisMethod: 615 | pass 616 | 617 | try: 618 | root = os.path.realpath(__file__) 619 | # versionfile_source is the relative path from the top of the source 620 | # tree (where the .git directory might live) to this file. Invert 621 | # this to find the root from __file__. 622 | for _ in cfg.versionfile_source.split('/'): 623 | root = os.path.dirname(root) 624 | except NameError: 625 | return {"version": "0+unknown", "full-revisionid": None, 626 | "dirty": None, 627 | "error": "unable to find root of source tree", 628 | "date": None} 629 | 630 | try: 631 | pieces = git_pieces_from_vcs(cfg.tag_prefix, root, verbose) 632 | return render(pieces, cfg.style) 633 | except NotThisMethod: 634 | pass 635 | 636 | try: 637 | if cfg.parentdir_prefix: 638 | return versions_from_parentdir(cfg.parentdir_prefix, root, verbose) 639 | except NotThisMethod: 640 | pass 641 | 642 | return {"version": "0+unknown", "full-revisionid": None, 643 | "dirty": None, 644 | "error": "unable to compute version", "date": None} 645 | -------------------------------------------------------------------------------- /tests/conftest.py: -------------------------------------------------------------------------------- 1 | from logging import getLogger 2 | import sys 3 | 4 | import py 5 | import pytest 6 | 7 | log = getLogger(__name__) 8 | MPI_ARGS = ("mpirun", "-n") 9 | PYTEST_ARGS = (sys.executable, "-mpytest") 10 | 11 | 12 | @pytest.fixture 13 | def has_mpi4py(): 14 | try: 15 | import mpi4py 16 | return True 17 | except ImportError: 18 | return False 19 | 20 | 21 | def _to_py_path(p): 22 | return py.path.local(p) 23 | 24 | 25 | def _to_pathlib(p): 26 | from pathlib import Path 27 | return Path(p) 28 | 29 | 30 | class MPITestdir(object): 31 | def __init__(self, request, config): 32 | method = request.config.getoption("--runpytest") 33 | if method == "inprocess": 34 | log.warn("To run the MPI tests, you need to use subprocesses") 35 | self._pytester = None 36 | self._testdir = None 37 | self._setup(request, config) 38 | 39 | def _setup(self, request, config): 40 | """ 41 | This handles the difference between Testdir and PyTester 42 | """ 43 | try: 44 | self._pytester = request.getfixturevalue("pytester") 45 | except: 46 | try: 47 | self._testdir = request.getfixturevalue("testdir") 48 | except: 49 | raise RuntimeError( 50 | "Unable to load either pytester or testdir fixtures. " 51 | "Check if pytester plugin is enabled." 52 | ) 53 | 54 | def makepyfile(self, *args, **kwargs): 55 | if self._pytester is not None: 56 | self._pytester.makepyfile(*args, **kwargs) 57 | else: 58 | self._testdir.makepyfile(*args, **kwargs) 59 | 60 | def runpytest(self, *args, **kwargs): 61 | if self._pytester is not None: 62 | return self._run_pytester(*args, **kwargs) 63 | return self._run_testdir(*args, **kwargs) 64 | 65 | def _run_testdir(self, *args, timeout=60, mpi_procs=2, max_retries=5): 66 | retries = 0 67 | p = py.path.local.make_numbered_dir( 68 | prefix="runpytest-", keep=None, rootdir=self._testdir.tmpdir 69 | ) 70 | args = ("--basetemp=%s" % p,) + args 71 | plugins = [x for x in self._testdir.plugins if isinstance(x, str)] 72 | if plugins: 73 | args = ("-p", plugins[0]) + args 74 | args = MPI_ARGS + (str(mpi_procs),) + PYTEST_ARGS + args 75 | while retries < max_retries: 76 | try: 77 | return self._testdir.run(*args, timeout=timeout) 78 | except self._testdir.TimeoutExpired as e: 79 | retries += 1 80 | if retries >= max_retries: 81 | raise 82 | raise e 83 | 84 | def _run_pytester(self, *args, timeout=60, mpi_procs=2, max_retries=5): 85 | retries = 0 86 | p = _to_pathlib(py.path.local.make_numbered_dir( 87 | prefix="runpytest-", keep=None, 88 | rootdir=_to_py_path(self._pytester.path) 89 | )) 90 | args = ("--basetemp=%s" % p,) + args 91 | plugins = [x for x in self._pytester.plugins if isinstance(x, str)] 92 | if plugins: 93 | args = ("-p", plugins[0]) + args 94 | args = MPI_ARGS + (str(mpi_procs),) + PYTEST_ARGS + args 95 | while retries < max_retries: 96 | try: 97 | return self._pytester.run(*args, timeout=timeout) 98 | except self._pytester.TimeoutExpired as e: 99 | retries += 1 100 | if retries >= max_retries: 101 | raise 102 | raise e 103 | 104 | 105 | @pytest.fixture 106 | def mpi_testdir(request, pytestconfig): 107 | return MPITestdir(request, pytestconfig) 108 | -------------------------------------------------------------------------------- /tests/test_fixtures.py: -------------------------------------------------------------------------------- 1 | from pytest_mpi._helpers import _fix_plural 2 | 3 | MPI_FILE_NAME_TEST_CODE = """ 4 | import pytest 5 | 6 | def test_file_name(mpi_file_name): 7 | from mpi4py import MPI 8 | comm = MPI.COMM_WORLD 9 | 10 | name = str(mpi_file_name) 11 | names = comm.gather(name, root=0) 12 | if comm.rank == 0: 13 | for n in names: 14 | assert n == name 15 | else: 16 | assert names is None 17 | 18 | """ 19 | MPI_TMPDIR_TEST_CODE = """ 20 | import pytest 21 | 22 | def test_file_name(mpi_tmpdir): 23 | from mpi4py import MPI 24 | comm = MPI.COMM_WORLD 25 | 26 | name = str(mpi_tmpdir) 27 | names = comm.gather(name, root=0) 28 | if comm.rank == 0: 29 | for n in names: 30 | assert n == name 31 | else: 32 | assert names is None 33 | 34 | """ 35 | MPI_TMP_PATH_TEST_CODE = """ 36 | import pytest 37 | 38 | def test_file_name(mpi_tmp_path): 39 | from mpi4py import MPI 40 | comm = MPI.COMM_WORLD 41 | 42 | name = str(mpi_tmp_path) 43 | names = comm.gather(name, root=0) 44 | if comm.rank == 0: 45 | for n in names: 46 | assert n == name 47 | else: 48 | assert names is None 49 | 50 | """ 51 | 52 | 53 | def test_mpi_file_name(mpi_testdir, has_mpi4py): 54 | mpi_testdir.makepyfile(MPI_FILE_NAME_TEST_CODE) 55 | 56 | result = mpi_testdir.runpytest("--with-mpi", timeout=5) 57 | 58 | if has_mpi4py: 59 | result.assert_outcomes(passed=1) 60 | else: 61 | result.assert_outcomes(**_fix_plural(errors=1)) 62 | 63 | 64 | def test_mpi_tmpdir(mpi_testdir, has_mpi4py): 65 | mpi_testdir.makepyfile(MPI_TMPDIR_TEST_CODE) 66 | 67 | result = mpi_testdir.runpytest("--with-mpi", timeout=5) 68 | 69 | if has_mpi4py: 70 | result.assert_outcomes(passed=1) 71 | else: 72 | result.assert_outcomes(**_fix_plural(errors=1)) 73 | 74 | 75 | def test_mpi_tmp_path(mpi_testdir, has_mpi4py): 76 | mpi_testdir.makepyfile(MPI_TMP_PATH_TEST_CODE) 77 | 78 | result = mpi_testdir.runpytest("--with-mpi", timeout=5) 79 | 80 | if has_mpi4py: 81 | result.assert_outcomes(passed=1) 82 | else: 83 | result.assert_outcomes(**_fix_plural(errors=1)) 84 | -------------------------------------------------------------------------------- /tests/test_markers.py: -------------------------------------------------------------------------------- 1 | from pytest_mpi._helpers import _fix_plural 2 | 3 | MPI_TEST_CODE = """ 4 | import pytest 5 | 6 | @pytest.mark.mpi 7 | def test_size(): 8 | from mpi4py import MPI 9 | comm = MPI.COMM_WORLD 10 | 11 | assert comm.size > 0 12 | 13 | @pytest.mark.mpi(min_size=2) 14 | def test_size_min_2(): 15 | from mpi4py import MPI 16 | comm = MPI.COMM_WORLD 17 | 18 | assert comm.size >= 2 19 | 20 | @pytest.mark.mpi(min_size=4) 21 | def test_size_min_4(): 22 | from mpi4py import MPI 23 | comm = MPI.COMM_WORLD 24 | 25 | assert comm.size >= 4 26 | 27 | @pytest.mark.mpi(2) 28 | def test_size_fail_pos(): 29 | from mpi4py import MPI 30 | comm = MPI.COMM_WORLD 31 | 32 | assert comm.size > 0 33 | 34 | def test_no_mpi(): 35 | assert True 36 | """ 37 | MPI_SKIP_TEST_CODE = """ 38 | import pytest 39 | 40 | @pytest.mark.mpi_skip 41 | def test_skip(): 42 | assert True 43 | """ 44 | MPI_XFAIL_TEST_CODE = """ 45 | import pytest 46 | 47 | @pytest.mark.mpi_xfail 48 | def test_xfail(): 49 | try: 50 | from mpi4py import MPI 51 | comm = MPI.COMM_WORLD 52 | assert comm.size < 2 53 | except ImportError: 54 | assert True 55 | """ 56 | 57 | 58 | def test_mpi(testdir): 59 | testdir.makepyfile(MPI_TEST_CODE) 60 | 61 | result = testdir.runpytest() 62 | 63 | result.assert_outcomes(skipped=4, passed=1) 64 | 65 | 66 | def test_mpi_with_mpi(mpi_testdir, has_mpi4py): 67 | mpi_testdir.makepyfile(MPI_TEST_CODE) 68 | 69 | result = mpi_testdir.runpytest("--with-mpi") 70 | 71 | if has_mpi4py: 72 | result.assert_outcomes(**_fix_plural(passed=3, errors=1, skipped=1)) 73 | else: 74 | result.assert_outcomes(**_fix_plural(passed=1, errors=4)) 75 | 76 | 77 | def test_mpi_only_mpi(mpi_testdir, has_mpi4py): 78 | mpi_testdir.makepyfile(MPI_TEST_CODE) 79 | 80 | result = mpi_testdir.runpytest("--only-mpi") 81 | 82 | if has_mpi4py: 83 | result.assert_outcomes(**_fix_plural(passed=2, errors=1, skipped=2)) 84 | else: 85 | result.assert_outcomes(**_fix_plural(errors=4, skipped=1)) 86 | 87 | 88 | def test_mpi_skip(testdir): 89 | testdir.makepyfile(MPI_SKIP_TEST_CODE) 90 | 91 | result = testdir.runpytest() 92 | 93 | result.assert_outcomes(passed=1) 94 | 95 | 96 | def test_mpi_skip_under_mpi(mpi_testdir): 97 | mpi_testdir.makepyfile(MPI_SKIP_TEST_CODE) 98 | 99 | result = mpi_testdir.runpytest("--with-mpi") 100 | 101 | result.assert_outcomes(skipped=1) 102 | 103 | 104 | def test_mpi_xfail(testdir): 105 | testdir.makepyfile(MPI_XFAIL_TEST_CODE) 106 | 107 | result = testdir.runpytest() 108 | 109 | result.assert_outcomes(passed=1) 110 | 111 | 112 | def test_mpi_xfail_under_mpi(mpi_testdir, has_mpi4py): 113 | mpi_testdir.makepyfile(MPI_XFAIL_TEST_CODE) 114 | 115 | result = mpi_testdir.runpytest("--with-mpi") 116 | 117 | if has_mpi4py: 118 | result.assert_outcomes(xfailed=1) 119 | else: 120 | result.assert_outcomes(xpassed=1) 121 | -------------------------------------------------------------------------------- /tox.ini: -------------------------------------------------------------------------------- 1 | # Tox (http://tox.testrun.org/) is a tool for running tests 2 | # in multiple virtualenvs. This configuration file will run the 3 | # test suite on all supported python versions. To use it, "pip install tox" 4 | # and then run "tox" from this directory. 5 | 6 | [tox] 7 | envlist = py{37,38,39,310,311}{-mpi,},flake8,pylint,docs,check-manifest,checkreadme 8 | 9 | [testenv] 10 | commands = 11 | pytest -p pytester --cov={envsitepackagesdir}/pytest_mpi --runpytest=subprocess {posargs} 12 | deps = 13 | pytest 14 | pytest-cov 15 | sybil 16 | -c known_broken_constraints.txt 17 | mpi: mpi4py 18 | basepython = 19 | py37: {env:TOXPYTHON:python3.7} 20 | py38: {env:TOXPYTHON:python3.8} 21 | py39: {env:TOXPYTHON:python3.9} 22 | py310: {env:TOXPYTHON:python3.10} 23 | py311: {env:TOXPYTHON:python3.11} 24 | py312: {env:TOXPYTHON:python3.12} 25 | flake8: {env:TOXPYTHON:python3} 26 | pylint: {env:TOXPYTHON:python3} 27 | docs: {env:TOXPYTHON:python3} 28 | check-manifest: {env:TOXPYTHON:python3} 29 | checkreadme: {env:TOXPYTHON:python3} 30 | 31 | [testenv:docs] 32 | changedir=docs 33 | deps=-rdoc-requirements.txt 34 | commands= 35 | sphinx-build -W -b html -d {envtmpdir}/doctrees . {envtmpdir}/html 36 | 37 | [testenv:flake8] 38 | deps=flake8 39 | commands= 40 | flake8 --exclude={envsitepackagesdir}/pytest_mpi/_version.py {envsitepackagesdir}/pytest_mpi 41 | 42 | [testenv:pylint] 43 | deps=pylint 44 | commands= 45 | pylint {envsitepackagesdir}/pytest_mpi 46 | 47 | [testenv:check-manifest] 48 | deps=check-manifest 49 | setenv = 50 | CHECK_MANIFEST=true 51 | commands= 52 | check-manifest 53 | 54 | [testenv:checkreadme] 55 | deps=readme_renderer 56 | commands= 57 | python setup.py check -s -r 58 | -------------------------------------------------------------------------------- /versioneer.py: -------------------------------------------------------------------------------- 1 | 2 | # Version: 0.21 3 | 4 | """The Versioneer - like a rocketeer, but for versions. 5 | 6 | The Versioneer 7 | ============== 8 | 9 | * like a rocketeer, but for versions! 10 | * https://github.com/python-versioneer/python-versioneer 11 | * Brian Warner 12 | * License: Public Domain 13 | * Compatible with: Python 3.6, 3.7, 3.8, 3.9 and pypy3 14 | * [![Latest Version][pypi-image]][pypi-url] 15 | * [![Build Status][travis-image]][travis-url] 16 | 17 | This is a tool for managing a recorded version number in distutils-based 18 | python projects. The goal is to remove the tedious and error-prone "update 19 | the embedded version string" step from your release process. Making a new 20 | release should be as easy as recording a new tag in your version-control 21 | system, and maybe making new tarballs. 22 | 23 | 24 | ## Quick Install 25 | 26 | * `pip install versioneer` to somewhere in your $PATH 27 | * add a `[versioneer]` section to your setup.cfg (see [Install](INSTALL.md)) 28 | * run `versioneer install` in your source tree, commit the results 29 | * Verify version information with `python setup.py version` 30 | 31 | ## Version Identifiers 32 | 33 | Source trees come from a variety of places: 34 | 35 | * a version-control system checkout (mostly used by developers) 36 | * a nightly tarball, produced by build automation 37 | * a snapshot tarball, produced by a web-based VCS browser, like github's 38 | "tarball from tag" feature 39 | * a release tarball, produced by "setup.py sdist", distributed through PyPI 40 | 41 | Within each source tree, the version identifier (either a string or a number, 42 | this tool is format-agnostic) can come from a variety of places: 43 | 44 | * ask the VCS tool itself, e.g. "git describe" (for checkouts), which knows 45 | about recent "tags" and an absolute revision-id 46 | * the name of the directory into which the tarball was unpacked 47 | * an expanded VCS keyword ($Id$, etc) 48 | * a `_version.py` created by some earlier build step 49 | 50 | For released software, the version identifier is closely related to a VCS 51 | tag. Some projects use tag names that include more than just the version 52 | string (e.g. "myproject-1.2" instead of just "1.2"), in which case the tool 53 | needs to strip the tag prefix to extract the version identifier. For 54 | unreleased software (between tags), the version identifier should provide 55 | enough information to help developers recreate the same tree, while also 56 | giving them an idea of roughly how old the tree is (after version 1.2, before 57 | version 1.3). Many VCS systems can report a description that captures this, 58 | for example `git describe --tags --dirty --always` reports things like 59 | "0.7-1-g574ab98-dirty" to indicate that the checkout is one revision past the 60 | 0.7 tag, has a unique revision id of "574ab98", and is "dirty" (it has 61 | uncommitted changes). 62 | 63 | The version identifier is used for multiple purposes: 64 | 65 | * to allow the module to self-identify its version: `myproject.__version__` 66 | * to choose a name and prefix for a 'setup.py sdist' tarball 67 | 68 | ## Theory of Operation 69 | 70 | Versioneer works by adding a special `_version.py` file into your source 71 | tree, where your `__init__.py` can import it. This `_version.py` knows how to 72 | dynamically ask the VCS tool for version information at import time. 73 | 74 | `_version.py` also contains `$Revision$` markers, and the installation 75 | process marks `_version.py` to have this marker rewritten with a tag name 76 | during the `git archive` command. As a result, generated tarballs will 77 | contain enough information to get the proper version. 78 | 79 | To allow `setup.py` to compute a version too, a `versioneer.py` is added to 80 | the top level of your source tree, next to `setup.py` and the `setup.cfg` 81 | that configures it. This overrides several distutils/setuptools commands to 82 | compute the version when invoked, and changes `setup.py build` and `setup.py 83 | sdist` to replace `_version.py` with a small static file that contains just 84 | the generated version data. 85 | 86 | ## Installation 87 | 88 | See [INSTALL.md](./INSTALL.md) for detailed installation instructions. 89 | 90 | ## Version-String Flavors 91 | 92 | Code which uses Versioneer can learn about its version string at runtime by 93 | importing `_version` from your main `__init__.py` file and running the 94 | `get_versions()` function. From the "outside" (e.g. in `setup.py`), you can 95 | import the top-level `versioneer.py` and run `get_versions()`. 96 | 97 | Both functions return a dictionary with different flavors of version 98 | information: 99 | 100 | * `['version']`: A condensed version string, rendered using the selected 101 | style. This is the most commonly used value for the project's version 102 | string. The default "pep440" style yields strings like `0.11`, 103 | `0.11+2.g1076c97`, or `0.11+2.g1076c97.dirty`. See the "Styles" section 104 | below for alternative styles. 105 | 106 | * `['full-revisionid']`: detailed revision identifier. For Git, this is the 107 | full SHA1 commit id, e.g. "1076c978a8d3cfc70f408fe5974aa6c092c949ac". 108 | 109 | * `['date']`: Date and time of the latest `HEAD` commit. For Git, it is the 110 | commit date in ISO 8601 format. This will be None if the date is not 111 | available. 112 | 113 | * `['dirty']`: a boolean, True if the tree has uncommitted changes. Note that 114 | this is only accurate if run in a VCS checkout, otherwise it is likely to 115 | be False or None 116 | 117 | * `['error']`: if the version string could not be computed, this will be set 118 | to a string describing the problem, otherwise it will be None. It may be 119 | useful to throw an exception in setup.py if this is set, to avoid e.g. 120 | creating tarballs with a version string of "unknown". 121 | 122 | Some variants are more useful than others. Including `full-revisionid` in a 123 | bug report should allow developers to reconstruct the exact code being tested 124 | (or indicate the presence of local changes that should be shared with the 125 | developers). `version` is suitable for display in an "about" box or a CLI 126 | `--version` output: it can be easily compared against release notes and lists 127 | of bugs fixed in various releases. 128 | 129 | The installer adds the following text to your `__init__.py` to place a basic 130 | version in `YOURPROJECT.__version__`: 131 | 132 | from ._version import get_versions 133 | __version__ = get_versions()['version'] 134 | del get_versions 135 | 136 | ## Styles 137 | 138 | The setup.cfg `style=` configuration controls how the VCS information is 139 | rendered into a version string. 140 | 141 | The default style, "pep440", produces a PEP440-compliant string, equal to the 142 | un-prefixed tag name for actual releases, and containing an additional "local 143 | version" section with more detail for in-between builds. For Git, this is 144 | TAG[+DISTANCE.gHEX[.dirty]] , using information from `git describe --tags 145 | --dirty --always`. For example "0.11+2.g1076c97.dirty" indicates that the 146 | tree is like the "1076c97" commit but has uncommitted changes (".dirty"), and 147 | that this commit is two revisions ("+2") beyond the "0.11" tag. For released 148 | software (exactly equal to a known tag), the identifier will only contain the 149 | stripped tag, e.g. "0.11". 150 | 151 | Other styles are available. See [details.md](details.md) in the Versioneer 152 | source tree for descriptions. 153 | 154 | ## Debugging 155 | 156 | Versioneer tries to avoid fatal errors: if something goes wrong, it will tend 157 | to return a version of "0+unknown". To investigate the problem, run `setup.py 158 | version`, which will run the version-lookup code in a verbose mode, and will 159 | display the full contents of `get_versions()` (including the `error` string, 160 | which may help identify what went wrong). 161 | 162 | ## Known Limitations 163 | 164 | Some situations are known to cause problems for Versioneer. This details the 165 | most significant ones. More can be found on Github 166 | [issues page](https://github.com/python-versioneer/python-versioneer/issues). 167 | 168 | ### Subprojects 169 | 170 | Versioneer has limited support for source trees in which `setup.py` is not in 171 | the root directory (e.g. `setup.py` and `.git/` are *not* siblings). The are 172 | two common reasons why `setup.py` might not be in the root: 173 | 174 | * Source trees which contain multiple subprojects, such as 175 | [Buildbot](https://github.com/buildbot/buildbot), which contains both 176 | "master" and "slave" subprojects, each with their own `setup.py`, 177 | `setup.cfg`, and `tox.ini`. Projects like these produce multiple PyPI 178 | distributions (and upload multiple independently-installable tarballs). 179 | * Source trees whose main purpose is to contain a C library, but which also 180 | provide bindings to Python (and perhaps other languages) in subdirectories. 181 | 182 | Versioneer will look for `.git` in parent directories, and most operations 183 | should get the right version string. However `pip` and `setuptools` have bugs 184 | and implementation details which frequently cause `pip install .` from a 185 | subproject directory to fail to find a correct version string (so it usually 186 | defaults to `0+unknown`). 187 | 188 | `pip install --editable .` should work correctly. `setup.py install` might 189 | work too. 190 | 191 | Pip-8.1.1 is known to have this problem, but hopefully it will get fixed in 192 | some later version. 193 | 194 | [Bug #38](https://github.com/python-versioneer/python-versioneer/issues/38) is tracking 195 | this issue. The discussion in 196 | [PR #61](https://github.com/python-versioneer/python-versioneer/pull/61) describes the 197 | issue from the Versioneer side in more detail. 198 | [pip PR#3176](https://github.com/pypa/pip/pull/3176) and 199 | [pip PR#3615](https://github.com/pypa/pip/pull/3615) contain work to improve 200 | pip to let Versioneer work correctly. 201 | 202 | Versioneer-0.16 and earlier only looked for a `.git` directory next to the 203 | `setup.cfg`, so subprojects were completely unsupported with those releases. 204 | 205 | ### Editable installs with setuptools <= 18.5 206 | 207 | `setup.py develop` and `pip install --editable .` allow you to install a 208 | project into a virtualenv once, then continue editing the source code (and 209 | test) without re-installing after every change. 210 | 211 | "Entry-point scripts" (`setup(entry_points={"console_scripts": ..})`) are a 212 | convenient way to specify executable scripts that should be installed along 213 | with the python package. 214 | 215 | These both work as expected when using modern setuptools. When using 216 | setuptools-18.5 or earlier, however, certain operations will cause 217 | `pkg_resources.DistributionNotFound` errors when running the entrypoint 218 | script, which must be resolved by re-installing the package. This happens 219 | when the install happens with one version, then the egg_info data is 220 | regenerated while a different version is checked out. Many setup.py commands 221 | cause egg_info to be rebuilt (including `sdist`, `wheel`, and installing into 222 | a different virtualenv), so this can be surprising. 223 | 224 | [Bug #83](https://github.com/python-versioneer/python-versioneer/issues/83) describes 225 | this one, but upgrading to a newer version of setuptools should probably 226 | resolve it. 227 | 228 | 229 | ## Updating Versioneer 230 | 231 | To upgrade your project to a new release of Versioneer, do the following: 232 | 233 | * install the new Versioneer (`pip install -U versioneer` or equivalent) 234 | * edit `setup.cfg`, if necessary, to include any new configuration settings 235 | indicated by the release notes. See [UPGRADING](./UPGRADING.md) for details. 236 | * re-run `versioneer install` in your source tree, to replace 237 | `SRC/_version.py` 238 | * commit any changed files 239 | 240 | ## Future Directions 241 | 242 | This tool is designed to make it easily extended to other version-control 243 | systems: all VCS-specific components are in separate directories like 244 | src/git/ . The top-level `versioneer.py` script is assembled from these 245 | components by running make-versioneer.py . In the future, make-versioneer.py 246 | will take a VCS name as an argument, and will construct a version of 247 | `versioneer.py` that is specific to the given VCS. It might also take the 248 | configuration arguments that are currently provided manually during 249 | installation by editing setup.py . Alternatively, it might go the other 250 | direction and include code from all supported VCS systems, reducing the 251 | number of intermediate scripts. 252 | 253 | ## Similar projects 254 | 255 | * [setuptools_scm](https://github.com/pypa/setuptools_scm/) - a non-vendored build-time 256 | dependency 257 | * [minver](https://github.com/jbweston/miniver) - a lightweight reimplementation of 258 | versioneer 259 | * [versioningit](https://github.com/jwodder/versioningit) - a PEP 518-based setuptools 260 | plugin 261 | 262 | ## License 263 | 264 | To make Versioneer easier to embed, all its code is dedicated to the public 265 | domain. The `_version.py` that it creates is also in the public domain. 266 | Specifically, both are released under the Creative Commons "Public Domain 267 | Dedication" license (CC0-1.0), as described in 268 | https://creativecommons.org/publicdomain/zero/1.0/ . 269 | 270 | [pypi-image]: https://img.shields.io/pypi/v/versioneer.svg 271 | [pypi-url]: https://pypi.python.org/pypi/versioneer/ 272 | [travis-image]: 273 | https://img.shields.io/travis/com/python-versioneer/python-versioneer.svg 274 | [travis-url]: https://travis-ci.com/github/python-versioneer/python-versioneer 275 | 276 | """ 277 | # pylint:disable=invalid-name,import-outside-toplevel,missing-function-docstring 278 | # pylint:disable=missing-class-docstring,too-many-branches,too-many-statements 279 | # pylint:disable=raise-missing-from,too-many-lines,too-many-locals,import-error 280 | # pylint:disable=too-few-public-methods,redefined-outer-name,consider-using-with 281 | # pylint:disable=attribute-defined-outside-init,too-many-arguments 282 | 283 | import configparser 284 | import errno 285 | import json 286 | import os 287 | import re 288 | import subprocess 289 | import sys 290 | from typing import Callable, Dict 291 | 292 | 293 | class VersioneerConfig: 294 | """Container for Versioneer configuration parameters.""" 295 | 296 | 297 | def get_root(): 298 | """Get the project root directory. 299 | 300 | We require that all commands are run from the project root, i.e. the 301 | directory that contains setup.py, setup.cfg, and versioneer.py . 302 | """ 303 | root = os.path.realpath(os.path.abspath(os.getcwd())) 304 | setup_py = os.path.join(root, "setup.py") 305 | versioneer_py = os.path.join(root, "versioneer.py") 306 | if not (os.path.exists(setup_py) or os.path.exists(versioneer_py)): 307 | # allow 'python path/to/setup.py COMMAND' 308 | root = os.path.dirname(os.path.realpath(os.path.abspath(sys.argv[0]))) 309 | setup_py = os.path.join(root, "setup.py") 310 | versioneer_py = os.path.join(root, "versioneer.py") 311 | if not (os.path.exists(setup_py) or os.path.exists(versioneer_py)): 312 | err = ("Versioneer was unable to run the project root directory. " 313 | "Versioneer requires setup.py to be executed from " 314 | "its immediate directory (like 'python setup.py COMMAND'), " 315 | "or in a way that lets it use sys.argv[0] to find the root " 316 | "(like 'python path/to/setup.py COMMAND').") 317 | raise VersioneerBadRootError(err) 318 | try: 319 | # Certain runtime workflows (setup.py install/develop in a setuptools 320 | # tree) execute all dependencies in a single python process, so 321 | # "versioneer" may be imported multiple times, and python's shared 322 | # module-import table will cache the first one. So we can't use 323 | # os.path.dirname(__file__), as that will find whichever 324 | # versioneer.py was first imported, even in later projects. 325 | my_path = os.path.realpath(os.path.abspath(__file__)) 326 | me_dir = os.path.normcase(os.path.splitext(my_path)[0]) 327 | vsr_dir = os.path.normcase(os.path.splitext(versioneer_py)[0]) 328 | if me_dir != vsr_dir: 329 | print("Warning: build in %s is using versioneer.py from %s" 330 | % (os.path.dirname(my_path), versioneer_py)) 331 | except NameError: 332 | pass 333 | return root 334 | 335 | 336 | def get_config_from_root(root): 337 | """Read the project setup.cfg file to determine Versioneer config.""" 338 | # This might raise OSError (if setup.cfg is missing), or 339 | # configparser.NoSectionError (if it lacks a [versioneer] section), or 340 | # configparser.NoOptionError (if it lacks "VCS="). See the docstring at 341 | # the top of versioneer.py for instructions on writing your setup.cfg . 342 | setup_cfg = os.path.join(root, "setup.cfg") 343 | parser = configparser.ConfigParser() 344 | with open(setup_cfg, "r") as cfg_file: 345 | parser.read_file(cfg_file) 346 | VCS = parser.get("versioneer", "VCS") # mandatory 347 | 348 | # Dict-like interface for non-mandatory entries 349 | section = parser["versioneer"] 350 | 351 | cfg = VersioneerConfig() 352 | cfg.VCS = VCS 353 | cfg.style = section.get("style", "") 354 | cfg.versionfile_source = section.get("versionfile_source") 355 | cfg.versionfile_build = section.get("versionfile_build") 356 | cfg.tag_prefix = section.get("tag_prefix") 357 | if cfg.tag_prefix in ("''", '""'): 358 | cfg.tag_prefix = "" 359 | cfg.parentdir_prefix = section.get("parentdir_prefix") 360 | cfg.verbose = section.get("verbose") 361 | return cfg 362 | 363 | 364 | class NotThisMethod(Exception): 365 | """Exception raised if a method is not valid for the current scenario.""" 366 | 367 | 368 | # these dictionaries contain VCS-specific tools 369 | LONG_VERSION_PY: Dict[str, str] = {} 370 | HANDLERS: Dict[str, Dict[str, Callable]] = {} 371 | 372 | 373 | def register_vcs_handler(vcs, method): # decorator 374 | """Create decorator to mark a method as the handler of a VCS.""" 375 | def decorate(f): 376 | """Store f in HANDLERS[vcs][method].""" 377 | HANDLERS.setdefault(vcs, {})[method] = f 378 | return f 379 | return decorate 380 | 381 | 382 | def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, 383 | env=None): 384 | """Call the given command(s).""" 385 | assert isinstance(commands, list) 386 | process = None 387 | for command in commands: 388 | try: 389 | dispcmd = str([command] + args) 390 | # remember shell=False, so use git.cmd on windows, not just git 391 | process = subprocess.Popen([command] + args, cwd=cwd, env=env, 392 | stdout=subprocess.PIPE, 393 | stderr=(subprocess.PIPE if hide_stderr 394 | else None)) 395 | break 396 | except OSError: 397 | e = sys.exc_info()[1] 398 | if e.errno == errno.ENOENT: 399 | continue 400 | if verbose: 401 | print("unable to run %s" % dispcmd) 402 | print(e) 403 | return None, None 404 | else: 405 | if verbose: 406 | print("unable to find command, tried %s" % (commands,)) 407 | return None, None 408 | stdout = process.communicate()[0].strip().decode() 409 | if process.returncode != 0: 410 | if verbose: 411 | print("unable to run %s (error)" % dispcmd) 412 | print("stdout was %s" % stdout) 413 | return None, process.returncode 414 | return stdout, process.returncode 415 | 416 | 417 | LONG_VERSION_PY['git'] = r''' 418 | # This file helps to compute a version number in source trees obtained from 419 | # git-archive tarball (such as those provided by githubs download-from-tag 420 | # feature). Distribution tarballs (built by setup.py sdist) and build 421 | # directories (produced by setup.py build) will contain a much shorter file 422 | # that just contains the computed version number. 423 | 424 | # This file is released into the public domain. Generated by 425 | # versioneer-0.21 (https://github.com/python-versioneer/python-versioneer) 426 | 427 | """Git implementation of _version.py.""" 428 | 429 | import errno 430 | import os 431 | import re 432 | import subprocess 433 | import sys 434 | from typing import Callable, Dict 435 | 436 | 437 | def get_keywords(): 438 | """Get the keywords needed to look up the version information.""" 439 | # these strings will be replaced by git during git-archive. 440 | # setup.py/versioneer.py will grep for the variable names, so they must 441 | # each be defined on a line of their own. _version.py will just call 442 | # get_keywords(). 443 | git_refnames = "%(DOLLAR)sFormat:%%d%(DOLLAR)s" 444 | git_full = "%(DOLLAR)sFormat:%%H%(DOLLAR)s" 445 | git_date = "%(DOLLAR)sFormat:%%ci%(DOLLAR)s" 446 | keywords = {"refnames": git_refnames, "full": git_full, "date": git_date} 447 | return keywords 448 | 449 | 450 | class VersioneerConfig: 451 | """Container for Versioneer configuration parameters.""" 452 | 453 | 454 | def get_config(): 455 | """Create, populate and return the VersioneerConfig() object.""" 456 | # these strings are filled in when 'setup.py versioneer' creates 457 | # _version.py 458 | cfg = VersioneerConfig() 459 | cfg.VCS = "git" 460 | cfg.style = "%(STYLE)s" 461 | cfg.tag_prefix = "%(TAG_PREFIX)s" 462 | cfg.parentdir_prefix = "%(PARENTDIR_PREFIX)s" 463 | cfg.versionfile_source = "%(VERSIONFILE_SOURCE)s" 464 | cfg.verbose = False 465 | return cfg 466 | 467 | 468 | class NotThisMethod(Exception): 469 | """Exception raised if a method is not valid for the current scenario.""" 470 | 471 | 472 | LONG_VERSION_PY: Dict[str, str] = {} 473 | HANDLERS: Dict[str, Dict[str, Callable]] = {} 474 | 475 | 476 | def register_vcs_handler(vcs, method): # decorator 477 | """Create decorator to mark a method as the handler of a VCS.""" 478 | def decorate(f): 479 | """Store f in HANDLERS[vcs][method].""" 480 | if vcs not in HANDLERS: 481 | HANDLERS[vcs] = {} 482 | HANDLERS[vcs][method] = f 483 | return f 484 | return decorate 485 | 486 | 487 | def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, 488 | env=None): 489 | """Call the given command(s).""" 490 | assert isinstance(commands, list) 491 | process = None 492 | for command in commands: 493 | try: 494 | dispcmd = str([command] + args) 495 | # remember shell=False, so use git.cmd on windows, not just git 496 | process = subprocess.Popen([command] + args, cwd=cwd, env=env, 497 | stdout=subprocess.PIPE, 498 | stderr=(subprocess.PIPE if hide_stderr 499 | else None)) 500 | break 501 | except OSError: 502 | e = sys.exc_info()[1] 503 | if e.errno == errno.ENOENT: 504 | continue 505 | if verbose: 506 | print("unable to run %%s" %% dispcmd) 507 | print(e) 508 | return None, None 509 | else: 510 | if verbose: 511 | print("unable to find command, tried %%s" %% (commands,)) 512 | return None, None 513 | stdout = process.communicate()[0].strip().decode() 514 | if process.returncode != 0: 515 | if verbose: 516 | print("unable to run %%s (error)" %% dispcmd) 517 | print("stdout was %%s" %% stdout) 518 | return None, process.returncode 519 | return stdout, process.returncode 520 | 521 | 522 | def versions_from_parentdir(parentdir_prefix, root, verbose): 523 | """Try to determine the version from the parent directory name. 524 | 525 | Source tarballs conventionally unpack into a directory that includes both 526 | the project name and a version string. We will also support searching up 527 | two directory levels for an appropriately named parent directory 528 | """ 529 | rootdirs = [] 530 | 531 | for _ in range(3): 532 | dirname = os.path.basename(root) 533 | if dirname.startswith(parentdir_prefix): 534 | return {"version": dirname[len(parentdir_prefix):], 535 | "full-revisionid": None, 536 | "dirty": False, "error": None, "date": None} 537 | rootdirs.append(root) 538 | root = os.path.dirname(root) # up a level 539 | 540 | if verbose: 541 | print("Tried directories %%s but none started with prefix %%s" %% 542 | (str(rootdirs), parentdir_prefix)) 543 | raise NotThisMethod("rootdir doesn't start with parentdir_prefix") 544 | 545 | 546 | @register_vcs_handler("git", "get_keywords") 547 | def git_get_keywords(versionfile_abs): 548 | """Extract version information from the given file.""" 549 | # the code embedded in _version.py can just fetch the value of these 550 | # keywords. When used from setup.py, we don't want to import _version.py, 551 | # so we do it with a regexp instead. This function is not used from 552 | # _version.py. 553 | keywords = {} 554 | try: 555 | with open(versionfile_abs, "r") as fobj: 556 | for line in fobj: 557 | if line.strip().startswith("git_refnames ="): 558 | mo = re.search(r'=\s*"(.*)"', line) 559 | if mo: 560 | keywords["refnames"] = mo.group(1) 561 | if line.strip().startswith("git_full ="): 562 | mo = re.search(r'=\s*"(.*)"', line) 563 | if mo: 564 | keywords["full"] = mo.group(1) 565 | if line.strip().startswith("git_date ="): 566 | mo = re.search(r'=\s*"(.*)"', line) 567 | if mo: 568 | keywords["date"] = mo.group(1) 569 | except OSError: 570 | pass 571 | return keywords 572 | 573 | 574 | @register_vcs_handler("git", "keywords") 575 | def git_versions_from_keywords(keywords, tag_prefix, verbose): 576 | """Get version information from git keywords.""" 577 | if "refnames" not in keywords: 578 | raise NotThisMethod("Short version file found") 579 | date = keywords.get("date") 580 | if date is not None: 581 | # Use only the last line. Previous lines may contain GPG signature 582 | # information. 583 | date = date.splitlines()[-1] 584 | 585 | # git-2.2.0 added "%%cI", which expands to an ISO-8601 -compliant 586 | # datestamp. However we prefer "%%ci" (which expands to an "ISO-8601 587 | # -like" string, which we must then edit to make compliant), because 588 | # it's been around since git-1.5.3, and it's too difficult to 589 | # discover which version we're using, or to work around using an 590 | # older one. 591 | date = date.strip().replace(" ", "T", 1).replace(" ", "", 1) 592 | refnames = keywords["refnames"].strip() 593 | if refnames.startswith("$Format"): 594 | if verbose: 595 | print("keywords are unexpanded, not using") 596 | raise NotThisMethod("unexpanded keywords, not a git-archive tarball") 597 | refs = {r.strip() for r in refnames.strip("()").split(",")} 598 | # starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of 599 | # just "foo-1.0". If we see a "tag: " prefix, prefer those. 600 | TAG = "tag: " 601 | tags = {r[len(TAG):] for r in refs if r.startswith(TAG)} 602 | if not tags: 603 | # Either we're using git < 1.8.3, or there really are no tags. We use 604 | # a heuristic: assume all version tags have a digit. The old git %%d 605 | # expansion behaves like git log --decorate=short and strips out the 606 | # refs/heads/ and refs/tags/ prefixes that would let us distinguish 607 | # between branches and tags. By ignoring refnames without digits, we 608 | # filter out many common branch names like "release" and 609 | # "stabilization", as well as "HEAD" and "master". 610 | tags = {r for r in refs if re.search(r'\d', r)} 611 | if verbose: 612 | print("discarding '%%s', no digits" %% ",".join(refs - tags)) 613 | if verbose: 614 | print("likely tags: %%s" %% ",".join(sorted(tags))) 615 | for ref in sorted(tags): 616 | # sorting will prefer e.g. "2.0" over "2.0rc1" 617 | if ref.startswith(tag_prefix): 618 | r = ref[len(tag_prefix):] 619 | # Filter out refs that exactly match prefix or that don't start 620 | # with a number once the prefix is stripped (mostly a concern 621 | # when prefix is '') 622 | if not re.match(r'\d', r): 623 | continue 624 | if verbose: 625 | print("picking %%s" %% r) 626 | return {"version": r, 627 | "full-revisionid": keywords["full"].strip(), 628 | "dirty": False, "error": None, 629 | "date": date} 630 | # no suitable tags, so version is "0+unknown", but full hex is still there 631 | if verbose: 632 | print("no suitable tags, using unknown + full revision id") 633 | return {"version": "0+unknown", 634 | "full-revisionid": keywords["full"].strip(), 635 | "dirty": False, "error": "no suitable tags", "date": None} 636 | 637 | 638 | @register_vcs_handler("git", "pieces_from_vcs") 639 | def git_pieces_from_vcs(tag_prefix, root, verbose, runner=run_command): 640 | """Get version from 'git describe' in the root of the source tree. 641 | 642 | This only gets called if the git-archive 'subst' keywords were *not* 643 | expanded, and _version.py hasn't already been rewritten with a short 644 | version string, meaning we're inside a checked out source tree. 645 | """ 646 | GITS = ["git"] 647 | TAG_PREFIX_REGEX = "*" 648 | if sys.platform == "win32": 649 | GITS = ["git.cmd", "git.exe"] 650 | TAG_PREFIX_REGEX = r"\*" 651 | 652 | _, rc = runner(GITS, ["rev-parse", "--git-dir"], cwd=root, 653 | hide_stderr=True) 654 | if rc != 0: 655 | if verbose: 656 | print("Directory %%s not under git control" %% root) 657 | raise NotThisMethod("'git rev-parse --git-dir' returned error") 658 | 659 | # if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty] 660 | # if there isn't one, this yields HEX[-dirty] (no NUM) 661 | describe_out, rc = runner(GITS, ["describe", "--tags", "--dirty", 662 | "--always", "--long", 663 | "--match", 664 | "%%s%%s" %% (tag_prefix, TAG_PREFIX_REGEX)], 665 | cwd=root) 666 | # --long was added in git-1.5.5 667 | if describe_out is None: 668 | raise NotThisMethod("'git describe' failed") 669 | describe_out = describe_out.strip() 670 | full_out, rc = runner(GITS, ["rev-parse", "HEAD"], cwd=root) 671 | if full_out is None: 672 | raise NotThisMethod("'git rev-parse' failed") 673 | full_out = full_out.strip() 674 | 675 | pieces = {} 676 | pieces["long"] = full_out 677 | pieces["short"] = full_out[:7] # maybe improved later 678 | pieces["error"] = None 679 | 680 | branch_name, rc = runner(GITS, ["rev-parse", "--abbrev-ref", "HEAD"], 681 | cwd=root) 682 | # --abbrev-ref was added in git-1.6.3 683 | if rc != 0 or branch_name is None: 684 | raise NotThisMethod("'git rev-parse --abbrev-ref' returned error") 685 | branch_name = branch_name.strip() 686 | 687 | if branch_name == "HEAD": 688 | # If we aren't exactly on a branch, pick a branch which represents 689 | # the current commit. If all else fails, we are on a branchless 690 | # commit. 691 | branches, rc = runner(GITS, ["branch", "--contains"], cwd=root) 692 | # --contains was added in git-1.5.4 693 | if rc != 0 or branches is None: 694 | raise NotThisMethod("'git branch --contains' returned error") 695 | branches = branches.split("\n") 696 | 697 | # Remove the first line if we're running detached 698 | if "(" in branches[0]: 699 | branches.pop(0) 700 | 701 | # Strip off the leading "* " from the list of branches. 702 | branches = [branch[2:] for branch in branches] 703 | if "master" in branches: 704 | branch_name = "master" 705 | elif not branches: 706 | branch_name = None 707 | else: 708 | # Pick the first branch that is returned. Good or bad. 709 | branch_name = branches[0] 710 | 711 | pieces["branch"] = branch_name 712 | 713 | # parse describe_out. It will be like TAG-NUM-gHEX[-dirty] or HEX[-dirty] 714 | # TAG might have hyphens. 715 | git_describe = describe_out 716 | 717 | # look for -dirty suffix 718 | dirty = git_describe.endswith("-dirty") 719 | pieces["dirty"] = dirty 720 | if dirty: 721 | git_describe = git_describe[:git_describe.rindex("-dirty")] 722 | 723 | # now we have TAG-NUM-gHEX or HEX 724 | 725 | if "-" in git_describe: 726 | # TAG-NUM-gHEX 727 | mo = re.search(r'^(.+)-(\d+)-g([0-9a-f]+)$', git_describe) 728 | if not mo: 729 | # unparsable. Maybe git-describe is misbehaving? 730 | pieces["error"] = ("unable to parse git-describe output: '%%s'" 731 | %% describe_out) 732 | return pieces 733 | 734 | # tag 735 | full_tag = mo.group(1) 736 | if not full_tag.startswith(tag_prefix): 737 | if verbose: 738 | fmt = "tag '%%s' doesn't start with prefix '%%s'" 739 | print(fmt %% (full_tag, tag_prefix)) 740 | pieces["error"] = ("tag '%%s' doesn't start with prefix '%%s'" 741 | %% (full_tag, tag_prefix)) 742 | return pieces 743 | pieces["closest-tag"] = full_tag[len(tag_prefix):] 744 | 745 | # distance: number of commits since tag 746 | pieces["distance"] = int(mo.group(2)) 747 | 748 | # commit: short hex revision ID 749 | pieces["short"] = mo.group(3) 750 | 751 | else: 752 | # HEX: no tags 753 | pieces["closest-tag"] = None 754 | count_out, rc = runner(GITS, ["rev-list", "HEAD", "--count"], cwd=root) 755 | pieces["distance"] = int(count_out) # total number of commits 756 | 757 | # commit date: see ISO-8601 comment in git_versions_from_keywords() 758 | date = runner(GITS, ["show", "-s", "--format=%%ci", "HEAD"], cwd=root)[0].strip() 759 | # Use only the last line. Previous lines may contain GPG signature 760 | # information. 761 | date = date.splitlines()[-1] 762 | pieces["date"] = date.strip().replace(" ", "T", 1).replace(" ", "", 1) 763 | 764 | return pieces 765 | 766 | 767 | def plus_or_dot(pieces): 768 | """Return a + if we don't already have one, else return a .""" 769 | if "+" in pieces.get("closest-tag", ""): 770 | return "." 771 | return "+" 772 | 773 | 774 | def render_pep440(pieces): 775 | """Build up version string, with post-release "local version identifier". 776 | 777 | Our goal: TAG[+DISTANCE.gHEX[.dirty]] . Note that if you 778 | get a tagged build and then dirty it, you'll get TAG+0.gHEX.dirty 779 | 780 | Exceptions: 781 | 1: no tags. git_describe was just HEX. 0+untagged.DISTANCE.gHEX[.dirty] 782 | """ 783 | if pieces["closest-tag"]: 784 | rendered = pieces["closest-tag"] 785 | if pieces["distance"] or pieces["dirty"]: 786 | rendered += plus_or_dot(pieces) 787 | rendered += "%%d.g%%s" %% (pieces["distance"], pieces["short"]) 788 | if pieces["dirty"]: 789 | rendered += ".dirty" 790 | else: 791 | # exception #1 792 | rendered = "0+untagged.%%d.g%%s" %% (pieces["distance"], 793 | pieces["short"]) 794 | if pieces["dirty"]: 795 | rendered += ".dirty" 796 | return rendered 797 | 798 | 799 | def render_pep440_branch(pieces): 800 | """TAG[[.dev0]+DISTANCE.gHEX[.dirty]] . 801 | 802 | The ".dev0" means not master branch. Note that .dev0 sorts backwards 803 | (a feature branch will appear "older" than the master branch). 804 | 805 | Exceptions: 806 | 1: no tags. 0[.dev0]+untagged.DISTANCE.gHEX[.dirty] 807 | """ 808 | if pieces["closest-tag"]: 809 | rendered = pieces["closest-tag"] 810 | if pieces["distance"] or pieces["dirty"]: 811 | if pieces["branch"] != "master": 812 | rendered += ".dev0" 813 | rendered += plus_or_dot(pieces) 814 | rendered += "%%d.g%%s" %% (pieces["distance"], pieces["short"]) 815 | if pieces["dirty"]: 816 | rendered += ".dirty" 817 | else: 818 | # exception #1 819 | rendered = "0" 820 | if pieces["branch"] != "master": 821 | rendered += ".dev0" 822 | rendered += "+untagged.%%d.g%%s" %% (pieces["distance"], 823 | pieces["short"]) 824 | if pieces["dirty"]: 825 | rendered += ".dirty" 826 | return rendered 827 | 828 | 829 | def pep440_split_post(ver): 830 | """Split pep440 version string at the post-release segment. 831 | 832 | Returns the release segments before the post-release and the 833 | post-release version number (or -1 if no post-release segment is present). 834 | """ 835 | vc = str.split(ver, ".post") 836 | return vc[0], int(vc[1] or 0) if len(vc) == 2 else None 837 | 838 | 839 | def render_pep440_pre(pieces): 840 | """TAG[.postN.devDISTANCE] -- No -dirty. 841 | 842 | Exceptions: 843 | 1: no tags. 0.post0.devDISTANCE 844 | """ 845 | if pieces["closest-tag"]: 846 | if pieces["distance"]: 847 | # update the post release segment 848 | tag_version, post_version = pep440_split_post(pieces["closest-tag"]) 849 | rendered = tag_version 850 | if post_version is not None: 851 | rendered += ".post%%d.dev%%d" %% (post_version+1, pieces["distance"]) 852 | else: 853 | rendered += ".post0.dev%%d" %% (pieces["distance"]) 854 | else: 855 | # no commits, use the tag as the version 856 | rendered = pieces["closest-tag"] 857 | else: 858 | # exception #1 859 | rendered = "0.post0.dev%%d" %% pieces["distance"] 860 | return rendered 861 | 862 | 863 | def render_pep440_post(pieces): 864 | """TAG[.postDISTANCE[.dev0]+gHEX] . 865 | 866 | The ".dev0" means dirty. Note that .dev0 sorts backwards 867 | (a dirty tree will appear "older" than the corresponding clean one), 868 | but you shouldn't be releasing software with -dirty anyways. 869 | 870 | Exceptions: 871 | 1: no tags. 0.postDISTANCE[.dev0] 872 | """ 873 | if pieces["closest-tag"]: 874 | rendered = pieces["closest-tag"] 875 | if pieces["distance"] or pieces["dirty"]: 876 | rendered += ".post%%d" %% pieces["distance"] 877 | if pieces["dirty"]: 878 | rendered += ".dev0" 879 | rendered += plus_or_dot(pieces) 880 | rendered += "g%%s" %% pieces["short"] 881 | else: 882 | # exception #1 883 | rendered = "0.post%%d" %% pieces["distance"] 884 | if pieces["dirty"]: 885 | rendered += ".dev0" 886 | rendered += "+g%%s" %% pieces["short"] 887 | return rendered 888 | 889 | 890 | def render_pep440_post_branch(pieces): 891 | """TAG[.postDISTANCE[.dev0]+gHEX[.dirty]] . 892 | 893 | The ".dev0" means not master branch. 894 | 895 | Exceptions: 896 | 1: no tags. 0.postDISTANCE[.dev0]+gHEX[.dirty] 897 | """ 898 | if pieces["closest-tag"]: 899 | rendered = pieces["closest-tag"] 900 | if pieces["distance"] or pieces["dirty"]: 901 | rendered += ".post%%d" %% pieces["distance"] 902 | if pieces["branch"] != "master": 903 | rendered += ".dev0" 904 | rendered += plus_or_dot(pieces) 905 | rendered += "g%%s" %% pieces["short"] 906 | if pieces["dirty"]: 907 | rendered += ".dirty" 908 | else: 909 | # exception #1 910 | rendered = "0.post%%d" %% pieces["distance"] 911 | if pieces["branch"] != "master": 912 | rendered += ".dev0" 913 | rendered += "+g%%s" %% pieces["short"] 914 | if pieces["dirty"]: 915 | rendered += ".dirty" 916 | return rendered 917 | 918 | 919 | def render_pep440_old(pieces): 920 | """TAG[.postDISTANCE[.dev0]] . 921 | 922 | The ".dev0" means dirty. 923 | 924 | Exceptions: 925 | 1: no tags. 0.postDISTANCE[.dev0] 926 | """ 927 | if pieces["closest-tag"]: 928 | rendered = pieces["closest-tag"] 929 | if pieces["distance"] or pieces["dirty"]: 930 | rendered += ".post%%d" %% pieces["distance"] 931 | if pieces["dirty"]: 932 | rendered += ".dev0" 933 | else: 934 | # exception #1 935 | rendered = "0.post%%d" %% pieces["distance"] 936 | if pieces["dirty"]: 937 | rendered += ".dev0" 938 | return rendered 939 | 940 | 941 | def render_git_describe(pieces): 942 | """TAG[-DISTANCE-gHEX][-dirty]. 943 | 944 | Like 'git describe --tags --dirty --always'. 945 | 946 | Exceptions: 947 | 1: no tags. HEX[-dirty] (note: no 'g' prefix) 948 | """ 949 | if pieces["closest-tag"]: 950 | rendered = pieces["closest-tag"] 951 | if pieces["distance"]: 952 | rendered += "-%%d-g%%s" %% (pieces["distance"], pieces["short"]) 953 | else: 954 | # exception #1 955 | rendered = pieces["short"] 956 | if pieces["dirty"]: 957 | rendered += "-dirty" 958 | return rendered 959 | 960 | 961 | def render_git_describe_long(pieces): 962 | """TAG-DISTANCE-gHEX[-dirty]. 963 | 964 | Like 'git describe --tags --dirty --always -long'. 965 | The distance/hash is unconditional. 966 | 967 | Exceptions: 968 | 1: no tags. HEX[-dirty] (note: no 'g' prefix) 969 | """ 970 | if pieces["closest-tag"]: 971 | rendered = pieces["closest-tag"] 972 | rendered += "-%%d-g%%s" %% (pieces["distance"], pieces["short"]) 973 | else: 974 | # exception #1 975 | rendered = pieces["short"] 976 | if pieces["dirty"]: 977 | rendered += "-dirty" 978 | return rendered 979 | 980 | 981 | def render(pieces, style): 982 | """Render the given version pieces into the requested style.""" 983 | if pieces["error"]: 984 | return {"version": "unknown", 985 | "full-revisionid": pieces.get("long"), 986 | "dirty": None, 987 | "error": pieces["error"], 988 | "date": None} 989 | 990 | if not style or style == "default": 991 | style = "pep440" # the default 992 | 993 | if style == "pep440": 994 | rendered = render_pep440(pieces) 995 | elif style == "pep440-branch": 996 | rendered = render_pep440_branch(pieces) 997 | elif style == "pep440-pre": 998 | rendered = render_pep440_pre(pieces) 999 | elif style == "pep440-post": 1000 | rendered = render_pep440_post(pieces) 1001 | elif style == "pep440-post-branch": 1002 | rendered = render_pep440_post_branch(pieces) 1003 | elif style == "pep440-old": 1004 | rendered = render_pep440_old(pieces) 1005 | elif style == "git-describe": 1006 | rendered = render_git_describe(pieces) 1007 | elif style == "git-describe-long": 1008 | rendered = render_git_describe_long(pieces) 1009 | else: 1010 | raise ValueError("unknown style '%%s'" %% style) 1011 | 1012 | return {"version": rendered, "full-revisionid": pieces["long"], 1013 | "dirty": pieces["dirty"], "error": None, 1014 | "date": pieces.get("date")} 1015 | 1016 | 1017 | def get_versions(): 1018 | """Get version information or return default if unable to do so.""" 1019 | # I am in _version.py, which lives at ROOT/VERSIONFILE_SOURCE. If we have 1020 | # __file__, we can work backwards from there to the root. Some 1021 | # py2exe/bbfreeze/non-CPython implementations don't do __file__, in which 1022 | # case we can only use expanded keywords. 1023 | 1024 | cfg = get_config() 1025 | verbose = cfg.verbose 1026 | 1027 | try: 1028 | return git_versions_from_keywords(get_keywords(), cfg.tag_prefix, 1029 | verbose) 1030 | except NotThisMethod: 1031 | pass 1032 | 1033 | try: 1034 | root = os.path.realpath(__file__) 1035 | # versionfile_source is the relative path from the top of the source 1036 | # tree (where the .git directory might live) to this file. Invert 1037 | # this to find the root from __file__. 1038 | for _ in cfg.versionfile_source.split('/'): 1039 | root = os.path.dirname(root) 1040 | except NameError: 1041 | return {"version": "0+unknown", "full-revisionid": None, 1042 | "dirty": None, 1043 | "error": "unable to find root of source tree", 1044 | "date": None} 1045 | 1046 | try: 1047 | pieces = git_pieces_from_vcs(cfg.tag_prefix, root, verbose) 1048 | return render(pieces, cfg.style) 1049 | except NotThisMethod: 1050 | pass 1051 | 1052 | try: 1053 | if cfg.parentdir_prefix: 1054 | return versions_from_parentdir(cfg.parentdir_prefix, root, verbose) 1055 | except NotThisMethod: 1056 | pass 1057 | 1058 | return {"version": "0+unknown", "full-revisionid": None, 1059 | "dirty": None, 1060 | "error": "unable to compute version", "date": None} 1061 | ''' 1062 | 1063 | 1064 | @register_vcs_handler("git", "get_keywords") 1065 | def git_get_keywords(versionfile_abs): 1066 | """Extract version information from the given file.""" 1067 | # the code embedded in _version.py can just fetch the value of these 1068 | # keywords. When used from setup.py, we don't want to import _version.py, 1069 | # so we do it with a regexp instead. This function is not used from 1070 | # _version.py. 1071 | keywords = {} 1072 | try: 1073 | with open(versionfile_abs, "r") as fobj: 1074 | for line in fobj: 1075 | if line.strip().startswith("git_refnames ="): 1076 | mo = re.search(r'=\s*"(.*)"', line) 1077 | if mo: 1078 | keywords["refnames"] = mo.group(1) 1079 | if line.strip().startswith("git_full ="): 1080 | mo = re.search(r'=\s*"(.*)"', line) 1081 | if mo: 1082 | keywords["full"] = mo.group(1) 1083 | if line.strip().startswith("git_date ="): 1084 | mo = re.search(r'=\s*"(.*)"', line) 1085 | if mo: 1086 | keywords["date"] = mo.group(1) 1087 | except OSError: 1088 | pass 1089 | return keywords 1090 | 1091 | 1092 | @register_vcs_handler("git", "keywords") 1093 | def git_versions_from_keywords(keywords, tag_prefix, verbose): 1094 | """Get version information from git keywords.""" 1095 | if "refnames" not in keywords: 1096 | raise NotThisMethod("Short version file found") 1097 | date = keywords.get("date") 1098 | if date is not None: 1099 | # Use only the last line. Previous lines may contain GPG signature 1100 | # information. 1101 | date = date.splitlines()[-1] 1102 | 1103 | # git-2.2.0 added "%cI", which expands to an ISO-8601 -compliant 1104 | # datestamp. However we prefer "%ci" (which expands to an "ISO-8601 1105 | # -like" string, which we must then edit to make compliant), because 1106 | # it's been around since git-1.5.3, and it's too difficult to 1107 | # discover which version we're using, or to work around using an 1108 | # older one. 1109 | date = date.strip().replace(" ", "T", 1).replace(" ", "", 1) 1110 | refnames = keywords["refnames"].strip() 1111 | if refnames.startswith("$Format"): 1112 | if verbose: 1113 | print("keywords are unexpanded, not using") 1114 | raise NotThisMethod("unexpanded keywords, not a git-archive tarball") 1115 | refs = {r.strip() for r in refnames.strip("()").split(",")} 1116 | # starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of 1117 | # just "foo-1.0". If we see a "tag: " prefix, prefer those. 1118 | TAG = "tag: " 1119 | tags = {r[len(TAG):] for r in refs if r.startswith(TAG)} 1120 | if not tags: 1121 | # Either we're using git < 1.8.3, or there really are no tags. We use 1122 | # a heuristic: assume all version tags have a digit. The old git %d 1123 | # expansion behaves like git log --decorate=short and strips out the 1124 | # refs/heads/ and refs/tags/ prefixes that would let us distinguish 1125 | # between branches and tags. By ignoring refnames without digits, we 1126 | # filter out many common branch names like "release" and 1127 | # "stabilization", as well as "HEAD" and "master". 1128 | tags = {r for r in refs if re.search(r'\d', r)} 1129 | if verbose: 1130 | print("discarding '%s', no digits" % ",".join(refs - tags)) 1131 | if verbose: 1132 | print("likely tags: %s" % ",".join(sorted(tags))) 1133 | for ref in sorted(tags): 1134 | # sorting will prefer e.g. "2.0" over "2.0rc1" 1135 | if ref.startswith(tag_prefix): 1136 | r = ref[len(tag_prefix):] 1137 | # Filter out refs that exactly match prefix or that don't start 1138 | # with a number once the prefix is stripped (mostly a concern 1139 | # when prefix is '') 1140 | if not re.match(r'\d', r): 1141 | continue 1142 | if verbose: 1143 | print("picking %s" % r) 1144 | return {"version": r, 1145 | "full-revisionid": keywords["full"].strip(), 1146 | "dirty": False, "error": None, 1147 | "date": date} 1148 | # no suitable tags, so version is "0+unknown", but full hex is still there 1149 | if verbose: 1150 | print("no suitable tags, using unknown + full revision id") 1151 | return {"version": "0+unknown", 1152 | "full-revisionid": keywords["full"].strip(), 1153 | "dirty": False, "error": "no suitable tags", "date": None} 1154 | 1155 | 1156 | @register_vcs_handler("git", "pieces_from_vcs") 1157 | def git_pieces_from_vcs(tag_prefix, root, verbose, runner=run_command): 1158 | """Get version from 'git describe' in the root of the source tree. 1159 | 1160 | This only gets called if the git-archive 'subst' keywords were *not* 1161 | expanded, and _version.py hasn't already been rewritten with a short 1162 | version string, meaning we're inside a checked out source tree. 1163 | """ 1164 | GITS = ["git"] 1165 | TAG_PREFIX_REGEX = "*" 1166 | if sys.platform == "win32": 1167 | GITS = ["git.cmd", "git.exe"] 1168 | TAG_PREFIX_REGEX = r"\*" 1169 | 1170 | _, rc = runner(GITS, ["rev-parse", "--git-dir"], cwd=root, 1171 | hide_stderr=True) 1172 | if rc != 0: 1173 | if verbose: 1174 | print("Directory %s not under git control" % root) 1175 | raise NotThisMethod("'git rev-parse --git-dir' returned error") 1176 | 1177 | # if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty] 1178 | # if there isn't one, this yields HEX[-dirty] (no NUM) 1179 | describe_out, rc = runner(GITS, ["describe", "--tags", "--dirty", 1180 | "--always", "--long", 1181 | "--match", 1182 | "%s%s" % (tag_prefix, TAG_PREFIX_REGEX)], 1183 | cwd=root) 1184 | # --long was added in git-1.5.5 1185 | if describe_out is None: 1186 | raise NotThisMethod("'git describe' failed") 1187 | describe_out = describe_out.strip() 1188 | full_out, rc = runner(GITS, ["rev-parse", "HEAD"], cwd=root) 1189 | if full_out is None: 1190 | raise NotThisMethod("'git rev-parse' failed") 1191 | full_out = full_out.strip() 1192 | 1193 | pieces = {} 1194 | pieces["long"] = full_out 1195 | pieces["short"] = full_out[:7] # maybe improved later 1196 | pieces["error"] = None 1197 | 1198 | branch_name, rc = runner(GITS, ["rev-parse", "--abbrev-ref", "HEAD"], 1199 | cwd=root) 1200 | # --abbrev-ref was added in git-1.6.3 1201 | if rc != 0 or branch_name is None: 1202 | raise NotThisMethod("'git rev-parse --abbrev-ref' returned error") 1203 | branch_name = branch_name.strip() 1204 | 1205 | if branch_name == "HEAD": 1206 | # If we aren't exactly on a branch, pick a branch which represents 1207 | # the current commit. If all else fails, we are on a branchless 1208 | # commit. 1209 | branches, rc = runner(GITS, ["branch", "--contains"], cwd=root) 1210 | # --contains was added in git-1.5.4 1211 | if rc != 0 or branches is None: 1212 | raise NotThisMethod("'git branch --contains' returned error") 1213 | branches = branches.split("\n") 1214 | 1215 | # Remove the first line if we're running detached 1216 | if "(" in branches[0]: 1217 | branches.pop(0) 1218 | 1219 | # Strip off the leading "* " from the list of branches. 1220 | branches = [branch[2:] for branch in branches] 1221 | if "master" in branches: 1222 | branch_name = "master" 1223 | elif not branches: 1224 | branch_name = None 1225 | else: 1226 | # Pick the first branch that is returned. Good or bad. 1227 | branch_name = branches[0] 1228 | 1229 | pieces["branch"] = branch_name 1230 | 1231 | # parse describe_out. It will be like TAG-NUM-gHEX[-dirty] or HEX[-dirty] 1232 | # TAG might have hyphens. 1233 | git_describe = describe_out 1234 | 1235 | # look for -dirty suffix 1236 | dirty = git_describe.endswith("-dirty") 1237 | pieces["dirty"] = dirty 1238 | if dirty: 1239 | git_describe = git_describe[:git_describe.rindex("-dirty")] 1240 | 1241 | # now we have TAG-NUM-gHEX or HEX 1242 | 1243 | if "-" in git_describe: 1244 | # TAG-NUM-gHEX 1245 | mo = re.search(r'^(.+)-(\d+)-g([0-9a-f]+)$', git_describe) 1246 | if not mo: 1247 | # unparsable. Maybe git-describe is misbehaving? 1248 | pieces["error"] = ("unable to parse git-describe output: '%s'" 1249 | % describe_out) 1250 | return pieces 1251 | 1252 | # tag 1253 | full_tag = mo.group(1) 1254 | if not full_tag.startswith(tag_prefix): 1255 | if verbose: 1256 | fmt = "tag '%s' doesn't start with prefix '%s'" 1257 | print(fmt % (full_tag, tag_prefix)) 1258 | pieces["error"] = ("tag '%s' doesn't start with prefix '%s'" 1259 | % (full_tag, tag_prefix)) 1260 | return pieces 1261 | pieces["closest-tag"] = full_tag[len(tag_prefix):] 1262 | 1263 | # distance: number of commits since tag 1264 | pieces["distance"] = int(mo.group(2)) 1265 | 1266 | # commit: short hex revision ID 1267 | pieces["short"] = mo.group(3) 1268 | 1269 | else: 1270 | # HEX: no tags 1271 | pieces["closest-tag"] = None 1272 | count_out, rc = runner(GITS, ["rev-list", "HEAD", "--count"], cwd=root) 1273 | pieces["distance"] = int(count_out) # total number of commits 1274 | 1275 | # commit date: see ISO-8601 comment in git_versions_from_keywords() 1276 | date = runner(GITS, ["show", "-s", "--format=%ci", "HEAD"], cwd=root)[0].strip() 1277 | # Use only the last line. Previous lines may contain GPG signature 1278 | # information. 1279 | date = date.splitlines()[-1] 1280 | pieces["date"] = date.strip().replace(" ", "T", 1).replace(" ", "", 1) 1281 | 1282 | return pieces 1283 | 1284 | 1285 | def do_vcs_install(manifest_in, versionfile_source, ipy): 1286 | """Git-specific installation logic for Versioneer. 1287 | 1288 | For Git, this means creating/changing .gitattributes to mark _version.py 1289 | for export-subst keyword substitution. 1290 | """ 1291 | GITS = ["git"] 1292 | if sys.platform == "win32": 1293 | GITS = ["git.cmd", "git.exe"] 1294 | files = [manifest_in, versionfile_source] 1295 | if ipy: 1296 | files.append(ipy) 1297 | try: 1298 | my_path = __file__ 1299 | if my_path.endswith(".pyc") or my_path.endswith(".pyo"): 1300 | my_path = os.path.splitext(my_path)[0] + ".py" 1301 | versioneer_file = os.path.relpath(my_path) 1302 | except NameError: 1303 | versioneer_file = "versioneer.py" 1304 | files.append(versioneer_file) 1305 | present = False 1306 | try: 1307 | with open(".gitattributes", "r") as fobj: 1308 | for line in fobj: 1309 | if line.strip().startswith(versionfile_source): 1310 | if "export-subst" in line.strip().split()[1:]: 1311 | present = True 1312 | break 1313 | except OSError: 1314 | pass 1315 | if not present: 1316 | with open(".gitattributes", "a+") as fobj: 1317 | fobj.write(f"{versionfile_source} export-subst\n") 1318 | files.append(".gitattributes") 1319 | run_command(GITS, ["add", "--"] + files) 1320 | 1321 | 1322 | def versions_from_parentdir(parentdir_prefix, root, verbose): 1323 | """Try to determine the version from the parent directory name. 1324 | 1325 | Source tarballs conventionally unpack into a directory that includes both 1326 | the project name and a version string. We will also support searching up 1327 | two directory levels for an appropriately named parent directory 1328 | """ 1329 | rootdirs = [] 1330 | 1331 | for _ in range(3): 1332 | dirname = os.path.basename(root) 1333 | if dirname.startswith(parentdir_prefix): 1334 | return {"version": dirname[len(parentdir_prefix):], 1335 | "full-revisionid": None, 1336 | "dirty": False, "error": None, "date": None} 1337 | rootdirs.append(root) 1338 | root = os.path.dirname(root) # up a level 1339 | 1340 | if verbose: 1341 | print("Tried directories %s but none started with prefix %s" % 1342 | (str(rootdirs), parentdir_prefix)) 1343 | raise NotThisMethod("rootdir doesn't start with parentdir_prefix") 1344 | 1345 | 1346 | SHORT_VERSION_PY = """ 1347 | # This file was generated by 'versioneer.py' (0.21) from 1348 | # revision-control system data, or from the parent directory name of an 1349 | # unpacked source archive. Distribution tarballs contain a pre-generated copy 1350 | # of this file. 1351 | 1352 | import json 1353 | 1354 | version_json = ''' 1355 | %s 1356 | ''' # END VERSION_JSON 1357 | 1358 | 1359 | def get_versions(): 1360 | return json.loads(version_json) 1361 | """ 1362 | 1363 | 1364 | def versions_from_file(filename): 1365 | """Try to determine the version from _version.py if present.""" 1366 | try: 1367 | with open(filename) as f: 1368 | contents = f.read() 1369 | except OSError: 1370 | raise NotThisMethod("unable to read _version.py") 1371 | mo = re.search(r"version_json = '''\n(.*)''' # END VERSION_JSON", 1372 | contents, re.M | re.S) 1373 | if not mo: 1374 | mo = re.search(r"version_json = '''\r\n(.*)''' # END VERSION_JSON", 1375 | contents, re.M | re.S) 1376 | if not mo: 1377 | raise NotThisMethod("no version_json in _version.py") 1378 | return json.loads(mo.group(1)) 1379 | 1380 | 1381 | def write_to_version_file(filename, versions): 1382 | """Write the given version number to the given _version.py file.""" 1383 | os.unlink(filename) 1384 | contents = json.dumps(versions, sort_keys=True, 1385 | indent=1, separators=(",", ": ")) 1386 | with open(filename, "w") as f: 1387 | f.write(SHORT_VERSION_PY % contents) 1388 | 1389 | print("set %s to '%s'" % (filename, versions["version"])) 1390 | 1391 | 1392 | def plus_or_dot(pieces): 1393 | """Return a + if we don't already have one, else return a .""" 1394 | if "+" in pieces.get("closest-tag", ""): 1395 | return "." 1396 | return "+" 1397 | 1398 | 1399 | def render_pep440(pieces): 1400 | """Build up version string, with post-release "local version identifier". 1401 | 1402 | Our goal: TAG[+DISTANCE.gHEX[.dirty]] . Note that if you 1403 | get a tagged build and then dirty it, you'll get TAG+0.gHEX.dirty 1404 | 1405 | Exceptions: 1406 | 1: no tags. git_describe was just HEX. 0+untagged.DISTANCE.gHEX[.dirty] 1407 | """ 1408 | if pieces["closest-tag"]: 1409 | rendered = pieces["closest-tag"] 1410 | if pieces["distance"] or pieces["dirty"]: 1411 | rendered += plus_or_dot(pieces) 1412 | rendered += "%d.g%s" % (pieces["distance"], pieces["short"]) 1413 | if pieces["dirty"]: 1414 | rendered += ".dirty" 1415 | else: 1416 | # exception #1 1417 | rendered = "0+untagged.%d.g%s" % (pieces["distance"], 1418 | pieces["short"]) 1419 | if pieces["dirty"]: 1420 | rendered += ".dirty" 1421 | return rendered 1422 | 1423 | 1424 | def render_pep440_branch(pieces): 1425 | """TAG[[.dev0]+DISTANCE.gHEX[.dirty]] . 1426 | 1427 | The ".dev0" means not master branch. Note that .dev0 sorts backwards 1428 | (a feature branch will appear "older" than the master branch). 1429 | 1430 | Exceptions: 1431 | 1: no tags. 0[.dev0]+untagged.DISTANCE.gHEX[.dirty] 1432 | """ 1433 | if pieces["closest-tag"]: 1434 | rendered = pieces["closest-tag"] 1435 | if pieces["distance"] or pieces["dirty"]: 1436 | if pieces["branch"] != "master": 1437 | rendered += ".dev0" 1438 | rendered += plus_or_dot(pieces) 1439 | rendered += "%d.g%s" % (pieces["distance"], pieces["short"]) 1440 | if pieces["dirty"]: 1441 | rendered += ".dirty" 1442 | else: 1443 | # exception #1 1444 | rendered = "0" 1445 | if pieces["branch"] != "master": 1446 | rendered += ".dev0" 1447 | rendered += "+untagged.%d.g%s" % (pieces["distance"], 1448 | pieces["short"]) 1449 | if pieces["dirty"]: 1450 | rendered += ".dirty" 1451 | return rendered 1452 | 1453 | 1454 | def pep440_split_post(ver): 1455 | """Split pep440 version string at the post-release segment. 1456 | 1457 | Returns the release segments before the post-release and the 1458 | post-release version number (or -1 if no post-release segment is present). 1459 | """ 1460 | vc = str.split(ver, ".post") 1461 | return vc[0], int(vc[1] or 0) if len(vc) == 2 else None 1462 | 1463 | 1464 | def render_pep440_pre(pieces): 1465 | """TAG[.postN.devDISTANCE] -- No -dirty. 1466 | 1467 | Exceptions: 1468 | 1: no tags. 0.post0.devDISTANCE 1469 | """ 1470 | if pieces["closest-tag"]: 1471 | if pieces["distance"]: 1472 | # update the post release segment 1473 | tag_version, post_version = pep440_split_post(pieces["closest-tag"]) 1474 | rendered = tag_version 1475 | if post_version is not None: 1476 | rendered += ".post%d.dev%d" % (post_version+1, pieces["distance"]) 1477 | else: 1478 | rendered += ".post0.dev%d" % (pieces["distance"]) 1479 | else: 1480 | # no commits, use the tag as the version 1481 | rendered = pieces["closest-tag"] 1482 | else: 1483 | # exception #1 1484 | rendered = "0.post0.dev%d" % pieces["distance"] 1485 | return rendered 1486 | 1487 | 1488 | def render_pep440_post(pieces): 1489 | """TAG[.postDISTANCE[.dev0]+gHEX] . 1490 | 1491 | The ".dev0" means dirty. Note that .dev0 sorts backwards 1492 | (a dirty tree will appear "older" than the corresponding clean one), 1493 | but you shouldn't be releasing software with -dirty anyways. 1494 | 1495 | Exceptions: 1496 | 1: no tags. 0.postDISTANCE[.dev0] 1497 | """ 1498 | if pieces["closest-tag"]: 1499 | rendered = pieces["closest-tag"] 1500 | if pieces["distance"] or pieces["dirty"]: 1501 | rendered += ".post%d" % pieces["distance"] 1502 | if pieces["dirty"]: 1503 | rendered += ".dev0" 1504 | rendered += plus_or_dot(pieces) 1505 | rendered += "g%s" % pieces["short"] 1506 | else: 1507 | # exception #1 1508 | rendered = "0.post%d" % pieces["distance"] 1509 | if pieces["dirty"]: 1510 | rendered += ".dev0" 1511 | rendered += "+g%s" % pieces["short"] 1512 | return rendered 1513 | 1514 | 1515 | def render_pep440_post_branch(pieces): 1516 | """TAG[.postDISTANCE[.dev0]+gHEX[.dirty]] . 1517 | 1518 | The ".dev0" means not master branch. 1519 | 1520 | Exceptions: 1521 | 1: no tags. 0.postDISTANCE[.dev0]+gHEX[.dirty] 1522 | """ 1523 | if pieces["closest-tag"]: 1524 | rendered = pieces["closest-tag"] 1525 | if pieces["distance"] or pieces["dirty"]: 1526 | rendered += ".post%d" % pieces["distance"] 1527 | if pieces["branch"] != "master": 1528 | rendered += ".dev0" 1529 | rendered += plus_or_dot(pieces) 1530 | rendered += "g%s" % pieces["short"] 1531 | if pieces["dirty"]: 1532 | rendered += ".dirty" 1533 | else: 1534 | # exception #1 1535 | rendered = "0.post%d" % pieces["distance"] 1536 | if pieces["branch"] != "master": 1537 | rendered += ".dev0" 1538 | rendered += "+g%s" % pieces["short"] 1539 | if pieces["dirty"]: 1540 | rendered += ".dirty" 1541 | return rendered 1542 | 1543 | 1544 | def render_pep440_old(pieces): 1545 | """TAG[.postDISTANCE[.dev0]] . 1546 | 1547 | The ".dev0" means dirty. 1548 | 1549 | Exceptions: 1550 | 1: no tags. 0.postDISTANCE[.dev0] 1551 | """ 1552 | if pieces["closest-tag"]: 1553 | rendered = pieces["closest-tag"] 1554 | if pieces["distance"] or pieces["dirty"]: 1555 | rendered += ".post%d" % pieces["distance"] 1556 | if pieces["dirty"]: 1557 | rendered += ".dev0" 1558 | else: 1559 | # exception #1 1560 | rendered = "0.post%d" % pieces["distance"] 1561 | if pieces["dirty"]: 1562 | rendered += ".dev0" 1563 | return rendered 1564 | 1565 | 1566 | def render_git_describe(pieces): 1567 | """TAG[-DISTANCE-gHEX][-dirty]. 1568 | 1569 | Like 'git describe --tags --dirty --always'. 1570 | 1571 | Exceptions: 1572 | 1: no tags. HEX[-dirty] (note: no 'g' prefix) 1573 | """ 1574 | if pieces["closest-tag"]: 1575 | rendered = pieces["closest-tag"] 1576 | if pieces["distance"]: 1577 | rendered += "-%d-g%s" % (pieces["distance"], pieces["short"]) 1578 | else: 1579 | # exception #1 1580 | rendered = pieces["short"] 1581 | if pieces["dirty"]: 1582 | rendered += "-dirty" 1583 | return rendered 1584 | 1585 | 1586 | def render_git_describe_long(pieces): 1587 | """TAG-DISTANCE-gHEX[-dirty]. 1588 | 1589 | Like 'git describe --tags --dirty --always -long'. 1590 | The distance/hash is unconditional. 1591 | 1592 | Exceptions: 1593 | 1: no tags. HEX[-dirty] (note: no 'g' prefix) 1594 | """ 1595 | if pieces["closest-tag"]: 1596 | rendered = pieces["closest-tag"] 1597 | rendered += "-%d-g%s" % (pieces["distance"], pieces["short"]) 1598 | else: 1599 | # exception #1 1600 | rendered = pieces["short"] 1601 | if pieces["dirty"]: 1602 | rendered += "-dirty" 1603 | return rendered 1604 | 1605 | 1606 | def render(pieces, style): 1607 | """Render the given version pieces into the requested style.""" 1608 | if pieces["error"]: 1609 | return {"version": "unknown", 1610 | "full-revisionid": pieces.get("long"), 1611 | "dirty": None, 1612 | "error": pieces["error"], 1613 | "date": None} 1614 | 1615 | if not style or style == "default": 1616 | style = "pep440" # the default 1617 | 1618 | if style == "pep440": 1619 | rendered = render_pep440(pieces) 1620 | elif style == "pep440-branch": 1621 | rendered = render_pep440_branch(pieces) 1622 | elif style == "pep440-pre": 1623 | rendered = render_pep440_pre(pieces) 1624 | elif style == "pep440-post": 1625 | rendered = render_pep440_post(pieces) 1626 | elif style == "pep440-post-branch": 1627 | rendered = render_pep440_post_branch(pieces) 1628 | elif style == "pep440-old": 1629 | rendered = render_pep440_old(pieces) 1630 | elif style == "git-describe": 1631 | rendered = render_git_describe(pieces) 1632 | elif style == "git-describe-long": 1633 | rendered = render_git_describe_long(pieces) 1634 | else: 1635 | raise ValueError("unknown style '%s'" % style) 1636 | 1637 | return {"version": rendered, "full-revisionid": pieces["long"], 1638 | "dirty": pieces["dirty"], "error": None, 1639 | "date": pieces.get("date")} 1640 | 1641 | 1642 | class VersioneerBadRootError(Exception): 1643 | """The project root directory is unknown or missing key files.""" 1644 | 1645 | 1646 | def get_versions(verbose=False): 1647 | """Get the project version from whatever source is available. 1648 | 1649 | Returns dict with two keys: 'version' and 'full'. 1650 | """ 1651 | if "versioneer" in sys.modules: 1652 | # see the discussion in cmdclass.py:get_cmdclass() 1653 | del sys.modules["versioneer"] 1654 | 1655 | root = get_root() 1656 | cfg = get_config_from_root(root) 1657 | 1658 | assert cfg.VCS is not None, "please set [versioneer]VCS= in setup.cfg" 1659 | handlers = HANDLERS.get(cfg.VCS) 1660 | assert handlers, "unrecognized VCS '%s'" % cfg.VCS 1661 | verbose = verbose or cfg.verbose 1662 | assert cfg.versionfile_source is not None, \ 1663 | "please set versioneer.versionfile_source" 1664 | assert cfg.tag_prefix is not None, "please set versioneer.tag_prefix" 1665 | 1666 | versionfile_abs = os.path.join(root, cfg.versionfile_source) 1667 | 1668 | # extract version from first of: _version.py, VCS command (e.g. 'git 1669 | # describe'), parentdir. This is meant to work for developers using a 1670 | # source checkout, for users of a tarball created by 'setup.py sdist', 1671 | # and for users of a tarball/zipball created by 'git archive' or github's 1672 | # download-from-tag feature or the equivalent in other VCSes. 1673 | 1674 | get_keywords_f = handlers.get("get_keywords") 1675 | from_keywords_f = handlers.get("keywords") 1676 | if get_keywords_f and from_keywords_f: 1677 | try: 1678 | keywords = get_keywords_f(versionfile_abs) 1679 | ver = from_keywords_f(keywords, cfg.tag_prefix, verbose) 1680 | if verbose: 1681 | print("got version from expanded keyword %s" % ver) 1682 | return ver 1683 | except NotThisMethod: 1684 | pass 1685 | 1686 | try: 1687 | ver = versions_from_file(versionfile_abs) 1688 | if verbose: 1689 | print("got version from file %s %s" % (versionfile_abs, ver)) 1690 | return ver 1691 | except NotThisMethod: 1692 | pass 1693 | 1694 | from_vcs_f = handlers.get("pieces_from_vcs") 1695 | if from_vcs_f: 1696 | try: 1697 | pieces = from_vcs_f(cfg.tag_prefix, root, verbose) 1698 | ver = render(pieces, cfg.style) 1699 | if verbose: 1700 | print("got version from VCS %s" % ver) 1701 | return ver 1702 | except NotThisMethod: 1703 | pass 1704 | 1705 | try: 1706 | if cfg.parentdir_prefix: 1707 | ver = versions_from_parentdir(cfg.parentdir_prefix, root, verbose) 1708 | if verbose: 1709 | print("got version from parentdir %s" % ver) 1710 | return ver 1711 | except NotThisMethod: 1712 | pass 1713 | 1714 | if verbose: 1715 | print("unable to compute version") 1716 | 1717 | return {"version": "0+unknown", "full-revisionid": None, 1718 | "dirty": None, "error": "unable to compute version", 1719 | "date": None} 1720 | 1721 | 1722 | def get_version(): 1723 | """Get the short version string for this project.""" 1724 | return get_versions()["version"] 1725 | 1726 | 1727 | def get_cmdclass(cmdclass=None): 1728 | """Get the custom setuptools/distutils subclasses used by Versioneer. 1729 | 1730 | If the package uses a different cmdclass (e.g. one from numpy), it 1731 | should be provide as an argument. 1732 | """ 1733 | if "versioneer" in sys.modules: 1734 | del sys.modules["versioneer"] 1735 | # this fixes the "python setup.py develop" case (also 'install' and 1736 | # 'easy_install .'), in which subdependencies of the main project are 1737 | # built (using setup.py bdist_egg) in the same python process. Assume 1738 | # a main project A and a dependency B, which use different versions 1739 | # of Versioneer. A's setup.py imports A's Versioneer, leaving it in 1740 | # sys.modules by the time B's setup.py is executed, causing B to run 1741 | # with the wrong versioneer. Setuptools wraps the sub-dep builds in a 1742 | # sandbox that restores sys.modules to it's pre-build state, so the 1743 | # parent is protected against the child's "import versioneer". By 1744 | # removing ourselves from sys.modules here, before the child build 1745 | # happens, we protect the child from the parent's versioneer too. 1746 | # Also see https://github.com/python-versioneer/python-versioneer/issues/52 1747 | 1748 | cmds = {} if cmdclass is None else cmdclass.copy() 1749 | 1750 | # we add "version" to both distutils and setuptools 1751 | from distutils.core import Command 1752 | 1753 | class cmd_version(Command): 1754 | description = "report generated version string" 1755 | user_options = [] 1756 | boolean_options = [] 1757 | 1758 | def initialize_options(self): 1759 | pass 1760 | 1761 | def finalize_options(self): 1762 | pass 1763 | 1764 | def run(self): 1765 | vers = get_versions(verbose=True) 1766 | print("Version: %s" % vers["version"]) 1767 | print(" full-revisionid: %s" % vers.get("full-revisionid")) 1768 | print(" dirty: %s" % vers.get("dirty")) 1769 | print(" date: %s" % vers.get("date")) 1770 | if vers["error"]: 1771 | print(" error: %s" % vers["error"]) 1772 | cmds["version"] = cmd_version 1773 | 1774 | # we override "build_py" in both distutils and setuptools 1775 | # 1776 | # most invocation pathways end up running build_py: 1777 | # distutils/build -> build_py 1778 | # distutils/install -> distutils/build ->.. 1779 | # setuptools/bdist_wheel -> distutils/install ->.. 1780 | # setuptools/bdist_egg -> distutils/install_lib -> build_py 1781 | # setuptools/install -> bdist_egg ->.. 1782 | # setuptools/develop -> ? 1783 | # pip install: 1784 | # copies source tree to a tempdir before running egg_info/etc 1785 | # if .git isn't copied too, 'git describe' will fail 1786 | # then does setup.py bdist_wheel, or sometimes setup.py install 1787 | # setup.py egg_info -> ? 1788 | 1789 | # we override different "build_py" commands for both environments 1790 | if 'build_py' in cmds: 1791 | _build_py = cmds['build_py'] 1792 | elif "setuptools" in sys.modules: 1793 | from setuptools.command.build_py import build_py as _build_py 1794 | else: 1795 | from distutils.command.build_py import build_py as _build_py 1796 | 1797 | class cmd_build_py(_build_py): 1798 | def run(self): 1799 | root = get_root() 1800 | cfg = get_config_from_root(root) 1801 | versions = get_versions() 1802 | _build_py.run(self) 1803 | # now locate _version.py in the new build/ directory and replace 1804 | # it with an updated value 1805 | if cfg.versionfile_build: 1806 | target_versionfile = os.path.join(self.build_lib, 1807 | cfg.versionfile_build) 1808 | print("UPDATING %s" % target_versionfile) 1809 | write_to_version_file(target_versionfile, versions) 1810 | cmds["build_py"] = cmd_build_py 1811 | 1812 | if 'build_ext' in cmds: 1813 | _build_ext = cmds['build_ext'] 1814 | elif "setuptools" in sys.modules: 1815 | from setuptools.command.build_ext import build_ext as _build_ext 1816 | else: 1817 | from distutils.command.build_ext import build_ext as _build_ext 1818 | 1819 | class cmd_build_ext(_build_ext): 1820 | def run(self): 1821 | root = get_root() 1822 | cfg = get_config_from_root(root) 1823 | versions = get_versions() 1824 | _build_ext.run(self) 1825 | if self.inplace: 1826 | # build_ext --inplace will only build extensions in 1827 | # build/lib<..> dir with no _version.py to write to. 1828 | # As in place builds will already have a _version.py 1829 | # in the module dir, we do not need to write one. 1830 | return 1831 | # now locate _version.py in the new build/ directory and replace 1832 | # it with an updated value 1833 | target_versionfile = os.path.join(self.build_lib, 1834 | cfg.versionfile_build) 1835 | print("UPDATING %s" % target_versionfile) 1836 | write_to_version_file(target_versionfile, versions) 1837 | cmds["build_ext"] = cmd_build_ext 1838 | 1839 | if "cx_Freeze" in sys.modules: # cx_freeze enabled? 1840 | from cx_Freeze.dist import build_exe as _build_exe 1841 | # nczeczulin reports that py2exe won't like the pep440-style string 1842 | # as FILEVERSION, but it can be used for PRODUCTVERSION, e.g. 1843 | # setup(console=[{ 1844 | # "version": versioneer.get_version().split("+", 1)[0], # FILEVERSION 1845 | # "product_version": versioneer.get_version(), 1846 | # ... 1847 | 1848 | class cmd_build_exe(_build_exe): 1849 | def run(self): 1850 | root = get_root() 1851 | cfg = get_config_from_root(root) 1852 | versions = get_versions() 1853 | target_versionfile = cfg.versionfile_source 1854 | print("UPDATING %s" % target_versionfile) 1855 | write_to_version_file(target_versionfile, versions) 1856 | 1857 | _build_exe.run(self) 1858 | os.unlink(target_versionfile) 1859 | with open(cfg.versionfile_source, "w") as f: 1860 | LONG = LONG_VERSION_PY[cfg.VCS] 1861 | f.write(LONG % 1862 | {"DOLLAR": "$", 1863 | "STYLE": cfg.style, 1864 | "TAG_PREFIX": cfg.tag_prefix, 1865 | "PARENTDIR_PREFIX": cfg.parentdir_prefix, 1866 | "VERSIONFILE_SOURCE": cfg.versionfile_source, 1867 | }) 1868 | cmds["build_exe"] = cmd_build_exe 1869 | del cmds["build_py"] 1870 | 1871 | if 'py2exe' in sys.modules: # py2exe enabled? 1872 | from py2exe.distutils_buildexe import py2exe as _py2exe 1873 | 1874 | class cmd_py2exe(_py2exe): 1875 | def run(self): 1876 | root = get_root() 1877 | cfg = get_config_from_root(root) 1878 | versions = get_versions() 1879 | target_versionfile = cfg.versionfile_source 1880 | print("UPDATING %s" % target_versionfile) 1881 | write_to_version_file(target_versionfile, versions) 1882 | 1883 | _py2exe.run(self) 1884 | os.unlink(target_versionfile) 1885 | with open(cfg.versionfile_source, "w") as f: 1886 | LONG = LONG_VERSION_PY[cfg.VCS] 1887 | f.write(LONG % 1888 | {"DOLLAR": "$", 1889 | "STYLE": cfg.style, 1890 | "TAG_PREFIX": cfg.tag_prefix, 1891 | "PARENTDIR_PREFIX": cfg.parentdir_prefix, 1892 | "VERSIONFILE_SOURCE": cfg.versionfile_source, 1893 | }) 1894 | cmds["py2exe"] = cmd_py2exe 1895 | 1896 | # we override different "sdist" commands for both environments 1897 | if 'sdist' in cmds: 1898 | _sdist = cmds['sdist'] 1899 | elif "setuptools" in sys.modules: 1900 | from setuptools.command.sdist import sdist as _sdist 1901 | else: 1902 | from distutils.command.sdist import sdist as _sdist 1903 | 1904 | class cmd_sdist(_sdist): 1905 | def run(self): 1906 | versions = get_versions() 1907 | self._versioneer_generated_versions = versions 1908 | # unless we update this, the command will keep using the old 1909 | # version 1910 | self.distribution.metadata.version = versions["version"] 1911 | return _sdist.run(self) 1912 | 1913 | def make_release_tree(self, base_dir, files): 1914 | root = get_root() 1915 | cfg = get_config_from_root(root) 1916 | _sdist.make_release_tree(self, base_dir, files) 1917 | # now locate _version.py in the new base_dir directory 1918 | # (remembering that it may be a hardlink) and replace it with an 1919 | # updated value 1920 | target_versionfile = os.path.join(base_dir, cfg.versionfile_source) 1921 | print("UPDATING %s" % target_versionfile) 1922 | write_to_version_file(target_versionfile, 1923 | self._versioneer_generated_versions) 1924 | cmds["sdist"] = cmd_sdist 1925 | 1926 | return cmds 1927 | 1928 | 1929 | CONFIG_ERROR = """ 1930 | setup.cfg is missing the necessary Versioneer configuration. You need 1931 | a section like: 1932 | 1933 | [versioneer] 1934 | VCS = git 1935 | style = pep440 1936 | versionfile_source = src/myproject/_version.py 1937 | versionfile_build = myproject/_version.py 1938 | tag_prefix = 1939 | parentdir_prefix = myproject- 1940 | 1941 | You will also need to edit your setup.py to use the results: 1942 | 1943 | import versioneer 1944 | setup(version=versioneer.get_version(), 1945 | cmdclass=versioneer.get_cmdclass(), ...) 1946 | 1947 | Please read the docstring in ./versioneer.py for configuration instructions, 1948 | edit setup.cfg, and re-run the installer or 'python versioneer.py setup'. 1949 | """ 1950 | 1951 | SAMPLE_CONFIG = """ 1952 | # See the docstring in versioneer.py for instructions. Note that you must 1953 | # re-run 'versioneer.py setup' after changing this section, and commit the 1954 | # resulting files. 1955 | 1956 | [versioneer] 1957 | #VCS = git 1958 | #style = pep440 1959 | #versionfile_source = 1960 | #versionfile_build = 1961 | #tag_prefix = 1962 | #parentdir_prefix = 1963 | 1964 | """ 1965 | 1966 | OLD_SNIPPET = """ 1967 | from ._version import get_versions 1968 | __version__ = get_versions()['version'] 1969 | del get_versions 1970 | """ 1971 | 1972 | INIT_PY_SNIPPET = """ 1973 | from . import {0} 1974 | __version__ = {0}.get_versions()['version'] 1975 | """ 1976 | 1977 | 1978 | def do_setup(): 1979 | """Do main VCS-independent setup function for installing Versioneer.""" 1980 | root = get_root() 1981 | try: 1982 | cfg = get_config_from_root(root) 1983 | except (OSError, configparser.NoSectionError, 1984 | configparser.NoOptionError) as e: 1985 | if isinstance(e, (OSError, configparser.NoSectionError)): 1986 | print("Adding sample versioneer config to setup.cfg", 1987 | file=sys.stderr) 1988 | with open(os.path.join(root, "setup.cfg"), "a") as f: 1989 | f.write(SAMPLE_CONFIG) 1990 | print(CONFIG_ERROR, file=sys.stderr) 1991 | return 1 1992 | 1993 | print(" creating %s" % cfg.versionfile_source) 1994 | with open(cfg.versionfile_source, "w") as f: 1995 | LONG = LONG_VERSION_PY[cfg.VCS] 1996 | f.write(LONG % {"DOLLAR": "$", 1997 | "STYLE": cfg.style, 1998 | "TAG_PREFIX": cfg.tag_prefix, 1999 | "PARENTDIR_PREFIX": cfg.parentdir_prefix, 2000 | "VERSIONFILE_SOURCE": cfg.versionfile_source, 2001 | }) 2002 | 2003 | ipy = os.path.join(os.path.dirname(cfg.versionfile_source), 2004 | "__init__.py") 2005 | if os.path.exists(ipy): 2006 | try: 2007 | with open(ipy, "r") as f: 2008 | old = f.read() 2009 | except OSError: 2010 | old = "" 2011 | module = os.path.splitext(os.path.basename(cfg.versionfile_source))[0] 2012 | snippet = INIT_PY_SNIPPET.format(module) 2013 | if OLD_SNIPPET in old: 2014 | print(" replacing boilerplate in %s" % ipy) 2015 | with open(ipy, "w") as f: 2016 | f.write(old.replace(OLD_SNIPPET, snippet)) 2017 | elif snippet not in old: 2018 | print(" appending to %s" % ipy) 2019 | with open(ipy, "a") as f: 2020 | f.write(snippet) 2021 | else: 2022 | print(" %s unmodified" % ipy) 2023 | else: 2024 | print(" %s doesn't exist, ok" % ipy) 2025 | ipy = None 2026 | 2027 | # Make sure both the top-level "versioneer.py" and versionfile_source 2028 | # (PKG/_version.py, used by runtime code) are in MANIFEST.in, so 2029 | # they'll be copied into source distributions. Pip won't be able to 2030 | # install the package without this. 2031 | manifest_in = os.path.join(root, "MANIFEST.in") 2032 | simple_includes = set() 2033 | try: 2034 | with open(manifest_in, "r") as f: 2035 | for line in f: 2036 | if line.startswith("include "): 2037 | for include in line.split()[1:]: 2038 | simple_includes.add(include) 2039 | except OSError: 2040 | pass 2041 | # That doesn't cover everything MANIFEST.in can do 2042 | # (http://docs.python.org/2/distutils/sourcedist.html#commands), so 2043 | # it might give some false negatives. Appending redundant 'include' 2044 | # lines is safe, though. 2045 | if "versioneer.py" not in simple_includes: 2046 | print(" appending 'versioneer.py' to MANIFEST.in") 2047 | with open(manifest_in, "a") as f: 2048 | f.write("include versioneer.py\n") 2049 | else: 2050 | print(" 'versioneer.py' already in MANIFEST.in") 2051 | if cfg.versionfile_source not in simple_includes: 2052 | print(" appending versionfile_source ('%s') to MANIFEST.in" % 2053 | cfg.versionfile_source) 2054 | with open(manifest_in, "a") as f: 2055 | f.write("include %s\n" % cfg.versionfile_source) 2056 | else: 2057 | print(" versionfile_source already in MANIFEST.in") 2058 | 2059 | # Make VCS-specific changes. For git, this means creating/changing 2060 | # .gitattributes to mark _version.py for export-subst keyword 2061 | # substitution. 2062 | do_vcs_install(manifest_in, cfg.versionfile_source, ipy) 2063 | return 0 2064 | 2065 | 2066 | def scan_setup_py(): 2067 | """Validate the contents of setup.py against Versioneer's expectations.""" 2068 | found = set() 2069 | setters = False 2070 | errors = 0 2071 | with open("setup.py", "r") as f: 2072 | for line in f.readlines(): 2073 | if "import versioneer" in line: 2074 | found.add("import") 2075 | if "versioneer.get_cmdclass()" in line: 2076 | found.add("cmdclass") 2077 | if "versioneer.get_version()" in line: 2078 | found.add("get_version") 2079 | if "versioneer.VCS" in line: 2080 | setters = True 2081 | if "versioneer.versionfile_source" in line: 2082 | setters = True 2083 | if len(found) != 3: 2084 | print("") 2085 | print("Your setup.py appears to be missing some important items") 2086 | print("(but I might be wrong). Please make sure it has something") 2087 | print("roughly like the following:") 2088 | print("") 2089 | print(" import versioneer") 2090 | print(" setup( version=versioneer.get_version(),") 2091 | print(" cmdclass=versioneer.get_cmdclass(), ...)") 2092 | print("") 2093 | errors += 1 2094 | if setters: 2095 | print("You should remove lines like 'versioneer.VCS = ' and") 2096 | print("'versioneer.versionfile_source = ' . This configuration") 2097 | print("now lives in setup.cfg, and should be removed from setup.py") 2098 | print("") 2099 | errors += 1 2100 | return errors 2101 | 2102 | 2103 | if __name__ == "__main__": 2104 | cmd = sys.argv[1] 2105 | if cmd == "setup": 2106 | errors = do_setup() 2107 | errors += scan_setup_py() 2108 | if errors: 2109 | sys.exit(1) 2110 | --------------------------------------------------------------------------------