Skip to content

Instantly share code, notes, and snippets.

@MichalMazurek
Last active December 25, 2018 01:58
Show Gist options
  • Save MichalMazurek/e2d47ac52587db62fc073cfc10845a1f to your computer and use it in GitHub Desktop.
Save MichalMazurek/e2d47ac52587db62fc073cfc10845a1f to your computer and use it in GitHub Desktop.
"""
Version retrieving.
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org>
"""
from pathlib import Path
__version__ = '0.0.0'
version_path: Path = Path(__file__).parent / 'version.txt'
if version_path.exists():
with version_path.open('r') as f:
__version__ = f.read().strip()
"""
Namespace __init__.py
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org>
"""
__path__ = __import__('pkgutil').extend_path(__path__, __name__)
__import__('pkg_resources').declare_namespace(packageName="some")
"""
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org>
"""
from setuptools import setup, find_packages
from pathlib import Path
import time
try:
import git
except ImportError:
git = False
main_module_path = Path('some') / 'module'
main_module = 'some.module'
project_name = 'some-module'
namespace_packages = ['some']
__author__ = 'Author Name'
__author_email__ = 'email@domain.ltd'
def version_date(commit: 'git.Commit') -> str:
version_date = (
time.strftime('%Y.%-m.%-d', time.gmtime(commit.authored_date))
)
return version_date
def generate_version():
VERSION = '0.0.0'
repo = git.Repo('./')
latest_date_commits = []
for commit in repo.iter_commits():
v_date = version_date(commit)
if latest_date_commits:
if v_date == latest_date_commits[0]:
latest_date_commits.append(v_date)
else:
break
else:
latest_date_commits.append(v_date)
if repo.active_branch.name == 'master':
# get info from tag
try:
VERSION = list(repo.tags)[-1]
except IndexError:
# no tags
pass
elif repo.active_branch.name == "develop":
# dev version
VERSION = f'{latest_date_commits[0]}.dev{len(latest_date_commits)}'
else:
VERSION = (
f'{latest_date_commits[0]}'
f'.a{len(latest_date_commits)}+{repo.commit().hexsha[:12]}'
)
return VERSION
VERSION = '0.0.1'
if git:
VERSION = generate_version()
version_file = Path(__file__).parent / main_module_path / 'version.txt'
with version_file.open('w') as v_file:
v_file.write(VERSION)
setup(
name=project_name,
author=__author__,
author_email=__author_email__,
python_requires='>=3.6',
install_requires=[
'click',
],
setup_requires=[
'gitpython'
],
tests_require=[
'pytest',
'pytest-cov',
'pytest-asyncio'
],
version=VERSION,
packages=find_packages(),
namespace_packages=namespace_packages,
entry_points={
'console_scripts': [
f'{project_name} = {main_module}.cli',
]
},
package_data={
f'{main_module}': ['version.txt']
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment