Skip to content

Commit

Permalink
version: try to get version from pip
Browse files Browse the repository at this point in the history
If we can't determine any version info from `git describe` try to get
the version from pip, otherwise fallback to the hard-coded package
version.

Also force to use the git version string both when we are installing
(setup.py) the package or when we are directly running vng from the
source repo.

Signed-off-by: Andrea Righi <[email protected]>
  • Loading branch information
arighi committed Jul 17, 2024
1 parent 30d7c33 commit fb9f6be
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
6 changes: 5 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
from setuptools import setup, Command
from setuptools.command.build_py import build_py
from setuptools.command.egg_info import egg_info
from virtme_ng.version import VERSION
from virtme_ng.version import get_version_string

os.environ["__VNG_LOCAL"] = "1"
VERSION = get_version_string()

# Source .config if it exists (where we can potentially defined config/build
# options)
Expand Down Expand Up @@ -52,6 +55,7 @@ def run(self):
for cmd in ("flake8", "pylint"):
command = [cmd]
for pattern in (
"vng",
"*.py",
"virtme/*.py",
"virtme/*/*.py",
Expand Down
17 changes: 14 additions & 3 deletions virtme_ng/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,23 @@

import os
from subprocess import check_output, DEVNULL, CalledProcessError
import pkg_resources

PKG_VERSION = "1.25"


def get_package_version():
try:
return pkg_resources.get_distribution("virtme-ng").version
except pkg_resources.DistributionNotFound:
return PKG_VERSION


def get_version_string():
try:
if not os.environ.get("__VNG_LOCAL"):
return get_package_version()

# Get the version from `git describe`.
#
# Make sure to get the proper git repository by using the directory
Expand All @@ -37,9 +48,9 @@ def get_version_string():

return version_pep440
except CalledProcessError:
# Default version if git describe fails (e.g., when building virtme-ng
# from a source package.
return PKG_VERSION
# If git describe fails to determine a version, try to use the version
# from pip, or ultimately simply return the hard-coded package version.
return get_package_version()


VERSION = get_version_string()
Expand Down
7 changes: 5 additions & 2 deletions vng
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

import os
import sys
from virtme_ng import run
os.environ["__VNG_LOCAL"] = "1"
from virtme_ng import run # noqa: E402


# Update PATH to make sure that virtme-ng can be executed directly from the
# source directory, without necessarily installing virtme-ng in the system.
Expand All @@ -17,5 +19,6 @@ def update_path():
new_path = f'{script_dir}:{current_path}'
os.environ['PATH'] = new_path


update_path()
exit(run.main())
sys.exit(run.main())

0 comments on commit fb9f6be

Please sign in to comment.