Skip to content

Commit 9c1085e

Browse files
committed
[enh] Add debugger feature for yunohost cli
1 parent fbf4b2d commit 9c1085e

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,83 @@ Any `yunohost` command will run from the code of the git clone.
283283

284284
The `use-git` action can be used for any package among `yunohost`, `yunohost-admin`, `moulinette` and `ssowat` with similar consequences.
285285

286+
### 4. Run with a graphical debugger
287+
288+
You can pilote your code with a graphical debugger like the one inside vscodium or with vimspector.
289+
290+
You have to run your `yunohost` command prefixed by `./ynh-dev debug `, for example:
291+
```
292+
./ynh-dev debug yunohost user list
293+
```
294+
The command will be suspended until a graphical debugger attach this process through the port 5678.
295+
296+
#### With vim
297+
1. Setup vimspector in your .vimrc, for example with Vundle:
298+
```
299+
Bundle 'puremourning/vimspector'
300+
301+
let g:vimspector_enable_mappings = 'HUMAN'
302+
"packadd! vimspector
303+
let g:vimspector_install_gadgets = [ 'debugpy']
304+
```
305+
2. Run vim, and install vimspector and debugpy gadget
306+
```
307+
:PluginInstall
308+
:VimspectorInstall
309+
```
310+
3. Configure a `.vimspector.json` at the root of your project:
311+
```
312+
{
313+
"configurations": {
314+
"run": {
315+
"adapter": "multi-session",
316+
"filetypes": [ "python" ], // optional
317+
"configuration": {
318+
"request": "attach",
319+
"pathMappings": [
320+
{"localRoot": "~/PATH_TO_YOUR_CODE/ynh-dev/yunohost/src/", "remoteRoot": "/usr/lib/python3/dist-packages/yunohost/"}
321+
],
322+
"justMyCode": false
323+
}
324+
}
325+
}
326+
}
327+
```
328+
4. Reopen vim, set some breakpoints with F9 and start with F5
329+
330+
#### With vscodium
331+
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...
332+
333+
1. Install python extension in vscodium
334+
2. Setup a launch.json at the root of your workspace
335+
```
336+
{
337+
// Utilisez IntelliSense pour en savoir plus sur les attributs possibles.
338+
// Pointez pour afficher la description des attributs existants.
339+
// Pour plus d'informations, visitez : https://go.microsoft.com/fwlink/?linkid=830387
340+
"version": "0.2.0",
341+
"configurations": [
342+
{
343+
"name": "Python Debugger: Remote Attach",
344+
"type": "debugpy",
345+
"request": "attach",
346+
"connect": {
347+
"host": "IP_OF_YOUR_INCUS",
348+
"port": 5678
349+
},
350+
"pathMappings": [
351+
{
352+
"localRoot": "${workspaceFolder}/src/",
353+
"remoteRoot": "/usr/lib/python3/dist-packages/yunohost/"
354+
}
355+
],
356+
"justMyCode": false
357+
}
358+
]
359+
}
360+
```
361+
3. Set some breakpoint and click on `Execute and debug` or F5
362+
286363
## Further Resources
287364

288365
- [yunohost.org/dev](https://yunohost.org/dev)

ynh-dev

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ function show_usage() {
2424
test [PKG] Deploy, update and run tests for some packages
2525
Tests for single modules and functions can ran with
2626
e.g. ./ynh-dev test yunohost/appurl:urlavailable
27+
debug YNH_COMMAND Allow to attach a graphical debugger like vscodium or vimspector on 5678 port
28+
e.g. ./ynh-dev debug yunohost user list
2729
dev Watch python files and restart yunohost-api and yunohost-portal-api when changes occur
2830
catalog
2931
build Rebuild the custom catalog
@@ -56,6 +58,7 @@ function main()
5658
dev|--dev) dev "${ARGUMENTS[@]}" ;;
5759
lint|--lint) run_linters "${ARGUMENTS[@]}" ;;
5860
test|--test) run_tests "${ARGUMENTS[@]}" ;;
61+
debug|--debug) run_debug "${ARGUMENTS[@]}" ;;
5962

6063
catalog|--catalog) catalog "${ARGUMENTS[@]}" ;;
6164

@@ -574,6 +577,14 @@ function run_tests()
574577
esac
575578
done
576579
}
580+
function run_debug() {
581+
if ! dpkg -l |grep python3-debugpy > /dev/null
582+
then
583+
apt install -y python3-debugpy
584+
fi
585+
yunohost firewall allow Both 5678 -4 > /dev/null 2> /dev/null
586+
python3 -Xfrozen_modules=off -m debugpy --listen 0.0.0.0:5678 --wait-for-client /usr/bin/$@
587+
}
577588

578589
function catalog()
579590
{

0 commit comments

Comments
 (0)