Skip to content

Instantly share code, notes, and snippets.

@gesquive
gesquive / self-update-script.py
Last active February 18, 2024 20:36
Stick this in a python script to self update the script from an online source
def update(dl_url, force_update=False):
"""
Attempts to download the update url in order to find if an update is needed.
If an update is needed, the current script is backed up and the update is
saved in its place.
"""
import urllib
import re
from subprocess import call
def compare_versions(vA, vB):
@gesquive
gesquive / print-program-options.cc
Last active June 12, 2022 11:55
Methods to print out boost::program_options objects
#include <string.h>
#include <iostream>
#include "boost/program_options.hpp"
#include "boost/filesystem.hpp"
#include "boost/any.hpp"
namespace po = boost::program_options;
inline void PrintUsage(const boost::program_options::options_description desc) {
std::cout << "Usage: " << app_name << " [options]" << std::endl;
@gesquive
gesquive / python-logger-snippet.py
Last active March 26, 2022 23:30
Code snippet to setup logger in python
import logging
import logging.handlers
LOG_FILE = 'path/to/logfile'
LOG_SIZE = 1024*1024*200
LOG_COUNT = 9
log_file = LOG_FILE
dir_path = os.path.dirname(log_file)
if os.access(dir_path, os.W_OK):
## Configure eth0
# vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE="eth0"
NM_CONTROLLED="yes"
ONBOOT=yes
HWADDR=A4:BA:DB:37:F1:04
TYPE=Ethernet
BOOTPROTO=static
NAME="System eth0"
@gesquive
gesquive / python-git-get.py
Last active August 29, 2015 14:06
Method to dl git repos or individual files
from sh import ErrorReturnCode, git
from cStringIO import StringIO
import tarfile
def git_get(git_repo, git_branch, git_path, destination_path=".", use_compression=True, verbose=False):
git_dir = os.path.dirname(git_path)
git_file = os.path.basename(git_path)
if git_dir.replace("/", "") == git_file:
git_file = ''
if git_dir.replace("/","").strip() == "":
git_loc = git_branch
@gesquive
gesquive / python-less-pager.py
Last active January 17, 2021 19:53
Use less to page output
import subprocess
import sys
import os
def output_to_pager(text):
try:
# args stolen fron git source, see `man less`
pager = subprocess.Popen(['less', '-F', '-R', '-S', '-X', '-K'],
stdin=subprocess.PIPE,
stdout=sys.stdout)
@gesquive
gesquive / python-config-args-parser.py
Last active August 29, 2015 14:14
Code snippet to parse arguments from a config file
import argparse
import ConfigParser
import sys
def main(argv=None):
# Do argv default this way, as doing it in the functional
# declaration sets it at compile time.
if argv is None:
argv = sys.argv[1]:
@gesquive
gesquive / python-get-config-path.py
Last active August 29, 2015 14:14
Code snippet to get the config location based on the XDG Base Directory Specification.
import os
def get_config_path(project_name=None, config_name=None):
'''
Gets the config location based on the XDG Base Directory Specification.
If no config is found, the home directory based path is returned.
'''
if not project_name:
project_name = os.path.basename(__file__).split('.')[0]
if not config_name:
@gesquive
gesquive / what-is-my-ip.json
Created March 22, 2015 20:05
Text based external IP locators
[
"http://whatismyip.akamai.com/",
"http://icanhazip.com",
"http://wtfismyip.com/text",
"http://whatsmyip.me/",
"http://api.ipify.org/",
"http://ip.catnapgames.com",
"http://ip.ryansanden.com",
]
#!/bin/sh
# Converts a mysqldump file into a Sqlite 3 compatible file. It also extracts the MySQL `KEY xxxxx` from the
# CREATE block and create them in separate commands _after_ all the INSERTs.
# Awk is choosen because it's fast and portable. You can use gawk, original awk or even the lightning fast mawk.
# The mysqldump file is traversed only once.
# Usage: $ ./mysql2sqlite mysqldump-opts db-name | sqlite3 database.sqlite
# Example: $ ./mysql2sqlite --no-data -u root -pMySecretPassWord myDbase | sqlite3 database.sqlite