ninjalog_uploader_wrapper.py 3.2 KB

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