A robust version management utility for Python projects
Project description
version_get
A robust and production-ready Python package for managing version numbers in your projects.
Features
- 🔍 Auto-detection: Automatically finds version files in your project
- 📝 Multiple formats: Supports various version file naming conventions
- 🎯 Flexible parsing: Handles different version string formats with regex
- 🔢 Version manipulation: Increment, decrement, and set version numbers
- 🏷️ Suffix support: Manage alpha, beta, dev, and custom suffixes
- 💻 CLI & API: Use as command-line tool or import as Python class
- 🔧 Production-ready: Robust error handling and type hints
- 📦 Zero dependencies: No external dependencies required
Installation
pip install version_get
Or install from source:
git clone https://github.com/cumulus13/version_get.git
cd version_get
pip install -e .
Quick Start
As a Python Module
from version_get import VersionGet
# Initialize (auto-detects version file)
vg = VersionGet() # or vg = VersionGet('/projects/myap')
# Get current version
print(vg.get()) # Output: 1.0.0
# Increment version
vg.increment_major() # 1.0.0 -> 2.0.0
vg.increment_minor() # 2.0.0 -> 2.1.0
vg.increment_patch() # 2.1.0 -> 2.1.1
# Set specific version
vg.set_version("3.0.0")
# Set suffixes
vg.set_alpha() # 3.0.alpha
vg.set_beta() # 3.0.beta
vg.set_dev() # 3.0.dev
# Custom suffix
vg.set_suffix("rc1") # 3.0.rc1
# Default path is parent directory but
# if version file is not exist then return to current directory
# Specify path
vg = VersionGet(path="/path/to/project")
# Create version file if missing
vg = VersionGet(create_if_missing=True)
Auto-reload
vg = VersionGet()
print(vg.get()) # 1.3.4
# Edit manual file → 2.0.0
vg.increment_patch() # Auto reload → 2.0.0 → 2.0.1 ✓
print(vg.get()) # 2.0.1
vg = VersionGet()
print(vg.get()) # 1.3.4
# Edit manual file → 2.0.0
print(vg.get(True)) # 2.0.0
Manual-reload
vg = VersionGet()
print(vg.get()) # 1.3.4
# Edit manual file → 2.0.0
vg.reload() # Reload dari file
print(vg.get()) # 2.0.0 ✓
Get from file directly
one-time check
vg = VersionGet()
print(vg.get()) # 1.3.4 (from memory)
# Edit manual file → 2.0.0
print(vg.get(from_file=True)) # 2.0.0 ✓ (langsung dari file)
print(vg.get()) # 2.0.0 (updated memory)
Disable Auto-reload (Optional)
vg.increment_patch(auto_reload=False) # Just use memory``
### As a Command-Line Tool
```bash
# Show current version
version_get
vget # Short alias
# Increment versions
version_get --increment-major # x.0.0
version_get --increment-minor # x.y.0
version_get --increment-patch # x.y.z
version_get --auto-add # Same as --increment-patch
# Decrement versions
version_get --decrement-major
version_get --decrement-minor
version_get --decrement-patch
# Set specific version
version_get --set 2.5.0
# Set suffixes
version_get --set-alpha
version_get --set-beta
version_get --set-dev
version_get --set-suffix rc1
# Specify project path
version_get --path /path/to/project
# Create version file if missing
version_get --create
# Quiet mode (only output version)
version_get --quiet
version_get -q
# Verbose mode
version_get --verbose
Supported Version File Names
The package automatically searches for these files (in priority order):
__version__.pyversion.py__VERSION__.pyVERSION.py__VER__.py__ver__.pyversionVERSION__version____VERSION____VER____ver__VERver
Version File Format
The package supports various version string formats:
# Standard formats
version = "1.0.0"
__version__ = "1.0.0"
VERSION = "1.0.0"
# With or without spaces
version="1.0.0"
version = "1.0.0"
# With suffixes
version = "1.0.alpha"
version = "1.0.beta"
version = "1.0.dev"
version = "1.0.rc1"
# Plain version files (no assignment)
1.0.0
Use in setup.py
from setuptools import setup
from version_get import VersionGet
# Get version from version file
vg = VersionGet()
version = vg.get()
setup(
name='myproject',
version=version,
# ... other setup parameters
)
Or using the recommended approach:
import os
import re
def get_version():
"""Get version from __version__.py"""
version_file = os.path.join(os.path.dirname(__file__), '__version__.py')
with open(version_file, 'r') as f:
content = f.read()
match = re.search(r'__version__\s*=\s*["\']([^"\']+)["\']', content)
if match:
return match.group(1)
return "1.0.0"
setup(
name='myproject',
version=get_version(),
# ... other setup parameters
)
Advanced Usage
Working with Different Paths
from version_get import VersionGet
# Auto-detect from current or parent directory
vg = VersionGet()
# Specify directory
vg = VersionGet(path="/path/to/project")
# Specify exact file
vg = VersionGet(path="/path/to/project/__version__.py")
Path Resolution
The package uses intelligent path resolution:
- If path is provided, use that path
- Check current working directory
- Check caller's directory (using
inspect) - Check parent of current directory
- Default to current directory
Version Manipulation
vg = VersionGet()
# Current: 1.2.3
vg.increment_major() # -> 2.0.0
vg.increment_minor() # -> 2.1.0
vg.increment_patch() # -> 2.1.1
# Decrement (minimum 0)
vg.decrement_patch() # -> 2.1.0
vg.decrement_minor() # -> 2.0.0
vg.decrement_major() # -> 1.0.0
# Set custom version
vg.set_version("3.5.7")
# Work with suffixes
vg.set_suffix("pre-release") # -> 3.5.pre-release
String Representation
vg = VersionGet()
# Get as string
print(str(vg)) # Output: 1.0.0
# Repr shows more info
print(repr(vg)) # Output: VersionGet(version='1.0.0', file=/path/to/__version__.py)
API Reference
Class: VersionGet
Constructor
VersionGet(path=None, create_if_missing=False)
Parameters:
path(str, optional): Path to directory or version filecreate_if_missing(bool): Create__version__.pyif not found
Methods
get()→ str: Get current versionincrement_major()→ str: Increment major version (x.0.0)increment_minor()→ str: Increment minor version (x.y.0)increment_patch()→ str: Increment patch version (x.y.z)decrement_major()→ str: Decrement major versiondecrement_minor()→ str: Decrement minor versiondecrement_patch()→ str: Decrement patch versionset_version(version)→ str: Set specific versionset_suffix(suffix)→ str: Set version suffixset_alpha()→ str: Set to alpha versionset_beta()→ str: Set to beta versionset_dev()→ str: Set to dev versionauto_add()→ str: Auto-increment patch (alias for increment_patch)
Examples
Example 1: CI/CD Integration
# bump_version.py
from version_get import VersionGet
vg = VersionGet()
current = vg.get()
print(f"Current version: {current}")
# Bump patch version for releases
new_version = vg.increment_patch()
print(f"New version: {new_version}")
Example 2: Pre-release Management
from version_get import VersionGet
vg = VersionGet()
# Create alpha release
vg.set_alpha()
print(f"Alpha: {vg.get()}") # 1.0.alpha
# Move to beta
vg.set_beta()
print(f"Beta: {vg.get()}") # 1.0.beta
# Final release
vg.increment_patch()
print(f"Release: {vg.get()}") # 1.0.1
Example 3: Automated Versioning
# In your CI/CD pipeline
# For feature branches
version_get --increment-minor --set-dev
# For release candidates
version_get --increment-major --set-suffix rc1
# For production releases
version_get --increment-patch
Error Handling
The package includes robust error handling:
- File not found: Uses default version "1.0.0"
- Parse errors: Falls back to default version
- Write errors: Prints error message and returns False
- Invalid formats: Shows warning but attempts to proceed
Development
Running Tests
# Install dev dependencies
pip install -e .[dev]
# Run tests
pytest
# Run with coverage
pytest --cov=version_get tests/
Code Formatting
# Format code
black version_get/
# Check style
flake8 version_get/
# Type checking
mypy version_get/
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
If you encounter any issues or have questions:
- Check the documentation
- Search existing issues
- Create a new issue
Acknowledgments
- Inspired by semantic versioning best practices
- Built with Python's standard library for zero dependencies
👤 Author
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file version_get-1.0.12.tar.gz.
File metadata
- Download URL: version_get-1.0.12.tar.gz
- Upload date:
- Size: 16.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3a9cd17aefc41be1388570366d40edba7a2b399e599674c8a2fe03f9ba2b34b8
|
|
| MD5 |
d841c0f8a826caf926676a9f936fe2fe
|
|
| BLAKE2b-256 |
c8e9b760d794c06f8cb2f7efda393594b56047ce14e132925e721508910b071f
|
File details
Details for the file version_get-1.0.12-py3-none-any.whl.
File metadata
- Download URL: version_get-1.0.12-py3-none-any.whl
- Upload date:
- Size: 12.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
70edf9cef3dce9e348741f6f0eecd2cef8efdfba607c3647ca1a48dcfb0d811d
|
|
| MD5 |
b61a2199c6b9bc7e2b43d80d639ead3b
|
|
| BLAKE2b-256 |
637fe20206cbddd9ea6fa5c575e9c2001bf5f4e9a9b4160b28a91a0fe786b851
|