Skip to content

Commit 2dda43a

Browse files
authored
Merge pull request #1 from dephell/init-all-rules
Add all rules and tests from DepHell
2 parents a68892e + d4e61f1 commit 2dda43a

27 files changed

Lines changed: 976 additions & 0 deletions

.editorconfig

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
# EditorConfig helps developers define and maintain consistent
3+
# coding styles between different editors and IDEs
4+
# https://editorconfig.org
5+
root = true
6+
7+
[*]
8+
end_of_line = lf
9+
charset = utf-8
10+
trim_trailing_whitespace = true
11+
insert_final_newline = true
12+
13+
[*.py]
14+
indent_style = space
15+
indent_size = 4
16+
17+
[*.{md,rst,txt}]
18+
indent_style = space
19+
indent_size = 4
20+
21+
[*.{ini,toml}]
22+
indent_style = space
23+
indent_size = 4
24+
25+
[*.{json,yml,yaml}]
26+
indent_style = space
27+
indent_size = 2

.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.pytest_cache/
2+
__pycache__/
3+
*.pyc
4+
5+
*.egg-info/
6+
dist/
7+
build/
8+
pip-wheel-metadata/
9+
10+
.tox
11+
.mypy_cache
12+
13+
.dephell_cache/
14+
.dephell_report/
15+
16+
README.rst
17+
setup.py

.travis.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
language: python
2+
3+
before_install:
4+
# show a little bit more information about environment
5+
- sudo apt-get install -y tree
6+
- env
7+
- tree
8+
# install DepHell
9+
# https://github.com/travis-ci/travis-ci/issues/8589
10+
- curl https://raw.githubusercontent.com/dephell/dephell/master/install.py | /opt/python/3.6/bin/python
11+
- dephell inspect self
12+
install:
13+
- dephell venv create --env=$ENV --python="/opt/python/$TRAVIS_PYTHON_VERSION/bin/python"
14+
- dephell deps install --env=$ENV
15+
script:
16+
- dephell venv run --env=$ENV
17+
18+
matrix:
19+
include:
20+
- python: "3.5"
21+
env: ENV=pytest
22+
- python: "3.6"
23+
env: ENV=pytest
24+
- python: "3.7-dev"
25+
env: ENV=pytest
26+
- python: "pypy3.5"
27+
env: ENV=pytest
28+
29+
- python: "3.6"
30+
env: ENV=flake8
31+
- python: "3.6"
32+
env: ENV=typing

