Need the help on hack gDesklets to kill the 'Linuxish' in
Python.
Alexander Leidinger
Alexander at Leidinger.net
Sat Sep 13 07:09:19 PDT 2003
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.
The new version of gdesklets also doesn't update the weather and pi's
network desklet...
Bye,
Alexander.
--
There's no place like ~
http://www.Leidinger.net Alexander @ Leidinger.net
GPG fingerprint = C518 BC70 E67F 143F BE91 3365 79E2 9C60 B006 3FE7
-------------- next part --------------
--- gDesklets-0.21.2/libdesklets/CPU.py.orig Sat Sep 13 13:09:32 2003
+++ gDesklets-0.21.2/libdesklets/CPU.py Sat Sep 13 14:31:15 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
@@ -39,16 +48,25 @@
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 --------------
--- gDesklets-0.21.2/libdesklets/Disk.py.orig Sat Sep 13 14:41:43 2003
+++ gDesklets-0.21.2/libdesklets/Disk.py Sat Sep 13 14:41:18 2003
@@ -3,6 +3,7 @@
import time
import os
import statvfs
+import libdesklets as lib
class Disk:
@@ -19,22 +20,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")):
- return self.__partitions
+ 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 --------------
--- gDesklets-0.21.2/libdesklets/Memory.py.orig Sat Sep 13 14:42:43 2003
+++ gDesklets-0.21.2/libdesklets/Memory.py Sat Sep 13 15:18:17 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("
+ 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 --------------
--- gDesklets-0.21.2/libdesklets/Network.py.orig Sat Sep 13 15:18:34 2003
+++ gDesklets-0.21.2/libdesklets/Network.py Sat Sep 13 15:37:29 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,26 +69,47 @@
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
@@ -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])
- found = 1
- break
- #end for
+ 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
+ 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 --------------
--- gDesklets-0.21.2/libdesklets/Sys.py.orig Sat Sep 13 15:37:53 2003
+++ gDesklets-0.21.2/libdesklets/Sys.py Sat Sep 13 15:59:12 2003
@@ -2,6 +2,7 @@
import commands
import time
+import libdesklets as lib
class Sys:
@@ -31,17 +32,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 +64,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 +105,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