Get package version from metadata when package has no __version__ attribute

This commit is contained in:
Davte 2024-08-04 18:49:16 +02:00
parent cc4cbbacbd
commit 02386d69da
Signed by: Davte
GPG Key ID: 8A65AF02E41CA5C9
2 changed files with 17 additions and 4 deletions

View File

@ -11,7 +11,7 @@ __author__ = "Davide Testa"
__email__ = "davide@davte.it" __email__ = "davide@davte.it"
__credits__ = ["Marco Origlia", "Nick Lee @Nickoala"] __credits__ = ["Marco Origlia", "Nick Lee @Nickoala"]
__license__ = "GNU General Public License v3.0" __license__ = "GNU General Public License v3.0"
__version__ = "2.10.4" __version__ = "2.10.5"
__maintainer__ = "Davide Testa" __maintainer__ = "Davide Testa"
__contact__ = "t.me/davte" __contact__ = "t.me/davte"

View File

@ -18,6 +18,7 @@ import re
import types import types
from collections import OrderedDict from collections import OrderedDict
from importlib.metadata import version as get_package_version_from_metadata
from typing import Union, List, Tuple from typing import Union, List, Tuple
# Third party modules # Third party modules
@ -44,6 +45,13 @@ variable_regex = re.compile(r"(?P<name>[a-zA-Z]\w*)\s*=\s*"
r"\"[^\"]*\")") r"\"[^\"]*\")")
def get_package_version(package: types.ModuleType):
"""Get version of given package."""
if hasattr(package, '__version__'):
return package.__version__
return get_package_version_from_metadata(package.__name__)
async def _forward_to(update, async def _forward_to(update,
bot: Bot, bot: Bot,
sender, sender,
@ -865,7 +873,12 @@ async def get_new_versions(bot: Bot,
"skipping...") "skipping...")
continue continue
new_version = web_page['info']['version'] new_version = web_page['info']['version']
current_version = package.__version__ try:
current_version = get_package_version(package)
except TypeError:
current_version = "NA"
logging.error("Could not get current version of "
"package %s", package.__name__)
notification_record = bot.db['updates_notifications'].find_one( notification_record = bot.db['updates_notifications'].find_one(
package=package.__name__, package=package.__name__,
order_by=['-id'], order_by=['-id'],
@ -895,7 +908,7 @@ async def version_command(bot: Bot, update: dict,
text += f'<b>Python: </b> <code>{platform.python_version()}</code>\n' text += f'<b>Python: </b> <code>{platform.python_version()}</code>\n'
text += '\n'.join( text += '\n'.join(
f"<b>{package.__name__}</b>: " f"<b>{package.__name__}</b>: "
f"<code>{package.__version__}</code>" f"<code>{get_package_version(package)}</code>"
for package in bot.packages for package in bot.packages
) )
temporary_message = await bot.send_message( temporary_message = await bot.send_message(
@ -939,7 +952,7 @@ async def notify_new_version(bot: Bot):
order_by=['-id'] order_by=['-id']
) )
current_versions = { current_versions = {
f"{package.__name__}_version": package.__version__ f"{package.__name__}_version": get_package_version(package)
for package in bot.packages for package in bot.packages
} }
current_versions['last_commit'] = last_commit current_versions['last_commit'] = last_commit