count_rosdistro_packages.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2017, Open Source Robotics Foundation
  3. # All rights reserved.
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions are met:
  7. #
  8. # * Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. # * Redistributions in binary form must reproduce the above copyright
  11. # notice, this list of conditions and the following disclaimer in the
  12. # documentation and/or other materials provided with the distribution.
  13. # * Neither the name of the Willow Garage, Inc. nor the names of its
  14. # contributors may be used to endorse or promote products derived from
  15. # this software without specific prior written permission.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. # POSSIBILITY OF SUCH DAMAGE.
  28. import argparse
  29. from dateutil import parser as dateparser
  30. import os
  31. import rosdistro
  32. import shutil
  33. import subprocess
  34. import tempfile
  35. parser = argparse.ArgumentParser(description='Count packages in the rosdistro')
  36. parser.add_argument('--repo-location', metavar='Path to rosdistro', type=str,
  37. help='The path to the rosdistro checkout')
  38. parser.add_argument('--output-file', metavar='Path to output file', type=str,
  39. help='The path to the output', default='output.csv')
  40. args = parser.parse_args()
  41. # if not os.path.exists(args.index_path):
  42. # parser.error("invalid rosdistro index url")
  43. valid_distros = ['groovy', 'hydro', 'indigo', 'jade', 'kinetic', 'lunar', 'melodic', 'noetic',
  44. 'ardent', 'bouncy', 'crystal', 'dashing', 'eloquent', 'foxy', 'galactic', 'rolling']
  45. FIRST_HASH = 'be9218681f14d0fac908da46902eb2f1dad084fa'
  46. OUTPUT_FILE = args.output_file
  47. def get_all_commits(repo_dir, first_hash):
  48. return subprocess.check_output('git -C %s rev-list --reverse %s..master' % (repo_dir, first_hash), shell=True).decode("utf-8").splitlines()
  49. def get_commit_date(repo_dir, commit):
  50. date_str = subprocess.check_output('git -C %s show -s --format=%%ci %s' % (repo_dir, commit), shell=True).decode("utf-8").strip()
  51. return date_str
  52. def get_rosdistro_counts(index_path):
  53. index_uri = os.path.join(index_path, 'index.yaml')
  54. if not os.path.exists(index_uri):
  55. print('failed to find %s falling back to v4' % index_uri)
  56. index_uri = os.path.join(index_path, 'index-v4.yaml')
  57. if not os.path.exists(index_uri):
  58. print('Could not find index at this path either %s %s' % (index_path, index_uri))
  59. subprocess.call('ls %s' % index_path, shell=True)
  60. return []
  61. index_uri = 'file://' + index_uri
  62. i = rosdistro.get_index(index_uri)
  63. results = []
  64. for d in valid_distros:
  65. try:
  66. d_file = rosdistro.get_distribution_file(i, d)
  67. count = len(d_file.release_packages)
  68. results.append(count)
  69. except:
  70. results.append(0)
  71. return results
  72. def monthly_commits(repo_dir, commits):
  73. '''A generator to downsample commits to be the first one per month.'''
  74. last_year = 0
  75. last_month = 0
  76. for commit in commits:
  77. dt = dateparser.parse(get_commit_date(repo_dir, commit))
  78. if dt.year > last_year:
  79. last_month = 0
  80. last_year = dt.year
  81. if dt.month > last_month:
  82. last_month = dt.month
  83. yield commit
  84. if args.repo_location:
  85. repo_location = args.repo_location
  86. else:
  87. repo_location = tempfile.mkdtemp()
  88. print("created repo_location %s" % repo_location)
  89. try:
  90. if os.path.exists(os.path.join(repo_location, '.git')):
  91. subprocess.check_call('git -C %s fetch' % repo_location, shell=True)
  92. else:
  93. subprocess.check_call('git clone https://github.com/ros/rosdistro.git %s' % repo_location, shell=True)
  94. print("Cloned to %s" % repo_location)
  95. commits = get_all_commits(repo_location, FIRST_HASH)
  96. print("Commits: %s" % len(commits))
  97. csv_strings = []
  98. for commit in monthly_commits(repo_location, commits):
  99. subprocess.check_call('git -C %s clean -fxd' % repo_location, shell=True)
  100. subprocess.check_call('git -C %s checkout --quiet %s' % (repo_location, commit), shell=True)
  101. commit_date = get_commit_date(repo_location, commit)
  102. counts = get_rosdistro_counts(repo_location)
  103. csv_strings.append(", ".join([commit_date] + [str(c) for c in counts]))
  104. print("progress: %s" % csv_strings[-1])
  105. # except Exception as ex:
  106. # print("Exception:: %s" % ex)
  107. finally:
  108. if not args.repo_location:
  109. shutil.rmtree(repo_location)
  110. print("cleaned up repo_location %s" % repo_location)
  111. with open(OUTPUT_FILE, 'w') as outfh:
  112. print("Writing to %s" % OUTPUT_FILE)
  113. outfh.write(', '.join(['date'] + valid_distros))
  114. for l in csv_strings:
  115. outfh.write(l + '\n')