Mercurial > hg > config
comparison python/netdiff.py @ 549:3168816f2d28
netdiff.py
| author | Jeff Hammel <jhammel@mozilla.com> |
|---|---|
| date | Mon, 18 Nov 2013 11:28:30 -0800 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 548:6b2c56da7c9d | 549:3168816f2d28 |
|---|---|
| 1 #!/usr/bin/env python | |
| 2 | |
| 3 """ | |
| 4 compare differences of url contents | |
| 5 """ | |
| 6 | |
| 7 import difflib | |
| 8 import optparse | |
| 9 import os | |
| 10 import sys | |
| 11 import urllib2 | |
| 12 | |
| 13 here = os.path.dirname(os.path.realpath(__file__)) | |
| 14 | |
| 15 def main(args=sys.argv[1:]): | |
| 16 | |
| 17 usage = '%prog [options] from-url to-url' | |
| 18 parser = optparse.OptionParser(usage=usage, description=__doc__) | |
| 19 parser.add_option('--bash', '--command', dest='command', | |
| 20 action='store_true', default=False, | |
| 21 help="prepend output with bash command") | |
| 22 options, args = parser.parse_args(args) | |
| 23 if len(args) != 2: | |
| 24 parser.print_usage() | |
| 25 parser.exit(1) | |
| 26 | |
| 27 contents = {} | |
| 28 for url in args: | |
| 29 contents[url] = urllib2.urlopen(url).read() | |
| 30 | |
| 31 diff = difflib.unified_diff(contents[args[0]], | |
| 32 contents[args[1]], | |
| 33 fromfile=args[0], | |
| 34 tofile=args[1], | |
| 35 lineterm='') | |
| 36 | |
| 37 # output | |
| 38 if options.command: | |
| 39 | |
| 40 template = """%(PS1)s diff <(curl --location %(fromfile)s 2> /dev/null) <(curl --location %(tofile)s 2> /dev/null)""" | |
| 41 print template % dict(PS1='#', | |
| 42 fromfile=args[0], | |
| 43 tofile=args[1]) | |
| 44 | |
| 45 print '\n'.join(list(diff)) | |
| 46 | |
| 47 if __name__ == '__main__': | |
| 48 main() |
