Wednesday, December 31, 2008

A Simple Python Interface for Gmail

The Problem:
I monitor processes, files, and tasks on my computer. Some monitors are for security others are just to know when a task has finished. Whatever the case, I would like to be notified vie email/text message when something has occurred.

The Solution:
As always... write a script. I wanted something that could easily be used to send an email with or without an attachment. I also wanted to be able to just call a mail function and pass in a recipient, subject, message and attachment.

The Script:
The following script is a pretty simple script that can be imported from another python file. I did not provide command line options because I usually use it in conjunction with another script, so it is always an import. I provide a main part so you can input your username, password, and some email to test the script out. Just run it from the command line to test it.
send_mail.py

Further Explanation:
This could easily extend a general class Mail that would have the same functionality, but you could tailor it to your individual email provider. Contact me if you want such help.

Monday, December 29, 2008

Create Optimized DVD/CD Images of Folder for Backup

The Problem:
I have music, videos, pictures, and files that all need backups. I have them organized how I want, but they don't always fit perfectly on to a DVD. I want a simple program that I point to a directory and it will create optimized image files for burning to a dvd. I would also like to do a few quick 'dry runs' to be sure that I like which files go in each image and that I don't have an image that is only 10% full!

The Solution:
Well, if you can't find one... make one! I decided to write a script that linked up with PowerISO's commandline tool (piso) to create images of files. You will need several Gigs of space to store the images... This is nice because you don't have to go through all your files and figure out how to divide them among images... You just do a few dry runs and then when you are happy, let it go all night!

The Script
This script uses the command line utility of PowerISO to create disc images of a folder and tries to maximize the disc usage while still preserving the file structure of the provided folder. You will need to add the PowerISO install path to your path environment variable
file_dvd_backup.py
Further Explanation:
OK, so as an example, I want to backup all of my pictures. I keep them (as seen in a previous post) in the structure of YYYY\MM month and when I burn a backup image of all of my pictures. I would prefer to keep as many pictures of the same year together as possible, however, if there is a large file (say a video clip) in one folder that doesn't fit on a dvd that is only 50% full, I would prefer it to be on a new dvd image, and then continue to fill first dvd, and then fill the new dvd image. Confusing I know, it creates dvd's that are usually about 99% full, even with a few large files mixed in.

I also hate searching for which dvd has what folder. The script creates a list of directories contained on each disc and add that file to each image. It makes it very easy for figuring out which disc has what pictures. The script could easily be altered to contain a list of all folders AND files, but I thought that was overkill.

Saturday, December 20, 2008

Blogger Syntax Highlighting

The Problem:
I am still using Blogger.com to host my blog, which works pretty good, you just don't have too many options and no server control. This means I can't just include my scripts on the fly (for now). I have been working with my layout, client side syntax highlighting, and general look and feel of the blog and I am getting somewhat comfortable with it. For syntax highlighting I decided to go with syntaxhighlighter, a google code project written in javascript. It works well, it's just slow. The problem with doing using plugins and including the script right into your post is that I have to generate the html for each blog post... and I want them all to be consistent.

The Solution:
Wanting things to be as simple as possible for me to post and edit my new scripts I wanted a simple code generator. I wanted it to create the html for adding a collapsed code section with a link to the script on my server on the top. I like this format because you can copy it, read it or just download it. Maybe I'll edit the syntaxhighlighter so that it takes a link for downloading the code... but for now this works. Just for fun, here's the code for my code generator:
convert_script.py
Note: The html for the presentation of my code generator on my blog was created by this script...

Further Explanation:
So in essence, all I have to do is point the script at a file on my server and bam! the code is in the clipboard! I love it. It supports code snippits and local or web script files. You can also add options for the syntaxhighlighter javascript. Yea... it's a bit rough, but what do you expect in under an hour...

Tuesday, December 16, 2008

Redundant MySQL backup with Python

The Story:
Ok so we lost our entire company wiki the day of launch... luckily I was able to recover over 60% of it from the cache of MANY people's computers (I just wrote a script that filtered their cache and spit out the wiki content pages, then a bunch of us went through the pages)... but that is a whole different subject. (Little Note: I was not responsible for backing up or transferring the database)

The Solution:
Since then I have been pretty paranoid about running backups and have volunteered for the task (because I have done a LOT of work on the wiki). I understand MySQL Replication, which is very powerful, but sometimes you just want the sql... so I created a redundant mysqldump script that will create zipped backups in the following structure:

<root>
<year>
January.zip
February.zip
...
Week1.zip
Week2.zip
Monday.zip
Tuesday.zip
...


The Script:
The script is pretty short (under 50 lines long). I put the parameters right into the script, but you could easily add them as options. I also have it backing up all databases. This could be run as a cron job/Scheduled Task and should run on Linux, Mac and Windows.
sql_backup.pyFurther Explanation:
The script is five parts:
  1. Parameters: This is really all you need to alter for the script to work for you. You will need to specify:
    • Target- The directory for all redundant backups
    • File Name- The name for all .sql files
    • Username/password- The authentication data used for signing into mysql
    • Dump command- ok this is just the command used to call mysqldump. You may need to include the full path to the executable if the mysql bin directory is not on your environment path variable
    • Host- this is your host- example.com
  2. The command for the dump
  3. Name Resolver: This just resolves the name of the backup (Monday, Tuesday, Week 1, Week 2, etc.)
  4. Writing the Zip file
  5. Cleanup- remove uncompressed sql file and move the zip file to the specified target

Saturday, December 13, 2008

Automatically Copy Your Pictures from a Digital Camera

The Problem
Ok... so who gets sick of all of the stupid pop-ups from the media detectors? I personally hate all media detectors because even the best are slow, don't organize your photos by date and month, and are always in your face!

The Solution
I believe the best structure for organizing pictures is: root\YYYY\MM month. This structure is by far easiest to navigate (using Picasa). Each folder does not get too big (I take a lot of pictures) and it is easy at least to remember, within a couple of months, when you took a picture. Also, it is nice because the years AND months actually sort correctly.


The Script
Anyways, I wrote a python script that reads your removable drives and finds DCIM folders and copies them to a specified root folder in the structure specified above.
This script works great for me, but you could add a bit more checking and error logging... It's just over a hundred lines and I wrote it in the last hour and a half (I got sick of copying all the pictures over manually...) You might want to add the -h option once for usage. So here it is:
copy_pics.pySo, if you're like me and have an all-in-one internal card reader, your drive letter basically never changes. I just create a batch file and put a link to it on my quick launch. This is my batch file:

copy_pics.bat


I'll probably create a registry key that will run every time I mount a removable drive and check for a DCIM folder... but that can wait till tomorrow... I told my wife I'd come to bed over an hour ago. Oops.

Friday, December 12, 2008

First post

Hello world!