Created
June 9, 2016 11:29
-
-
Save hensing/58a05d0c5e13d48d0698f69149a2a2b0 to your computer and use it in GitHub Desktop.
git: configures git to filter / clear the output of ipython notebooks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# coding: utf-8 | |
""" | |
configures git to filter / clear the output of ipython notebooks by | |
patching the .git/config and .gitattributes and adding 'filter_notebook' as filter | |
""" | |
import os | |
import sys | |
import warnings | |
import distutils.spawn | |
__author__ = 'Henning Dickten' | |
def add_filter(repopath=os.path.curdir): | |
"main routine" | |
# check if filter_notebook is in PATH | |
if distutils.spawn.find_executable('filter_notebook', path=None) is None: | |
warnings.warn('filter_notebook not in PATH') | |
sys.exit(os.errno.ENOPKG) | |
# check for git | |
if not os.path.exists(os.path.join(repopath, '.git')): | |
warnings.warn('.git directory not found! EXIT.') | |
sys.exit(os.errno.ENOENT) | |
# patch .git/config | |
with open(os.path.join(repopath, '.git', 'config'), 'r') as fobj: | |
config = [line.rstrip('\r\n') for line in fobj.readlines()] | |
line = '[filter "filter_notebook"]' | |
if line not in config: | |
with open(os.path.join(repopath, '.git', 'config'), 'w') as fobj: | |
print('patching %s ...' % fobj.name) | |
config.append(line) | |
config.append('\tclean = "./filter_notebook"') | |
fobj.writelines([line + '\n' for line in config]) | |
# add .gitattributes | |
with open(os.path.join(repopath, '.gitattributes'), 'a+') as fobj: | |
attributes = [line.rstrip('\r\n') for line in fobj.readlines()] | |
line = '*.ipynb filter=filter_notebook' | |
if line not in attributes: | |
with open(os.path.join(repopath, '.gitattributes'), 'w') as fobj: | |
print('patching %s ...' % fobj.name) | |
attributes.append(line) | |
fobj.writelines([line + '\n' for line in attributes]) | |
print('done!\n') | |
if __name__ == '__main__': | |
if len(sys.argv) == 2: | |
add_filter(sys.argv[1]) | |
elif len(sys.argv) == 1: | |
add_filter() | |
else: | |
print("usage: %s [path to repository]\n" % sys.argv[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment