run_javac.py 2.9 KB

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