Useful Hacks

This page lists scripts I quickly hacked up to solve a problem but haven't had time to clean up for general use. Feel free to use them if you like. Unless otherwise stated, they're licensed under the GNU GPL version 2 or later.

doc2war

"""
A simple little script to convert Microsoft Word files into Konqueror .war 
archives (an HTML file and it's dependencies inside a renamed .tar file)
using wvHtml.

TODO:
- Add a file magic check to identify and rename RTF files with .doc extensions.
"""

enc_ogg+flac.sh

#!/bin/sh
# enc_ogg+flac.sh
# By: Stephan Sokolow
#
# A wrapper script to allow KAudioCreator to encode to both Ogg Vorbis and FLAC
# in one run Also normalizes the input file to avoid the need to do so twice
# later on.
#
# Licensed under the GNU GPL 2 or later.

fdupes_mp3.py

"""
A crude script for identifying audio files that generate duplicate waveforms

Originally written as an experiment in identifying files that differ only in
metadata. Supports anything sox does.

It's CPU-bound, but because of sparse documentation on the MP3 format, it's
the best I can do for now.

prints duplicates to stdout (one per-line) with groups of duplicates separated
by empty lines. Status messages are sent to stderr.

Warning: Seems to get stuck on .mpg files.

Requires: sox
"""

gif.py

"""
By: Stephan Sokolow (deitarion/SSokolow)
A pure Python GIF validator which also counts the number of frames.
Originally conceived as simiply a static/animated detector.

TODO:
- Clean up and reorganize the code some more.
- Validate whatever I can in the color table and the other bit flags.
- Find other things I can validate.
- Provide a function to extract a GIF's comment blocks.
"""

index.cgi

"""
A single-file Python CGI script for effortless sharing of other single-file 
scripts. If you're viewing a "Useful Hacks" list on my website, this is the 
code behind it.

Simply put your desired description into each file's docstring (for shell 
scripts, it takes every commented line starting with the shebang and ending
with the first non-comment line) and drop them into a folder along with 
this script. Currently supports Bourne-compatible shell scripts and Python 
scripts. Other languages under consideration.

Warnings:
- This code is a quick hackjob. I'm not kidding.
- Don't forget to change the <title> and remove my Google Analytics footer.

TODO:
- Switch to Genshi for templating?
- Detect and hyperlink URLs in docstrings.
- Support a delimiter for ending the displayed description before the end of
  an overly long docstring. Maybe something like "--clip--".
"""

install_kernel.py

"""
A convenience wrapper for building and installing a new kernel on Gentoo 
Linux, complete with some extra bits to make maintaining a couple of 
backup kernels easy. Also handles mounting and unmounting /boot and calling
module-rebuild to regenerate external kernel modules.

This only writes a tertiary kernel if none exists to prevent two builds 
in a row from deleting the only good kernels available, so you'll want to 
add this to your /etc/conf.d/local.start:

mount /boot
if [ -e /boot/old_kernel_emergency ]; then
	echo " * Boot considered successful. Purging tertiary kernel."
	rm -rf /boot/old_kernel_emergency
fi
umount /boot

Read the source for the rest of the details.

TODO:
- Should I have this regenerate the fbcondecor initrd?
- Better instructions. Possibly a zip bundle with a README
"""

rar.py

"""
A pure-Python module for identifying and examining RAR files developed without
any exposure to the original unrar code. (Just format docs from www.wotsit.org)

TODO:
- Document this module properly.
- Complete the parsing of the RAR metadata.
  (eg. Identify directories, check CRCs, etc.)
- Make sure this has the same coding and error conventions as my gif.py.
- Consider releasing this under PSF 2.3 license instead.
- Support extraction of files stored with no compression. (eg. XviD movies)
- Look into supporting split RARs properly
"""

ScratchTray.py

"""
A quick-access auto-saving, auto-hiding scratchpad for jots, multi-step
copy-pasting, and anything else where a more specialized app is over-thinking
the problem and opening a plain old plaintext editor (like leafpad, notepad, or
kedit) is inefficient and potentially clutter-inducing.

Requires: PyGTK
Recommended: GtkSourceView and its Python bindings (Undo/Redo support)
Usage: Run it and then click the white line along the left edge of the desktop.
       (Also supports drag-and-drop)

TODO:
- Include a "pushpin" icon/button in the lower-right corner to lock the tray open.
- Pressing escape should collapse the tray
- Implement some form of multi-note support for storing stuff that needs to be
  "backgrounded". Maybe tabbing. (Similar reason to having a few virtual 
  desktops)
- Support some sort of resize handle or handles.

Known Bugs:
- The window hides while the context menu is visible (harmless but unintuitive)
- Quitting by closing the X connection (xkill) doesn't commit pending changes.
- A drag-and-drop which sends a drag motion event to this but then ends in a
  drop to another window will temporarily confuse the auto-hide.
- ScratchTray currently depends on fcntl... which is non-portable. I'll update
  it to use a portable wrapper once I've made appropriate preparations so that
  my index.cgi script accepts zipped bundles.
  ( http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65203 )
"""

screensaver_saver.py

"""
A workaround for KDE's poorly thought out system for disabling the screensaver.

Runs in the background and re-enables the screensaver if it stays disabled for
more than TIMEOUT_DURATION, checking every SLEEP_DURATION. (defaults are 
two hours and one minute, respectively)
"""

swab.py

"""
A horrendously CPU-bound but functional script for swapping the audio-track
byte order in cdrdao .BIN files so that a .CUE file generated by toc2cue can
be mounted by DOSBox or CDEmu.

TODO:
- Rewrite in C and optimize
"""

switch_corners.py

"""
switch_corners.py

A simple little tool to allow binding of actions to the mouse buttons and
scroll wheel when the pointer is in one or more of the desktop corners.
Mimicks MacOS 7 rounded desktop corners as a side-effect. (I'll add support
for using composited transparency to avoid it once KDE 4 is comfortable)

The default configuration uses PyDCOP to map mouse scrolling to
desktop switching and right-clicking to the K menu (Behaviour
identical to my system when using a WinDeco theme where the
corners stay rounded when maximized) but there is no in-built
dependency on any specific desktop environment.

Trivial code editing is required to edit the configuration and
allows mapping of actions to any scroll direction or mouse button as well
as disabling specific corners. (eg. to prevent them from interfering with
Compiz Fusion edge actions)

Requires: PyGTK

Code based on the shaped window example from the PyGTK 2.x tutorial.
"""

upd_hosts.py

"""upd_hosts.py
Automatically generates /etc/hosts from /etc/hosts.local and the MVPS 
ad-blocking hosts list.

Instructions:
Put this file in /etc/cron.monthly and chmod it executable.

TODO:
- Use If-Modified-Since and ETags on the MVPS file so I can safely run this 
  more often. (Perhaps also use the ZIP download to save bandwidth?)
- Add support for masking out entries in the MVPS hosts file. 
  (eg. Project Wonderful)
"""

wine_run

#!/bin/bash
# A three-line wrapper script to allow double-clicked .exe files to be run
# with the working directory they expect.