Skip to content

Commit

Permalink
Change the way version info is handled in packaging (unitaryfund#247)
Browse files Browse the repository at this point in the history
* Add .python-version to .gitignore for pyenv

* Add VERSION.txt to MANIFEST.in so it can be used in packaging

* Add version.py file that gets overwritten when packaging

* Overwrite and restore version.py in setup.py

* Import from version.py in __init__.py rather than parsing VERSION.txt
  • Loading branch information
karalekas authored Jul 17, 2020
1 parent c0ee807 commit 138d061
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.DS_Store
.vscode
.python-version
.idea/
.coverage*
*.dat
Expand Down
3 changes: 2 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
include README.md
include LICENSE
include README.md
include VERSION.txt
include requirements.txt
include development_requirements.txt
include test_build.sh
Expand Down
7 changes: 2 additions & 5 deletions mitiq/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
This is the top level module from which functions and classes of
Mitiq can be directly imported.
"""
from mitiq.version import __version__

import os
from typing import Union

Expand Down Expand Up @@ -32,11 +34,6 @@
# this must be after QPROGRAM as the zne.py module imports QPROGRAM
from mitiq.zne import execute_with_zne, mitigate_executor

_directory_of_this_file = os.path.dirname(os.path.abspath(__file__))

with open(str(_directory_of_this_file) + "/../VERSION.txt", "r") as f:
__version__ = f.read().strip()


def version():
"""Returns the Mitiq version number."""
Expand Down
9 changes: 9 additions & 0 deletions mitiq/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""
NOTE: This file will be overwritten by the packaging process.
"""
import os

directory_of_this_file = os.path.dirname(os.path.abspath(__file__))

with open(f"{directory_of_this_file}/../VERSION.txt", "r") as f:
__version__ = f.read().strip()
12 changes: 12 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
with open('development_requirements.txt') as f:
dev_requirements = f.read().splitlines()

# save the source code in version.py
with open("mitiq/version.py", "r") as f:
version_file_source = f.read()

# overwrite version.py in the source distribution
with open("mitiq/version.py", "w") as f:
f.write(f"__version__ = '{__version__}'\n")

setup(
name='mitiq',
version=__version__,
Expand All @@ -35,3 +43,7 @@
url = "https://unitary.fund",

)

# restore version.py to its previous state
with open("mitiq/version.py", "w") as f:
f.write(version_file_source)

0 comments on commit 138d061

Please sign in to comment.