This commit is contained in:
Amber Brown (HawkOwl) 2015-11-30 01:03:10 +08:00
parent 17f26ac88a
commit b2e812003b
4 changed files with 37 additions and 68 deletions

View File

@ -11,45 +11,17 @@ Incremental is a small library that versions your Python projects.
Quick Start
-----------
Add this to the top of your ``setup.py``, assuming your code is called ``widgetbox``:
.. code::
my_project = 'widgetbox'
import os, importlib
def install_incremental():
import importlib
try:
importlib.import_module('incremental')
except ImportError:
import pip
pip.main(['install', 'incremental>=0.1.0'])
finally:
globals()['incremental'] = importlib.import_module('incremental')
install_incremental()
# PICK ONE OF:
# If you have a src/ dir
base_dir = os.path.dirname(__file__)
src_dir = os.path.join(base_dir, "src")
# If you do not
src_dir = os.path.dirname(__file__)
version = incremental.get_version_from_project(my_project, src_dir)
And in the ``setup`` call, add:
Add this to your ``setup.py``\ 's ``setup()`` call:
.. code::
setup(
name=my_project,
version=version.base(),
use_incremental=True,
setup_requires=['incremental'],
...
}
Then in your project add a ``_version.py`` that contains:
.. code::
@ -60,7 +32,7 @@ Then in your project add a ``_version.py`` that contains:
__all__ = ["__version__"]
Then in your project's ``__init__.py`` add:
Then, so users of your project can find your version, in your project's ``__init__.py`` add:
.. code::

View File

@ -1,30 +1,9 @@
my_project = 'exampleproj'
import os, importlib
def install_incremental():
import importlib
try:
importlib.import_module('incremental')
except ImportError:
import pip
pip.main(['install', 'incremental>=15.0.0'])
finally:
globals()['incremental'] = importlib.import_module('incremental')
install_incremental()
# PICK ONE OF:
# If you have a src/ dir
base_dir = os.path.dirname(__file__)
src_dir = os.path.join(base_dir, "src")
version = incremental.get_version_from_project(my_project, src_dir)
# Install the package
from setuptools import setup
setup(
name=my_project,
version=version.base()
name='exampleproj',
package_dir={"": "src"},
packages=["exampleproj"],
use_incremental=True,
setup_requires=['incremental'],
)

View File

@ -34,5 +34,10 @@ setup(
package_dir={"": "src"},
py_modules=['incremental'],
license="MIT",
long_description=open('README.rst').read()
zip_safe=False,
long_description=open('README.rst').read(),
entry_points="""
[distutils.setup_keywords]
use_incremental = incremental:_get_version
""",
)

View File

@ -403,18 +403,31 @@ def getVersionString(version):
return result
def get_version_from_project(project, src_dir):
def _get_version(dist, keyword, value):
"""
Get a L{Version} object from a flat source directory.
Get the version from the package listed in the Distribution.
"""
version_file = {}
if not value:
return
with open(os.path.join(src_dir, project, "_version.py")) as f:
exec(f.read(), version_file)
from distutils.command import build_py
return version_file["__version__"]
sp_command = build_py.build_py(dist)
sp_command.finalize_options()
for item in sp_command.find_all_modules():
if item[1] == "_version":
version_file = {}
with open(item[2]) as f:
exec(f.read(), version_file)
dist.metadata.version = version_file["__version__"].short()
return None
raise Exception("No _version.py found.")
__version__ = Version("incremental", 15, 0, 0)
__version__ = Version("incremental", 15, 2, 0)
__all__ = ["__version__", "Version", "get_version_from_project"]
__all__ = ["__version__", "Version"]