setup_util.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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']:
  27. if envname in os.environ:
  28. mini_environ[envname] = os.environ[envname]
  29. for key in os.environ:
  30. if key.startswith('TFB_'): # Any TFB_ 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 = subprocess.check_output(setup_env, shell=True, env=mini_environ,
  39. executable='/bin/bash')
  40. for line in env.split('\n'):
  41. try:
  42. key, value = line.split('=', 1)
  43. # If we already have this TFB_ variable, do not overwrite
  44. if key.startswith('TFB_') and key in mini_environ:
  45. os.environ[key]=mini_environ[key]
  46. else:
  47. os.environ[key]=value
  48. except:
  49. if not line: # Don't warn for empty line
  50. continue
  51. print "WARN: Line '%s' from '%s' is not an environment variable" % (line, config)
  52. continue
  53. if print_result:
  54. out = subprocess.check_output('env', shell=True, executable='/bin/bash')
  55. print "Environment after loading %s" %config
  56. print out
  57. # Queries the shell for the value of FWROOT
  58. def get_fwroot():
  59. if platform.system().lower() == 'windows':
  60. fwroot = "C:\FrameworkBenchmarks"
  61. return fwroot
  62. else:
  63. try:
  64. # Use printf to avoid getting a newline
  65. # Redirect to avoid stderr printing
  66. fwroot = subprocess.check_output('printf $FWROOT 2> /dev/null', shell=True, executable='/bin/bash')
  67. return fwroot
  68. except subprocess.CalledProcessError:
  69. return "";
  70. # Turns absolute path into path relative to FWROOT
  71. # Assumes path is underneath FWROOT, not above
  72. #
  73. # Useful for clean presentation of paths
  74. # e.g. /foo/bar/benchmarks/go/bash_profile.sh
  75. # v.s. FWROOT/go/bash_profile.sh
  76. def path_relative_to_root(path):
  77. # Requires bash shell parameter expansion
  78. return subprocess.check_output("D=%s && printf \"${D#%s}\""%(path, get_fwroot()), shell=True, executable='/bin/bash')