Bash Alias for Python/Virtualenv
If you are writing Python apps, you should definitely be using virtualenv. It creates an isolated virtual environment (hence the name), where you can install Python apps without affecting the rest of the apps on your machine. For example, if I have a website that relies on Django 1.2, and other sites that rely on Django 1.3, I can put them in virtualenvs and just install the correct package versions in each. This can also be applied to running different Python versions per project as well, such as 2.5 for one and 3.0 in another. Yet another great use of virtualenvs is testing your code in multiple version of Python.
BASH: How To Write Better Scripts
If you haven't read my article on Automated Installer for Nginx, you should (not to toot my own horn). It is a script containing around 200 lines of code, including the templates. I wrote it in about 2 hours, mostly, because I had a great environment for testing. I'm going to share my story and my clean up script, so that hopefully, it helps you while you write your scripts.
Automated WordPress Install with Nginx Script
It is a pain to follow my own tutorial over and over for each domain. So I automated it! This script is tested ONLY on the official Ubuntu 10.04 32 bit AMI in a tiny instance, following the install instructions . I have yet to test it anywhere else, but if it works for you, please leave a comment at the bottom with your configuration. Also, if any Bash gurus have tips for the script, your input would be very much appreciated.
Add Aliases in Ubuntu Shortcut
I love the Ubuntu terminal. I spend more time in it than any sane person should. So obviously, I customize it as much as possible to make it work exactly how I want. The easiest way I've found is through aliases. An example of an alias is when I type "update" into my terminal, the real command it runs is "sudo apt-get -y update && sudo apt-get -y upgrade" (Automagically updates and upgrades all the packages on my system). While searching for other fun shortcuts, I came across this helpful post. It provides a quick way to install aliases into your system. I've modified it to make it a bit better (I think). All credit for the idea still goes to Brock Angelo, the original author.
Python: Make Directory
As I read other people's Python code, I notice almost every project has a utils.py file. I'm going to shamelessly post the most interesting (or most useful) bits that I find, with proper credit. The first one is from my work on modifying Gitosis to use a MySQL backend. Here's a quick way to make a directory:
"""
Create a new directory on a Unix system
@param directory_path
@param (optional) permissions
@exception OSERROR If directory cannot be made
"""
def mkdir(*a, **kw):
try:
os.mkdir(*a, **kw)
except OSError, e:
if e.errno == errno.EEXIST:
pass
else:
raise
Credit goes to the Gitosis project.
Python: Run Shell Commands
A lot of my scripts need a simple way to run shell commands. So I went ahead and wrote a method that makes it super easy to execute commands, and keep the output of both STDOUT and STDERR logged.
Python Script to Manage Virtual Hosts
I am sick of manually creating virtual hosts! So I will turn to my trusty Python skills, and have something out pretty quick here in a rough form. However, if you are looking for something right now, check this out. It is simple, and easy to understand (though lacking comments..).
http://www.straw-dogs.co.uk/12/10/python-virtual-host-creator/
Expect an update soon!
GitHub Setup Utility
Every 6 months or so, I wipe my computers and install the new version of Ubuntu. And each time, I have to set up git and GitHub again. And every time, I have to go find their support docs for just how to do it, which is annoying. So I wrote a quick little utility that prompts for your username and email, and gets all your keys and such ready for GitHub. It even copies your public key to the clipboard, for easy pasting into the GitHub key interface!
BASH: 32 or 64 bit check
Most packages that aren’t gotten through the default package manager are compiled for a single architecture (x86, amd64, ARM, etc), therefore it is useful to have a quick way to check whether the system is 32 or 64 bit, so an if statement can be used to do the appropriate action.
Robust Bash Scripting
Every Bash scripting tutorial I’ve ever read has missed a lot of key points that David Pashley brings up in his blog. I’ve gone through and added a lot of his recommendations to all my scripts and tested them. Expect to start seeing them on this blog!