Useful Hacks [Projects]

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.

Note: quicktile.py is now available as ssokolow/quicktile on GitHub.

Note: Find Dupes Fast (A.K.A. fastdupes.py) is now available as ssokolow/fastdupes on GitHub.

active_corners.py Flattr this

"""
active_corners.py (formerly kde_gui_fixes.py)

A quick script to let me bind arbitrary actions to clicks and scrolls in the
corners of the screen.

Works by creating un-managed, shaped, windows (4px by 4px) in the corners which
receive and act on events.

Requires: PyGTK, dbus-python (optional, default configuration only)
(I may switch it to PyQt when I can spare the time)

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

doc2war Flattr this

"""
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 Flattr this

#!/bin/bash
# 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 Flattr this

"""
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.

Probably best to use FDMF (http://w140.com/audio/) with some stricter-than-
default thresholds until I find the time to rewrite this to be I/O-bound.

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
"""

Fuzzy Tray Clock Flattr this

"""
A simple clone of the KDE Fuzzy Clock widget for use in other desktops.
The time appears as a tooltip if you hover your mouse over the tray icon.

If you'd like additional levels of fuzziness, just ask.
"""

gif.py Flattr this

"""
A pure Python GIF metadata extractor.
Supports adjustable detail to fine-tune performance.

Example code and full epydoc docstrings included.

Uses:
 - Identifying whether a GIF is static or animated.
 - Extracting the dimensions, pixel aspect ratio, number of frames, loop count,
    global palette or palette size, and background color.
 - Extracting comments and other plaintext.
 - Testing for various structural errors.

TODO:
 - Provide basic support for XMP Metadata extraction
   - http://en.wikipedia.org/wiki/Extensible_Metadata_Platform#Location_in_file_types
   - http://www.matthewflickinger.com/lab/whatsinagif/bits_and_bytes.asp
   - Generate test GIF with http://code.google.com/p/python-xmp-toolkit/

Changelog:
 - 0.2.2: Audited the code and made some corrections.
 - 0.2.1: 40% speed improvement (went from 15 to 9 seconds for 1000 images)
 - 0.2.0: Feature-complete
 - 0.1.0: Initial release
"""

install_kernel.py Flattr this

"""
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
"""

KDE GUI Fixes Flattr this

"""
kde_gui_fixes.py

A quick script to fix a couple of my KDE pet peeves:
- The screensaver disabling mechanism doesn't guarantee it'll get re-enabled.
- There's no way to ensure scrolling in the corners will switch desktops.

Requires: PyGTK, PyDCOP (optional, default configuration only)
(I used PyGTK because I'm more familiar with it than PyQt and I was rushed)

Some code based on the shaped window example from the PyGTK 2.x tutorial.

TODO:
- Merge this with another idea of mine and whip up a nice GUI for it.
"""

Lazybones Script Lister Flattr this

"""
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 shabang 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.

Non-obvious Features:
- Hyperlinks URLs and obfuscates e-mail addresses in script descriptions.
- Configurable license name hyperlinking

Warnings:
- The HTML templating is a quick hackjob. I'm not kidding.
- Don't forget to remove the template bits specific to my site.

TODO:
- Switch to a proper templating solution? (No longer a single-file script)
- Add caching eventually (current run time for my site, 0.1 seconds)
- Add a 5px inset border and subtle "rounded CRT glare" gradients to <pre>
"""

rar.py Flattr this

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

It was, however, influenced by the zipfile module in the Python standard
library as, having already decided to match the zipfile.ZipFile API as closely
as feasibly possible, I didn't see a point to doing extra work to come up with
new ways of laying out my code for no good reason.

@todo: Determine how rarfile (http://rarfile.berlios.de/) compares to this in
various target metrics. If it is superior or close enough on all fronts,
patch it as necessary and plan a migration path. Otherwise, do the following:
 - Complete the parsing of the RAR metadata.
   (eg. Get data from archive header, check CRCs, read cleartext comments, etc.)
 - Optimize further and write a test suite.
 - Double-check that ZipFile/ZipInfo API compatibility has been maintained
   wherever feasible.
 - Support extraction of files stored with no compression.
 - Look into supporting split and password-protected RARs.
 - Some password-protected RAR files use blocks with types 0x30, 0x60, and 0xAD
   according to this code. Figure out whether it's a bug or whether they're really
   completely new kinds of blocks. (Encrypted headers for filename-hiding?)
 - When the appropriate code is available, use the following message for failure
   to extract compressed files::
    For reasions of patent, performance, and a general lack of motivation on the
    author's part, this module does not extract compressed files.
"""

ScratchTray Flattr this

"""
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)
- Resizing the tray on resolution change is currently broken. I'll take a look
  at it soon.
"""

swab.py Flattr this

"""
A 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.

2016-03-03: Since I've been meaning to fix this since 2009 and it was
            embarassing, I rewrote it just enough that it isn't doing a syscall
            for every two bytes.

TODO:
- Rewrite in Rust and optimize.
"""

upd_hosts.py Flattr this

"""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.

Edit the ADHOST_SUFFIX_WHITELIST variable if you want. (Default is to allow
only Project Wonderful because I respect them and they don't serve flash ads)

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 a mode which doesn't require the local hosts file to be moved to
  /etc/hosts.local
"""

wine_run Flattr this

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