Skip to content

Instantly share code, notes, and snippets.

@ekimekim
ekimekim / pyman
Last active December 17, 2015 20:29
A shortcut to look up the python online help() for a given module or symbol from a module. Examples: $ pyman subprocess $ pyman os.path $ pyman sys setprofile $ pyman . open
#!/usr/bin/env python
from sys import argv, exit
if len(argv) < 2:
print 'USAGE: %s MODULE [SYMBOL]\nOpen help() for given module, or given symbol from module.' % \
argv[0]
exit(1)
module = argv[1]
@ekimekim
ekimekim / xinitrc
Created June 4, 2013 19:30
A mechanism for running commands in a seperate X session. Requires two files - the script to run the command through, and the xinitrc file to run the command after X starts.
# This file should be renamed ~/.xinitrc
# However, CAUTION: Your window manager / desktop environment may already use this file.
# In which case you should PREPEND the file with this file's contents.
# (The same applies if you have a custom xinitrc - just add this file's contents where appropriate.
# For example, in my home .xinitrc, this clause comes after common setup (such as setting the cursor)
# but before specifics for setting up my window manager.)
if [ -n "$XRUN" ]; then
eval $XRUN
exit
@ekimekim
ekimekim / npcombat.py
Created June 11, 2013 17:55
An arbitrary-complexity calculator for neptune's pride battles, including documenting losses of fleets. Currently only has a python interface, and it's horrible. Also included, code for a simple two-way-only calculator.
def combat(*forces):
"""Run a combat between any combination of forces.
Each force represents all involved ships owned by a given player.
The first force should be the defender, with forces then proceeding in order of player index (wrapping around).
Each force has form (player_WS, force_list).
Each force list should be a list of integers, giving the ships of the player and how they're split.
The force list should be the order in which the ships fight.
Note that, in a normal situation, a player's ships orbiting a star fight first,
followed by each fleet they have, from largest to smallest.
@ekimekim
ekimekim / gerrit-fetch-changes
Last active December 31, 2015 21:39
Pulls every change ref from gerrit remote (TODO: configurable remote), and assigns latest patch to refs/changes/CHANGE_ID
#!/bin/env python
import sys
from subprocess import check_call as cmd
from subprocess import check_output as cmd_out
from subprocess import CalledProcessError
out = cmd_out(['git', 'ls-remote', 'origin'])
refs = [line.split("\t") for line in out.strip().split("\n")]
IDENTITY = lambda x: x
class Pool(object):
"""Implements a generic pool of resources.
The resource is an object that:
* is created by the given factory function / class
* is intended to be re-used
* is hashable and unique (ie. multiple calls to factory() won't return the same object)
* optionally, gets prepared for re-use by given clean_fn
@ekimekim
ekimekim / lddcopy
Last active December 4, 2015 06:04
lddcopy - A script to copy all required libraries to a output directory
#!/bin/bash
USAGE="$0 EXECUTABLE PATH
For all shared libraries used by EXECUTABLE, copy that .so file to PATH"
if [ "$#" -ne 2 ]; then
echo "$USAGE" >&2
exit 1
fi
import os
import random
import re
import sys
import argh
ROOT = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), '..'))
import os
import signal
import subprocess
class Timeout(Exception):
"""This is raised when a timeout occurs"""
@ekimekim
ekimekim / run_in_netns.py
Last active January 22, 2018 17:08
run_in_netns.py: A script for running a command in a new network namespace with NATting
import functools
import itertools
import sys
from collections import namedtuple
import argh
from ipaddress import IPv4Network
import easycmd
// Render any jsonnet value (except functions) to TOML format.
local
inlineTable(body) =
"{" +
std.join(", ", ["%s = %s" % [escapeKeyToml(k), renderBody(body[k])] for k in std.objectFields(body)]) +
"}",
renderArray(body) =
local