Jun 27
I wrote a super simple script to fix some issues I have with some defaults in the latest Ubuntu release. Nothing majour here, just annoyances really. Below is the script, read it and comment out any lines you do not want to take effect on your system. It requires root access for a few of the commands, such as managing packages and removing system-wide files. Run it as a regular user and the sudo command will be used to prompt you for a password, run like this:
$ chmod +x ubuntu-fix.sh
$ ./ubuntu-fix.sh
And the following is the source code or download the script here.
#!/usr/bin/env bash
#
# Simple script to remove some of what I consider to be annoyances with
# Ubuntu 10.04 Lucid Lynx (and probably future versions).
#
set -e
# keep location bar always visible instead of breadcrumbs
gconftool-2 --set '/apps/nautilus/preferences/always_use_location_entry' \
--type bool 'true'
# move buttons to the correct side and add menu on left icon
gconftool-2 --set '/apps/metacity/general/button_layout' --type string \
'menu:minimize,maximize,close'
# make fonts a reasonable size
gconftool-2 --set '/desktop/gnome/interface/font_name' --type string 'Sans 9'
# remove the poorly named and useless (to me) "ubuntuone" client package
if [ `dpkg -s ubuntuone-client | grep -c 'not-installed'` -ne 1 ]
then
sudo apt-get remove --yes --purge ubuntuone-client
sudo rm -f /etc/xdg/autostart/ubuntuone-launch.desktop
fi
# remove the panel thing with email and other icons with wrong bg color
if [ `dpkg -s indicator-applet | grep -c 'not-installed'` -ne 1 ]
then
sudo apt-get remove --yes --purge indicator-applet
sudo rm -f /etc/xdg/autostart/indicator-applet.desktop
fi
# add the regular volume icon back at startup
if [ ! -f /etc/xdg/autostart/gnome-volume-control-applet.desktop ]
then
sudo cat /etc/xdg/autostart/gnome-volume-control-applet.desktop<<EOF
[Desktop Entry]
Name=Volume Control Applet
Comment=Show desktop volume control
Icon=multimedia-volume-control
Exec=gnome-volume-control-applet
Terminal=false
Type=Application
Categories=
NoDisplay=true
OnlyShowIn=XFCE;
X-GNOME-Bugzilla-Bugzilla=GNOME
X-GNOME-Bugzilla-Product=gnome-media
X-GNOME-Bugzilla-Component=gnome-volume-control
# See http://bugzilla.gnome.org/show_bug.cgi?id=568320
#X-GNOME-Autostart-Phase=Panel
X-GNOME-Autostart-Notify=true
X-GNOME-Autostart-Delay=2
X-Ubuntu-Gettext-Domain=gnome-media-2.0
EOF
fi
# offer to logout
echo -n "Done fixing Ubuntu, you need to log out and log back in for some of \
the changes to take effect.
Log out now? [y/N] "
read DOLOGOUT
if [ ! -z $DOLOGOUT ] && [ $DOLOGOUT == "yes" ] || [ $DOLOGOUT == "y" ] || \
[ $DOLOGOUT == "YES" ] || [ $DOLOGOUT == "Y" ]
then
/usr/bin/gnome-session-save --kill
fi
Jan 09
A little Whois reader I wrote while messing around with sockets in Python. It doesn’t do any processing on the reply, it just returns the whole thing as a string. It’s also missing any mechanism to determine which Whois servers to use for which TLDs. But anyway, it’s a good start!
#!/usr/bin/env python
import socket
import select
PORT = 43
BUFSIZE = 1024
LINEEND = '\r\n'
def whois(domain, server, port=PORT):
'''
Perform a WHOIS search for domain on server/port.
'''
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((server, port))
msg = ''
lookup = domain + LINEEND
readable, writable, error = select.select([],[s],[],60)
if s in writable: s.send(lookup)
readable, writable, error = select.select([s],[],[],60)
while s in readable:
data = s.recv(1024)
msg = msg + data
if not data:
s.shutdown(socket.SHUT_RDWR)
s.close()
break
readable, writable, error = select.select([s],[],[],60)
else:
s.shutdown(socket.SHUT_RDRW)
s.close()
return msg.strip()
if __name__ == '__main__':
print whois('google.com', 'whois.markmonitor.com')
Oct 05
I’ve written a very simple python module/script to help find postal code, city, province, latitude/longitude matching the supplied postal code, city, province, or coordinate.
Check out the Google Code Project page for more information.
You can checkout the source code by issuing the following command:
svn checkout http://python-geolocate.googlecode.com/svn/trunk/ python-geolocate-read-only
Sep 20
As part of one of my current projects, maptop, I needed a simple module to detect whether or not a GPS receiver is connected to the computer, and if so, which port is it connected to.
For the task, I used PySerial, along with some of their example code, to test if each serial port can be opened, and if so, if there are NMEA GPS sentences coming in on it. It uses a fixed baud rate of 4800, which is what my receiver is using and is also specified here. The timeout of 2 seconds seems to work (at least with my device). Both the baud rate and the timeout can be changed by modifying the BAUD_RATE and TIMEOUT “constants” at the top of the script.
I’m not sure if this works on Windows or not, I haven’t tested it yet, but it *should work*.
Below is the code for the module:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
| #!/usr/bin/env python
#
# Copyright 2009 Matthew Brush < mbrush AT leftclick DOT ca >
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
__version__ = "0.1"
__all__ = [ 'get_gps_port', 'test_port' ]
import serial
import glob
import re
import platform
BAUD_RATE = 4800 # the standard nmea baud rate
TIMEOUT = 2 # time to wait for nmea sentences
class NoGpsReceiverError(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
def scan():
""" Scan for available ports. return a list of device names. """
if platform.system == 'Windows':
import scanwin32
ports = []
for order, port, desc, hwid in sorted(scanwin32.comports()):
ports.append(port)
return ports
else:
import scanlinux
return scanlinux.scan()
def test_port(port):
""" Detects whether NMEA GPS sentences can be read from the port. """
nmea_gps_pattern = '^\$GP[A-Z]{3},'
gps_detected = False
try:
s = serial.Serial(port, BAUD_RATE, timeout=TIMEOUT)
for i in range(1,10): # try 10 lines in a row
line = s.readline().strip()
m = re.match(nmea_gps_pattern, line)
if m:
gps_detected = True
break
s.close()
return gps_detected
except serial.SerialException:
return False
def get_gps_port():
""" Looks for GPS data coming in on any serial port and returns the port name. """
for port in scan():
if test_port(port):
return port
raise NoGpsReceiverError('Unable to locate a suitable GPS receiver.')
def main():
print "Testing ports to find GPS receiver..."
try:
if platform.system == 'Windows':
print " Warning: gpsdetect not tested in Windows yet."
for port in scan():
if test_port(port):
print "Detected GPS receiver on '" + port + "'."
found_gps = True
break
except NoGpsReceiverError:
print "No GPS receivers were detected on the system."
if __name__ == '__main__': main() |
The complete source can be downloaded here.
Sep 11
I’ve had this super simple “Web Service” up on my other website for some time now, and I’ve used it fairly often through the browser and in scripts. I decided to make an even simpler one on CodeBrainz.ca for others and myself to use. The old service returned a single-node XML document, which is basically pointless and just adds a tiny bit of complexity on the client who is using it. The new address of the service is:
http://showip.codebrainz.ca
The actual code for the new service is a whopping 1 line of code:
<?php
echo $_SERVER['REMOTE_ADDR'];
?>
I’ve whipped up a couple ultra simple apps and a pointlessly simple library to save writing a few lines of code. The bash script is for for *nix-style systems while the .NET binaries are for Windows/.NET/Mono.
Here are links to each file:
I hope someone out there finds this simple service as useful as I have.
Sep 08
Here’s a very simple PHP script I whipped up to get the names, sizes, and mount state of a disk or partition in GNU/Linux or similar OS. It’s meant to be used on the shell in other scripts, so the output is easy to parse.
Use is as follows:
diskinfo [options] DISKNAME
DISKNAME is the name of the disk or partition, with or without the /dev/ portion. The script accepts several options, the --version option shows version and license information, the --help shows the version message and a help/usage message. The -s option makes the size of the disk or partition be printed in bytes, with -b in blocks, or with -h as a more appropriate unit (KB, MB, GB, etc.). The -m option displays whether the disk or partition is mounted or not, 1 if it is or 0 if it’s not. Have a look at the code, it’s fairly easy to see what’s going on and/or modify the behavior.
Download the script file, make it executable, and put it in a location in your $PATH environment variable (ex. /usr/bin) or call it otherwise.