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.

90 lines
2.1 KiB

4 years ago
  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. f = open(options.output, 'wb')
  49. f.write(result)
  50. f.close()
  51. def read_file(filename):
  52. if filename == '-':
  53. c = sys.stdin.read()
  54. elif not os.path.exists(filename):
  55. raise OSError(
  56. "Input file %s does not exist" % filename)
  57. else:
  58. f = open(filename, 'rb')
  59. c = f.read()
  60. f.close()
  61. return c
  62. body_start_re = re.compile(
  63. r"<body.*?>", re.I|re.S)
  64. body_end_re = re.compile(
  65. r"</body.*?>", re.I|re.S)
  66. def split_body(html):
  67. pre = post = ''
  68. match = body_start_re.search(html)
  69. if match:
  70. pre = html[:match.end()]
  71. html = html[match.end():]
  72. match = body_end_re.search(html)
  73. if match:
  74. post = html[match.start():]
  75. html = html[:match.start()]
  76. return pre, html, post
  77. def annotate(options, args):
  78. print("Not yet implemented")
  79. sys.exit(1)