Skip to content

Commit

Permalink
add version
Browse files Browse the repository at this point in the history
  • Loading branch information
mhmohona committed Aug 21, 2024
1 parent a82b9f1 commit c305e3e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/scribe_data/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from scribe_data.cli.interactive import start_interactive_mode
from scribe_data.cli.list import list_wrapper
from scribe_data.cli.total import get_total_lexemes
from scribe_data.cli.version import get_version_message

LIST_DESCRIPTION = "List languages, data types and combinations of each that Scribe-Data can be used for."
GET_DESCRIPTION = (
Expand All @@ -50,7 +51,11 @@ def main() -> None:

parser._actions[0].help = "Show this help message and exit."
parser.add_argument(
"-v", "--version", help="Show the version of the Scribe-Data CLI."
"-v",
"--version",
action="version",
version=f"{get_version_message()}",
help="Show the local and latest versions of the Scribe-Data CLI.",
)
parser.add_argument("-u", "--upgrade", help="Upgrade the Scribe-Data CLI.")

Expand Down
62 changes: 62 additions & 0 deletions src/scribe_data/cli/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""
Functions for checking current version of the Scribe-Data CLI.
.. raw:: html
<!--
* Copyright (C) 2024 Scribe
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
-->
"""

import pkg_resources
import requests


def get_local_version():
try:
return pkg_resources.get_distribution("scribe-data").version
except pkg_resources.DistributionNotFound:
return "Unknown (Not installed via pip)"


def get_latest_version():
try:
response = requests.get(
"https://api.github.com/repos/scribe-org/Scribe-Data/releases/latest"
)
version = response.json()["name"]
return version
except Exception:
return "Unknown (Unable to fetch version)"


def get_version_message():
local_version = "Scribe-Data v" + get_local_version()
latest_version = get_latest_version()

if (
local_version == "Unknown (Not installed via pip)"
or latest_version == "Unknown (Unable to fetch version)"
):
return f"{local_version}"

if local_version == latest_version:
return f"{local_version}"
else:
update_message = (
f"Scribe-Data v{local_version} (Update available: v{latest_version})\n"
)
update_message += "To update, run: pip scribe-data --upgrade"
return update_message

0 comments on commit c305e3e

Please sign in to comment.