Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--version points to git hash #756

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,29 @@ check_platform() {
return 0
}

setup_version_file() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would not work in the pypi install, rpm install, or any install that does not use the install.sh.

Copy link
Member

@engelmi engelmi Feb 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we simply use a setup.py.in with a placeholder for the version, i.e. version=@VERSION@, which we can replace at build-time? This could then be used by all means to install ramalama by simply adding this prepare step. Additionally, the version would be hard-coded and uses the git hash only for non-release builds. We use that approach for example in BlueChi
WDYT? @dougsland @rhatdan

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@engelmi I like the idea but maybe we will close this one. WDYT @rhatdan @engelmi ?

local config_dir="$HOME/.config/ramalama"
local version_file="$config_dir/version"

# Ensure ~/.config/ramalama/ exists
mkdir -p "$config_dir"

version="0.0.0"
# Try to get version from Git
if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
version=$(git describe --tags --long --always)
# Format version properly (strip 'g' from commit hash)
if [[ "$version" =~ ([0-9]+\.[0-9]+\.[0-9]+)-([0-9]+)-g([a-f0-9]+) ]]; then
version="${BASH_REMATCH[1]}.dev${BASH_REMATCH[2]}+${BASH_REMATCH[3]}"
fi
fi

# Save version to file
echo "$version" > "$version_file"

echo "Saved version: $version in $version_file"
}

setup_ramalama() {
local binfile="ramalama"
local from_file="${binfile}"
Expand Down Expand Up @@ -181,7 +204,7 @@ main() {
trap cleanup EXIT

setup_ramalama "$bindir"
setup_version_file
}

main "$@"

25 changes: 14 additions & 11 deletions ramalama/version.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import importlib.metadata

"""Version of RamaLamaPy."""
import os

CONFIG_DIR = os.path.expanduser("~/.config/ramalama")
VERSION_FILE = os.path.join(CONFIG_DIR, "version")

def version():
try:
return importlib.metadata.version("ramalama")
except importlib.metadata.PackageNotFoundError:
return "0"

return "0"
"""Reads the version from ~/.config/ramalama/version."""
if os.path.exists(VERSION_FILE):
try:
with open(VERSION_FILE, "r") as f:
return f.read().strip()
except (IOError, OSError) as e:
return f"Error reading version file: {VERSION_FILE}. Ensure you have the correct permissions. Details: {e}"

return f"cannot be detected, see if exists {VERSION_FILE}. Please run sudo ./install.sh"

def print_version(args):
print("ramalama version %s" % version())
def print_version(args=None):
"""Prints the current version of the package."""
print(f"ramalama version {version()}")
Loading