Skip to content

Instantly share code, notes, and snippets.

View Bachsau's full-sized avatar

Bachsau

View GitHub Profile
@Bachsau
Bachsau / virtual_oserror.py
Last active May 27, 2020 01:02
A custom exception class that closely resembles Python’s OSError
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# A custom exception class that closely resembles the behavior of
# Python’s built-in OSError, including arguments and automatic
# subclassing. Useful if a fine-grained error handling based on
# error numbers is needed.
class VirtualOSError(Exception):
"""Base exception for all virtual OS related errors.
@Bachsau
Bachsau / stop_dangerous_names.php
Last active August 27, 2020 19:37
Stop PHP’s dangerous name handling
<?php
// Stop PHP’s dangerous name handling
function undefined_name_error($errno, $errstr, $errfile='', $errline=0, $errcontext=NULL) {
if (stripos($errstr, 'Undefined') !== false) {
error_log("PHP Fatal error: $errstr in $errfile on line $errline");
exit(255);
}
else {
return false;
@Bachsau
Bachsau / pythonrc.py
Created July 19, 2020 16:28
~/.pythonrc to make `exit` work without parentheses
# ~/.pythonrc
# -*- coding: utf-8 -*-
# Make `exit` work without parentheses
import sys
if "site" in sys.modules:
Quitter = type(exit)
Quitter.__str__ = Quitter.__repr__
Quitter.__repr__ = Quitter.__call__
del Quitter
@Bachsau
Bachsau / backup-history.sh
Last active February 15, 2023 14:26
Bash history backup script
#!/bin/bash --login
# Make a backup of the user's Bash history
if [ -z "${HISTFILE}" ]; then
cd "${HOME}"
hfile='.bash_history'
else
cd "${HISTFILE%/*}"
hfile="${HISTFILE##*/}"
fi
@Bachsau
Bachsau / update-certbot
Last active February 19, 2023 03:13
Single command to update a Certbot installed via pip
#!/bin/sh -eu
python3 -m venv --upgrade /opt/certbot
python3 -m venv --upgrade-deps /opt/certbot
/opt/certbot/bin/pip install --upgrade certbot certbot-dns-standalone
@Bachsau
Bachsau / update-mysql-timezones
Last active February 19, 2023 03:14
Single command to import system timezones into MySQL
#!/bin/sh -eu
if [ $(id -u) -ne 0 ]; then
echo 'Only root can do this' >&2
exit 1
fi
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql mysql
mysql_tzinfo_to_sql --leap /etc/localtime | mysql mysql
systemctl restart mysql.service
@Bachsau
Bachsau / dwarf-fortress.svg
Last active February 26, 2023 01:22
Handmade vector icon for Dwarf Fortress
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@Bachsau
Bachsau / configuration_manager.py
Last active March 2, 2023 15:29
A simple abstraction of Python’s ConfigParser
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# A simple abstraction of Python’s ConfigParser.
# It features implicit type conversion and defaults through prior
# registration of settings. It can be used to save and read settings
# without bothering about the specifics of ConfigParser or the INI files
# themselves. It could also serve as a starting point to abstract
# platform-specific saving methods through its general API.
@Bachsau
Bachsau / post:10etckeeper.sh
Created February 28, 2023 21:15
Certbot hooks for etckeeper; place them in /etc/letsencrypt/renewal-hooks/(pre|post)/
#!/bin/sh -eu
# Run etckeeper after certificate renewal (by Bachsau)
if command -v etckeeper >/dev/null && etckeeper unclean; then
etckeeper commit "committing updates to certificate configuration files after renewal"
fi
@Bachsau
Bachsau / deploy.cgi
Last active March 13, 2023 10:57
The simplest of Git deployment scripts: Just ping it!
#!/bin/sh -eu
while read -r discard; do :; done; unset -v discard
echo 'Content-Type: text/plain; charset=UTF-8'
echo 'Cache-Control: no-store, max-age=0'
echo
git fetch -q github main
git reset --hard github/main
git clean -d -e '/deploy.cgi' -x