Skip to content

Instantly share code, notes, and snippets.

View techtonik's full-sized avatar

anatoly techtonik techtonik

View GitHub Profile
@techtonik
techtonik / static.wsgi
Created November 8, 2013 17:04
WSGI middleware to serve static resource from /static endpoint. Useful for development.
# --- [middleware helper to serve static resources] ---
# Placed in public domain
import os
import mimetypes
# directory with static resources (default: 'static' in current dir)
STATIC_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), 'static'))
def appstatic(app, environ, start_response):
@techtonik
techtonik / maintenance.wsgi
Last active December 22, 2015 09:59
Scheduled maintenance WSGI notice. Insert into your .wsgi script and change MAINTENANCE parameter to True.
#!/usr/bin/env python
"""
WSGI script that shows mainenance notice to web site visitors.
Features Z-Type and elapsed time counter based on timestamp
of this file.
You may safely copy-paste this code into your own scripts.
Public domain work by anatoly techtonik <[email protected]>
"""
@techtonik
techtonik / hpkg_header.py
Created June 21, 2013 18:54
calculate length of Haiku binary package format header
#!/usr/bin/env python
"""
Calculate length of Haiku Package file format header
http://dev.haiku-os.org/wiki/PackageManagement/FileFormat
"""
# needs python-cffi
from cffi import FFI
@techtonik
techtonik / bootstrap.py
Last active December 18, 2015 14:49
locally - unpack subdirectory from .zip archive
def extract_zip(zippath, subdir, target):
'''extract entries from `subdir` of `zipfile` into `target`/ directory'''
from os.path import join, exists
import shutil
import zipfile
zf = zipfile.ZipFile(zippath)
for entry in zf.namelist():
if subdir:
if not entry.startswith(subdir + '/'):
@techtonik
techtonik / bootstrap.py
Last active December 18, 2015 14:39
locally - download and apply patch
# locally recipe that downloads patch and applies it
# --- bootstrap .locally ---
#
# this creates .locally/ subdirectory in the script's dir
# and sets a few global variables for convenience:
#
# ROOT - absolute path to bootstrap.py dir, ends with /
# LOOT - absolute path to the .locally/ subdir
# LIB - path to downloaded helper libs added to sys.path
@techtonik
techtonik / bootstrap.py
Last active December 18, 2015 14:38
locally - repository checkout w/out moving
# locally recipe that checks out Rietveld sources
# sources go into subdir, because hg is unable to
# checkout into dirty dir. no custom move logic
# is done also
# [ ] check rietveld checkout into given dir
# [ ] check dir/
# [ ] check dir/.hg/
# [ ] check dir/codereview/
# [ ] clone
@techtonik
techtonik / Makefile
Last active December 18, 2015 13:18
Using Python inside Makefile
# Black magic to use Python logic for setting Makefile variables
# Real world example that is detecting AppEngine SDK with Python
# Helper code to detect SDK location
define DETECT_SDK
import os
locations = [
"../google_appengine",
"/usr/local/google_appengine",
"../.locally/google_appengine",
@techtonik
techtonik / findfiles.py
Created June 2, 2013 20:23
Python - case-insensitive glob
# snippet is placed into public domain by
# anatoly techtonik <[email protected]>
# http://stackoverflow.com/questions/8151300/ignore-case-in-glob-on-linux
import fnmatch
import os
import re
def findfiles(which, where='.'):
@techtonik
techtonik / hashdeep.py
Created March 16, 2013 10:45
Python - hashdeep.py - recursive hash of directory tree files in hashdeep format
"""
Build recursive hash of files in directory tree in hashdeep format.
Hashdeep format description:
http://md5deep.sourceforge.net/start-hashdeep.html
hashdeep.py differences from original hashdeep:
- if called without arguments, automatically starts to build
@techtonik
techtonik / setup.py
Created March 5, 2013 18:38
Python - setup.py - auto link issues in reST files (such as CHANGES.rst)
import re
# tested with BitBucket
TRACKER = 'http://bitbucket.org/tarek/distribute'
# return contents of reStructureText file with linked issue references
def _linkified(rstfile):
revision = re.compile(r'\b(issue\s*#?\d+)\b', re.M | re.I)
rstext = open(rstfile).read()