Skip to content

Instantly share code, notes, and snippets.

@kalebo
kalebo / matchit.h
Last active February 28, 2022 17:34
matchit tweak test for msvc<=19.28
/*
* Copyright (c) 2021 Bowen Fu
* Distributed Under The Apache-2.0 License
*/
#ifndef MATCHIT_H
#define MATCHIT_H
#ifndef MATCHIT_CORE_H
#define MATCHIT_CORE_H
@kalebo
kalebo / cl_include_tree_search.py
Created May 3, 2021 20:59
Finds the include path for probematic files in a DAG from CL output
import sys
from pathlib import Path
from functools import partial, reduce
from pprint import pprint
eval_path = partial(reduce, lambda d, k: d[k])
def extract_includes(output_path):
results = []
with open(output_path, 'r', encoding='utf-16') as f:
@kalebo
kalebo / adduser_from_gitlab.sh
Last active September 4, 2020 14:36
Automates the first few steps of creating a user and poplating it with an allowed ssh key from gitlab
NEWUSER=$1
USERFULLNAME=$2
PASS=$(diceware)
adduser --disabled-password --gecos "$USERFULLNAME,,," $NEWUSER
echo "$NEWUSER:$PASS" | chpasswd
sudo -u $NEWUSER -s <<USERSCRIPT
mkdir -p ~/.ssh
umask 177
@kalebo
kalebo / active_wg_users.py
Created September 2, 2020 20:58
A simple script that will map wireguard peers to usernames. Assmues that script lives in a directory where subdirectories named after the user contain the users publickey file
#!/usr/bin/env python3
from subprocess import run
from pathlib import Path as path
from functools import lru_cache
def wg_show(interface, field):
res = run(["wg", "show", interface, field], check=True, text=True, capture_output=True)
return map(lambda l: l.split(maxsplit=1), res.stdout.strip().split('\n'))
@kalebo
kalebo / subprocess_tee.py
Last active March 13, 2024 20:53
Emulate tee with python's async subprocess (>= 3.6)
class RunOutput():
def __init__(self, returncode, stdout, stderr):
self.returncode = returncode
self.stdout = stdout
self.stderr = stderr
# the following three commands are adapted from https://kevinmccarthy.org/2016/07/25/streaming-subprocess-stdin-and-stdout-with-asyncio-in-python/
async def _read_stream(stream, callback):
while True:
line = await stream.readline()
@kalebo
kalebo / result.py
Created November 20, 2019 21:19
Result monad for Python
class Result(metaclass=ABCMeta): # abstract base class
'''A result monad whose interface is patterned after that of Rust's std::Result<T, E> '''
def is_ok(self) -> bool: return NotImplemented
def unwrap_or_else(self, func): return NotImplemented
def unwrap_or(self, val): return NotImplemented
def then(self, f): return NotImplemented # techincally this breaks the monad rules because f might be a functor (A->Result(B)) or it could be just a function (A->B)
class Error(Result):
def __init__(self, err):
self._err = err
@kalebo
kalebo / gestationlength_vs_bodymass
Created March 13, 2019 19:30
A vega plot of the correlation between gestation length and body mass of many species
{
"$schema": "https://vega.github.io/schema/vega/v3.json",
"autosize": "pad",
"padding": 5,
"width": 500,
"height": 400,
"style": "cell",
"data": [
{"name": "selector091_store"},
{
# Make arrays for sets of machines so you don't have to remember the name
$S415_all = @(
"ESC-1C58BM1",
"ESC-CBX7JN1",
"ESC-2958BM1",
"ESC-B858BM1",
"ESC-5858BM1",
"ESC-6B58BM1",
"ESC-2C58BM1",
"ESC-F2BVLL1",
@kalebo
kalebo / selinux_related_commands
Last active October 17, 2017 20:52
Useful selinux commands
@kalebo
kalebo / Instructions.md
Created October 4, 2017 17:35
Instructions on setting up a FlexLM daemon for SystemD

Setting up a FlexLM service for SystemD

Create directory structure

  1. Create the directory for the flexlm user to use, e.g., /opt/flexlm
  2. Place all the binaries for the licence manager and the licence in a vendor specific subdirectory

At this point you should have something similar to following directory structure:

/opt/flexlm/
└── VENDOR