run_javac.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import sys
  2. import subprocess
  3. import optparse
  4. import re
  5. def parse_args():
  6. parser = optparse.OptionParser()
  7. parser.disable_interspersed_args()
  8. parser.add_option('--sources-list')
  9. parser.add_option('--verbose', default=False, action='store_true')
  10. parser.add_option('--remove-notes', default=False, action='store_true')
  11. parser.add_option('--ignore-errors', default=False, action='store_true')
  12. parser.add_option('--kotlin', default=False, action='store_true')
  13. return parser.parse_args()
  14. COLORING = {
  15. r'^(?P<path>.*):(?P<line>\d*): error: (?P<msg>.*)': lambda m: '[[unimp]]{path}[[rst]]:[[alt2]]{line}[[rst]]: [[c:light-red]]error[[rst]]: [[bad]]{msg}[[rst]]'.format(
  16. path=m.group('path'),
  17. line=m.group('line'),
  18. msg=m.group('msg'),
  19. ),
  20. r'^(?P<path>.*):(?P<line>\d*): warning: (?P<msg>.*)': lambda m: '[[unimp]]{path}[[rst]]:[[alt2]]{line}[[rst]]: [[c:light-yellow]]warning[[rst]]: {msg}'.format(
  21. path=m.group('path'),
  22. line=m.group('line'),
  23. msg=m.group('msg'),
  24. ),
  25. r'^warning: ': lambda m: '[[c:light-yellow]]warning[[rst]]: ',
  26. r'^error: (?P<msg>.*)': lambda m: '[[c:light-red]]error[[rst]]: [[bad]]{msg}[[rst]]'.format(msg=m.group('msg')),
  27. r'^Note: ': lambda m: '[[c:light-cyan]]Note[[rst]]: ',
  28. }
  29. def colorize(err):
  30. for regex, sub in COLORING.iteritems():
  31. err = re.sub(regex, sub, err, flags=re.MULTILINE)
  32. return err
  33. def remove_notes(err):
  34. return '\n'.join([line for line in err.split('\n') if not line.startswith('Note:')])
  35. def main():
  36. opts, cmd = parse_args()
  37. with open(opts.sources_list) as f:
  38. input_files = f.read().strip().split()
  39. if opts.kotlin:
  40. input_files = [i for i in input_files if i.endswith('.kt')]
  41. if not input_files:
  42. if opts.verbose:
  43. sys.stderr.write('No files to compile, javac is not launched.\n')
  44. else:
  45. p = subprocess.Popen(cmd, stderr=subprocess.PIPE)
  46. _, err = p.communicate()
  47. rc = p.wait()
  48. if opts.remove_notes:
  49. err = remove_notes(err)
  50. try:
  51. err = colorize(err)
  52. except Exception:
  53. pass
  54. if opts.ignore_errors and rc:
  55. sys.stderr.write('error: javac actually failed with exit code {}\n'.format(rc))
  56. rc = 0
  57. sys.stderr.write(err)
  58. sys.exit(rc)
  59. if __name__ == '__main__':
  60. main()