README.md

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
## dephell_versioning
2+
3+
[![travis](https://travis-ci.org/dephell/dephell_versioning.svg?branch=master)](https://travis-ci.org/dephell/dephell_versioning)
4+
[![appveyor](https://ci.appveyor.com/api/projects/status/github/dephell/dephell_versioning?svg=true)](https://ci.appveyor.com/project/orsinium/dephell-versioning)
5+
[![MIT License](https://img.shields.io/pypi/l/dephell-versioning.svg)](https://github.com/dephell/dephell_versioning/blob/master/LICENSE)
6+
7+
Library for bumping project version.
8+
9+
Available schemes:
10+
11+
+ `calver`
12+
+ `comver`
13+
+ `pep`
14+
+ `roman`
15+
+ `romver`
16+
+ `semver`
17+
+ `serial`
18+
+ `zerover`
19+
20+
Available rules (and aliases):
21+
22+
+ `init` -- initialize versioning
23+
+ Main parts:
24+
+ `major` (`breaking`)
25+
+ `minor` (`feature`)
26+
+ `patch` (`fix`, `micro`)
27+
+ Additional parts:
28+
+ `dev`
29+
+ `local`
30+
+ `post`
31+
+ Pre-release management:
32+
+ `pre` (`rc`, `alpha`, `beta`)
33+
+ `premajor` (`prebreaking`)
34+
+ `preminor` (`prefeature`)
35+
+ `prepatch` (`prefix`, `premicro`)
36+
+ `release`
37+
38+
Read more about schemes and rules in the documentation for [dephell project bump](https://dephell.readthedocs.io/en/latest/cmd-project-bump.html).
39+
40+
## Installation
41+
42+
install from [PyPI](https://pypi.org/project/dephell-versioning/):
43+
44+
```bash
45+
python3 -m pip install --user dephell_versioning
46+
```
47+
48+
## Usage
49+
50+
Get available schemes, rules, and aliases:
51+
52+
```python
53+
from dephell_versioning import get_aliases, get_rules, get_schemes
54+
get_schemes()
55+
# frozenset({'roman', 'pep', ..., 'comver'})
56+
57+
get_rules()
58+
# frozenset({'local', 'minor', ..., 'dev', 'preminor'})
59+
60+
get_aliases()
61+
# frozenset({'alpha', 'rc', ..., 'micro', 'breaking'})
62+
63+
# get rules for some scheme:
64+
get_rules(scheme='calver')
65+
# frozenset({'major', 'patch', 'init'})
66+
67+
# get aliases for specific rules:
68+
get_aliases(rules={'major', 'minor'})
69+
# frozenset({'feature', 'breaking'})
70+
71+
```
72+
73+
Bump version:
74+
75+
```python
76+
from dephell_versioning import bump_version
77+
78+
bump_version(version='1.2.3', rule='minor', scheme='semver')
79+
# '1.3.0'
80+
81+
# pass aliase instead of rule:
82+
bump_version(version='1.2.3', rule='feature', scheme='semver')
83+
# '1.3.0'
84+
85+
# start rule from `+` to attach local version number:
86+
bump_version(version='1.2.3', rule='+456', scheme='semver')
87+
# '1.2.3+456'
88+
89+
# for `init` version is optional
90+
bump_version(version='', rule='init', scheme='semver')
91+
# '0.1.0'
92+
```
93+
94+
Bump version in a python file:
95+
96+
```python
97+
from dephell_versioning import bump_file
98+
from pathlib import Path
99+
100+
# returns `True` if version was bumped
101+
bump_file(path=Path('dephell_versioning', '__init__.py'), old='0.1.0', new='0.1.1')
102+
# True
103+
104+
# old version is optional: any version will be bumped if old isn't found
105+
bump_file(path=Path('dephell_versioning', '__init__.py'), old='', new='0.1.2')
106+
# True
107+
```
108+
109+
Use [dephell_discover](https://github.com/dephell/dephell_discover) to find out the current version in a python project:
110+
111+
```python
112+
from dephell_discover import Root
113+
from pathlib import Path
114+
root = Root(path=Path(), name='dephell_discover')
115+
116+
# root.metainfo can be None if project isn't found in the given directory
117+
if root.metainfo:
118+
print(root.metainfo.version)
119+
# '0.1.2'
120+
```

appveyor.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# AppVeyor.com is a Continuous Integration service to build and run tests under Windows
2+
# https://www.tjelvarolsson.com/blog/how-to-continuously-test-your-python-code-on-windows-using-appveyor/
3+
# https://github.com/AndrewAnnex/SpiceyPy/blob/master/appveyor.yml
4+
build: false
5+
6+
environment:
7+
matrix:
8+
- PYTHON: "C:\\Python35-x64"
9+
PYTHON_VERSION: "3.5.x"
10+
ARCH: "64"
11+
WINDOWS_SDK_VERSION: "v7.1"
12+
PLAT_NAME: "win-amd64"
13+
PY_TAG: "cp35"
14+
15+
- PYTHON: "C:\\Python36-x64"
16+
PYTHON_VERSION: "3.6.x"
17+
ARCH: "64"
18+
WINDOWS_SDK_VERSION: "v7.1"
19+
PLAT_NAME: "win-amd64"
20+
PY_TAG: "cp36"
21+
22+
- PYTHON: "C:\\Python37-x64"
23+
PYTHON_VERSION: "3.7.x"
24+
ARCH: "64"
25+
WINDOWS_SDK_VERSION: "v7.1"
26+
PLAT_NAME: "win-amd64"
27+
PY_TAG: "cp37"
28+
29+
init:
30+
- "ECHO %PYTHON% %PYTHON_VERSION% %PYTHON_ARCH%"
31+
32+
install:
33+
- "%PYTHON%/Scripts/pip.exe install pytest"
34+
- "%PYTHON%/Scripts/pip.exe install packaging"
35+
36+
test_script:
37+
- "%PYTHON%/Scripts/pytest"

dephell_versioning/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from ._core import bump_file, bump_version, get_aliases, get_rules, get_schemes
2+
from ._schemes import BaseScheme, SCHEMES
3+
4+
5+
__version__ = '0.1.0'
6+
7+
8+
__all__ = [
9+
'BaseScheme',
10+
'bump_file',
11+
'bump_version',
12+
'get_aliases',
13+
'get_rules',
14+
'get_schemes',
15+
'SCHEMES',
16+
]

dephell_versioning/_core.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import re
2+
from pathlib import Path
3+
from typing import FrozenSet, Union, Set, Iterable
4+
5+
from packaging.version import Version, VERSION_PATTERN
6+
7+
from ._schemes import BaseScheme, SCHEMES
8+
9+
10+
REX_VERSION = re.compile(VERSION_PATTERN, re.VERBOSE | re.IGNORECASE)
11+
PREFIXES = {'__version__', 'VERSION', 'version'}
12+
13+
14+
def get_schemes() -> FrozenSet[str]:
15+
return frozenset(SCHEMES)
16+
17+
18+
def get_rules(scheme: str = None) -> FrozenSet[str]:
19+
if scheme is not None:
20+
return SCHEMES[scheme].rules
21+
22+
rules = set() # type: Set[str]
23+
for scheme in SCHEMES.values():
24+
rules.update(scheme.rules)
25+
return frozenset(rules)
26+
27+
28+
def get_aliases(rules: Iterable[str] = None) -> FrozenSet[str]:
29+
if rules is None:
30+
return frozenset(BaseScheme.aliases)
31+
32+
result = set() # type: Set[str]
33+
for alias, rule in BaseScheme.aliases.items():
34+
if rule in rules:
35+
result.add(alias)
36+
return frozenset(result)
37+
38+
39+
def bump_version(version: Union[Version, str], rule: str, scheme: str = 'semver') -> str:
40+
scheme_manager = SCHEMES.get(scheme)
41+
if scheme_manager is None:
42+
raise LookupError('invalid scheme: {}'.format(scheme))
43+
return scheme_manager.bump(version=version, rule=rule)
44+
45+
46+
def bump_file(path: Path, old: str, new: str) -> bool:
47+
file_bumped = False
48+
new_content = []
49+
with path.open('r') as stream:
50+
for line in stream:
51+
prefix, sep, _version = line.partition('=')
52+
if not sep:
53+
new_content.append(line)
54+
continue
55+
if prefix.rstrip() not in PREFIXES:
56+
new_content.append(line)
57+
continue
58+
59+
# replace old version
60+
if old:
61+
new_line = line.replace(old, new, 1)
62+
if new_line != line:
63+
new_content.append(new_line)
64+
file_bumped = True
65+
continue
66+
67+
# replace any version
68+
new_line, count = REX_VERSION.subn(new, line)
69+
if count == 1:
70+
new_content.append(new_line)
71+
file_bumped = True
72+
continue
73+
74+
new_content.append(line)
75+
if file_bumped:
76+
path.write_text(''.join(new_content))
77+
return file_bumped
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from types import MappingProxyType
2+
3+
from ._base import BaseScheme
4+
from ._calver import CalVerScheme
5+
from ._comver import ComVerScheme
6+
from ._pep import PEPScheme
7+
from ._roman import RomanScheme
8+
from ._romver import RomVerScheme
9+
from ._semver import SemVerScheme
10+
from ._serial import SerialScheme
11+
from ._zerover import ZeroVerScheme
12+
13+
14+
__all__ = [
15+
'SCHEMES',
16+
17+
'BaseScheme',
18+
'CalVerScheme',
19+
'ComVerScheme',
20+
'PEPScheme',
21+
'RomanScheme',
22+
'RomVerScheme',
23+
'SemVerScheme',
24+
'SerialScheme',
25+
'ZeroVerScheme',
26+
]
27+
28+
29+
SCHEMES = MappingProxyType(dict(
30+
calver=CalVerScheme(),
31+
comver=ComVerScheme(),
32+
pep=PEPScheme(),
33+
roman=RomanScheme(),
34+
romver=RomVerScheme(),
35+
semver=SemVerScheme(),
36+
serial=SerialScheme(),
37+
zerover=ZeroVerScheme(),
38+
))

0 commit comments

Comments
 (0)