setup_util.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. os.environ.clear()
  26. # Use FWROOT if explicitely provided
  27. if root is not None:
  28. mini_environ['FWROOT']=root
  29. # Run command, source config file, and store resulting environment
  30. setup_env = "%s && . %s && env" % (command, config)
  31. env = subprocess.check_output(setup_env, shell=True, env=mini_environ,
  32. executable='/bin/bash')
  33. for line in env.split('\n'):
  34. try:
  35. key, value = line.split('=', 1)
  36. os.environ[key]=value
  37. except:
  38. if not line: # Don't warn for empty line
  39. continue
  40. print "WARN: Line '%s' from '%s' is not an environment variable" % (line, config)
  41. continue
  42. if print_result:
  43. out = subprocess.check_output('env', shell=True, executable='/bin/bash')
  44. print "Environment after loading %s" %config
  45. print out
  46. # Queries the shell for the value of FWROOT
  47. def get_fwroot():
  48. try:
  49. # Use printf to avoid getting a newline
  50. # Redirect to avoid stderr printing
  51. fwroot = subprocess.check_output('printf $FWROOT 2> /dev/null', shell=True, executable='/bin/bash')
  52. return fwroot
  53. except subprocess.CalledProcessError:
  54. return "";
  55. # Turns absolute path into path relative to FWROOT
  56. # Assumes path is underneath FWROOT, not above
  57. #
  58. # Useful for clean presentation of paths
  59. # e.g. /foo/bar/benchmarks/go/bash_profile.sh
  60. # v.s. FWROOT/go/bash_profile.sh
  61. def path_relative_to_root(path):
  62. # Requires bash shell parameter expansion
  63. return subprocess.check_output("D=%s && printf \"${D#%s}\""%(path, get_fwroot()), shell=True, executable='/bin/bash')