Skip to content

Commit

Permalink
Add version_file option
Browse files Browse the repository at this point in the history
  • Loading branch information
dolfinus committed Sep 10, 2020
1 parent 4bce22a commit 5c4dd0f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ Automatically set package version using git tag/hash

## Compairing with other packages

| Package/Function | Lastest release | Python2 support | Python3 support | PEP 440 compatible | Separated template for not tagged HEAD | Separated template for dirty run | Using functions outside setup.py | Returning fixed version if no tags | Returning callback if no tags |
|:----------------------------------------------------------------------------------------------------|----------------:|:---------------:|:---------------:|:------------------:|:--------------------------------------:|:--------------------------------:|:--------------------------------:|:--------------------------------:|:--------------------------------:|
| [setuptools-git-versioning](https://github.com/dolfinus/setuptools-git-versioning) | 2020 | + | + | + | + | + | + | + | + |
| [setuptools-git-ver](https://github.com/camas/setuptools-git-ver) (Base package) | 2020 | - | + | + | + | + | - | - | - |
| [even-better-setuptools-git-version](https://github.com/ktemkin/even-better-setuptools-git-version) | 2019 | - | + | + | - | - | + | + | - |
| [better-setuptools-git-version](https://github.com/vivin/better-setuptools-git-version) | 2018 | - | + | + | - | - | + | + | - |
| [very-good-setuptools-git-version](https://github.com/Kautenja/very-good-setuptools-git-version) | 2018 | - | + | - | - | - | + | - | - |
| [setuptools-git-version](https://github.com/pyfidelity/setuptools-git-version) | 2018 | + | + | - | - | - | - | - | - |
| Package/Function | Lastest release | Python2 support | Python3 support | PEP 440 compatible | Separated template for not tagged HEAD | Separated template for dirty run | Using functions outside setup.py | Returning fixed version if no tags | Returning callback if no tags | Reading VERSION file if no tags |
|:----------------------------------------------------------------------------------------------------|----------------:|:---------------:|:---------------:|:------------------:|:--------------------------------------:|:--------------------------------:|:--------------------------------:|:----------------------------------:|:-----------------------------:|:-------------------------------:|
| [setuptools-git-versioning](https://github.com/dolfinus/setuptools-git-versioning) | 2020 | + | + | + | + | + | + | + | + | + |
| [setuptools-git-ver](https://github.com/camas/setuptools-git-ver) (Base package) | 2020 | - | + | + | + | + | - | - | - | - |
| [even-better-setuptools-git-version](https://github.com/ktemkin/even-better-setuptools-git-version) | 2019 | - | + | + | - | - | + | + | - | - |
| [better-setuptools-git-version](https://github.com/vivin/better-setuptools-git-version) | 2018 | - | + | + | - | - | + | + | - | - |
| [very-good-setuptools-git-version](https://github.com/Kautenja/very-good-setuptools-git-version) | 2018 | - | + | - | - | - | + | - | - | - |
| [setuptools-git-version](https://github.com/pyfidelity/setuptools-git-version) | 2018 | + | + | - | - | - | - | - | - | - |

## Installation

Expand Down Expand Up @@ -48,6 +48,7 @@ setuptools.setup(
"dirty_template": "{tag}.dev{ccount}+git.{sha}.dirty",
"starting_version": "0.0.1",
"version_callback": None,
"version_file": None,
},
...
setup_requires=['setuptools-git-versioning'],
Expand All @@ -66,6 +67,9 @@ setuptools.setup(
- `starting_version`: static value, used if not tags exist in repo

- `version_callback`: variable or callback function to get version instead of using `starting_version`

- `version_file`: path to VERSION file, to read version from it instead of using `static_version`

### Format Options

- `{tag}`: Latest tag in the repository
Expand Down
17 changes: 15 additions & 2 deletions setuptools_git_versioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,31 @@ def parse_config(dist, _, value): # type: (Distribution, Any, Any) -> None
dirty_template = value.get('dirty_template', DEFAULT_DIRTY_TEMPLATE)
starting_version = value.get('starting_version', DEFAULT_STARTING_VERSION)
version_callback = value.get('version_callback', None)
version_file = value.get('version_file', None)

version = version_from_git(
template=template,
dev_template=dev_template,
dirty_template=dirty_template,
starting_version=starting_version,
version_callback=version_callback,
version_file=version_file,
)
dist.metadata.version = version


def read_version_from_file(path):
with open(path, 'r') as file:
return file.read().strip()


def version_from_git(template=DEFAULT_TEMPLATE,
dev_template=DEFAULT_DEV_TEMPLATE,
dirty_template=DEFAULT_DIRTY_TEMPLATE,
starting_version=DEFAULT_STARTING_VERSION,
version_callback=None,
): # type: (str, str, str, str, Optional[Any, Callable]) -> str
version_file=None,
): # type: (str, str, str, str, Optional[Any, Callable], Optional[str]) -> str

# Check if PKG-INFO exists and return value in that if it does
if os.path.exists('PKG-INFO'):
Expand All @@ -121,7 +129,12 @@ def version_from_git(template=DEFAULT_TEMPLATE,
return version_callback()
else:
return version_callback
return starting_version

if not os.path.exists(version_file):
return starting_version
else:
tag = read_version_from_file(version_file)
return tag

dirty = is_dirty()
tag_sha = get_sha(tag)
Expand Down

0 comments on commit 5c4dd0f

Please sign in to comment.