Library API
High-Level Functions
- versioningit.get_version(project_dir: Union[str, pathlib.Path] = '.', config: Optional[dict] = None, write: bool = False, fallback: bool = True) str [source]
Determine the version for the project at
project_dir
.If
config
isNone
, thenproject_dir
must contain apyproject.toml
file containing a[tool.versioningit]
table; if it does not, aNotVersioningitError
is raised. Ifconfig
is notNone
, then anypyproject.toml
file inproject_dir
will be ignored, and the configuration will be taken fromconfig
instead; see “Passing Explicit Configuration”.If
write
is true, then the file specified in the[tool.versioningit.write]
subtable, if any, will be updated.If
fallback
is true, then ifproject_dir
is not under version control (or if the VCS executable is not installed),versioningit
will assume that the directory is an unpacked sdist and will read the version from thePKG-INFO
file.- Raises
NotVCSError – if
fallback
is false andproject_dir
is not under version controlNotSdistError – if
fallback
is true,project_dir
is not under version control, and there is noPKG-INFO
file inproject_dir
if
config
isNone
andproject_dir
does not contain apyproject.toml
fileif the
pyproject.toml
file does not contain a[tool.versioningit]
table
ConfigError – if any of the values in
config
are not of the correct typeMethodError – if a method returns a value of the wrong type
- versioningit.get_next_version(project_dir: Union[str, pathlib.Path] = '.', config: Optional[dict] = None) str [source]
New in version 0.3.0.
Determine the next version after the current VCS-tagged version for
project_dir
.If
config
isNone
, thenproject_dir
must contain apyproject.toml
file containing a[tool.versioningit]
table; if it does not, aNotVersioningitError
is raised. Ifconfig
is notNone
, then anypyproject.toml
file inproject_dir
will be ignored, and the configuration will be taken fromconfig
instead; see “Passing Explicit Configuration”.- Raises
NotVCSError – if
project_dir
is not under version controlif
config
isNone
andproject_dir
does not contain apyproject.toml
fileif the
pyproject.toml
file does not contain a[tool.versioningit]
table
ConfigError – if any of the values in
config
are not of the correct typeMethodError – if a method returns a value of the wrong type
- versioningit.get_cmdclasses(bases: Optional[Dict[str, Type[Command]]] = None) Dict[str, Type[Command]] [source]
New in version 1.1.0.
Return a
dict
of custom setuptoolsCommand
classes, suitable for passing to thecmdclass
argument ofsetuptools.setup()
, that run theonbuild
step for the project when building an sdist or wheel. Specifically, thedict
contains a subclass ofsetuptools.command.sdist.sdist
at the"sdist"
key and a subclass ofsetuptools.command.build_py.build_py
at the"build_py"
key.A
dict
of alternative base classes can optionally be supplied; if thedict
contains an"sdist"
entry, that entry will be used as the base class for the customizedsdist
command, and likewise for"build_py"
. All other classes in the inputdict
are passed through unchanged.
Low-Level Class
- class versioningit.Versioningit[source]
A class for getting a version-controlled project’s current version based on its most recent tag and the difference therefrom
- classmethod from_project_dir(project_dir: Union[str, pathlib.Path] = '.', config: Optional[dict] = None) versioningit.core.Versioningit [source]
Construct a
Versioningit
object for the project rooted atproject_dir
(default: the current directory).If
config
isNone
, thenproject_dir
must contain apyproject.toml
file containing a[tool.versioningit]
table; if it does not, aNotVersioningitError
is raised. Ifconfig
is notNone
, then anypyproject.toml
file inproject_dir
will be ignored, and the configuration will be taken fromconfig
instead. See “Passing Explicit Configuration”.- Raises
ConfigError – if the
tool.versioningit
key,config
, or any subfields are not of the correct type
- get_version() str [source]
Determine the version for
project_dir
- Raises
MethodError – if a method returns a value of the wrong type
- do_vcs() versioningit.core.VCSDescription [source]
Run the
vcs
step- Raises
MethodError – if the method does not return a
VCSDescription
- do_tag2version(tag: str) str [source]
Run the
tag2version
step- Raises
MethodError – if the method does not return a
str
- do_next_version(version: str, branch: Optional[str]) str [source]
Run the
next-version
step- Raises
MethodError – if the method does not return a
str
- do_format(description: versioningit.core.VCSDescription, version: str, next_version: str) str [source]
Run the
format
step- Raises
MethodError – if the method does not return a
str
Exceptions
- exception versioningit.ConfigError[source]
Bases:
versioningit.errors.Error
,ValueError
Raised when the
versioningit
configuration contain invalid settings
- exception versioningit.InvalidTagError[source]
Bases:
versioningit.errors.Error
,ValueError
Raised by
tag2version
methods when passed a tag that they cannot work with
- exception versioningit.InvalidVersionError[source]
Bases:
versioningit.errors.Error
,ValueError
Raised by
next-version
methods when passed a version that they cannot work with
- exception versioningit.MethodError[source]
Bases:
versioningit.errors.Error
Raised when a method is invalid or returns an invalid value
- exception versioningit.NoTagError[source]
Bases:
versioningit.errors.Error
Raised when a tag cannot be found in version control
- exception versioningit.NotSdistError[source]
Bases:
versioningit.errors.Error
Raised when attempting to read a
PKG-INFO
file from a directory that doesn’t have one
- exception versioningit.NotVCSError[source]
Bases:
versioningit.errors.Error
Raised when
versioningit
is run in a directory that is not under version control or when the relevant VCS program is not installed
- exception versioningit.NotVersioningitError[source]
Bases:
versioningit.errors.Error
Raised when
versioningit
is used on a project that does not haveversioningit
enabled
Utilities
- class versioningit.VCSDescription(tag: str, state: str, branch: Optional[str], fields: Dict[str, Any])[source]
A description of the state of a version control repository
- branch: Optional[str]
The name of the repository’s current branch, or
None
if it cannot be determined or does not apply
- fields: Dict[str, Any]
A
dict
of additional information about the repository state to make available to theformat
method Customvcs
methods are advised to adhere closely to the set of fields used by the built-in methods.
- state: str
The relationship of the repository’s current state to the tag. If the repository state is exactly the tagged state, this field should equal
"exact"
; otherwise, it will be a string that will be used as a key in the[tool.versioningit.format]
subtable. Recommended values are"distance"
,"dirty"
, and"distance-dirty"
.
- versioningit.get_version_from_pkg_info(project_dir: Union[str, pathlib.Path]) str [source]
Return the Version field from the
PKG-INFO
file inproject_dir
- Raises
NotSdistError – if there is no
PKG-INFO
fileValueError – if the
PKG-INFO
file does not contain a Version field
- versioningit.run_onbuild(*, build_dir: Union[str, pathlib.Path], is_source: bool, version: str, project_dir: Union[str, pathlib.Path] = '.', config: Optional[dict] = None) None [source]
New in version 1.1.0.
Run the
onbuild
step for the given project.If
config
isNone
, thenproject_dir
must contain apyproject.toml
file containing a[tool.versioningit]
table; if it does not, aNotVersioningitError
is raised. Ifconfig
is notNone
, then anypyproject.toml
file inproject_dir
will be ignored, and the configuration will be taken fromconfig
instead; see “Passing Explicit Configuration”.- Parameters
- Raises
if
config
isNone
andproject_dir
does not contain apyproject.toml
fileif the
pyproject.toml
file does not contain a[tool.versioningit]
table
ConfigError – if any of the values in
config
are not of the correct typeMethodError – if a method returns a value of the wrong type
Passing Explicit Configuration
The functions & methods that take a path to a project directory normally read
the project’s configuration from the pyproject.toml
file therein, but
they can also be passed a config
argument to take the configuration from
instead, in which case pyproject.toml
will be ignored and need not even
exist.
A config
argument must be a dict
whose structure mirrors the structure of
the [tool.versioningit]
table in pyproject.toml
. For example, the
following TOML configuration:
[tool.versioningit.vcs]
method = "git"
match = ["v*"]
[tool.versioningit.next-version]
method = { module = "setup", value = "my_next_version" }
[tool.versioningit.format]
distance = "{next_version}.dev{distance}+{vcs}{rev}"
dirty = "{version}+dirty"
distance-dirty = "{next_version}.dev{distance}+{vcs}{rev}.dirty"
corresponds to the following Python config
value:
{
"vcs": {
"method": "git",
"match": ["v*"],
},
"next-version": {
"method": {
"module": "setup",
"value": "my_next_version",
},
},
"format": {
"distance": "{next_version}.dev{distance}+{vcs}{rev}",
"dirty": "{version}+dirty",
"distance-dirty": "{next_version}.dev{distance}+{vcs}{rev}.dirty",
},
}
This is the same structure that you would get by reading from the
pyproject.toml
file like so:
import tomli
with open("pyproject.toml", "rb") as fp:
config = tomli.load(fp)["tool"]["versioningit"]
When passing versioningit
configuration as a config
argument, an
alternative way to specify methods becomes available: in place of a method
specification, one can pass a callable object directly.