setup_java_tmpdir.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import os
  2. import sys
  3. import platform
  4. import subprocess
  5. def fix_tmpdir(cmd):
  6. if not cmd:
  7. return cmd
  8. java_id, option_name = None, None
  9. for i, java in enumerate(cmd):
  10. if java.endswith('java') or java.endswith('java.exe'):
  11. java_id = i
  12. option_name = '-Djava.io.tmpdir='
  13. break
  14. if java.endswith('javac') or java.endswith('javac.exe'):
  15. java_id = i
  16. option_name = '-J-Djava.io.tmpdir='
  17. break
  18. if java_id is None:
  19. return cmd
  20. for arg in cmd[java_id:]:
  21. if arg.startswith(option_name):
  22. return cmd
  23. tmpdir = os.environ.get('TMPDIR') or os.environ.get('TEMPDIR')
  24. if not tmpdir:
  25. return cmd
  26. return cmd[:java_id + 1] + ['{}{}'.format(option_name, tmpdir)] + cmd[java_id + 1:]
  27. def just_do_it():
  28. args = fix_tmpdir(sys.argv[1:])
  29. if platform.system() == 'Windows':
  30. sys.exit(subprocess.Popen(args).wait())
  31. else:
  32. os.execv(args[0], args)
  33. if __name__ == '__main__':
  34. just_do_it()