setup_util.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 = os.environ.copy()
  22. mini_environ.clear()
  23. if 'HOME' in os.environ.keys():
  24. mini_environ['HOME']=os.environ['HOME']
  25. if 'PATH' in os.environ.keys():
  26. mini_environ['PATH']=os.environ['PATH']
  27. if 'USER' in os.environ.keys():
  28. mini_environ['USER']=os.environ['USER']
  29. if 'LD_LIBRARY_PATH' in os.environ.keys():
  30. mini_environ['LD_LIBRARY_PATH'] = os.environ['LD_LIBRARY_PATH']
  31. if 'PYTHONPATH' in os.environ.keys():
  32. mini_environ['PYTHONPATH'] = os.environ['PYTHONPATH']
  33. if root is not None:
  34. mini_environ['FWROOT']=root
  35. elif 'FWROOT' in os.environ.keys():
  36. mini_environ['FWROOT']=os.environ['FWROOT']
  37. os.environ.clear()
  38. # Run command, source config file, and store resulting environment
  39. setup_env = "%s && . %s && env" % (command, config)
  40. env = subprocess.check_output(setup_env, shell=True, env=mini_environ,
  41. executable='/bin/bash')
  42. for line in env.split('\n'):
  43. try:
  44. split=line.index('=')
  45. key=line[:split]
  46. value=line[split+1:]
  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. try:
  60. # Use printf to avoid getting a newline
  61. # Redirect to avoid stderr printing
  62. fwroot = subprocess.check_output('printf $FWROOT 2> /dev/null', shell=True, executable='/bin/bash')
  63. return fwroot
  64. except subprocess.CalledProcessError:
  65. return "";
  66. # Turns absolute path into path relative to FWROOT
  67. # Assumes path is underneath FWROOT, not above
  68. #
  69. # Useful for clean presentation of paths
  70. # e.g. /foo/bar/benchmarks/go/bash_profile.sh
  71. # v.s. FWROOT/go/bash_profile.sh
  72. def path_relative_to_root(path):
  73. # Requires bash shell parameter expansion
  74. return subprocess.check_output("D=%s && printf \"${D#%s}\""%(path, get_fwroot()), shell=True, executable='/bin/bash')