win32spawn.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #
  2. # File : win32spawn.py
  3. # This file is part of RT-Thread RTOS
  4. # COPYRIGHT (C) 2006 - 2025, RT-Thread Development Team
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License along
  17. # with this program; if not, write to the Free Software Foundation, Inc.,
  18. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. #
  20. # Change Logs:
  21. # Date Author Notes
  22. # 2015-01-20 Bernard Add copyright information
  23. #
  24. import os
  25. import subprocess
  26. class Win32Spawn:
  27. def spawn(self, sh, escape, cmd, args, env):
  28. # deal with the cmd build-in commands which cannot be used in
  29. # subprocess.Popen
  30. if cmd == 'del':
  31. for f in args[1:]:
  32. try:
  33. os.remove(f)
  34. except Exception as e:
  35. print('Error removing file: ' + e)
  36. return -1
  37. return 0
  38. newargs = ' '.join(args[1:])
  39. cmdline = cmd + " " + newargs
  40. # Make sure the env is constructed by strings
  41. _e = dict([(k, str(v)) for k, v in env.items()])
  42. # Windows(tm) CreateProcess does not use the env passed to it to find
  43. # the executables. So we have to modify our own PATH to make Popen
  44. # work.
  45. old_path = os.environ['PATH']
  46. os.environ['PATH'] = _e['PATH']
  47. try:
  48. proc = subprocess.Popen(cmdline, env=_e, shell=False)
  49. except Exception as e:
  50. print('Error in calling command:' + cmdline.split(' ')[0])
  51. print('Exception: ' + os.strerror(e.errno))
  52. if (os.strerror(e.errno) == "No such file or directory"):
  53. print ("\nPlease check Toolchains PATH setting.\n")
  54. return e.errno
  55. finally:
  56. os.environ['PATH'] = old_path
  57. return proc.wait()