Browse Source

Added a bunch of small utilities

Getty Ritter 9 years ago
parent
commit
5812ac2536

+ 3 - 0
scripts/add-link

@@ -0,0 +1,3 @@
+#!/bin/sh
+
+LIB_REPO=/home/gdritter/Projects/sites/lib-data /home/gdritter/Projects/sites/lib-static/utils/add-link $@

+ 3 - 0
scripts/add-quip

@@ -0,0 +1,3 @@
+#!/bin/sh
+
+LIB_REPO=/home/gdritter/Projects/sites/lib-data /home/gdritter/Projects/sites/lib-static/utils/add-quip $@

+ 3 - 0
scripts/add-quote

@@ -0,0 +1,3 @@
+#!/bin/sh
+
+LIB_REPO=/home/gdritter/Projects/sites/lib-data /home/gdritter/Projects/sites/lib-static/utils/add-quote $@

+ 35 - 0
scripts/adjust-screens

@@ -0,0 +1,35 @@
+#!/usr/bin/python2
+
+import subprocess
+import sys
+
+def invoke(args):
+    sys.stderr.write('invoke("{0}")\n'.format(' '.join(args)))
+    p = subprocess.Popen(args, stdout=subprocess.PIPE)
+    return p.stdout.readlines()
+
+disps = set()
+for x in invoke(['xrandr']):
+    if x and not x[0].isspace():
+        cdisp = x.split()[0]
+    else:
+        disps.add(cdisp)
+
+if disps:
+    sys.stderr.write("connected displays: {0}\n".format(', '.join(disps)))
+
+if len(disps) <= 0:
+    sys.stderr.write("no displays connected.\n")
+    sys.exit(1)
+elif len(disps) == 1:
+    args = ['xrandr', '--auto']
+    invoke(args)
+elif len(disps) == 2:
+    other = (disps ^ set(['LVDS1'])).pop()
+    invoke(['xrandr', '--output', 'LVDS1', '--primary', '--left-of',
+            other, '--output', other, '--auto'])
+else:
+    sys.stderr.write("more than two connected displays;\n")
+    sys.stderr.write("unsure how to continue\n")
+    sys.exit(1)
+invoke(['/home/gdritter/Pictures/ghibli/rand.sh'])

+ 6 - 0
scripts/avg

@@ -0,0 +1,6 @@
+#!/bin/python2
+
+import sys
+
+nums = [int(l) for l in sys.stdin.readlines()]
+print sum(nums) / float(len(nums))

+ 9 - 0
scripts/docker-delete-images

@@ -0,0 +1,9 @@
+#!/bin/bash
+
+if [ "x$1" = "x--YES" ]; then
+  sudo docker rm $(sudo docker ps -a -q)
+  sudo docker rmi $(sudo docker images -a -q)
+else
+  echo "ARE YOU SURE YOU WANT TO DO THIS?"
+  echo "If so, re-run with --YES"
+fi

+ 3 - 0
scripts/gdr-img

@@ -0,0 +1,3 @@
+#!/bin/sh
+
+scp $1 rosencrantz:/srv/http/gdr/www/imgs/.

+ 24 - 0
scripts/ghc-clean-pkgs

@@ -0,0 +1,24 @@
+# unregister broken GHC packages. Run this a few times to resolve dependency rot in installed packages.                                                                                                                                     
+# ghc-pkg-clean -f cabal/dev/packages*.conf also works.                                                                                                                                                                                     
+function ghc-pkg-clean() {
+    for p in `ghc-pkg check $* 2>&1  | grep problems | awk '{print $6}' | sed -e 's/:$//'`
+    do
+        echo unregistering $p; ghc-pkg $* unregister $p
+    done
+}
+
+# remove all installed GHC/cabal packages, leaving ~/.cabal binaries and docs in place.                                                                                                                                                     
+# When all else fails, use this to get out of dependency hell and start over.                                                                                                                                                               
+function ghc-pkg-reset() {
+    read -p 'erasing all your user ghc and cabal packages - are you sure (y/n) ? ' ans
+    test x$ans == xy && ( \
+        echo 'erasing directories under ~/.ghc'; rm -rf `find ~/.ghc -maxdepth 1 -type d`; \
+        echo 'erasing ~/.cabal/lib'; rm -rf ~/.cabal/lib; \
+        # echo 'erasing ~/.cabal/packages'; rm -rf ~/.cabal/packages; \                                                                                                                                                                     
+        # echo 'erasing ~/.cabal/share'; rm -rf ~/.cabal/share; \                                                                                                                                                                           
+        )
+}
+
+alias cabalupgrades="cabal list --installed  | egrep -iv '(synopsis|homepage|license)'"
+
+ghc-pkg-clean

