setup_util.py 3.0 KB

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