-
Notifications
You must be signed in to change notification settings - Fork 993
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
[question] Recommended way of calling conan from python program #16209
Comments
Thanks for your question. It depends on what you need. For most cases, the recommended way would be the Conan PythonAPI that has been made public and documented in Conan 2: https://docs.conan.io/2/reference/extensions/python_api.html. The documentation is still WIP and it might still suffer some changes as a result of the experimental status, but we try not to break if possible. Also, for most cases, it is further recommended to not use the PythonAPI "bare", but use the new custom commands extensions, that allows you to create and manage your own commands built on top of the PythonAPI and other commands. Check: https://docs.conan.io/2/reference/extensions/custom_commands.html. This is a very powerful approach, as it also allows to publish, share and update your custom commands with the Regarding the usage of other commands, the Conan 2 PythonAPI also provides the "CommandAPI" in https://docs.conan.io/2/reference/extensions/python_api/CommandAPI.html, which is specifically intended to call other commands, and interface at the Python level, which will be way more convenient than a @conan_command(group="custom commands", formatters={"json": format_graph_json})
def mycommand(conan_api, parser, *args, **kwargs):
\""" mycommand help \"""
result = conan_api.command.run(["create", ".", "--version=1.0.0"])
return result This is a very new addition, and docs are not there yet, but feel free to try it and ask questions as necessary. |
Hi @memsharded Thanks for your answer.
and I get this error: |
I see using the CLI works but I'm not sure how to do this with the CommandAPI?
Looks a bit strange to me that the CommandAPI is depending on the CLI as I thought the CLI is a layer built on top of the ConanAPI |
The custom commands are already Python code. The idea is that you don't need to have some Python scripts with a custom interface, but you can structure and manage your Python scripts as Conan commands. Instead of launching your python script as
yes, the CommandAPI needs an entry point to the Cli object, because that is the one that contains the defined commands. Something like: conan_api.command.cli = conan_cli
The thing is that the commands are modular and extensible, you can add your own commands. Then, there is no hardcoded definition of commands, and it is the In any case, if you use the custom commands, all of this is automatically managed. So far, users that have gone for the custom commands are very happy about it, it is very recommended, better than just python scripts using the api. |
Thanks for your help! :)
|
Sounds good, thanks for the explanations! As this is not documented yet, let me check it, try it, and I will probably propose a PR to the docs with the necessary steps. |
This is the working implementation atm: from conan.api.conan_api import ConanAPI
from conan.cli.cli import Cli
api = ConanAPI()
cli = Cli(api)
cli._add_commands()
result = api.command.run(["remote", "list"])
print(result) The missing piece was a I will do a PR to clean the interface a little bit. You might use this solution in the meantime, but please take into account that it will break next release. UPDATE: The line |
#16213 will make |
What is your question?
Hi,
I want to write a conan wrapper around the conan CLI for conan v1 and conan v2.
I see two possible solutions:
I'm not sure what the better approach is. Regarding approach 2 I'm not sure if the ConanAPI is still WIP or if it's even meant to be used like this? I haven't had a deeper look into the conan v1 API but regarding conan v2 I saw that for example there is no high level CreateAPI and the create CLI command uses more fine granular API commands from other APIs(ExportAPI, GraphAPI, InstallAPI, ...) for package creation. This would make it easier to go for approach 1 as it seems like the CLI provides a simpler/more high level abstraction than the API.
Is there a general recommendation or programs that try to do something similar?
Thank's in advance for your help
Have you read the CONTRIBUTING guide?
The text was updated successfully, but these errors were encountered: