Need the help on hack gDesklets to kill the 'Linuxish' in Python.

Jeremy Messenger mezz7 at cox.net
Sat Sep 13 14:15:36 PDT 2003


On Sat, 13 Sep 2003 16:11:06 +0200, Alexander Leidinger 
<Alexander at Leidinger.net> wrote:

> On Sat, 13 Sep 2003 02:46:41 -0500
> Jeremy Messenger <mezz7 at cox.net> wrote:
>
>> The new version of gDesklets just released yesterday and it has the new
>> directory called libgdesklets. All of them will not work on FreeBSD,
>> because it is too 'Linuxish' by read the /proc/* stuff. They are written
>> in Python and I don't know Python that much and much, but I am trying to
>> learn it. The gDesklets developer said that they will accept the
>> patch(es)[1] as long it will work on Linux too, so you can send me the
>> patch(es) too and I will add it in the gDesklets port until one of your
>> patch(es) make it in gDesklets. It doesn't matter if you only can get it
>> works on FreeBSD but not on Linux, then just send it to me.
>
>> [1] Send the patch(es) to gDesklets at http://bugzilla.gnome.org ..
>
> Patches attached, because of lack of a Display which uses them I haven't
> tested the patches... beware, it may eat your pets because there are
> maybe some typos.

Ok, I have tested them and they don't patch correct, so here's attaches of 
cleaned patches.. I cleaned very few mistyped too. :-) I have tried to run 
it with the SysInfo and I keep get this..

==============================
Traceback (most recent call last):
   File "/usr/X11R6/share/gnome/gdesklets/factory/SensorFactory.py", line 
55, in create_sensor
     module = __import__(name)
   File "./SysInfo/__init__.py", line 5, in ?
     import libdesklets as lib
   File "/usr/X11R6/share/gnome/gdesklets/libdesklets/__init__.py", line 
56, in ?    cpu = CPU()
   File "/usr/X11R6/share/gnome/gdesklets/libdesklets/CPU.py", line 25, in 
__init__
     os = lib.sys.get_os()
AttributeError: 'module' object has no attribute 'sys'
'module' object has no attribute 'sys'
==============================

I tried to add the 'import sys' and it still doesn't work.. I am not sure 
what to do with the 'lib.sys.get_os()', which I normal use 'sys.platform' 
for OS stuff.

Cheers,
Mezz

> The new version of gdesklets also doesn't update the weather and pi's
> network desklet...
>
> Bye,
> Alexander.


-- 
bsdforums.org 's moderator, mezz.
-------------- next part --------------
--- libdesklets/CPU.py.orig	Sat Sep 13 14:23:43 2003
+++ libdesklets/CPU.py	Sat Sep 13 14:41:25 2003
@@ -1,5 +1,5 @@
 import polling
-
+import libdesklets as lib
 
 #
 # TODO: support SMP
@@ -21,8 +21,17 @@
 
         self.get_load = polling.wrap(self.__poll_load, 0.2)
 
+        os = lib.sys.get_os()
+
         try:
-            fd = open("/proc/cpuinfo", "r")
+            if os == "FreeBSD":
+                print "FreeBSD"
+                fd = os.popen("grep -3 CPU /var/run/dmesg.boot | head -7 | tail -4", "r")
+            elif os == "Linux":
+                fd = open("/proc/cpuinfo", "r")
+            else:
+                print "Unknown OS, strange things may happen."
+                return
         except IOError, e:
             import traceback; traceback.print_exc()
             print e
@@ -40,15 +49,24 @@
 
     def __poll_cpu(self):
 
-        import libdesklets as lib
+        import re
         arch = lib.sys.get_arch()
+        os = lib.sys.get_os()
         if (arch in ["i386", "i486", "i586", "i686"]):
-            fields = self.__lines[4].split()
-            model_name = " ".join(fields[3:])
-            fields = self.__lines[6].split()
-            cpu_mhz = fields[3]
-            fields = self.__lines[7].split()
-            cpu_cache = " ".join(fields[3:5])
+            if ("FreeBSD" == os):
+                m = re.search('^CPU: (.*) \(([0-9]+.*)-MHz [0-9]+-class CPU\)', self.__lines[0])
+                model_name = " ".join(m.group(0)) # or sysctl hw.model
+                cpu_mhz = m.group(1)              # or sysctl hw.clockrate
+                cpu_cache = " "                   # not available by default
+            elif ("Linux" == os):
+                fields = self.__lines[4].split()
+                model_name = " ".join(fields[3:])
+                fields = self.__lines[6].split()
+                cpu_mhz = fields[3]
+                fields = self.__lines[7].split()
+                cpu_cache = " ".join(fields[3:5])
+            else:
+                pass
 
         elif (arch == "ppc"):
             fields = self.__lines[0].split()
@@ -68,17 +86,34 @@
 
     def __poll_load(self):
 
-        fd = open("/proc/stat", "r")
+        os = lib.sys.get_os()
+
+        if ("FreeBSD" == os):
+            fd = os.popen("iostat -n 0", "r")
+            line = 2
+        elif ("Linux" == os):
+            fd = open("/proc/stat", "r")
+            line = 0
+        else:
+            return
         data = fd.read()
         fd.close()
 
         data = data.splitlines()
-        fields = data[0].split()
+        fields = data[line].split()
 
-        u = float(fields[1])
-        s = float(fields[2])
-        n = float(fields[3])
-        i = float(fields[4])
+        if ("FreeBSD" == os):
+            u = float(fields[2]) + float(fields[3])
+            s = float(fields[4])
+            n = float(fields[5])
+            i = float(fields[6])
+        elif ("Linux" == os):
+            u = float(fields[1])
+            s = float(fields[2])
+            n = float(fields[3])
+            i = float(fields[4])
+        else:
+            pass
 
         total = ((u - self.__uT) + (s - self.__sT) + (n - self.__nT) +
                 (i - self.__iT))
-------------- next part --------------
--- libdesklets/Disk.py.orig	Sat Sep 13 14:44:30 2003
+++ libdesklets/Disk.py	Sat Sep 13 14:51:45 2003
@@ -3,7 +3,7 @@
 import time
 import os
 import statvfs
-
+import libdesklets as lib
 
 class Disk:
 
@@ -19,21 +19,33 @@
 
     def __poll_partitions(self):
 
+        os = lib.sys.get_os()
+
         # we don't have to reread the file if it hasn't changed
-        if (self.__partitions_last_read >= os.path.getmtime("/etc/mtab")):
+        if (os == "Linux" and self.__partitions_last_read >= os.path.getmtime("/etc/mtab")):
             return self.__partitions
         else:
             self.__partitions_last_read = time.time()
 
         # /etc/mtab is more portable than /proc/mount, so we use it
-        fd = open("/etc/mtab", "r")
+        if (os == "Linux"):
+            fd = open("/etc/mtab", "r")
+        else:
+            fd = os.popen("mount", "r")
         lines = fd.readlines()
         fd.close()
 
         partitions = []
         for l in lines:
             parts = l.split()
-            device, mpoint, fstype = parts[:3]
+            if (os == "Linux"):
+                device, mpoint, fstype = parts[:3]
+            elif (os == "FreeBSD"):
+                device = parts[0]
+                mpoint = parts[2]
+                import re
+                m = re.search('\(([a-zA-Z]+)[,)]', parts[3])
+                fstype = m.group(0)
             # FIXME: is this OK? it might be better to check if the device
             #        actually is a file in /dev
             if (fstype in ("ext2", "ext3", "msdos", "vfat", "ntfs", "hpfs"
-------------- next part --------------
--- libdesklets/Memory.py.orig	Sat Sep 13 14:52:40 2003
+++ libdesklets/Memory.py	Sat Sep 13 15:03:06 2003
@@ -17,28 +17,76 @@
 
     def __poll_total_ram(self):
         
-        memtotal = os.stat("/proc/kcore")[stat.ST_SIZE]
+        if ("FreeBSD" == os):
+            fd = os.popen("sysctl hw.physmem")
+            physmem = fd.readlines()
+            fd.close()
+            lines = physmem.splitlines()
+
+            memtotal = lines[0].split()[1]
+        elif ("Linux" == os):
+            memtotal = os.stat("/proc/kcore")[stat.ST_SIZE]
+        else:
+            memtotal = 0
         return memtotal
 
 
         
     def __poll_mem(self, mode):
 
-        fd = open("/proc/meminfo", "r")
+        if ("FreeBSD" == os):
+            fd = os.popen("vmstat -n 0", "r")
+        elif ("Linux" == os):
+            fd = open("/proc/meminfo", "r")
+        else:
+            return (0, 0)
         mem = fd.read()
         fd.close()
         lines = mem.splitlines()
 
         # RAM
         if (mode == 0):
-            nil, total, used , free, share, buffers, cached = lines[1].split()
+            if ("FreeBSD" == os):
+                fd = os.popen("vmstat -n 0", "r")
+            elif ("Linux" == os):
+                fd = open("/proc/meminfo", "r")
+            else:
+                return (0, 0)
+            mem = fd.read()
+            fd.close()
+            lines = mem.splitlines()
+
+            if ("FreeBSD" == os):
+                # this may be larger than total, as this is the active virtual
+                # memory, not the active physical memory
+                used = int(lines[2].split()[3]) * 1024
+            elif ("Linux" == os):
+                nil, total, used , free, share, buffers, cached = lines[1].split()
+                used = int(used) - int(cached)
+
             total = self.__get_total_ram()
-            used = int(used) - int(cached)
 
         # Swap
         elif (mode == 1):
-            nil, total, used, free = lines[2].split()
-            total = int(total)
-            used = int(used)
+            if ("FreeBSD" == os):
+                fd = os.popen("pstat -T", "r")
+            elif ("Linux" == os):
+                fd = open("/proc/meminfo", "r")
+            else:
+                return (0, 0)
+            mem = fd.read()
+            fd.close()
+            lines = mem.splitlines()
+
+            if ("FreeBSD" == os):
+                used, total = lines[1].split()[0].split("/")
+                used = long(used[0:-2]) * 1024 * 1024
+                total = long(total[0:-2]) * 1024 * 1024
+            elif ("Linux" == os):
+                nil, total, used, free = lines[2].split()
+                total = int(total)
+                used = int(used)
+            else:
+                pass
 
         return (total, used)
-------------- next part --------------
--- libdesklets/Network.py.orig	Sat Sep 13 15:06:34 2003
+++ libdesklets/Network.py	Sat Sep 13 15:26:16 2003
@@ -2,6 +2,7 @@
 
 import os
 import time
+import libdesklets as lib
 
 class Network:
 
@@ -25,22 +26,42 @@
 
     def __poll_devices(self):
         
-        fd = open("/proc/net/dev", "r")
+        os = lib.sys.get_os()
+
+        if ("FreeBSD" == os):
+            fd = os.popen("ifconfig -a | grep mtu", "r")
+        elif ("Linux" == os):
+            fd = open("/proc/net/dev", "r")
+        else:
+            return []
         data = fd.readlines()
         fd.close()
         
         devices = []
-        for lines in data[2:]:
-            l = lines.strip()
-            l = l.replace(":", " ")
-            fields = l.split()
-
-            if (fields[0] == "lo"):
-                continue
-            else: # (fields[0].startswith("eth")):
-                device = fields[0]
-                devices.append(device)
-        #end for
+
+        if ("FreeBSD" == os):
+            for lines in data:
+                fields = lines.strip().strip(":")
+
+                if (fields[0] == "lo"):
+                    continue
+                else:
+                    device = fields[0]
+                    devices.append(device)
+        elif ("Linux" == os):
+            for lines in data[2:]:
+                l = lines.strip()
+                l = l.replace(":", " ")
+                fields = l.split()
+
+                if (fields[0] == "lo"):
+                    continue
+                else: # (fields[0].startswith("eth")):
+                    device = fields[0]
+                    devices.append(device)
+            #end for
+        else:
+            pass
 
         return devices
 
@@ -48,27 +69,48 @@
 
     def __poll_ipaddr(self, dev):
 
-        fd = open("/proc/net/rt_cache", "r")
+        os = lib.sys.get_os()
+
+        if ("FreeBSD" == os):
+            fd = os.popen("ifconfig " + device, "r")
+        elif ("Linux" == os):
+            fd = open("/proc/net/rt_cache", "r")
+        else:
+            return ("xxx.xxx.xxx.xxx")
+
         data = fd.readlines()
         fd.close()
-        for l in data:
-            l = l.strip()
-            if (l.startswith(dev)):
+
+        if ("FreeBSD" == os):
+            for l in data:
+                l = l.strip()
                 fields = l.split()
-                addr = fields[7]
-                addr4 = int(addr[0:2], 16)
-                addr3 = int(addr[2:4], 16)
-                addr2 = int(addr[4:6], 16)
-                addr1 = int(addr[6:8], 16)
-                return (str(addr1) + "." + str(addr2) + "."
+
+                if fields[0] == "inet":
+                    return fields[1]
+        elif ("Linux" == os):
+            for l in data:
+                l = l.strip()
+                if (l.startswith(dev)):
+                    fields = l.split()
+                    addr = fields[7]
+                    addr4 = int(addr[0:2], 16)
+                    addr3 = int(addr[2:4], 16)
+                    addr2 = int(addr[4:6], 16)
+                    addr1 = int(addr[6:8], 16)
+                    return (str(addr1) + "." + str(addr2) + "."
                         + str(addr3) + "." + str(addr4))
-            
+        else:
+            pass
+
         return ("xxx.xxx.xxx.xxx")
 
 
 
     def __poll_in_out(self, dev):
 
+        os = lib.sys.get_os()
+
         t = time.time()
         interval = t - self.__time
         self.__time = t
@@ -79,23 +121,38 @@
         speed_in = 0
         speed_out = 0
 
-        fd = open("/proc/net/dev", "r")
+        if ("FreeBSD" == os):
+            fd = os.popen("netstat -b -I " + dev + " | grep Link", "r")
+        elif ("Linux" == os):
+            fd = open("/proc/net/dev", "r")
+        else:
+            return (bytes_in, bytes_out, pack_in, pack_out, speed_in, speed_out)
         data = fd.read()
         fd.close()
         lines = data.splitlines()
 
         # look for the device
         found = 0
-        for l in lines:
-            l.strip()
-            l = l.replace(":", " ")
-            fields = l.split()
-            if (fields[0] == dev):
-                bytes_in, pack_in, bytes_out, pack_out = \
-                int(fields[1]), int(fields[2]), int(fields[9]), int(fields[10])
+        if ("FreeBSD" == os):
+            for l in lines:
                 found = 1
+                fields = l.strip().split()
+                bytes_in, pack_in, bytes_out, pack_out = \
+                    int(fields[6]), int(fields[4]), int(fields[9]), int(fields[7])
                 break
-        #end for
+        elif ("Linux" == os):
+            for l in lines:
+                l.strip()
+                l = l.replace(":", " ")
+                fields = l.split()
+                if (fields[0] == dev):
+                    bytes_in, pack_in, bytes_out, pack_out = \
+                    int(fields[1]), int(fields[2]), int(fields[9]), int(fields[10])
+                    found = 1
+                    break
+            #end for
+        else:
+            pass
 
         # warn if we didn't find the device
         if (not found): print ("WARNING:: Device %(dev)s not found!") % vars()
-------------- next part --------------
--- libdesklets/Sys.py.orig	Sat Sep 13 15:27:22 2003
+++ libdesklets/Sys.py	Sat Sep 13 15:37:16 2003
@@ -2,7 +2,7 @@
 
 import commands
 import time
-
+import libdesklets as lib
 
 class Sys:
 
@@ -31,17 +31,29 @@
 
     def __poll_os(self):
 
-        os = commands.getoutput("uname -o")
+        os = commands.getoutput("uname -s")
         return os
 
 
     def __poll_uptime(self, mode):
 
-        fd = open("/proc/uptime", "r")
-        data = fd.readlines()
-        fd.close()
+        os = lib.sys.get_os()
+
+        if ("FreeBSD" == os):
+            bt = commands.getoutput("sysctl kern.boottime")
+            boottime = int(bt.strip().split()[4])
+            uptime = int(time.time() - float(boottime))
+            idletime = 0
+        elif ("Linux" == os):
+            fd = open("/proc/uptime", "r")
+            data = fd.readlines()
+            fd.close()
         
-        uptime, idletime = data[0].split()
+            uptime, idletime = data[0].split()
+            boottime = int(time.time() - float(uptime))
+        else:
+            uptime = 0
+            idletime = 0
 
         # uptime
         if (mode == 0):
@@ -51,16 +63,32 @@
             return int(float(idletime))
         # sys start
         elif (mode == 2):
-            now = time.time()
-            return int(now - float(uptime))
+            return boottime
 
 
     def __poll_load_avg(self, mode):
 
-        fd = open("/proc/loadavg", "r")
+        import re
+        os = lib.sys.get_os()
+
+        if ("FreeBSD" == os):
+            fd = os.popen("uptime")
+        elif ("Linux" == os):
+            fd = open("/proc/loadavg", "r")
+        else:
+            return float(0.0)
         data = fd.readlines()
         fd.close()
-        load1, load5, load15, t, d = data[0].split()
+
+        if ("FreeBSD" == os):
+            m = re.search('load averages: ([0-9]\.[0-9]+), ([0-9]\.[0-9]+), ([0-9]\.[0-9]+)', data)
+            load1  = m.group(0)
+            load5  = m.group(1)
+            load15 = m.group(2)
+        elif ("Linux" == os):
+            load1, load5, load15, t, d = data[0].split()
+        else:
+            pass
 
         # avg over 1 minute
         if (mode == 0):
@@ -76,11 +104,25 @@
 
     def __poll_tasks(self):
 
-        fd = open("/proc/loadavg", "r")
+        os = lib.sys.get_os()
+
+        if ("FreeBSD" == os):
+            fd = os.popen("vmstat -n 0")
+        elif ("Linux" == os):
+            fd = open("/proc/loadavg", "r")
+        else:
+            return (int(0), int(0))
         data = fd.readlines()
         fd.close()
 
-        parts = data[0].split()
-        running, tasks = parts[3].split("/")
+        if ("FreeBSD" == os):
+            parts = data[2].split()
+            running = parts[0]
+            tasks = parts[0] + parts[1] + parts[2]
+        elif ("Linux" == os):
+            parts = data[0].split()
+            running, tasks = parts[3].split("/")
+        else:
+            pass
 
         return (int(tasks), int(running))


More information about the freebsd-gnome mailing list