Created
July 17, 2013 17:36
-
-
Save spalladino/6022705 to your computer and use it in GitHub Desktop.
Updates the icml files for the handbook's chapters based on a diff file from txt extracts
This file contains hidden or 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
| import re | |
| import fileinput | |
| import os | |
| from glob import glob | |
| ICML_DIR = "icml" | |
| OUT_DIR = "output" | |
| # Get icml files in memory | |
| data = {} | |
| for path in glob(ICML_DIR + '/*.icml'): | |
| with open(path, 'r') as ficml: | |
| data[path] = ''.join(ficml.readlines()) | |
| # Get all diffs to process | |
| diffinfo = ''.join(fileinput.input()) | |
| diffs = re.split(r"\n(?=diff\b)", diffinfo) | |
| # Go through the diffs and apply them to all files | |
| for diff in diffs: | |
| chapter = re.match(r'diff \s --git \s "? a/(\d+)[^\s]+ "? \s "? b/[^\s]+"?', diff, re.X | re.I).group(1) | |
| for line in diff.split("\n"): | |
| if line.startswith("-") and not line.startswith("---"): | |
| oldstr = line[1:] | |
| elif line.startswith("+") and not line.startswith("+++"): | |
| newstr = line[1:] | |
| for path, contents in data.iteritems(): | |
| data[path] = contents.replace(oldstr, newstr) | |
| # Write all files to output | |
| os.makedirs('output') | |
| for path, contents in data.iteritems(): | |
| with open(path.replace(ICML_DIR + '/', OUT_DIR + '/'), 'w') as out: | |
| out.write(contents) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment