Skip to content

Instantly share code, notes, and snippets.

View kotnik's full-sized avatar

Nikola Kotur kotnik

View GitHub Profile
@kotnik
kotnik / gist:6569574
Created September 15, 2013 10:36
Proper way to consistently read `/proc/mounts`.
import re
import collections
def get_current_mounts():
""" Get the currently mounted filesystems. """
# Reading from /proc/mounts is only consistent within a single read(3)
# call. Unfortunately, the maximum amount of data you can get from
# this file in a single call is hardcoded by the kernel to PAGE_SIZE
# (4096 bytes on x86). As a consequence, there is no way to read
@kotnik
kotnik / gist:6561982
Last active December 23, 2015 01:39
Validate schema with Colander for either uuid or mail are in the request.
@deferred
def deferred_check_missing(node, kw):
try:
json_request = kw.get("request").json_body
except ValueError:
raise Invalid(node, "Malformed JSON provided")
if "mail" not in json_request and "uuid" not in json_request:
raise Invalid(node, "Both mail and uuid are missing")
@kotnik
kotnik / gist:6204246
Created August 11, 2013 09:55
Honeypot trap to add in *_form_alter (Drupal 7).
// Add Honeypot protection.
if (module_exists('honeypot')) {
honeypot_add_form_protection($form, $form_state, array('honeypot', 'time_restriction'));
}
@kotnik
kotnik / gist:6191642
Created August 9, 2013 06:48
Simple sample Vagrantfile.
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
# Every Vagrant virtual environment requires a box to build off of.
config.vm.box = "precise64"
# The url from where the 'config.vm.box' box will be fetched if it
# doesn't already exist on the user's system.
config.vm.box_url = "http://files.vagrantup.com/precise64.box"
@kotnik
kotnik / fabfile.py
Created July 19, 2013 13:40 — forked from joshkehn/fabfile.py
Update all packages with Fabric calling pip.
from fabric.api import local
import pip
def freeze ():
local("pip freeze > requirements.txt")
local("git add requirements.txt")
local("git commit -v")
def upgrade ():
for dist in pip.get_installed_distributions():
@kotnik
kotnik / transitive.php
Created July 3, 2013 08:47
Beware of ==
<?php
// == is not transitive
if (0 == FALSE) {
echo "0 == FALSE\n";
}
if ("string" == TRUE) {
echo "'string' is TRUE\n";
@kotnik
kotnik / small-dns-server.py
Created June 6, 2013 14:51
Example of how to create a DNS server in Python, based on Twisted libraries.
from collections import Mapping
from twisted.names import dns, server, client, cache
from twisted.application import service, internet
class MapResolver(client.Resolver):
def __init__(self, mapping, servers):
client.Resolver.__init__(self, servers=servers)
@kotnik
kotnik / json-me.py
Last active December 16, 2015 07:09
Flask app that always return the same JSON, regardless of the path.
import json
from flask import Flask, Response
app = Flask(__name__)
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def catch_all(path):
ret = {
"something": 1,
@kotnik
kotnik / git-commit.py
Last active December 16, 2015 02:39
Parse git commit in Python.
def parse_commit(commit):
r = {}
info, diffstat = commit.split("ENDOFOUTPUTGITMESSAGEHERE")
info = info.strip()
lines = info.split('\n')
r["sha1"] = lines[0]
r["parent"] = lines[1]
r["author"] = lines[2]
r["commiter"] = lines[3]
r["timestamp"] = lines[4]
@kotnik
kotnik / pastebin.sh
Created April 8, 2013 14:49
Self-hosted pastebin. Pipe code/text to be automatically uploaded to your server for sharing.
#!/bin/bash
USER=YOUR_USERNAME
HOST=YOUR_SERVER_HOSTNAME
WEBPATH=PATH_TO_WEBSERVER_ROOT
WEBURL=YOUR_URL
BACKUP_DIR=DIR_FOR_BACKUP
file=$(mktemp $BACKUP_DIR/XXXXXX)
mv $file $file.html