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