run_javac.py 2.8 KB

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