Skip to content

Instantly share code, notes, and snippets.

View mgedmin's full-sized avatar

Marius Gedminas mgedmin

View GitHub Profile
@mgedmin
mgedmin / run_in_thread.py
Last active October 5, 2015 09:48
value = runInThread(fn, args...) for Python
def runInThread(fn, *args, **kw):
"""Execute fn(*args, **kw) in a new thread context and return the result.
The execution is synchronous, i.e. blocking.
Typical use cases for runInThread will be the inspection of side effects
that are normally invisible to other threads until you commit a transaction
or something like that.
"""
# Aargh! Why can't threading.Thread(target=fn).join() return the result of
@mgedmin
mgedmin / subprocess_timeout.py
Last active January 24, 2016 01:13
@timeout(10) decorator for unittest.TestCase methods that I never ended up using
import signal
import sys
from functools import contextmanager
@contextmanager
def timeout(p, seconds):
"""Kill subprocess ``p`` if the with block times out.
Usage example ::
@mgedmin
mgedmin / bashrc_screen_and_tmux_sessions.sh
Created May 31, 2012 17:16
.bashrc snippet to remind me about running screen/tmux sessions
# check for screen sessions
if [ -d /var/run/screen/S-$USER/ ]; then
n=$(find /var/run/screen/S-$USER/ -type p|wc -l)
if [ $n -gt 0 ]; then
test x"$TERM" = xscreen && test -n "$WINDOW" \
&& echo "You have $n active screen sessions (and this is one of them)." \
|| echo "You have $n active screen sessions."
fi
fi
@mgedmin
mgedmin / bitten-notifications.patch
Created June 5, 2012 13:59
Patch to trac-bitten-slave to enable notifications on 1st success after failure, and hacky hacky IRC announcements
--- bitten/notify.py.orig 2010-10-21 00:00:00.000000000 -0400
+++ bitten/notify.py 2012-05-22 12:41:56.088838001 -0400
@@ -27,6 +27,10 @@
'notify_on_successful_build', 'false',
"""Notify if bitten build succeeds.""")
+ notify_on_success_after_failure = BoolOption('notification',
+ 'notify_on_successful_build_after_failure', 'false',
+ '''Notify if bitten build succeeds after a build failed.''')
+
@mgedmin
mgedmin / get-zope-project.sh
Created June 7, 2012 11:48
get-zope-project [-b branchname] [--git] zope.foo -> checks out from svn.zope.org
#!/bin/sh
# Check out one of Zope 3 subversion projects
USAGE="Usage: $0 [--git] [-b branchname] [-n|--dry-run] projectname [directory]"
BASEURL=svn+ssh://svn.zope.org/repos/main
project=
branch=trunk
dir=
git=0
dry_run=
while true; do
@mgedmin
mgedmin / disk-inventory.py
Created June 22, 2012 11:17
disk-inventory
#!/usr/bin/python
"""
Produce a disk inventory for fridge:
- how many hard disks and how large
- how are they partitioned
- how are the RAID devices defined
- where are they mounted
- how much space is used and how much is free
@mgedmin
mgedmin / firefox_tab_reordering.patch
Last active October 13, 2015 09:28
Initial patch to make Firefox swap tabs on Ctrl+Shift+PgUp/PgDn (later updated, fixed and merged upstream!)
Ctrl+Shift+PageUp/Down reorder tabs (try #3, this one works and seems to be in the right place)
https://bugzilla.mozilla.org/show_bug.cgi?id=702960
diff -r d2fbc67f69f5 browser/base/content/tabbrowser.xml
--- a/browser/base/content/tabbrowser.xml Fri Nov 30 13:51:45 2012 +0000
+++ b/browser/base/content/tabbrowser.xml Mon Dec 03 14:35:29 2012 +0200
@@ -2504,6 +2504,25 @@
return;
}
@mgedmin
mgedmin / mako_tb.py
Created December 12, 2012 16:14 — forked from anonymous/mako_tb.py
Fixing Mako tracebacks (version 1)
import linecache
import traceback
import sys
import mako.template
def mako_error_handler(context, error):
"""Decorate tracebacks when Mako errors happen.
Evil hack: walk the traceback frames, find compiled Mako templates,
stuff their (transformed) source into linecache.cache.
@mgedmin
mgedmin / demo.txt
Created December 12, 2012 16:25
Fixing Mako tracebacks (version 2)
$ virtualenv /tmp/sandbox
...
$ /tmp/sandbox/bin/pip install mako
...
$ /tmp/sandbox/bin/python mako_tb.py
= Default traceback =
@mgedmin
mgedmin / check_manifest.py
Last active October 14, 2015 00:08
Beginning of a tool to check Python MANIFEST.in for completeness. MOVED TO https://github.com/mgedmin/check-manifest
#!/usr/bin/python
"""Check the MANIFEST.in file in a Python source package for completeness.
Here's the plan:
This script works by building a source distribution archive (by running
setup.py sdist), then checking the file list in the archive against the
file list in version control (Subversion, Git, Mercurial, Bazaar are
supported).
Since the first check can fail to catch missing MANIFEST.in entries when