Added a bunch of small utilities
Getty Ritter
9 years ago
1 | #!/bin/sh | |
2 | ||
3 | LIB_REPO=/home/gdritter/Projects/sites/lib-data /home/gdritter/Projects/sites/lib-static/utils/add-link $@ |
1 | #!/bin/sh | |
2 | ||
3 | LIB_REPO=/home/gdritter/Projects/sites/lib-data /home/gdritter/Projects/sites/lib-static/utils/add-quip $@ |
1 | #!/bin/sh | |
2 | ||
3 | LIB_REPO=/home/gdritter/Projects/sites/lib-data /home/gdritter/Projects/sites/lib-static/utils/add-quote $@ |
1 | #!/usr/bin/python2 | |
2 | ||
3 | import subprocess | |
4 | import sys | |
5 | ||
6 | def invoke(args): | |
7 | sys.stderr.write('invoke("{0}")\n'.format(' '.join(args))) | |
8 | p = subprocess.Popen(args, stdout=subprocess.PIPE) | |
9 | return p.stdout.readlines() | |
10 | ||
11 | disps = set() | |
12 | for x in invoke(['xrandr']): | |
13 | if x and not x[0].isspace(): | |
14 | cdisp = x.split()[0] | |
15 | else: | |
16 | disps.add(cdisp) | |
17 | ||
18 | if disps: | |
19 | sys.stderr.write("connected displays: {0}\n".format(', '.join(disps))) | |
20 | ||
21 | if len(disps) <= 0: | |
22 | sys.stderr.write("no displays connected.\n") | |
23 | sys.exit(1) | |
24 | elif len(disps) == 1: | |
25 | args = ['xrandr', '--auto'] | |
26 | invoke(args) | |
27 | elif len(disps) == 2: | |
28 | other = (disps ^ set(['LVDS1'])).pop() | |
29 | invoke(['xrandr', '--output', 'LVDS1', '--primary', '--left-of', | |
30 | other, '--output', other, '--auto']) | |
31 | else: | |
32 | sys.stderr.write("more than two connected displays;\n") | |
33 | sys.stderr.write("unsure how to continue\n") | |
34 | sys.exit(1) | |
35 | invoke(['/home/gdritter/Pictures/ghibli/rand.sh']) |
1 | #!/bin/python2 | |
2 | ||
3 | import sys | |
4 | ||
5 | nums = [int(l) for l in sys.stdin.readlines()] | |
6 | print sum(nums) / float(len(nums)) |
1 | #!/bin/bash | |
2 | ||
3 | if [ "x$1" = "x--YES" ]; then | |
4 | sudo docker rm $(sudo docker ps -a -q) | |
5 | sudo docker rmi $(sudo docker images -a -q) | |
6 | else | |
7 | echo "ARE YOU SURE YOU WANT TO DO THIS?" | |
8 | echo "If so, re-run with --YES" | |
9 | fi |
1 | # unregister broken GHC packages. Run this a few times to resolve dependency rot in installed packages. | |
2 | # ghc-pkg-clean -f cabal/dev/packages*.conf also works. | |
3 | function ghc-pkg-clean() { | |
4 | for p in `ghc-pkg check $* 2>&1 | grep problems | awk '{print $6}' | sed -e 's/:$//'` | |
5 | do | |
6 | echo unregistering $p; ghc-pkg $* unregister $p | |
7 | done | |
8 | } | |
9 | ||
10 | # remove all installed GHC/cabal packages, leaving ~/.cabal binaries and docs in place. | |
11 | # When all else fails, use this to get out of dependency hell and start over. | |
12 | function ghc-pkg-reset() { | |
13 | read -p 'erasing all your user ghc and cabal packages - are you sure (y/n) ? ' ans | |
14 | test x$ans == xy && ( \ | |
15 | echo 'erasing directories under ~/.ghc'; rm -rf `find ~/.ghc -maxdepth 1 -type d`; \ | |
16 | echo 'erasing ~/.cabal/lib'; rm -rf ~/.cabal/lib; \ | |
17 | # echo 'erasing ~/.cabal/packages'; rm -rf ~/.cabal/packages; \ | |
18 | # echo 'erasing ~/.cabal/share'; rm -rf ~/.cabal/share; \ | |
19 | ) | |
20 | } | |
21 | ||
22 | alias cabalupgrades="cabal list --installed | egrep -iv '(synopsis|homepage|license)'" | |
23 | ||
24 | ghc-pkg-clean |
1 | #!/bin/bash -e | |
2 | ||
3 | # ghc wrapper script (for managing installed GHC versions) | |
4 | # this is a small script I use that allows multiple simultaneous ghc | |
5 | # installations. This makes the following assumptions about how | |
6 | # you want to set up your system: | |
7 | # - GHC version {X} is installed with prefix ~/install/ghc-${X} | |
8 | # - A file naming the current selected GHC version is placed | |
9 | # at ~/.current-ghc | |
10 | # - cabal is configured to point to this script instead of ghc | |
11 | # - If using fetch, then mktemp must result in a directory to which | |
12 | # this script can write, as well | |
13 | # This script is being run as a user that can read and modify things | |
14 | # in your $HOME directory. | |
15 | ||
16 | if [[ "$1" = "list" ]]; then | |
17 | for f in $(ls ~/install/ | grep ghc); do | |
18 | # It's possible (for various reasons) for /none/ of the | |
19 | # current versions to be selected, in which case all of | |
20 | # them will be prefixed by a -; otherwise, the one that | |
21 | # is selected will be prefixed by a +. | |
22 | if [[ "$f" = "$(cat ~/.current-ghc)" ]]; then | |
23 | echo "+" $f | |
24 | else | |
25 | echo "-" $f | |
26 | fi | |
27 | done | |
28 | elif [[ "$1" = "set" ]]; then | |
29 | MATCHES=($(ls ~/install | grep ghc | grep -e "$2")) | |
30 | if [[ -z $MATCHES ]]; then | |
31 | # There is nothing in ~/install that matches that version | |
32 | echo "Unknown GHC version: $2" | |
33 | elif [ "${#MATCHES[@]}" -eq "1" ]; then | |
34 | # There is exactly one relevant match---we can use that one! | |
35 | echo "Setting GHC version to $MATCHES" | |
36 | echo $MATCHES >~/.current-ghc | |
37 | else | |
38 | # The regex given matches too many versions. | |
39 | echo "Ambiguous GHC version: argument matches" | |
40 | for f in "${MATCHES[@]}"; do | |
41 | echo " " $f | |
42 | done | |
43 | fi | |
44 | elif [[ "$1" = "fetch" ]]; then | |
45 | VERSION="$2" | |
46 | URL="https://www.haskell.org/ghc/dist/$VERSION/ghc-$VERSION-src.tar.xz" | |
47 | LOC=$(mktemp -d) | |
48 | ( | |
49 | echo "Attempting to fetch $URL..." && | |
50 | curl -L "$URL" >"$LOC/ghc.tar.xz" && | |
51 | cd "$LOC" && | |
52 | echo "...tarball fetched; unpacking..." && | |
53 | tar -xf "ghc.tar.xz" && | |
54 | cd "$LOC/ghc-$VERSION" && | |
55 | echo "Creating $HOME/install/ghc-$VERSION" && | |
56 | mkdir -p "$HOME/install/ghc-$VERSION" && | |
57 | ./configure --prefix="$HOME/install/ghc-$VERSION" && | |
58 | echo "... starting build process..." && | |
59 | make && | |
60 | make install && | |
61 | echo "...finished!" | |
62 | ) | |
63 | elif [[ "$1" = "--wrapper-help" ]]; then | |
64 | cat <<EOF | |
65 | $0 [wrapper script] | |
66 | Usage: | |
67 | $0 list List installed versions of ghc | |
68 | $0 set [regex] Set current GHC to the version that uniquely | |
69 | matches the supplied regex | |
70 | $0 fetch [version] Fetch, build, and install the specified version | |
71 | of GHC. (Does not change the current version.) | |
72 | $0 --wrapper-help See this help menu | |
73 | $0 [anything else] Use GHC normally | |
74 | ||
75 | This wrapper script assumes that you'll always install your GHC versions | |
76 | to ~/install/ghc-{VERSION}, and will store the currently selected GHC version | |
77 | in ~/.current-ghc. | |
78 | ||
79 | WARNING: This script is quite brittle and makes a lot of assumptions about | |
80 | how you lay things out! While I suspect it shouldn't actively break anything, | |
81 | be careful about using it! | |
82 | EOF | |
83 | else | |
84 | exec /home/gdritter/install/$(cat ~/.current-ghc)/bin/$(basename $0) "$@" | |
85 | fi |
1 | #!/bin/sh -e | |
2 | ||
3 | if [ $# -lt 1 ]; then | |
4 | echo "Usage: $0 package [package ...]" >&2 | |
5 | exit 1 | |
6 | fi | |
7 | ||
8 | DIR=$(mktemp -d) | |
9 | PKG=Temporary | |
10 | ||
11 | touch $DIR/LICENSE | |
12 | ||
13 | cat >$DIR/$PKG.hs <<EOF | |
14 | module $PKG where | |
15 | EOF | |
16 | ||
17 | cat >$DIR/$PKG.cabal <<EOF | |
18 | name: $PKG | |
19 | version: 0.0.0 | |
20 | license: OtherLicense | |
21 | license-file: LICENSE | |
22 | cabal-version: >= 1.10 | |
23 | build-type: Simple | |
24 | ||
25 | library | |
26 | default-language: Haskell2010 | |
27 | exposed-modules: $PKG | |
28 | build-depends: $1, | |
29 | EOF | |
30 | ||
31 | shift | |
32 | for DEP in $@; do | |
33 | cat >>$DIR/$PKG.cabal <<EOF | |
34 | $DEP, | |
35 | EOF | |
36 | done | |
37 | cat >>$DIR/$PKG.cabal <<EOF | |
38 | base | |
39 | EOF | |
40 | ||
41 | cd $DIR | |
42 | cabal sandbox init | |
43 | cabal install | |
44 | cabal configure | |
45 | exec cabal repl |
1 | #!/bin/sh | |
2 | ||
3 | if [ "$#" -lt "2" ]; then | |
4 | APPNAME=$(basename $0) | |
5 | echo "USAGE: $APPNAME [input pdf] [output pdf]" | |
6 | exit 1 | |
7 | fi | |
8 | ||
9 | IN=$1 | |
10 | OUT=$2 | |
11 | ||
12 | echo "Optimizing $IN for screen resolution, and saving to $OUT." | |
13 | ||
14 | gs -o $OUT \ | |
15 | -dNOPAUSE \ | |
16 | -sDEVICE=pdfwrite \ | |
17 | -dCompatibilityLevel=1.4 \ | |
18 | -dPDFSETTINGS=/screen \ | |
19 | $IN |
1 | #!/bin/sh | |
2 | ||
3 | if [ "$#" -lt "2"]; then | |
4 | APPNAME=$(basename $0) | |
5 | echo "USAGE: $APPNAME [input-file]" | |
6 | exit 1 | |
7 | fi | |
8 | ||
9 | if [[ "$1" = http* ]]; then | |
10 | FILE=$(mktemp) | |
11 | curl $1 >$FILE | |
12 | else | |
13 | FILE="$1" | |
14 | fi | |
15 | ||
16 | if [[ $FILE = *pdf ]]; then | |
17 | TMPF=$(mktemp) | |
18 | pdf2ps $FILE $TMPF | |
19 | ||
20 | elif [[ $FILE = *ps ]]; then | |
21 | TMPF=$FILE | |
22 | else | |
23 | echo "Unknown file type: $FILE" | |
24 | exit 2 | |
25 | fi | |
26 | ||
27 | cat $TMPF | psbook | psnup -pletter -2 | lpr -o sides=two-sided-short-edge -P hp4200n |
1 | #!/bin/sh -e | |
2 | ||
3 | if [ "$#" -lt "1" ]; then | |
4 | APPNAME=$(basename $0) | |
5 | echo "not enough arguments to $APPNAME" | |
6 | exit 1 | |
7 | fi | |
8 | ||
9 | OPTS="-P hp4200n -o number-up=2 -o sides=two-sided-long-edge" | |
10 | ||
11 | if [[ "$1" = http* ]]; then | |
12 | FILE=$(mktemp) | |
13 | curl "$1" >$FILE | |
14 | lpr $OPTS $FILE | |
15 | rm $FILE | |
16 | else | |
17 | lpr $OPTS "$1" | |
18 | fi |
1 | #!/bin/sh | |
2 | ||
3 | RES="xrandr | grep '\*' | grep -o '[0-9]*x[0-9]'*" | |
4 | rdesktop -g $RES porthole.galois.com |
1 | #!/usr/bin/python | |
2 | ||
3 | import sys | |
4 | import yaml | |
5 | ||
6 | def comma(x, y): | |
7 | if x: | |
8 | return '{0},{1}'.format(x, y) | |
9 | else: | |
10 | return str(y) | |
11 | ||
12 | def print_obj(obj, lead=''): | |
13 | if type(obj) == dict: | |
14 | for k in obj: | |
15 | print_obj(obj[k], comma(lead, k)) | |
16 | else: | |
17 | print(comma(lead, obj)) | |
18 | ||
19 | with open(sys.argv[1]) as f: | |
20 | print_obj(yaml.load(f)) |