Skip to content

Instantly share code, notes, and snippets.

View chipaca's full-sized avatar
🌼

John Lenton chipaca

🌼
View GitHub Profile
@chipaca
chipaca / squash-bench.sh
Created June 14, 2019 10:28
Benchmark different squashfs options for different snaps
#!/bin/sh
set -e
free_caches() {
sudo sync
printf "Freeing caches: "
for i in 1 2 3; do
sudo sysctl -q vm.drop_caches="$i"
done
#!/usr/bin/python3
import json
import os
import sys
import time
from urllib.parse import urlencode
from urllib.request import urlopen
@chipaca
chipaca / delog.py
Last active April 30, 2019 13:25
Turn snapd HTTP logs into something more human-friendly
#!/usr/bin/python3
# TODO: multiline continuations
import argparse
import json
import os
import re
import signal
import sys
@chipaca
chipaca / gh.go
Last active January 28, 2020 10:05
Get summary of a repo's open pull requests reviews
package main
import (
"context"
"flag"
"fmt"
"math"
"net/http"
"os"
"strconv"
package main
import (
"github.com/coreos/go-systemd/activation"
"context"
"fmt"
"net/http"
"os"
"sync/atomic"
@chipaca
chipaca / README.md
Last active August 3, 2018 05:49
Unmarshaling a single key from a dict

In Go, what's the best way of loading the value of a single entry of a json object that doesn't involve writing your own json parser?

So far, the best way I've found involvs reflect: you build a struct with a single field tagged with the entry you're looking for. Needs 1.7+. If you can't use that (and that's a shame as it's the only one that isn't O(N) memory), stepping through the tokens seems to be the next best bet.

$ /snap/bin/go test . -bench=. -benchmem -v
goos: linux
goarch: amd64
Benchmark______0_Full-8      	  300000	      4078 ns/op	  21.09 MB/s	    1408 B/op	      40 allocs/op
Benchmark_____10_Full-8      	   50000	     40087 ns/op	  22.90 MB/s	    9989 B/op	     371 allocs/op
@chipaca
chipaca / round.go
Last active February 2, 2018 17:48
A round robin writer
// (c) 2018 John Lenton
// MIT licensed but I'm lazy
// Package round implements round.Robin, which is an in-memory io.Writer
// (like bytes.Buffer) that uses a fixed amount of memory.
//
// It's a silly toy thing, really; otherwise I'd've made the buffer size
// settable without editing the source. But it's useful to me...
// Enjoy!
package round
@chipaca
chipaca / Makefile
Last active March 14, 2017 17:27
syscall.Exec losing setuid
ALL=a_c a_p a_go b
all: $(ALL)
CFLAGS=-Wall
a_p : LDFLAGS=-pthread
%: %.go
go build -o $@ $<
# on the device:
# * get the resolution
SIZE=$(fbset|sed -n -e's/^mode.*\"\([0-9]\+x[0-9]\+\)[-\"].*$/\1/p')
# * record 300 frames at 1/4 framerate, output to home (output is raw => HUGE file)
mirscreencast -n 300 --cap-interval 4 -f /home/phablet/mir_screencast_$(date -Iseconds)_${SIZE}.rgba
#
# 'adb pull' or scp the raw video over. Slow, but quicker than encoding on the device.
# and then, if F is the filename, S is the 1/scale wanted for the gif, and W and H are the width and height of the device, then
mplayer -demuxer rawvideo -rawvideo fps=60:w=${W}:h=${H}:format=rgba "$F" -vf scale=$((W/S)):$((H/S)) -vo "png:z=9:outdir="${F}.d":prefix=${F}_"
# that'll give you a bunch of PNGs; use gifscicle or whatever to turn them into the final gif you want.