Skip to content

Commit

Permalink
cli: remove _version.py file
Browse files Browse the repository at this point in the history
Add a change to create this version dynamically. In addition, for dev
setups, this commit adds the ability of showing the version of the
without having to be in the popper repo folder.
  • Loading branch information
ivotron committed Jul 16, 2020
1 parent ce2302c commit f01f05a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 28 deletions.
43 changes: 32 additions & 11 deletions src/popper/__init__.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,42 @@
import importlib
import pathlib
import os

__version__ = "0.0.0"

_repo_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "..", "..")
_git_dir = os.path.join(_repo_dir, ".git")
_version_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), "_version.py")

# check if dunamai is available
dunamai_spec = importlib.util.find_spec("dunamai")
dunamai_found = dunamai_spec is not None
if dunamai_found:
_dunamai_spec = importlib.util.find_spec("dunamai")
_dunamai_found = _dunamai_spec is not None

# if codebase is inside a git repo and dunamai available
if os.path.isdir(_git_dir) and _dunamai_found:

# if dunamai is found, then we use it to display the version
import dunamai

__version__ = dunamai.Version.from_any_vcs().serialize()
_ver = f'__popper_version__ = "{__version__}"'
# define GIT_DIR so dunamai gets the info for the popper repo instead of wherever
# popper is being invoked from
os.environ["GIT_DIR"] = _git_dir

try:
__version__ = dunamai.Version.from_git().serialize()
_ver = f"__popper_version__ = '{__version__}'"
except RuntimeError as e:
# only raise if not a dunamai-raised error
if "This does not appear to be a Git project" not in str(e):
raise e

_init_script_dir = pathlib.Path(__file__).parent.absolute()
_version_path_ = os.path.join(_init_script_dir, "_version.py")
with open(_version_path_, "w") as v:
with open(_version_file, "w") as v:
v.write(_ver + "\n")
else:
from popper._version import __popper_version__ as __version__

# unset GIT_DIR
os.environ.pop("GIT_DIR")

elif os.path.isfile(_version_file):
# codebase not in a popper repo, and version file exists, so let's read from it
from popper._version import __popper_version__ as _ver

__version__ = _ver
2 changes: 1 addition & 1 deletion src/popper/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__popper_version__ = "0.0.0"
__popper_version__ = '2.7.0.post2.dev0+bed65005'
19 changes: 11 additions & 8 deletions src/setup.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import os

from setuptools import setup

version = {}
with open("popper/_version.py") as f:
exec(f.read(), version)
version = {"__popper_version__": "0.0.0"}
version_file = "./popper/_version.py"
if os.path.isfile(version_file):
with open(version_file) as f:
exec(f.read(), version)

setup(
name="popper",
version=version["__popper_version__"],
author="The Popper Development Team",
author_email="ivo@cs.ucsc.edu",
url="http://falsifiable.us",
author_email="ivotron@ucsc.edu",
url="https://getpopper.io",
description="Popper CLI tool to generate reproducible papers.",
packages=["popper", "popper.commands"],
include_package_data=True,
Expand All @@ -32,9 +36,8 @@
""",
project_urls={
"Documentation": "https://popper.readthedocs.io",
"Say Thanks!": "http://gitter.im/falsifiable-us/popper",
"Source": "https://github.com/systemslab/popper/",
"Tracker": "https://github.com/systemslab/popper/",
"Source": "https://github.com/getpopper/popper/",
"Tracker": "https://github.com/getpopper/popper/issues",
},
keywords="popper reproducibility automation continuous-integration",
license="MIT",
Expand Down
16 changes: 8 additions & 8 deletions src/test/test_cmd_version.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
from click.testing import CliRunner

from popper import __version__
from popper._version import __popper_version__
from popper import _version_file
from popper.commands import cmd_version
from .test_common import PopperTest


class TestCommandVersion(PopperTest):
def test_version(self):

self.assertIsNot("0.0.0", __version__)
with self.assertLogs("popper") as test:
runner = CliRunner()
result = runner.invoke(cmd_version.cli)
self.assertEqual(result.exit_code, 0)
self.assertIsNot(__version__, "0.0.0")
self.assertEqual(__version__, __popper_version__)
self.assertTrue(__version__ in test.output[0])
result = CliRunner().invoke(cmd_version.cli)
self.assertTrue(__version__ in test.output[0])
self.assertEqual(0, result.exit_code)

with open(_version_file) as f:
self.assertEqual(f"__popper_version__ = '{__version__}'\n", f.read())

0 comments on commit f01f05a

Please sign in to comment.