ninjalog_uploader_wrapper.py 3.7 KB

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