do_upload.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env python3
  2. import glob
  3. import itertools
  4. import os
  5. from subprocess import Popen
  6. def run_command(command: list[str], log_file: str):
  7. with open(log_file, "wb") as f:
  8. return Popen(command, stdout=f, stderr=f)
  9. def main():
  10. """
  11. First we get the arguments passed to the upload artifacts action via the env vars,
  12. then we build the command for uploading coverage, run it in a thread, and build the
  13. command for uploading test results and run it in a thread. We wait for both commands
  14. to finish running then print their logs sequentially.
  15. When we run the commands we're piping their stdout and stderr to a file so we can print the
  16. contents of the files sequentially. We don't want the output of each command to be interleaved.
  17. --plugin noop is passed to both commands, we don't need the CLI plugins because we're already
  18. generating the coverage files ourselves.
  19. """
  20. input_token = os.getenv("INPUT_TOKEN")
  21. input_commit_sha = os.getenv("INPUT_COMMIT_SHA")
  22. input_type = os.getenv("INPUT_TYPE")
  23. input_files = os.getenv("INPUT_FILES", "").split(",")
  24. input_test_result_files = os.getenv("INPUT_TEST_RESULT_FILES", "").split(",")
  25. glob_expanded_coverage_files = [glob.glob(file, recursive=True) for file in input_files]
  26. coverage_files = list(itertools.chain.from_iterable(glob_expanded_coverage_files))
  27. codecov_base_cmd = ["./codecov", "--verbose"]
  28. upload_flags = [
  29. "-t",
  30. input_token,
  31. "--commit-sha",
  32. input_commit_sha,
  33. "--plugin",
  34. "noop",
  35. "--flag",
  36. input_type,
  37. ]
  38. upload_coverage_cmd = [*codecov_base_cmd, "upload-process", *upload_flags]
  39. for file in coverage_files:
  40. upload_coverage_cmd += ["--file", file]
  41. upload_coverage_log_file = "coverage-upload.log"
  42. glob_expanded_test_result_files = [
  43. glob.glob(file, recursive=True) for file in input_test_result_files
  44. ]
  45. test_result_files = list(itertools.chain.from_iterable(glob_expanded_test_result_files))
  46. upload_test_results_cmd = [
  47. *codecov_base_cmd,
  48. "do-upload",
  49. "--report-type",
  50. "test_results",
  51. *upload_flags,
  52. ]
  53. for file in test_result_files:
  54. upload_test_results_cmd += ["--file", file]
  55. upload_test_results_log_file = "upload-test-results.log"
  56. # so that the logs are not interleaved when printed
  57. jobs = [
  58. run_command(upload_test_results_cmd, upload_test_results_log_file),
  59. run_command(upload_coverage_cmd, upload_coverage_log_file),
  60. ]
  61. tail_args = ("tail", "-f", "--sleep-interval", "3")
  62. for job in jobs:
  63. tail_args += ("--pid", str(job.pid))
  64. tail_args += (
  65. upload_coverage_log_file,
  66. upload_test_results_log_file,
  67. )
  68. # wait, while showing un-interleaved logs
  69. jobs.append(Popen(tail_args))
  70. for job in jobs:
  71. job.wait()
  72. if __name__ == "__main__":
  73. main()