+ 85 - 0
scripts/ghc-wrapper

@@ -0,0 +1,85 @@
+#!/bin/bash -e
+
+# ghc wrapper script (for managing installed GHC versions)
+# this is a small script I use that allows multiple simultaneous ghc
+# installations. This makes the following assumptions about how
+# you want to set up your system:
+# - GHC version {X} is installed with prefix ~/install/ghc-${X}
+# - A file naming the current selected GHC version is placed
+#   at ~/.current-ghc
+# - cabal is configured to point to this script instead of ghc
+# - If using fetch, then mktemp must result in a directory to which
+#   this script can write, as well
+# This script is being run as a user that can read and modify things
+# in your $HOME directory.
+
+if [[ "$1" = "list" ]]; then
+    for f in $(ls ~/install/ | grep ghc); do
+        # It's possible (for various reasons) for /none/ of the
+        # current versions to be selected, in which case all of
+        # them will be prefixed by a -; otherwise, the one that
+        # is selected will be prefixed by a +.
+        if [[ "$f" = "$(cat ~/.current-ghc)" ]]; then
+            echo "+" $f
+        else
+            echo "-" $f
+        fi
+    done
+elif [[ "$1" = "set" ]]; then
+    MATCHES=($(ls ~/install | grep ghc | grep -e "$2"))
+    if [[ -z $MATCHES ]]; then
+        # There is nothing in ~/install that matches that version
+        echo "Unknown GHC version: $2"
+    elif [ "${#MATCHES[@]}" -eq "1" ]; then
+        # There is exactly one relevant match---we can use that one!
+        echo "Setting GHC version to $MATCHES"
+        echo $MATCHES >~/.current-ghc
+    else
+        # The regex given matches too many versions.
+        echo "Ambiguous GHC version: argument matches"
+        for f in "${MATCHES[@]}"; do
+            echo "  " $f
+        done
+    fi
+elif [[ "$1" = "fetch" ]]; then
+    VERSION="$2"
+    URL="https://www.haskell.org/ghc/dist/$VERSION/ghc-$VERSION-src.tar.xz"
+    LOC=$(mktemp -d)
+    (
+        echo "Attempting to fetch $URL..." &&
+        curl -L "$URL" >"$LOC/ghc.tar.xz" &&
+        cd "$LOC" &&
+        echo "...tarball fetched; unpacking..." &&
+        tar -xf "ghc.tar.xz" &&
+        cd "$LOC/ghc-$VERSION" &&
+        echo "Creating $HOME/install/ghc-$VERSION" &&
+        mkdir -p "$HOME/install/ghc-$VERSION" &&
+        ./configure --prefix="$HOME/install/ghc-$VERSION" &&
+        echo "... starting build process..." &&
+        make &&
+        make install &&
+        echo "...finished!"
+    )
+elif [[ "$1" = "--wrapper-help" ]]; then
+    cat <<EOF
+$0 [wrapper script]
+Usage:
+  $0 list              List installed versions of ghc
+  $0 set [regex]       Set current GHC to the version that uniquely
+                          matches the supplied regex
+  $0 fetch [version]   Fetch, build, and install the specified version
+                          of GHC. (Does not change the current version.)
+  $0 --wrapper-help    See this help menu
+  $0 [anything else]   Use GHC normally
+
+This wrapper script assumes that you'll always install your GHC versions
+to ~/install/ghc-{VERSION}, and will store the currently selected GHC version
+in ~/.current-ghc.
+
+WARNING: This script is quite brittle and makes a lot of assumptions about
+how you lay things out! While I suspect it shouldn't actively break anything,
+be careful about using it!
+EOF
+else
+   exec /home/gdritter/install/$(cat ~/.current-ghc)/bin/$(basename $0) "$@"
+fi

+ 45 - 0
scripts/ghci-with

