metrics_utils.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/usr/bin/env python
  2. # Copyright (c) 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 scm
  6. import subprocess2
  7. import sys
  8. from third_party import colorama
  9. APP_URL = 'https://cit-cli-metrics.appspot.com'
  10. NOTICE_COUNTDOWN_HEADER = (
  11. '*****************************************************\n'
  12. '* METRICS COLLECTION WILL START IN %2d EXECUTIONS *'
  13. )
  14. NOTICE_COLLECTION_HEADER = (
  15. '*****************************************************\n'
  16. '* METRICS COLLECTION IS TAKING PLACE *'
  17. )
  18. NOTICE_FOOTER = (
  19. '* *\n'
  20. '* For more information, and for how to disable this *\n'
  21. '* message, please see metrics.README.md in your *\n'
  22. '* depot_tools checkout. *\n'
  23. '*****************************************************\n'
  24. )
  25. KNOWN_PROJECT_URLS = {
  26. 'https://chrome-internal.googlesource.com/chrome/ios_internal',
  27. 'https://chrome-internal.googlesource.com/infra/infra_internal',
  28. 'https://chromium.googlesource.com/breakpad/breakpad',
  29. 'https://chromium.googlesource.com/chromium/src',
  30. 'https://chromium.googlesource.com/chromium/tools/depot_tools',
  31. 'https://chromium.googlesource.com/crashpad/crashpad',
  32. 'https://chromium.googlesource.com/external/gyp',
  33. 'https://chromium.googlesource.com/external/naclports',
  34. 'https://chromium.googlesource.com/infra/goma/client',
  35. 'https://chromium.googlesource.com/infra/infra',
  36. 'https://chromium.googlesource.com/native_client/',
  37. 'https://chromium.googlesource.com/syzygy',
  38. 'https://chromium.googlesource.com/v8/v8',
  39. 'https://dart.googlesource.com/sdk',
  40. 'https://pdfium.googlesource.com/pdfium',
  41. 'https://skia.googlesource.com/buildbot',
  42. 'https://skia.googlesource.com/skia',
  43. 'https://webrtc.googlesource.com/src',
  44. }
  45. def get_python_version():
  46. """Return the python version in the major.minor.micro format."""
  47. return '{v.major}.{v.minor}.{v.micro}'.format(v=sys.version_info)
  48. def return_code_from_exception(exception):
  49. """Returns the exit code that would result of raising the exception."""
  50. if exception is None:
  51. return 0
  52. if isinstance(exception[1], SystemExit):
  53. return exception[1].code
  54. return 1
  55. def seconds_to_weeks(duration):
  56. """Transform a |duration| from seconds to weeks approximately.
  57. Drops the lowest 19 bits of the integer representation, which ammounts to
  58. about 6 days.
  59. """
  60. return int(duration) >> 19
  61. def get_repo_timestamp(path_to_repo):
  62. """Get an approximate timestamp for the upstream of |path_to_repo|.
  63. Returns the top two bits of the timestamp of the HEAD for the upstream of the
  64. branch path_to_repo is checked out at.
  65. """
  66. # Get the upstream for the current branch. If we're not in a branch, fallback
  67. # to HEAD.
  68. try:
  69. upstream = scm.GIT.GetUpstreamBranch(path_to_repo)
  70. except subprocess2.CalledProcessError:
  71. upstream = 'HEAD'
  72. # Get the timestamp of the HEAD for the upstream of the current branch.
  73. p = subprocess2.Popen(
  74. ['git', '-C', path_to_repo, 'log', '-n1', upstream, '--format=%at'],
  75. stdout=subprocess2.PIPE, stderr=subprocess2.PIPE)
  76. stdout, _ = p.communicate()
  77. # If there was an error, give up.
  78. if p.returncode != 0:
  79. return None
  80. # Get the age of the checkout in weeks.
  81. return seconds_to_weeks(stdout.strip())
  82. def print_notice(countdown):
  83. """Print a notice to let the user know the status of metrics collection."""
  84. colorama.init()
  85. print colorama.Fore.RED + '\033[1m'
  86. if countdown:
  87. print NOTICE_COUNTDOWN_HEADER % countdown
  88. else:
  89. print NOTICE_COLLECTION_HEADER
  90. print NOTICE_FOOTER + colorama.Style.RESET_ALL