Enable PHP autocompletion in Eclipse (Ubuntu 11.04)

Monday, Oct 3, 2011 1:29 pm
William Barnes

For some reason, Eclipse (using PHP Development Tools) doesn’t include built-in PHP functions with a new project. Assuming you downloaded Eclipse from the Ubuntu repository and added PDT through Eclipse’s “Install new software” dialog: in Eclipse, right click on your project in the Explorer pane; choose Include Path, Configure Include Path; select the Libraries tab; click Add External Source Folder. The folder you need may vary depending on the version of Eclipse, it should look something like “~/.eclipse/org.eclipse.platform*/plugins/org.eclipse.php_core*/Resources/language/phpVERSION”.

Python script to fix broken symlinks

Monday, Sep 26, 2011 4:47 pm
William Barnes

I recently added a new hard drive and reorganized my increasingly chaotic and whimsically named storage and backup partitions. In the process, I moved a folder containing hundreds of symlinks to a different drive resulting in hundreds of broken symlinks (I should have used rsync).

This script will repair symlinks after you’ve moved a folder. Change BASEDIR to the current location of your files and OLDBASE to where they used to be. For example, if you moved all your files from /mnt/Backup2 to /mnt/Backup2, then BASEDIR = ‘/mnt/Backup2’ and OLDBASE = ‘/mnt/Backup1’. If you want to test (to make sure it will do what you expect) then change DEBUG to True.

This script will only fix symlinks that point to files/directories within the BASEDIR.

  1. #!/usr/bin/env python
  2.  
  3. import os
  4.  
  5. # Configuration
  6.  
  7. BASEDIR = '/mnt/NewStorage'
  8. OLDBASE = '/mnt/OldStorage'
  9. DEBUG = False # I recommend a test run first
  10.  
  11. def relink(path):
  12. old_target = os.path.realpath(path)
  13. new_target = old_target.replace(OLDBASE,BASEDIR,1)
  14. if DEBUG:
  15. print "Relink: " + path + "\n\tfrom " + old_target + "\n\tto " + new_target
  16. else:
  17. os.remove(path)
  18. os.symlink(new_target,path)
  19.  
  20. for root, dirs, files in os.walk(BASEDIR):
  21. for filename in files:
  22. fullpath = os.path.join(root,filename)
  23. if os.path.islink(fullpath):
  24. relink(fullpath)
  25. for dirname in dirs:
  26. fullpath = os.path.join(root,dirname)
  27. if os.path.islink(fullpath):
  28. relink(fullpath)

If you save the script as ~/link-fix.py then run it with python ~/link-fix.py.

Toggle Gnome screensaver lock on USB key insertion

Sunday, Sep 25, 2011 9:39 am
William Barnes

While I like my computer to lock itself automatically when I leave, it gets annoying when it does it every time I get up for a few minutes. I decided that it would be nice if I could toggle the screen lock only when I’m out of the apartment. I decided that the trigger should be the USB key I have on my keychain. It’s always with me. If it’s plugged in to the computer, then I’m likely home. I apologize for the (lack of) formatting of the code, I will be fixing that when I get my computer back up and running.

Ubuntu uses an event-based system called udev that, among other things, can run a script when a USB device is plugged in or unplugged. Local (ie: user-created) udev rules are stored in /etc/udev/rules.d/. Before you can create a rule, you need to know a little about the device that is going to trigger it. Open a terminal and type:

udevadm monitor --udev --environment

Connect your USB device. It doesn’t have to be a USB key, it could be a phone, for example. A bunch of text will come up. Look for “ID_SERIAL” and “ID_VENDOR_ID”. Write down the values of those. You can use other variables if they suit your device better, just change the rules file accordingly.

Create a file called “/etc/udev/rules.d/85-screen-lock-toggle.rules”. Put the following in it, replacing VALUE with the proper value:

  1. ACTION=="remove", ENV{ID_SERIAL}=="VALUE", ENV{ID_VENDOR_ID}=="VALUE", RUN+="/usr/local/bin/gnome-lock-enable"
  2. ACTION=="add", ENV{ID_SERIAL}=="VALUE", ENV{ID_VENDOR_ID}=="VALUE", RUN+="/usr/local/bin/gnome-lock-disable"

