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

Adapt module #5

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/conan_check_updates/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def is_latest(result: CheckUpdateResult) -> bool:

results = list(filterfalse(is_latest, results))
if not results:
print("No requirements found")
print("No requirements to update found")
return

# output update results
Expand Down
29 changes: 10 additions & 19 deletions src/conan_check_updates/conan.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from itertools import chain
from pathlib import Path
from typing import AsyncIterator, List, Optional, Tuple
import json

from .version import (
Version,
Expand Down Expand Up @@ -117,7 +118,8 @@ def find_conanfile(path: Path) -> Path:
rf"(?P<package>{_REGEX_CONAN_ATTRIBUTE})"
rf"\/(?P<version>{_REGEX_CONAN_VERSION})"
rf"(?:#(?P<revision>{_REGEX_CONAN_REVISION_MD5_SHA1}))?" # optional
rf"(?:@(?P<user>{_REGEX_CONAN_ATTRIBUTE})\/(?P<channel>{_REGEX_CONAN_ATTRIBUTE}))?" # optional
rf"(?:@(?P<user>{_REGEX_CONAN_ATTRIBUTE}))?"
rf"(?:\/(?P<channel>{_REGEX_CONAN_ATTRIBUTE}))?" # optional
)

_PATTERN_CONAN_REFERENCE = re.compile(_REGEX_CONAN_REFERENCE)
Expand Down Expand Up @@ -193,16 +195,15 @@ def inspect_requirements_conanfile_py(conanfile: Path) -> List[ConanReference]:
# ignore empty line or line comments
if not line or line.startswith("#"):
continue
res = re.search(r"self\.(?:tool_)*requires\((.*)\)", line)
res = re.search(r'self\.(?:(tool|python)_)*requires\("(.*)"(?:(\)|,.*))', line)
if res is None:
res = re.search(r'(tool|python)_requires = "(.*)"', line)
if res:
args = res.group(1)
arg = args.partition(",")[0].strip() # get first argument -> reference string
ref = _dequote(arg)
ref = res.group(2)
if len(ref) > 0:
refs.append(ref)
return list(map(ConanReference.parse, refs))


def inspect_requires_conanfile_py(conanfile: Path) -> List[ConanReference]:
"""Get requirements of conanfile.py with `conan inspect`."""
assert conanfile.name == "conanfile.py"
Expand All @@ -212,20 +213,12 @@ def get_command():
args = chain.from_iterable(("-a", attr) for attr in _REQUIRES_ATTRIBUTES)
return ("conan", "inspect", str(conanfile), *args)
if conan_version().major == 2: # noqa: PLR2004
return ("conan", "inspect", str(conanfile))
return ("conan", "inspect", str(conanfile), "--format=json")
raise RuntimeError(f"Conan version {conan_version()!s} not supported")

stdout, _ = _run_capture(*get_command(), timeout=TIMEOUT)

def gen_dict():
for line in stdout.decode().splitlines():
key, _, value = (part.strip() for part in line.partition(":"))
if key and value:
if value.startswith(("(", "[")) and value.endswith((")", "]")):
value = literal_eval(value)
yield key, value

attributes = dict(gen_dict())
attributes = dict(json.loads(stdout.decode()))

def gen_requires():
for key, value in attributes.items():
Expand Down Expand Up @@ -269,9 +262,7 @@ def gen_requires():
def inspect_requires_conanfile(conanfile: Path) -> List[ConanReference]:
"""Get requirements of conanfile.py/conanfile.py"""
if conanfile.name == "conanfile.py":
return inspect_requires_conanfile_py(conanfile) + inspect_requirements_conanfile_py(
conanfile
)
return inspect_requires_conanfile_py(conanfile) + inspect_requirements_conanfile_py(conanfile)
if conanfile.name == "conanfile.txt":
return inspect_requires_conanfile_txt(conanfile)
raise ValueError(f"Invalid conanfile: {conanfile!s}")
Expand Down
14 changes: 13 additions & 1 deletion src/conan_check_updates/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,19 @@ async def check_updates(
progress_callback: Callback for progress updates
"""
refs = inspect_requires_conanfile(conanfile)
if package_filter:

print("\n[Requirements parsed]")
for ref in refs:
pkg_name = ref.package
if ref.version:
pkg_name += f"/{ref.version}"
if ref.user:
pkg_name += f"@{ref.user}"
if ref.channel:
pkg_name += f"/{ref.channel}"
print(pkg_name)

if package_filter and package_filter[-1] == "filter":
refs = [ref for ref in refs if matches_any(ref.package, *package_filter)]

done = 0
Expand Down
Loading