Skip to content

Instantly share code, notes, and snippets.

import httplib
import urllib
class WebServer(object):
def __init__(self, host):
self.host = host
def _make_request(self, method, url, data, headers):
conn = httplib.HTTPConnection(self.host)
@adamv
adamv / gist:736636
Created December 10, 2010 19:09
admin.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django.contrib.auth.models import Group, User
# Override username field require email address
class UserCreationForm2(UserCreationForm):
email = forms.CharField(max_length=75, required=True)
def print_warning(msg):
print fabric.colors.red("WARNING: ") + msg
def print_error(msg):
print fabric.colors.red("ERROR: ") + msg
def print_label(msg):
print fabric.colors.blue("==> ", bold=True) + msg
@adamv
adamv / maven-nexus.py
Created November 18, 2010 17:11
Uploading to Nexus using Maven from the command line (in Python). Requires Maven and Curl.
def local2(command, print_command=False):
"Run a command, returning the exit code, output, and stderr."
from subprocess import Popen, PIPE
p = Popen(command, stdout=PIPE, stderr=PIPE)
if print_command: print " ".join(command)
output, errput = p.communicate()
return p.returncode, output, errput
@adamv
adamv / gist:674295
Created November 12, 2010 16:24
Cheesy "run command and give me status, stdout, stderr" Python function
from subprocess import Popen, PIPE
def local2(command, print_command=False):
"Run a command, returning the exit code, output, and stderr."
p = Popen(command, stdout=PIPE, stderr=PIPE)
if print_command: print " ".join(command)
output, errput = p.communicate()
return p.returncode, output, errput
@adamv
adamv / setting.py
Created November 11, 2010 17:49
Find a setting/variable assignment-looking-thing.
def find_setting(source, setting, unquote=True):
re_setting = r'^\s*' + re.escape(setting) + r'\s*=\s*(.+)$'
m = re.search(re_setting, source, re.M)
if not m:
return None
value = m.group(1).strip()
if unquote:
if value.startswith("'") and value.endswith("'") or \
value.startswith('"') and value.endswith('"'):
@adamv
adamv / f.rb
Created November 5, 2010 04:24
Search for man pages
#!/usr/bin/ruby
search = ARGV.first
manpath = `manpath`.split(':')
pages = Dir["/usr/share/man/man?/*"].map do |f|
File.basename f
end.map do |f|
f =~ /(.+?)(\.(gz|bz2|lzma|Z))?$/
$1
@adamv
adamv / watch_log.sh
Created November 4, 2010 22:35
Colorize access_log
#!/bin/bash
# Escaped color codes
BLACK=`echo -e '\033[0;30m'`
DK_GREY=`echo -e '\033[1;30m'`
RED=`echo -e '\033[0;31m'`
PINK=`echo -e '\033[1;31m'`
GREEN=`echo -e '\033[0;32m'`
LT_GREEN=`echo -e '\033[1;32m'`
BROWN=`echo -e '\033[0;33m'`
@adamv
adamv / jms.py
Created October 20, 2010 22:18
Listen to a JMS topic on a Tibco bus.
#!/usr/bin/env jython -J-XstartOnFirstThread -Dpython.path=jms.jar:tibcrypt.jar:tibjms.jar
import time
from javax.jms import TopicConnectionFactory, MessageListener, Session
from com.tibco.tibjms import TibjmsQueueConnectionFactory, TibjmsTopicConnectionFactory
class Listener(MessageListener):
def onMessage(self, message):
@adamv
adamv / executil.py
Created September 15, 2010 22:06
Common scripty helpers in Python
import sys
import os
import contextlib
from subprocess import Popen, PIPE
def die(message, error_code=1):
print message
sys.exit(error_code)