annotate python/netdiff.py @ 571:0dbde82d1c82

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