utils.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. """
  2. This is free and unencumbered software released into the public domain.
  3. Anyone is free to copy, modify, publish, use, compile, sell, or
  4. distribute this software, either in source code form or as a compiled
  5. binary, for any purpose, commercial or non-commercial, and by any
  6. means.
  7. In jurisdictions that recognize copyright laws, the author or authors
  8. of this software dedicate any and all copyright interest in the
  9. software to the public domain. We make this dedication for the benefit
  10. of the public at large and to the detriment of our heirs and
  11. successors. We intend this dedication to be an overt act of
  12. relinquishment in perpetuity of all present and future rights to this
  13. software under copyright law.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  17. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. For more information, please refer to [http://unlicense.org]
  22. """
  23. import os
  24. import sys
  25. import subprocess
  26. import shutil
  27. from threading import Timer
  28. from subprocess import Popen, PIPE
  29. def get_cwd_no_link():
  30. # get current working directory even though we are in a symlinked directory
  31. # the shell argument must be set to True in this case
  32. cwd = Popen(['pwd'], stdout=PIPE, shell=True).communicate()[0].strip()
  33. return cwd.decode('ascii')
  34. def kill_proc(proc, f, timeout_is_fatal):
  35. proc.kill()
  36. f.write("Terminated after timeout")
  37. if timeout_is_fatal:
  38. sys.exit(1)
  39. def print_file(file_name):
  40. with open(file_name, "r") as fin:
  41. for line in fin:
  42. print(line)
  43. def run_with_ascii_output(cmd, cwd):
  44. # does not return exit code, neither provides timeout
  45. return Popen(cmd, cwd=cwd, stdout=PIPE).communicate()[0].strip().decode('ascii')
  46. def execute(cmd, cwd, timeout_sec, timeout_is_fatal, outfile, shell=False):
  47. if shell:
  48. # for shell=True, the command must be a single string
  49. cmd = str.join(" ", cmd)
  50. proc = Popen(cmd, shell=shell, cwd=cwd, stdout=outfile, stderr=subprocess.STDOUT)
  51. timer = Timer(timeout_sec, kill_proc, [proc, outfile, timeout_is_fatal])
  52. try:
  53. timer.start()
  54. exit_code = proc.wait()
  55. finally:
  56. timer.cancel()
  57. return exit_code
  58. # can be simplified by using subprocess.run from Python 3.5
  59. def run(
  60. cmd,
  61. cwd=os.getcwd(),
  62. fout_name="",
  63. append_path_to_output=False,
  64. print_redirected_output=False,
  65. timeout_sec=60,
  66. timeout_is_fatal = True,
  67. verbose=True,
  68. shell=False
  69. ):
  70. if verbose:
  71. log(" Executing: '" + str.join(" ", cmd) + "' " + str(cmd) + " in '" + cwd + "'")
  72. if fout_name:
  73. if append_path_to_output:
  74. full_fout_path = os.path.join(cwd, fout_name)
  75. else:
  76. full_fout_path = fout_name
  77. with open(full_fout_path, "w") as f:
  78. f.write("cwd: " + cwd + "\n")
  79. f.write(str.join(" ", cmd) + "\n") # first item is the command being executed
  80. # run the actual command
  81. exit_code = execute(cmd, cwd, timeout_sec, timeout_is_fatal, f, shell=shell)
  82. if (print_redirected_output):
  83. print_file(full_fout_path)
  84. else:
  85. exit_code = execute(cmd, cwd, timeout_sec, timeout_is_fatal, sys.stdout, shell=shell)
  86. if verbose:
  87. log("Exit code: " + str(exit_code))
  88. return exit_code
  89. def log(msg):
  90. print("* " + msg)
  91. sys.stdout.flush()
  92. def warning(msg):
  93. print("* Warning: " + msg)
  94. sys.stdout.flush()
  95. def fatal_error(msg):
  96. print("* Error: " + msg)
  97. sys.stdout.flush()
  98. sys.exit(1)
  99. def check_ec(ec, cmd):
  100. if ec != 0:
  101. cmd_str = str.join(" ", cmd)
  102. fatal_error("Error: command '" + cmd_str + "' failed, terminating.")