From 9c1085e74d0e53eee95aa43411d748b2e3b976f0 Mon Sep 17 00:00:00 2001 From: ljf Date: Tue, 28 Jan 2025 00:26:09 +0100 Subject: [PATCH 1/2] [enh] Add debugger feature for yunohost cli --- README.md | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ ynh-dev | 11 ++++++++ 2 files changed, 88 insertions(+) diff --git a/README.md b/README.md index e116671..7b4e673 100644 --- a/README.md +++ b/README.md @@ -283,6 +283,83 @@ Any `yunohost` command will run from the code of the git clone. The `use-git` action can be used for any package among `yunohost`, `yunohost-admin`, `moulinette` and `ssowat` with similar consequences. +### 4. Run with a graphical debugger + +You can pilote your code with a graphical debugger like the one inside vscodium or with vimspector. + +You have to run your `yunohost` command prefixed by `./ynh-dev debug `, for example: +``` +./ynh-dev debug yunohost user list +``` +The command will be suspended until a graphical debugger attach this process through the port 5678. + +#### With vim +1. Setup vimspector in your .vimrc, for example with Vundle: +``` +Bundle 'puremourning/vimspector' + +let g:vimspector_enable_mappings = 'HUMAN' +"packadd! vimspector +let g:vimspector_install_gadgets = [ 'debugpy'] +``` +2. Run vim, and install vimspector and debugpy gadget +``` +:PluginInstall +:VimspectorInstall +``` +3. Configure a `.vimspector.json` at the root of your project: +``` +{ + "configurations": { + "run": { + "adapter": "multi-session", + "filetypes": [ "python" ], // optional + "configuration": { + "request": "attach", + "pathMappings": [ + {"localRoot": "~/PATH_TO_YOUR_CODE/ynh-dev/yunohost/src/", "remoteRoot": "/usr/lib/python3/dist-packages/yunohost/"} + ], + "justMyCode": false + } + } + } +} +``` +4. Reopen vim, set some breakpoints with F9 and start with F5 + +#### With vscodium +Note: the workspace needs to be a trusted workspace, if you don't trust a big repo like yunohost, you can still copy src python directory into a trusted workspace... + +1. Install python extension in vscodium +2. Setup a launch.json at the root of your workspace +``` +{ + // Utilisez IntelliSense pour en savoir plus sur les attributs possibles. + // Pointez pour afficher la description des attributs existants. + // Pour plus d'informations, visitez : https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Python Debugger: Remote Attach", + "type": "debugpy", + "request": "attach", + "connect": { + "host": "IP_OF_YOUR_INCUS", + "port": 5678 + }, + "pathMappings": [ + { + "localRoot": "${workspaceFolder}/src/", + "remoteRoot": "/usr/lib/python3/dist-packages/yunohost/" + } + ], + "justMyCode": false + } + ] +} +``` +3. Set some breakpoint and click on `Execute and debug` or F5 + ## Further Resources - [yunohost.org/dev](https://yunohost.org/dev) diff --git a/ynh-dev b/ynh-dev index 85da4c8..d472f83 100755 --- a/ynh-dev +++ b/ynh-dev @@ -24,6 +24,8 @@ function show_usage() { test [PKG] Deploy, update and run tests for some packages Tests for single modules and functions can ran with e.g. ./ynh-dev test yunohost/appurl:urlavailable + debug YNH_COMMAND Allow to attach a graphical debugger like vscodium or vimspector on 5678 port + e.g. ./ynh-dev debug yunohost user list dev Watch python files and restart yunohost-api and yunohost-portal-api when changes occur catalog build Rebuild the custom catalog @@ -56,6 +58,7 @@ function main() dev|--dev) dev "${ARGUMENTS[@]}" ;; lint|--lint) run_linters "${ARGUMENTS[@]}" ;; test|--test) run_tests "${ARGUMENTS[@]}" ;; + debug|--debug) run_debug "${ARGUMENTS[@]}" ;; catalog|--catalog) catalog "${ARGUMENTS[@]}" ;; @@ -574,6 +577,14 @@ function run_tests() esac done } +function run_debug() { + if ! dpkg -l |grep python3-debugpy > /dev/null + then + apt install -y python3-debugpy + fi + yunohost firewall allow Both 5678 -4 > /dev/null 2> /dev/null + python3 -Xfrozen_modules=off -m debugpy --listen 0.0.0.0:5678 --wait-for-client /usr/bin/$@ +} function catalog() { From 2c2d5cf607cd810a1282916fd53b88df9ee0402a Mon Sep 17 00:00:00 2001 From: ljf Date: Tue, 28 Jan 2025 09:14:58 +0100 Subject: [PATCH 2/2] [fix] bash linter --- ynh-dev | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ynh-dev b/ynh-dev index d472f83..5bd4b02 100755 --- a/ynh-dev +++ b/ynh-dev @@ -583,7 +583,7 @@ function run_debug() { apt install -y python3-debugpy fi yunohost firewall allow Both 5678 -4 > /dev/null 2> /dev/null - python3 -Xfrozen_modules=off -m debugpy --listen 0.0.0.0:5678 --wait-for-client /usr/bin/$@ + python3 -Xfrozen_modules=off -m debugpy --listen 0.0.0.0:5678 --wait-for-client "/usr/bin/$@" } function catalog()