setup_util.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import re
  2. import os
  3. import subprocess
  4. import platform
  5. # Replaces all text found using the regular expression to_replace with the supplied replacement.
  6. def replace_text(file, to_replace, replacement):
  7. with open(file, "r") as conf:
  8. contents = conf.read()
  9. replaced_text = re.sub(to_replace, replacement, contents)
  10. with open(file, "w") as f:
  11. f.write(replaced_text)
  12. # Replaces the current process environment with the one found in
  13. # config file. Retains a few original vars (HOME,PATH, etc) by default.
  14. # Optionally allows specification of a command to be run before loading
  15. # the environment, to allow the framework to set environment variables
  16. # Note: This command *cannot* print to stdout!
  17. #
  18. # Note: This will not replace the sudo environment (e.g. subprocess.check_call("sudo <command>")).
  19. # If you must use sudo, consider sudo sh -c ". <config> && your_command"
  20. def replace_environ(config=None, root=None, print_result=False, command='true'):
  21. if platform.system().lower() == 'windows':
  22. pass
  23. else:
  24. # Clean up our current environment, preserving some important items
  25. mini_environ = {}
  26. for envname in ['HOME', 'PATH', 'USER', 'LD_LIBRARY_PATH', 'PYTHONPATH', 'FWROOT', 'TRAVIS']:
  27. if envname in os.environ:
  28. mini_environ[envname] = os.environ[envname]
  29. for key in os.environ:
  30. if key.startswith(('TFB_', 'TRAVIS_')): # Any TFB_* and TRAVIS_* variables are preserved
  31. mini_environ[key] = os.environ[key]
  32. os.environ.clear()
  33. # Use FWROOT if explicitely provided
  34. if root is not None:
  35. mini_environ['FWROOT']=root
  36. # Run command, source config file, and store resulting environment
  37. setup_env = "%s && . %s && env" % (command, config)
  38. env = ""
  39. try:
  40. env = subprocess.check_output(setup_env, shell=True, env=mini_environ,
  41. executable='/bin/bash')
  42. except subprocess.CalledProcessError:
  43. # Ensure that an error here does not crash the toolset
  44. print "CRITICAL: Loading %s returned non-zero exit" % config
  45. for key,value in mini_environ.iteritems():
  46. os.environ[key]=value
  47. return
  48. for line in env.split('\n'):
  49. try:
  50. key, value = line.split('=', 1)
  51. # If we already have this TFB_ variable, do not overwrite
  52. if key.startswith('TFB_') and key in mini_environ:
  53. os.environ[key]=mini_environ[key]
  54. else:
  55. os.environ[key]=value
  56. except Exception:
  57. if not line: # Don't warn for empty line
  58. continue
  59. print "WARN: Line '%s' from '%s' is not an environment variable" % (line, config)
  60. continue
  61. if print_result:
  62. out = subprocess.check_output('env', shell=True, executable='/bin/bash')
  63. print "Environment after loading %s" %config
  64. print out
  65. # Queries the shell for the value of FWROOT
  66. def get_fwroot():
  67. if platform.system().lower() == 'windows':
  68. fwroot = "C:\FrameworkBenchmarks"
  69. return fwroot
  70. else:
  71. try:
  72. # Use printf to avoid getting a newline
  73. # Redirect to avoid stderr printing
  74. fwroot = subprocess.check_output('printf $FWROOT 2> /dev/null', shell=True, executable='/bin/bash')
  75. return fwroot
  76. except subprocess.CalledProcessError:
  77. # Make a last-guess effort ;-)
  78. return os.getcwd();
  79. # Turns absolute path into path relative to FWROOT
  80. # Assumes path is underneath FWROOT, not above
  81. #
  82. # Useful for clean presentation of paths
  83. # e.g. /foo/bar/benchmarks/go/bash_profile.sh
  84. # v.s. FWROOT/go/bash_profile.sh
  85. def path_relative_to_root(path):
  86. # Requires bash shell parameter expansion
  87. return subprocess.check_output("D=%s && printf \"${D#%s}\""%(path, get_fwroot()), shell=True, executable='/bin/bash')