Next create the scripts to actually disable and enable locking:

  1. #!/bin/bash
  2.  
  3. user=`ps aux | grep gnome-screensaver | head -n 1 | awk '{print $1}'`
  4.  
  5. if [ -n $user ]; then
  6. GNOME_SCREENSAVER_PROC=`ps xa | grep gnome-screensaver | head -n 1 | awk '{print $1}'`
  7. export `grep -z DBUS_SESSION_BUS_ADDRESS /proc/$GNOME_SCREENSAVER_PROC/environ`
  8. su $user -c "gconftool-2 --set "/apps/gnome-screensaver/lock_enabled" --type bool 1"
  9. fi
  1. #!/bin/bash
  2.  
  3. user=`ps aux | grep gnome-screensaver | head -n 1 | awk '{print $1}'`
  4.  
  5. if [ -n $user ]; then
  6. GNOME_SCREENSAVER_PROC=`ps xa | grep gnome-screensaver | head -n 1 | awk '{print $1}'`
  7. export `grep -z DBUS_SESSION_BUS_ADDRESS /proc/$GNOME_SCREENSAVER_PROC/environ`
  8. su $user -c "gconftool-2 --set "/apps/gnome-screensaver/lock_enabled" --type bool 0"
  9. fi

Make the scripts executable:

sudo chmod a+x /usr/local/bin/gnome-lock-enable /usr/local/bin/gnome-lock-disable

Restart udev:

sudo restart udev

Enjoy the convenience.

Gentoo Day 1: Kernel, framebuffer, Xorg

Tuesday, Feb 16, 2010 10:52 pm
William Barnes

Lately I’ve felt that I’ve gotten too lazy. Ubuntu requires almost no work to get running and I have little to no idea what all the fancy stuff they’re doing actually means. So I’m ditching Ubuntu (on my personal desktop, anyway) and installing Gentoo. For my own records and to help anybody else who has similar troubles, I’m keeping a journal of everything here.

Read the rest of this entry »

Backing up Gmail on an Ubuntu mailserver

Wednesday, Dec 31, 2008 12:47 pm
William Barnes

Backstory

I used to run my own Courier/Postfix mailserver to manage my family’s email (technically, I still do, but more later). Of late, I’ve been finding this to be a little bit of a hassle. Mostly because my laptop battery occasionally dies and I want to check my email from a public computer. This is a less than optimal situation. I have to run a webmail program on the server (RoundCube is nice though) and I have to type my server login into a strange computer. So I decided to switch to Gmail and it is wonderful. I like the interface, I can use a different password for it than for my home network, and somebody else does the work of keeping it running.

But I’m not too thrilled about all my email “living in the cloud.” I trust Google. I have no choice. If I’m going to give personal information to anyone, it would probably be to Google (since they probably already know more about me than I do). But there is always the chance that they will turn off POP/IMAP access to my email and it will be trapped there. Multiple simultaneous meteor strikes could destroy the data centres where my email is housed. If that happens, I’ll be unable to find all my LOLcat emails. That just wouldn’t do.

Solution

Enable POP on Gmail, download the email with Fetchmail, archive it to Amazon S3 once a month.

POP on Gmail

Gmail POP

In Gmail: open Settings; choose the “Forwarding and POP/IMAP” tab; choose one of the “Enable POP” options.

Download with Fetchmail

You’ll need to have a mailserver running on your computer. I suggest Postfix. You’ll probably also want to get access to your mail. If you’re installing this on your desktop, then most clients will be able to access your Maildir directly. If you’re installing this on a server, I suggest Courier. Both of these are available through apt. I would suggest reading a more detailed tutorial (see esp. pg. 5-6) if you have never done this before.

You will of course need fetchmail (sudo apt-get install fetchmail). You need a config file (sudo touch /etc/fetchmailrc & chmod 600 /etc/fetchmailrc) with your email addresses and passwords like so:

set daemon 3600

poll pop.gmail.com port 995 with protocol pop3
        user {gmailuser}@gmail.com with password {gmailpassword} is {localuser} here options ssl

Replace {gmailuser} with your Gmail username, {gmailpassword} with your Gmail password, and {localuser} with your username on your Linux box. Run sudo /etc/fetchmail restart and your email should be getting downloaded to your computer.

Archiving

You might be fine stopping there. You now have a local copy of all your mail. On the other hand, maybe you want more. It would be nice to have the mail sorted into folders automatically and it would be nice to have a second copy. I have a cron.monthly script that automatically sorts my Inbox into folders by month and year and copies the archived mail to Amazon S3.

The cleanup-maildir python script can be found here (check the comments for a bug fix). Copy that to somewhere in your $PATH (try /usr/local/bin). My archive script can be found here (or Bzip2 compressed). Copy it to someplace like /usr/local/bin and create an executable file in /etc/cron.monthly which contains a line like: maildir-backup /mnt/backup/Mail/username username. The first parameter is the backup destination and the second one is the local user to back up.

You can use the command-line version of JungleDisk to mount your Amazon S3 space and set that as your backup destination.

Hopefully this helps somebody someday. If nothing else, now I’ll remember what I did for when I upgrade my server next.