Skip to content

Instantly share code, notes, and snippets.

@techtonik
Last active December 18, 2015 14:39
Show Gist options
  • Save techtonik/5798745 to your computer and use it in GitHub Desktop.
Save techtonik/5798745 to your computer and use it in GitHub Desktop.
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
# (.locally/.bootstrap)
#
# this provides some helpers:
#
# localdir(name) - returns absolute path to the new `name`
# dir created .locally, ends with /
#
# this also downloads and imports the following helper libs:
#
# wget - for fetching stuff from the internet
# patch - for patching files
import os
import sys
import urllib
# 1. create LIB dir for locally modules (.locally/.bootstrap)
# and add it to Python sys.path to make them importable
ROOT = os.path.abspath(os.path.dirname(__file__)) + '/'
LOOT = os.path.join(os.path.dirname(__file__), '.locally/')
def localdir(name):
'''create dir in LOOT if needed, return path with ending '/' '''
nupath = LOOT + '/' + name + '/'
if not os.path.exists(nupath):
os.makedirs(nupath)
return nupath
LIB = localdir('.bootstrap')
sys.path.insert(0, LIB)
# 2. download required Python helper libs and import them
required_files = [
('wget.py', 'https://bitbucket.org/techtonik/python-wget/raw/2.0/wget.py'),
('patch.py', 'http://python-patch.googlecode.com/svn/tags/1.12.11/patch.py'),
]
for f, url in required_files:
if os.path.exists(LIB + f):
print("Downloading " + f + " skipped (already downloaded).")
else:
print("Downloading %s into %s" % (f, LIB))
urllib.urlretrieve(url, LIB + f)
import wget
# --- /bootstrap
# 3. patching Makefile
print("Patching Makefile to find AppEngine SDK")
link = 'https://codereview.appspot.com/download/issue5671091_12002_21001.diff'
filename = LOOT + wget.filename_from_url(link)
if os.path.exists(filename):
print("..path is already downloaded to " + filename)
else:
print("..downloading patch %s" % link)
downfile = wget.download(link)
# this moves downloaded file into LOOT directory
shutil.move(downfile, filename)
print("..patching")
import patch
pt = patch.fromfile(filename)
if not pt:
print("..ERROR: patch parsing failed")
else:
import logging
logger = logging.getLogger('patch')
logger.setLevel(logging.DEBUG)
loghandler = logging.StreamHandler()
logger.addHandler(loghandler)
# Actually this doesn't work until Rietveld stores patches in native format
if not pt.apply():
print("..ERROR: patch failed")
print("..done.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment