Skip to content

Commit

Permalink
Fix incorrect parsing of versions from dependencies if e.g. extras ar…
Browse files Browse the repository at this point in the history
…e stated to be installed #48 #42
  • Loading branch information
proycon committed Nov 27, 2023
1 parent c4d5afb commit a7dd786
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion codemeta/parsers/python.py
Expand Up @@ -2,6 +2,7 @@
import os
import importlib
import re
import ast
from typing import Union, IO

if sys.version_info.minor < 8:
Expand Down Expand Up @@ -31,6 +32,11 @@ def splitdependencies(s: str):
"""Split a string of multiple (python) dependencies"""
begin = 0
depth = 0
if s and s.find("{") != -1 and s.strip()[-1] == "}":
# dependency is a dict-like structures usually from pyproject.toml {'version':.. , 'extras':... }, it does not contain multiple, return as is
yield s
return
#normal behaviour; PEP-style dependency parsing
for i, c in enumerate(s):
if c == "(":
depth += 1
Expand All @@ -53,6 +59,7 @@ def parsedependency(s: str):
s.find("~") if s.find("~") != -1 else 999999,
s.find("=") if s.find("=") != -1 else 999999,
s.find("^") if s.find("^") != -1 else 999999,
s.find("{") if s.find("{") != -1 else 999999,
)
if end != 999999:
identifier = s[:end]
Expand All @@ -66,7 +73,20 @@ def parsedependency(s: str):
break
if versionbegin != -1:
operator = s[end:versionbegin].strip()
if operator in ("=", "=="):
if s[versionbegin] == "{":
version = s[versionbegin:].strip()
if version and version[0] == "{" and version[-1] == "}":
try:
data = ast.literal_eval(version)
if 'version' in data:
return identifier, data['version']
else:
print("Unable to parse dependency version (key not found):", version,file=sys.stderr)
version = ""
except:
print("Unable to parse dependency version: ", version,file=sys.stderr)
version = ""
elif operator in ("=", "=="):
version = s[versionbegin:].strip()
else:
version = operator + " " + s[versionbegin:].strip()
Expand Down

0 comments on commit a7dd786

Please sign in to comment.