You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

89 lines
2.1 KiB

  1. from __future__ import absolute_import
  2. import optparse
  3. import sys
  4. import re
  5. import os
  6. from .diff import htmldiff
  7. description = """\
  8. """
  9. parser = optparse.OptionParser(
  10. usage="%prog [OPTIONS] FILE1 FILE2\n"
  11. "%prog --annotate [OPTIONS] INFO1 FILE1 INFO2 FILE2 ...",
  12. description=description,
  13. )
  14. parser.add_option(
  15. '-o', '--output',
  16. metavar="FILE",
  17. dest="output",
  18. default="-",
  19. help="File to write the difference to",
  20. )
  21. parser.add_option(
  22. '-a', '--annotation',
  23. action="store_true",
  24. dest="annotation",
  25. help="Do an annotation")
  26. def main(args=None):
  27. if args is None:
  28. args = sys.argv[1:]
  29. options, args = parser.parse_args(args)
  30. if options.annotation:
  31. return annotate(options, args)
  32. if len(args) != 2:
  33. print('Error: you must give two files')
  34. parser.print_help()
  35. sys.exit(1)
  36. file1, file2 = args
  37. input1 = read_file(file1)
  38. input2 = read_file(file2)
  39. body1 = split_body(input1)[1]
  40. pre, body2, post = split_body(input2)
  41. result = htmldiff(body1, body2)
  42. result = pre + result + post
  43. if options.output == '-':
  44. if not result.endswith('\n'):
  45. result += '\n'
  46. sys.stdout.write(result)
  47. else:
  48. with open(options.output, 'wb') as f:
  49. f.write(result)
  50. def read_file(filename):
  51. if filename == '-':
  52. c = sys.stdin.read()
  53. elif not os.path.exists(filename):
  54. raise OSError(
  55. "Input file %s does not exist" % filename)
  56. else:
  57. with open(filename, 'rb') as f:
  58. c = f.read()
  59. return c
  60. body_start_re = re.compile(
  61. r"<body.*?>", re.I|re.S)
  62. body_end_re = re.compile(
  63. r"</body.*?>", re.I|re.S)
  64. def split_body(html):
  65. pre = post = ''
  66. match = body_start_re.search(html)
  67. if match:
  68. pre = html[:match.end()]
  69. html = html[match.end():]
  70. match = body_end_re.search(html)
  71. if match:
  72. post = html[match.start():]
  73. html = html[:match.start()]
  74. return pre, html, post
  75. def annotate(options, args):
  76. print("Not yet implemented")
  77. sys.exit(1)