Skip to content

Instantly share code, notes, and snippets.

@temoto
temoto / mysql-convert-myisam-to-innodb.sh
Last active March 15, 2016 10:57
MySQL administration: convert MyISAM tables to InnoDB
#!/bin/bash
sql_list="select table_schema, table_name from information_schema.tables where lower(engine) = 'myisam' and table_schema not in ('mysql', 'information_schema', 'performance_schema') order by data_length;"
mysql --batch --silent -e "$sql_list" |while read -r d t; do
printf "\n---\n$d.$t "
mysql --batch --silent -e "select (data_length+index_length)/1e6 from information_schema.tables where table_schema='$d' and table_name='$t';"
time mysql --batch --silent -e "alter table $d.$t engine='innodb';"
done
@temoto
temoto / megafon-account.bash
Last active March 3, 2016 03:33
Экспорт балансов Мегафона. Работает с TLSv1 уральского региона (информация актуальна 2016-03-03).
#!/bin/bash
# https://gist.github.com/temoto/efc4cf36fe388b41715b
set -e
umask 0077
cookie_path='/tmp/megafon-jar'
output_path=~/megafon-account.csv
username='EDIT'
password='EDIT'
url_base='https://EDIT'
url_path_init='/sc_cp_apps/login'
@temoto
temoto / asset-clean
Created February 18, 2016 10:21
[website deploy] Find references to missing assets files. Helps to prevent 404 and 500 (pagespeed beacon) errors.
#!/bin/bash
set -e
find . -print0 |fgrep -v --null-data --null '/.hg/' >tmp-asset-clean-all
egrep -hIor "/\w+/\w+\.(gif|jpg|jpeg|png|eot|svg|ttf|woff|webm|webp)" |sort -u >tmp-asset-clean-refs
for f in $(cat tmp-asset-clean-refs); do
dir=${f%%/*}
name=${f##*/}
if ! egrep --max-count=1 --null-data -q $f tmp-asset-clean-all; then
echo -e "\n\n$f not found. References:\n $(egrep -Inr $f)\n\n"
fi
@temoto
temoto / ring.c
Created January 28, 2016 08:14
Ring buffer in C, push to tail, pop from head, single size define <=256
#include <stdbool.h> // bool, required
#include <stdio.h> // printf, only for debug
#include <stdlib.h> // uint8_t, required
#include <string.h> // memset, required
#define RING_BUFFER_SIZE 4
#if RING_BUFFER_SIZE > 256
#error "RING_BUFFER_SIZE must be <=256"
#endif
import multiprocessing as mp
import sys
import resource
def memory():
self = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
children = resource.getrusage(resource.RUSAGE_CHILDREN).ru_maxrss
return self + children
@temoto
temoto / Dockerfile
Last active June 20, 2023 08:34
File layout template for project with multiple docker containers in single repo
# I will eat you family and your dog if you use FROM ubuntu for container that runs a service in production
# Reading: http://phusion.github.io/baseimage-docker/
FROM phusion/baseimage:0.9.15
# inspired by https://github.com/progrium/buildstep
RUN mkdir /build
ADD ./files-build/ /build/
RUN chmod --recursive go-rwx /build
RUN LC_ALL=C DEBIAN_FRONTEND=noninteractive /bin/bash /build/prepare
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
@temoto
temoto / shingle-compare.py
Created November 27, 2014 17:24
Shingle based fuzzy text comparison
def shinglize(s):
return frozenset(s[i:i + 3] for i in xrange(len(s) - 2))
def shingle_compare(sh1, string):
'''set(shingle), 'string' -> float 0..1
'''
sh2 = shinglize(string)
if not sh1 or not sh2:
return 0
@temoto
temoto / table2cron.py
Last active August 29, 2015 14:06
Manage cron jobs in a nice spreadsheet, export to crontab format using this script.
#!/usr/bin/env python3
'''
каждый день{tab}13:45{tab}игнорируется{tab}http://ping.url/
->
45 13 * * *{tab}/usr/bin/curl -fsS 'http://ping.url/'
'''
import re
import sys
@temoto
temoto / sql-trace.py
Created August 1, 2014 05:22
Django SQL DB trace: see where your queries are coming from. Now you may find https://pypi.python.org/pypi/django-devserver better.
from django.db.models.sql import Query
from functools import wraps
import inspect
TRACE_THIS_QUERY = u'SELECT "blog_group"."id", "blog_group"."name", "blog_group"."arg" FROM "blog_group" INNER JOIN "blog_group_members" ON ("blog_group"."id" = "blog_group_members"."group_id") WHERE "blog_group_members"."user_id" = %s '
def trace_get_compiler(fun):
@wraps(fun)
@temoto
temoto / sql-stat.py
Last active August 29, 2015 14:04
Django SQL DB query statistics. Now you may find https://pypi.python.org/pypi/django-devserver SQLSummaryModule better.
try:
# DB profiling.
# TODO: extract this to separate module.
TERMINAL_WIDTH = 124 # fine for 15" in 12px mono font.
SHOW_TOP_N_QUERIES = 5
import itertools
import textwrap
queries = django.db.connection.queries
if queries: