Affichage des articles dont le libellé est archaeology. Afficher tous les articles
Affichage des articles dont le libellé est archaeology. Afficher tous les articles

dimanche 21 juin 2015

python binding for libgtop2

Where is it ?! It's my code and I can't find it anymore. I'm not even sure about where is the precious 'gtop.c' file within gnome. I've just found it in the archives: gtop.c and I have immediately clone this and gdesklets.

I have also written a silly email to the debian maintainers to understand what is going on because the gtop.c is still shipped within source package gnome-python-desktop but it is not built. And the old gdesklets package ships its own file.

The gdesklets version will do for now. Here's how to grab it and use

# Get it
$ aptitude download gdesklets
Get :  1 http://ftp.fr.debian.org/debian/ sid/main gdesklets amd64 0.36.1-7 [2 699 kB]
 2 699 kB downloaded en 1s (1 868 kB/s)


# Extract it
$ ar x gdesklets_0.36.1-7_amd64.deb
$ tar xJf data.tar.xz ./usr/lib/gdesklets/libdesklets/system/gtop.so
$ mv ./usr/lib/gdesklets/libdesklets/system/gtop.so .


# Check it
$ ldd -r gtop.so | grep libgtop
        libgtop-2.0.so.7 => /usr/lib/libgtop-2.0.so.7 (0x00007feda0cb4000)


# If you don't have this
# apt-get install libgtop2-7

# Here's a simple example
Python 2.7.10 (default, Jun  1 2015, 16:21:46)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import gtop
>>> gtop.loadavg()
Struct { .last_pid = 9065, .loadavg1 = 0.14, .loadavg15 = 0.14, .loadavg5 = 0.1, .nr_running = 2, .nr_tasks = 398 }

There's also the new binding based on the Gnome-Instrospection framework but I'm afraid it's currently broken. It's autogenerated, and I have started to fix it. It's shipped with package gir1.2-gtop-2.0. Here's a sample code, it's very closed to the C usage.

#!/usr/bin/env python3

from gi.repository import GTop

GTop.glibtop_init()

buf = GTop.glibtop_loadavg()
GTop.glibtop_get_loadavg(buf)
print(dict((k, getattr(buf, k)) for k in dir(buf) if not k.startswith('__')))

And it runs (probably because of my local unsent patches):
$ python3 loadavg.py
server=0x7fe25d788c00 features=0 flags=0 server_flags=0
server=0x7fe25d788c00 features=20 flags=0 server_flags=b0000
{'flags': 15, 'last_pid': 9590, 'nr_tasks': 401, 'nr_running': 2, 'loadavg': [0.02, 0.04, 0.07]}