Skip to content

Commit 5c4dd0f

Browse files
committed
Add version_file option
1 parent 4bce22a commit 5c4dd0f

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

README.md

+12-8
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ Automatically set package version using git tag/hash
88

99
## Compairing with other packages
1010

11-
| 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 |
12-
|:----------------------------------------------------------------------------------------------------|----------------:|:---------------:|:---------------:|:------------------:|:--------------------------------------:|:--------------------------------:|:--------------------------------:|:--------------------------------:|:--------------------------------:|
13-
| [setuptools-git-versioning](https://github.com/dolfinus/setuptools-git-versioning) | 2020 | + | + | + | + | + | + | + | + |
14-
| [setuptools-git-ver](https://github.com/camas/setuptools-git-ver) (Base package) | 2020 | - | + | + | + | + | - | - | - |
15-
| [even-better-setuptools-git-version](https://github.com/ktemkin/even-better-setuptools-git-version) | 2019 | - | + | + | - | - | + | + | - |
16-
| [better-setuptools-git-version](https://github.com/vivin/better-setuptools-git-version) | 2018 | - | + | + | - | - | + | + | - |
17-
| [very-good-setuptools-git-version](https://github.com/Kautenja/very-good-setuptools-git-version) | 2018 | - | + | - | - | - | + | - | - |
18-
| [setuptools-git-version](https://github.com/pyfidelity/setuptools-git-version) | 2018 | + | + | - | - | - | - | - | - |
11+
| 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 |
12+
|:----------------------------------------------------------------------------------------------------|----------------:|:---------------:|:---------------:|:------------------:|:--------------------------------------:|:--------------------------------:|:--------------------------------:|:----------------------------------:|:-----------------------------:|:-------------------------------:|
13+
| [setuptools-git-versioning](https://github.com/dolfinus/setuptools-git-versioning) | 2020 | + | + | + | + | + | + | + | + | + |
14+
| [setuptools-git-ver](https://github.com/camas/setuptools-git-ver) (Base package) | 2020 | - | + | + | + | + | - | - | - | - |
15+
| [even-better-setuptools-git-version](https://github.com/ktemkin/even-better-setuptools-git-version) | 2019 | - | + | + | - | - | + | + | - | - |
16+
| [better-setuptools-git-version](https://github.com/vivin/better-setuptools-git-version) | 2018 | - | + | + | - | - | + | + | - | - |
17+
| [very-good-setuptools-git-version](https://github.com/Kautenja/very-good-setuptools-git-version) | 2018 | - | + | - | - | - | + | - | - | - |
18+
| [setuptools-git-version](https://github.com/pyfidelity/setuptools-git-version) | 2018 | + | + | - | - | - | - | - | - | - |
1919

2020
## Installation
2121

@@ -48,6 +48,7 @@ setuptools.setup(
4848
"dirty_template": "{tag}.dev{ccount}+git.{sha}.dirty",
4949
"starting_version": "0.0.1",
5050
"version_callback": None,
51+
"version_file": None,
5152
},
5253
...
5354
setup_requires=['setuptools-git-versioning'],
@@ -66,6 +67,9 @@ setuptools.setup(
6667
- `starting_version`: static value, used if not tags exist in repo
6768

6869
- `version_callback`: variable or callback function to get version instead of using `starting_version`
70+
71+
- `version_file`: path to VERSION file, to read version from it instead of using `static_version`
72+
6973
### Format Options
7074

7175
- `{tag}`: Latest tag in the repository

setuptools_git_versioning.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -88,23 +88,31 @@ def parse_config(dist, _, value): # type: (Distribution, Any, Any) -> None
8888
dirty_template = value.get('dirty_template', DEFAULT_DIRTY_TEMPLATE)
8989
starting_version = value.get('starting_version', DEFAULT_STARTING_VERSION)
9090
version_callback = value.get('version_callback', None)
91+
version_file = value.get('version_file', None)
9192

9293
version = version_from_git(
9394
template=template,
9495
dev_template=dev_template,
9596
dirty_template=dirty_template,
9697
starting_version=starting_version,
9798
version_callback=version_callback,
99+
version_file=version_file,
98100
)
99101
dist.metadata.version = version
100102

101103

104+
def read_version_from_file(path):
105+
with open(path, 'r') as file:
106+
return file.read().strip()
107+
108+
102109
def version_from_git(template=DEFAULT_TEMPLATE,
103110
dev_template=DEFAULT_DEV_TEMPLATE,
104111
dirty_template=DEFAULT_DIRTY_TEMPLATE,
105112
starting_version=DEFAULT_STARTING_VERSION,
106113
version_callback=None,
107-
): # type: (str, str, str, str, Optional[Any, Callable]) -> str
114+
version_file=None,
115+
): # type: (str, str, str, str, Optional[Any, Callable], Optional[str]) -> str
108116

109117
# Check if PKG-INFO exists and return value in that if it does
110118
if os.path.exists('PKG-INFO'):
@@ -121,7 +129,12 @@ def version_from_git(template=DEFAULT_TEMPLATE,
121129
return version_callback()
122130
else:
123131
return version_callback
124-
return starting_version
132+
133+
if not os.path.exists(version_file):
134+
return starting_version
135+
else:
136+
tag = read_version_from_file(version_file)
137+
return tag
125138

126139
dirty = is_dirty()
127140
tag_sha = get_sha(tag)

0 commit comments

Comments
 (0)