Skip to content

Instantly share code, notes, and snippets.

@dahlia
Last active January 11, 2022 04:40
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dahlia/477d1ffbd41bdca468c257c90799bcdd to your computer and use it in GitHub Desktop.
Save dahlia/477d1ffbd41bdca468c257c90799bcdd to your computer and use it in GitHub Desktop.
Bencodex diff

Comparing two Bencodex binary files

A small CLI program to easily compare two Bencodex trees:

$ pip3 install --user -r requirements.txt
$ ./bdiff.py ./a.dat ./b.dat
--- a.dat
+++ b.dat
  {'bar': [None, 1, 2, 3],
   'baz': {b'\x00\x01': 'a', b'\x02': 'b'},
-  'foo': [True, False],
+  'foo': [True, False, 'added'],
?                     +++++++++
   'qux': None,
   'quxx': {'a': 1, 'b': 2, 'c': 3}}
#!/usr/bin/env python3
import difflib
import pprint
import sys
from bencodex import load
def read_format(path):
with open(path, 'rb') as f:
tree = load(f)
lines = pprint.pformat(tree).splitlines(keepends=True)
if lines and not lines[-1].endswith('\n'):
lines[-1] += '\n'
return lines
def main():
if len(sys.argv) < 3:
print('error: too few arguments', file=sys.stderr)
print('usage:', sys.argv[0], 'FILE FILE', file=sys.stderr)
raise SystemExit(1)
print('---', sys.argv[1])
print('+++', sys.argv[2])
a = read_format(sys.argv[1])
b = read_format(sys.argv[2])
d = difflib.Differ()
result = d.compare(a, b)
sys.stdout.writelines(result)
if __name__ == '__main__':
main()
bencodex >= 1.0.1, < 1.1.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment