Skip to content

Instantly share code, notes, and snippets.

@thepaul
thepaul / git-get-remote-head.sh
Created December 18, 2020 17:22
Get the name of the remote HEAD (the default branch on that remote)
#!/bin/bash -e
REMOTE=${1:-origin}
ref=$(git rev-parse --symbolic-full-name "$REMOTE/HEAD" 2>/dev/null) || {
git remote set-head "$REMOTE" -a >/dev/null
ref=$(git rev-parse --symbolic-full-name "$REMOTE/HEAD")
}
echo "${ref#refs/remotes/$REMOTE/}"
@thepaul
thepaul / pg_bytes.sql
Created April 12, 2018 15:23
postgresql PL/PGSQL functions for encoding 64-bit numbers as 8-byte strings and vice versa
-- transform 8 bytes into an int64
CREATE FUNCTION bytes_to_int64(s BYTEA, OUT result INT8)
AS $$
BEGIN
SELECT bit_or(get_byte(s, ind)::INT8 << ((7 - ind) * 8))
INTO result
FROM generate_series(0, 7) AS ind;
END;
$$ LANGUAGE 'plpgsql';

Keybase proof

I hereby claim:

  • I am thepaul on github.
  • I am thepaul (https://keybase.io/thepaul) on keybase.
  • I have a public key ASDVjs42Wo8PNSFWHDKiZmIsGEemdqZ4En1SFeO0uzkhswo

To claim this, I am signing this object:

git for-each-ref --format '%(refname) %(authoremail) %(committerdate:raw)' refs/remotes/origin/ \
| sort -k2,3 \
| awk '$3 < (systime()-(86400*30)) {
gsub(/refs\/remotes\//, "", $1);
printf "%-30s %-26s %s\n", $1, $2, strftime("%Y-%m-%d %H:%M", $3)
}'
@thepaul
thepaul / yieldscanner.py
Created January 12, 2016 00:57
Similar to re.Scanner but works as a coroutine, yielding tokens as found
import re
class NotAToken(Exception):
pass
class YieldScanner(re.Scanner):
def scan(self, inputstr):
match = self.scanner.scanner(inputstr).match
pos = 0
while True:
diff --git a/tools/dockerz/Dockerfile b/tools/dockerz/Dockerfile
new file mode 100644
index 0000000..0eafb35
--- /dev/null
+++ b/tools/dockerz/Dockerfile
@@ -0,0 +1,17 @@
+FROM ubuntu:trusty
+MAINTAINER <[email protected]>
+
+RUN apt-get -y update \
@thepaul
thepaul / gist:0e611da8b4fadec94568
Created July 9, 2015 21:29
wait for all subprocesses in a list to exit
import signal
def wait_for_subprocs(procs, cb=lambda proc: 0):
# do-nothing handler for SIGCHLD, just so it's something other than SIG_DFL.
# otherwise, Python won't interrupt syscalls.
oldhandler = signal.signal(signal.SIGCHLD, lambda *_: None)
try:
while procs:
signal.pause()
aliveprocs = []
@thepaul
thepaul / gist:aec592cb62294e587ef8
Created June 17, 2015 22:33
things in the Python standard library which use super()
argparse (all classes)
abc.ABCMeta
collections.Counter
fractions.Fraction
plistlib.Dict and plistlib.Plist
random.Random
unittest.TextTestResult and unittest.FunctionTestCase
weakref.KeyedRef
zipfile.ZipExtFile
ctypes.py_object
@thepaul
thepaul / ssh_tunnel_control.py
Created May 14, 2014 20:32
ssh tunnel up/down
import contextlib
import os
import subprocess
import tempfile
class SshTunnelDefinition:
def __init__(self, host, user=None, executable='ssh', localforwards=(),
remoteforwards=(), port=None, options=None):
self.executable = executable
@thepaul
thepaul / SanerConfigParser.py
Created May 13, 2014 22:42
# the ConfigParser.*ConfigParser family is just comically terrible
import ConfigParser
class SanerConfigParser(ConfigParser.SafeConfigParser):
def __init__(self, defaults=None, dict_type=dict, allow_no_value=False):
ConfigParser.SafeConfigParser.__init__(self, dict_type=dict_type,
allow_no_value=allow_no_value)
self.mydefaults = defaults or {}
no_default = object()