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

[bug] ConanAPI: inspect fails #14305

Closed
tbsuht opened this issue Jul 14, 2023 · 2 comments
Closed

[bug] ConanAPI: inspect fails #14305

tbsuht opened this issue Jul 14, 2023 · 2 comments
Assignees

Comments

@tbsuht
Copy link

tbsuht commented Jul 14, 2023

Environment details

  • Operating System+version: -
  • Compiler+version: -
  • Conan version: 2.0.7
  • Python version: 3.11.2

I'm trying to use the python ConanAPI, which fails with the following error.

Steps to reproduce

from conan.api.conan_api import ConanAPI
from conans.model.recipe_ref import RecipeReference
conan = ConanAPI()
conan.local.inspect(conan.cache.export_path(RecipeReference.loads("zlib/1.2.13")), [], None)

I think the package reference doesn't matter, use one from your local cache.

Logs

Traceback (most recent call last):
  File "/opt/venv/lib/python3.11/site-packages/conans/client/loader.py", line 313, in load_python_file
    loaded = imp_util.module_from_spec(spec)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 570, in module_from_spec
AttributeError: 'NoneType' object has no attribute 'loader'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/venv/lib/python3.11/site-packages/conans/client/loader.py", line 55, in load_basic_module
    module, conanfile = parse_conanfile(conanfile_path)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.11/site-packages/conans/client/loader.py", line 288, in parse_conanfile
    module, filename = load_python_file(conanfile_path)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.11/site-packages/conans/client/loader.py", line 351, in load_python_file
    raise ConanException("Unable to load conanfile in %s\n%s" % (conan_file_path,
conans.errors.ConanException: Unable to load conanfile in /home/user/.conan2/p/zlib15353192ba4c5/e
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 570, in module_from_spec
AttributeError: 'NoneType' object has no attribute 'loader'


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/venv/lib/python3.11/site-packages/conan/api/subapi/local.py", line 109, in inspect
    conanfile = app.loader.load_named(conanfile_path, name=None, version=None,
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.11/site-packages/conans/client/loader.py", line 98, in load_named
    conanfile, _ = self.load_basic_module(conanfile_path, graph_lock, remotes=remotes,
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.11/site-packages/conans/client/loader.py", line 79, in load_basic_module
    raise ConanException("Error loading conanfile at '{}': {}".format(conanfile_path, e))
conans.errors.ConanException: Error loading conanfile at '/home/user/.conan2/p/zlib15353192ba4c5/e': Unable to load conanfile in /home/user/.conan2/p/zlib15353192ba4c5/e
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 570, in module_from_spec
AttributeError: 'NoneType' object has no attribute 'loader'
@AbrilRBS AbrilRBS self-assigned this Jul 14, 2023
@AbrilRBS
Copy link
Member

Hi @tbsuht thanks a lot for your question.
As you can see, the API is currently not extensively documented, and this is on purpose - it allows us to have a bit more leeway to stabilize the API until we are happy with it.

Having said that, checking how the CLI uses these methods is usually a good starting point. In this case, the inspect() method is used here. As you can see, this needs to be passed a path to a conanfile.py, while the export method you called returns a folder.

You would probably need to do something like this (Not tested, might not actually work/the syntax might be a bit off, and please do not use it as public documentation!)

    export_path = conan_api.cache.export_path(RecipeReference.loads(ref))
    path = conan_api.local.get_conanfile_path(export_path, os.getcwd(), py=True)
    conanfile = conan_api.local.inspect(path, remotes=[], lockfile=None)

Let me know if that helps! :)

@tbsuht
Copy link
Author

tbsuht commented Jul 17, 2023

hi @RubenRBS

thanks, that's working! Don't know whether it makes sense to improve the API to be able to also work with a path to a directory, so that calling get_conanfile_path is not necessary (is then done internally). And of course it shouldn't crash with an exception, but tell me that my path was not providing a conanfile. :)

@tbsuht tbsuht closed this as completed Jul 18, 2023
@AbrilRBS AbrilRBS linked a pull request Aug 8, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants