Skip to content

Instantly share code, notes, and snippets.

@meeuw
meeuw / singleprocess.py
Last active August 29, 2015 14:05
usage: with singleprocess('/tmp/test.pid'): pass
import time
class singleprocess:
def __init__(self, pidfile):
self.pidfile = pidfile
def __enter__(self):
pid = str(os.getpid())
if os.path.isfile(self.pidfile+'.0'):
self.realpidfile = self.pidfile+'.1'
if os.path.isfile(self.pidfile+'.1'):
raise Exception("%s already exists, exiting" % self.realpidfile)
@meeuw
meeuw / pivot.sql
Created September 1, 2014 07:10
merge multiple tables (same tables from different databases) into one table (@from is import table name, @contents is column, @TagName is key, @site is column)
insert into pivot
select distinct NULL, `@from`.`@tagname`, '@from-@contents', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL from `@from`
left join pivot on `pivot`.`tagname` = `@from`.`@tagname` and '@from-@contents' = `pivot`.`category`
where `pivot`.`tagname` is null;
update `pivot`
inner join `@from` on `pivot`.`tagname` = `@from`.`@tagname` and '@from-@contents' = `pivot`.`category`
set `pivot`.`@site` = `@contents`;
@meeuw
meeuw / print_match.py
Created September 23, 2014 13:42
grep but only output what matches
#!/usr/bin/python
import fileinput
import sys
import re
pattern = sys.argv.pop(1)
for line in fileinput.input():
m = re.search(pattern, line)
if m:
for g in m.groups(): print g
@meeuw
meeuw / runcmd.py
Last active October 23, 2015 19:40
start ssh connection from bottle and send some data to stdin
import threading
from StringIO import StringIO
runcmdLock = threading.Lock()
def runcmd(keyf, username, send):
runcmdLock.acquire(True)
while 1:
if not (keyf, username) in default_app().chan:
key = RSAKey.from_private_key(file_obj=StringIO(keyf))
client = SSHClient()
@meeuw
meeuw / tailf.py
Created October 31, 2014 20:05
match error_log and access_log tailing
import tailer
import time
import datetime
import re
dt = datetime.timedelta(0, 1)
with file('/var/log/httpd/error_log') as error_log:
with file('/var/log/httpd/access_log') as access_log:
tailer_error_log = tailer.follow(error_log, 0)
tailer_access_log = tailer.follow(access_log, 0)
error_log_queue = []
mysql --skip-column-names -Be "SELECT TABLE_NAME from information_schema.TABLES where TABLE_SCHEMA = '$DATABASE' AND engine = 'MyISAM';"|while read TABLE ; do echo "alter table $TABLE ENGINE=InnoDB; " ; done
@meeuw
meeuw / findfiles.py
Last active August 29, 2015 14:10
find files on a samba share
import hashlib
import sys
import os
import pexpect
import re
class SMBClient():
def __init__(self, host, share, username, password):
self.p = pexpect.spawn('smbclient //%s/%s -U "%s%%%s"' % (host, share, username, password))
self.cmd()
@meeuw
meeuw / replacexmlfields.py
Created December 4, 2014 09:42
replace fields in xml file
import re
import fileinput
i = 0
output = {}
def repl(m):
global i
global output
i += 1
output[i] = m.group(2)
@meeuw
meeuw / doctrine_mapping.php
Created December 25, 2014 18:43
doctrine enum mapping to string
$conn = $entityManager->getConnection();
$conn->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
@meeuw
meeuw / mplayer.html
Last active May 30, 2018 09:37
control mplayer with python bottle web interface
<html>
<head>
<link href="http://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.10.3/css/base/jquery.ui.all.css" rel="stylesheet">
<link href="http://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.10.2/css/lightness/jquery-ui-1.10.2.custom.min.css" rel="stylesheet">
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jqueryui-touch-punch/0.2.2/jquery.ui.touch-punch.min.js"></script>
<script src="http://localhost:8080/mplayer"></script>
<script>
$(function () {