Skip to content

Instantly share code, notes, and snippets.

@yfuruyama
Created February 19, 2014 15:44
Show Gist options
  • Select an option

  • Save yfuruyama/9094681 to your computer and use it in GitHub Desktop.

Select an option

Save yfuruyama/9094681 to your computer and use it in GitHub Desktop.
diff
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import difflib
FILE1 = 'file1'
FILE2 = 'file2'
def main():
fh1 = open(FILE1, 'r')
fh2 = open(FILE2, 'r')
c1 = fh1.read()
c2 = fh2.read()
differ = difflib.Differ()
diff = differ.compare(c1.split('\n'), c2.split('\n'))
orig_line_num = 1
diffs = []
left_start = right_start = left_n_line = right_n_line = 0
initial_skip = 2
for line in difflib.unified_diff(c1.split('\n'), c2.split('\n'), n=0):
print line
if initial_skip > 0:
initial_skip -= 1
continue
if line.startswith('@'):
left_range, right_range = line.split()[1:3]
left = left_range.split(',')
right = right_range.split(',')
left_start = abs(int(left[0]))
right_start = int(right[0])
left_n_line = 1 if len(left) == 1 else int(left[1])
right_n_line = 1 if len(right) == 1 else int(right[1])
else:
delta_mark = line[0]
code = line[1:]
if delta_mark == '+':
orig_line_num = left_start
if left_n_line == 0:
orig_line_num = left_start + 1
diffs.append({
'action': 'insert',
'orig_line_num': orig_line_num,
'code': code,
})
elif delta_mark == '-':
diffs.append({
'action': 'delete',
'orig_line_num': left_start,
'code': code,
})
left_start += 1
print
print diffs
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment