qnxnto.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. # Jérôme Carretero 2011 (zougloub)
  4. # QNX neutrino compatibility functions
  5. import sys, os
  6. from waflib import Utils
  7. class Popen(object):
  8. """
  9. Popen cannot work on QNX from a threaded program:
  10. Forking in threads is not implemented in neutrino.
  11. Python's os.popen / spawn / fork won't work when running in threads (they will if in the main program thread)
  12. In waf, this happens mostly in build.
  13. And the use cases can be replaced by os.system() calls.
  14. """
  15. __slots__ = ["prog", "kw", "popen", "verbose"]
  16. verbose = 0
  17. def __init__(self, prog, **kw):
  18. try:
  19. self.prog = prog
  20. self.kw = kw
  21. self.popen = None
  22. if Popen.verbose:
  23. sys.stdout.write("Popen created: %r, kw=%r..." % (prog, kw))
  24. do_delegate = kw.get('stdout') == -1 and kw.get('stderr') == -1
  25. if do_delegate:
  26. if Popen.verbose:
  27. print("Delegating to real Popen")
  28. self.popen = self.real_Popen(prog, **kw)
  29. else:
  30. if Popen.verbose:
  31. print("Emulating")
  32. except Exception as e:
  33. if Popen.verbose:
  34. print("Exception: %s" % e)
  35. raise
  36. def __getattr__(self, name):
  37. if Popen.verbose:
  38. sys.stdout.write("Getattr: %s..." % name)
  39. if name in Popen.__slots__:
  40. return object.__getattribute__(self, name)
  41. else:
  42. if self.popen is not None:
  43. if Popen.verbose:
  44. print("from Popen")
  45. return getattr(self.popen, name)
  46. else:
  47. if name == "wait":
  48. return self.emu_wait
  49. else:
  50. raise Exception("subprocess emulation: not implemented: %s" % name)
  51. def emu_wait(self):
  52. if Popen.verbose:
  53. print("emulated wait (%r kw=%r)" % (self.prog, self.kw))
  54. if isinstance(self.prog, str):
  55. cmd = self.prog
  56. else:
  57. cmd = " ".join(self.prog)
  58. if 'cwd' in self.kw:
  59. cmd = 'cd "%s" && %s' % (self.kw['cwd'], cmd)
  60. return os.system(cmd)
  61. if sys.platform == "qnx6":
  62. Popen.real_Popen = Utils.subprocess.Popen
  63. Utils.subprocess.Popen = Popen