@@ -0,0 +1,45 @@
+#!/bin/sh -e
+
+if [ $# -lt 1 ]; then
+    echo "Usage: $0 package [package ...]" >&2
+    exit 1
+fi
+
+DIR=$(mktemp -d)
+PKG=Temporary
+
+touch $DIR/LICENSE
+
+cat >$DIR/$PKG.hs <<EOF
+module $PKG where
+EOF
+
+cat >$DIR/$PKG.cabal <<EOF
+name:           $PKG
+version:        0.0.0
+license:        OtherLicense
+license-file:   LICENSE
+cabal-version:  >= 1.10
+build-type:     Simple
+
+library
+  default-language: Haskell2010
+  exposed-modules: $PKG
+  build-depends: $1,
+EOF
+
+shift
+for DEP in $@; do
+    cat >>$DIR/$PKG.cabal <<EOF
+                 $DEP,
+EOF
+done
+cat >>$DIR/$PKG.cabal <<EOF
+                 base
+EOF
+
+cd $DIR
+cabal sandbox init
+cabal install
+cabal configure
+exec cabal repl

+ 19 - 0
scripts/minimize-pdf-file-size

@@ -0,0 +1,19 @@
+#!/bin/sh
+
+if [ "$#" -lt "2" ]; then
+	APPNAME=$(basename $0)
+	echo "USAGE: $APPNAME [input pdf] [output pdf]"
+	exit 1
+fi
+
+IN=$1
+OUT=$2
+
+echo "Optimizing $IN for screen resolution, and saving to $OUT."
+
+gs -o $OUT                      \
+   -dNOPAUSE                    \
+   -sDEVICE=pdfwrite            \
+   -dCompatibilityLevel=1.4     \
+   -dPDFSETTINGS=/screen        \
+   $IN

+ 27 - 0
scripts/pr-pamphlet

@@ -0,0 +1,27 @@
+#!/bin/sh
+
+if [ "$#" -lt "2"]; then
+	APPNAME=$(basename $0)
+	echo "USAGE: $APPNAME [input-file]"
+	exit 1
+fi
+
+if [[ "$1" = http* ]]; then
+	FILE=$(mktemp)
+	curl $1 >$FILE
+else
+	FILE="$1"
+fi
+
+if [[ $FILE = *pdf ]]; then
+	TMPF=$(mktemp)
+	pdf2ps $FILE $TMPF
+	
+elif [[ $FILE = *ps ]]; then
+	TMPF=$FILE
+else
+	echo "Unknown file type: $FILE"
+	exit 2
+fi
+
+cat $TMPF | psbook | psnup -pletter -2 | lpr -o sides=two-sided-short-edge -P hp4200n

+ 18 - 0
scripts/pr-paper

@@ -0,0 +1,18 @@
+#!/bin/sh -e
+
+if [ "$#" -lt "1" ]; then
+	APPNAME=$(basename $0)
+	echo "not enough arguments to $APPNAME"
+	exit 1
+fi
+
+OPTS="-P hp4200n -o number-up=2 -o sides=two-sided-long-edge"
+
+if [[ "$1" = http* ]]; then
+	FILE=$(mktemp)
+	curl "$1" >$FILE
+	lpr $OPTS $FILE
+    rm $FILE
+else
+	lpr $OPTS "$1"
+fi

+ 4 - 0
scripts/twit

@@ -0,0 +1,4 @@
+#!/bin/sh
+
+THEME=solarized-dark TWIT=true BARE=true emacs
+

+ 4 - 0
scripts/windows

@@ -0,0 +1,4 @@
+#!/bin/sh
+
+RES="xrandr | grep '\*' | grep -o '[0-9]*x[0-9]'*"
+rdesktop -g $RES porthole.galois.com

+ 20 - 0
scripts/yaml2csv

@@ -0,0 +1,20 @@
+#!/usr/bin/python
+
+import sys
+import yaml
+
+def comma(x, y):
+  if x:
+    return '{0},{1}'.format(x, y)
+  else:
+    return str(y)
+
+def print_obj(obj, lead=''):
+  if type(obj) == dict:
+    for k in obj:
+      print_obj(obj[k], comma(lead, k))
+  else:
+    print(comma(lead, obj))
+
+with open(sys.argv[1]) as f:
+  print_obj(yaml.load(f))