ninjalog_uploader_wrapper.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #!/usr/bin/env python3
  2. # Copyright 2018 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. import json
  6. import os
  7. import platform
  8. import subprocess
  9. import sys
  10. import ninjalog_uploader
  11. import subprocess2
  12. THIS_DIR = os.path.dirname(__file__)
  13. UPLOADER = os.path.join(THIS_DIR, "ninjalog_uploader.py")
  14. CONFIG = os.path.join(THIS_DIR, "ninjalog.cfg")
  15. VERSION = 3
  16. def LoadConfig():
  17. if os.path.isfile(CONFIG):
  18. with open(CONFIG, "r") as f:
  19. try:
  20. config = json.load(f)
  21. except Exception:
  22. # Set default value when failed to load config.
  23. config = {
  24. "is-googler": ninjalog_uploader.IsGoogler(),
  25. "countdown": 10,
  26. "version": VERSION,
  27. }
  28. if config["version"] == VERSION:
  29. config["countdown"] = max(0, config["countdown"] - 1)
  30. return config
  31. return {
  32. "is-googler": ninjalog_uploader.IsGoogler(),
  33. "countdown": 10,
  34. "version": VERSION,
  35. }
  36. def SaveConfig(config):
  37. with open(CONFIG, "w") as f:
  38. json.dump(config, f)
  39. def ShowMessage(countdown):
  40. allowlisted = "\n".join(
  41. [" * %s" % config for config in ninjalog_uploader.ALLOWLISTED_CONFIGS])
  42. print("""
  43. Your ninjalog will be uploaded to build stats server. The uploaded log will be
  44. used to analyze user side build performance.
  45. The following information will be uploaded with ninjalog.
  46. * OS (e.g. Win, Mac or Linux)
  47. * number of cpu cores of building machine
  48. * build targets (e.g. chrome, browser_tests)
  49. * parallelism passed by -j flag
  50. * following build configs
  51. %s
  52. Uploading ninjalog will be started after you run autoninja another %d time(s).
  53. If you don't want to upload ninjalog, please run following command.
  54. $ python3 %s opt-out
  55. If you want to allow upload ninjalog from next autoninja run, please run the
  56. following command.
  57. $ python3 %s opt-in
  58. If you have questions about this, please send mail to infra-dev@chromium.org
  59. You can find a more detailed explanation in
  60. %s
  61. or
  62. https://chromium.googlesource.com/chromium/tools/depot_tools/+/main/ninjalog.README.md
  63. """ % (
  64. allowlisted,
  65. countdown,
  66. __file__,
  67. __file__,
  68. os.path.abspath(os.path.join(THIS_DIR, "ninjalog.README.md")),
  69. ))
  70. def main():
  71. config = LoadConfig()
  72. if len(sys.argv) == 2 and sys.argv[1] == "opt-in":
  73. config["opt-in"] = True
  74. config["countdown"] = 0
  75. SaveConfig(config)
  76. print("ninjalog upload is opted in.")
  77. return 0
  78. if len(sys.argv) == 2 and sys.argv[1] == "opt-out":
  79. config["opt-in"] = False
  80. SaveConfig(config)
  81. print("ninjalog upload is opted out.")
  82. return 0
  83. if "opt-in" in config and not config["opt-in"]:
  84. # Upload is opted out.
  85. return 0
  86. if not config.get("is-googler", False):
  87. # Not googler.
  88. return 0
  89. if config.get("countdown", 0) > 0:
  90. # Need to show message.
  91. ShowMessage(config["countdown"])
  92. # Only save config if something has meaningfully changed.
  93. SaveConfig(config)
  94. return 0
  95. if len(sys.argv) == 1:
  96. # dry-run for debugging.
  97. print("upload ninjalog dry-run")
  98. return 0
  99. # Run upload script without wait.
  100. devnull = open(os.devnull, "w")
  101. creationflags = 0
  102. if platform.system() == "Windows":
  103. creationflags = subprocess.CREATE_NEW_PROCESS_GROUP
  104. subprocess2.Popen(
  105. [sys.executable, UPLOADER] + sys.argv[1:],
  106. stdout=devnull,
  107. stderr=devnull,
  108. creationflags=creationflags,
  109. )
  110. if __name__ == "__main__":
  111. sys.exit(main())