sbs_env.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #
  2. # Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
  3. # All rights reserved.
  4. # This component and the accompanying materials are made available
  5. # under the terms of the License "Eclipse Public License v1.0"
  6. # which accompanies this distribution, and is available
  7. # at the URL "http://www.eclipse.org/legal/epl-v10.html".
  8. #
  9. # Initial Contributors:
  10. # Nokia Corporation - initial contribution.
  11. #
  12. # Contributors:
  13. #
  14. # Description:
  15. # set up environment for programs that want to use python
  16. import os
  17. import sys
  18. if not 'SBS_HOME' in os.environ:
  19. # effectively this determines if the script is not being run from a wrapper
  20. # so that we can try to set up roughly what the wrapper would set up.
  21. # e.g. this should make it easier to run raptor from a python debugger
  22. sbh = os.path.abspath(os.path.join(os.path.split(sys.argv[0])[0],".."))
  23. os.environ['SBS_HOME'] = sbh
  24. if 'SBS_PYTHONPATH' in os.environ:
  25. pyp_index = 0
  26. if 'PYTHONPATH' in os.environ:
  27. pyp = os.environ['PYTHONPATH'].split(os.pathsep)
  28. # Try to remember where pythonpath appears in the overall path
  29. # Usually it's after all the default paths but we don't
  30. # know where those are:
  31. pyp_index = sys.path.index(pyp[0])
  32. # delete the python path from the front of the system path
  33. # so that we can replace it with "SBS_PYTHONPATH"
  34. for p in pyp:
  35. try:
  36. sys.path.remove(p)
  37. except ValueError as e:
  38. pass
  39. # insert SBS_PYTHONPATH at the same point where PYTHONPATH appeared
  40. sys.path[pyp_index:pyp_index] = os.environ['SBS_PYTHONPATH'].split(os.pathsep)
  41. if not 'HOSTPLATFORM' in os.environ: # set platform details if not already done.
  42. plat = sys.platform.lower()
  43. if plat.startswith("win"):
  44. # Assume win32 for now since raptor has to operate at that level.
  45. os.environ['HOSTPLATFORM']='win 32'
  46. os.environ['HOSTPLATFORM32_DIR']='win32'
  47. os.environ['HOSTPLATFORM_DIR']='win32'
  48. else:
  49. # Call the gethost.sh script using the -e option to obtain platform details
  50. try:
  51. import subprocess # avoid doing this import if we don't have to
  52. p = subprocess.Popen(args=[os.path.join(os.environ['SBS_HOME'],"bin", "gethost.sh"),"-e"],stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
  53. (output,errors) = p.communicate()
  54. rc = p.wait()
  55. if type(output) is not str:
  56. output = output.decode()
  57. for term in output.split("\n"):
  58. try:
  59. left,right=term.split("=")
  60. left = left.split(" ")[1] # get rid of "export"
  61. right = right.strip("'")
  62. except ValueError as e:
  63. continue
  64. if left in ['HOSTPLATFORM', 'HOSTPLATFORM_DIR', 'HOSTPLATFORM32_DIR']:
  65. os.environ[left] = right
  66. except Exception as e:
  67. import traceback
  68. traceback.print_exc()
  69. print("sbs: error: while determining host platform details - {0}".format(e))
  70. sys.exit(1)
  71. # The SBS_HOME dir is in pythonpath to support the raptor pacakge
  72. # SBS_HOME/raptor is in the pythonpath to support planb and old filters.
  73. raptor_pythondirs = [ os.path.join(os.environ['SBS_HOME'],'raptor'), os.environ['SBS_HOME'] ]
  74. sys.path[0:0] = raptor_pythondirs # going in before everything - even the default paths
  75. # Transmit a similar path down to subprocesses - unavoidably slightly different because it will go in after
  76. # the default paths rather than at the start. One doesn't know where default paths start/end
  77. # so there's not much one can do about that. Subprocesses like planb need this to work.
  78. if 'PYTHONPATH' in os.environ:
  79. pp_env = os.path.pathsep + os.environ['PYTHONPATH']
  80. else:
  81. pp_env=''
  82. os.environ['PYTHONPATH']= os.pathsep.join(raptor_pythondirs) + pp_env