check_rosdistro_repos.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #!/usr/bin/env python
  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. from __future__ import print_function
  29. import argparse
  30. import shutil
  31. import subprocess
  32. import sys
  33. import tempfile
  34. from catkin_pkg.packages import find_package_paths
  35. from rosdistro import get_distribution_file, get_index, get_index_url
  36. def check_git_repo(url, version):
  37. cmd = ['git', 'ls-remote', url]
  38. try:
  39. output = subprocess.check_output(cmd)
  40. except subprocess.CalledProcessError as e:
  41. raise RuntimeError('not a valid git repo url')
  42. if version:
  43. for line in output.splitlines():
  44. if line.endswith('/%s' % version):
  45. return
  46. raise RuntimeError('version not found')
  47. def check_hg_repo(url, version):
  48. cmd = ['hg', 'identify', url]
  49. if version:
  50. cmd.extend(['-r', version])
  51. try:
  52. subprocess.check_output(cmd, stderr=subprocess.STDOUT)
  53. except subprocess.CalledProcessError as e:
  54. if not version:
  55. raise RuntimeError('not a valid hg repo url')
  56. cmd = ['hg', 'identify', url]
  57. try:
  58. subprocess.check_output(cmd, stderr=subprocess.STDOUT)
  59. except subprocess.CalledProcessError as e:
  60. raise RuntimeError('not a valid hg repo url')
  61. raise RuntimeError('version not found')
  62. def check_svn_repo(url, version):
  63. cmd = ['svn', '--non-interactive', '--trust-server-cert', 'info', url]
  64. if version:
  65. cmd.extend(['-r', version])
  66. try:
  67. output = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
  68. except subprocess.CalledProcessError as e:
  69. raise RuntimeError('not a valid svn repo url')
  70. def clone_git_repo(url, version, path):
  71. cmd = ['git', 'clone', url, '-b', version, '-q']
  72. try:
  73. subprocess.check_call(cmd, cwd=path)
  74. except subprocess.CalledProcessError as e:
  75. raise RuntimeError('not a valid git repo url')
  76. def clone_hg_repo(url, version, path):
  77. cmd = ['hg', 'clone', url, '-q']
  78. if version:
  79. cmd.extend(['-b', version])
  80. try:
  81. subprocess.check_call(cmd, stderr=subprocess.STDOUT, cwd=path)
  82. except subprocess.CalledProcessError as e:
  83. raise RuntimeError('not a valid hg repo url')
  84. def checkout_svn_repo(url, version, path):
  85. cmd = ['svn', '--non-interactive', '--trust-server-cert', 'checkout', url, '-q']
  86. if version:
  87. cmd.extend(['-r', version])
  88. try:
  89. subprocess.check_call(cmd, stderr=subprocess.STDOUT, cwd=path)
  90. except subprocess.CalledProcessError as e:
  91. raise RuntimeError('not a valid svn repo url')
  92. def main(repo_type, rosdistro_name, check_for_wet_packages=False):
  93. index = get_index(get_index_url())
  94. try:
  95. distribution_file = get_distribution_file(index, rosdistro_name)
  96. except RuntimeError as e:
  97. print("Could not load distribution file for distro '%s': %s" % (rosdistro_name, e), file=sys.stderr)
  98. return False
  99. for repo_name in sorted(distribution_file.repositories.keys()):
  100. sys.stdout.write('.')
  101. sys.stdout.flush()
  102. repo = distribution_file.repositories[repo_name]
  103. if repo_type == 'doc':
  104. repo = repo.doc_repository
  105. if repo_type == 'source':
  106. repo = repo.source_repository
  107. if not repo:
  108. continue
  109. try:
  110. if (repo.type == 'git'):
  111. check_git_repo(repo.url, repo.version)
  112. elif (repo.type == 'hg'):
  113. check_hg_repo(repo.url, repo.version)
  114. elif (repo.type == 'svn'):
  115. check_svn_repo(repo.url, repo.version)
  116. else:
  117. print()
  118. print("Unknown type '%s' for repository '%s'" % (repo.type, repo.name), file=sys.stderr)
  119. continue
  120. except RuntimeError as e:
  121. print()
  122. print("Could not fetch repository '%s': %s (%s) [%s]" % (repo.name, repo.url, repo.version, e), file=sys.stderr)
  123. continue
  124. if check_for_wet_packages:
  125. path = tempfile.mkdtemp()
  126. try:
  127. if repo.type == 'git':
  128. clone_git_repo(repo.url, repo.version, path)
  129. elif repo.type == 'hg':
  130. clone_hg_repo(repo.url, repo.version, path)
  131. elif repo.type == 'svn':
  132. checkout_svn_repo(repo.url, repo.version, path)
  133. except RuntimeError as e:
  134. print()
  135. print("Could not clone repository '%s': %s (%s) [%s]" % (repo.name, repo.url, repo.version, e), file=sys.stderr)
  136. continue
  137. else:
  138. package_paths = find_package_paths(path)
  139. if not package_paths:
  140. print()
  141. print("Repository '%s' (%s [%s]) does not contain any wet packages" % (repo.name, repo.url, repo.version), file=sys.stderr)
  142. continue
  143. finally:
  144. shutil.rmtree(path)
  145. print()
  146. return True
  147. if __name__ == '__main__':
  148. parser = argparse.ArgumentParser(description='Checks whether the referenced branches for the doc/source repositories exist')
  149. parser.add_argument('repo_type', choices=['doc', 'source'], help='The repository type')
  150. parser.add_argument('rosdistro_name', help='The ROS distro name')
  151. parser.add_argument('--check-for-wet-packages', action='store_true', help='Check if the repository contains wet packages rather then dry packages')
  152. args = parser.parse_args()
  153. if not main(args.repo_type, args.rosdistro_name, args.check_for_wet_packages):
  154. sys.exit(1)