update_glslang_sources.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #!/usr/bin/env python
  2. # Copyright 2017 The Glslang Authors. All rights reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """Get source files for Glslang and its dependencies from public repositories.
  16. """
  17. from __future__ import print_function
  18. import argparse
  19. import json
  20. import distutils.dir_util
  21. import os.path
  22. import subprocess
  23. import sys
  24. KNOWN_GOOD_FILE = 'known_good.json'
  25. SITE_TO_KNOWN_GOOD_FILE = { 'github' : 'known_good.json',
  26. 'gitlab' : 'known_good_khr.json' }
  27. # Maps a site name to its hostname.
  28. SITE_TO_HOST = { 'github' : 'https://github.com/',
  29. 'gitlab' : 'git@gitlab.khronos.org:' }
  30. VERBOSE = True
  31. def command_output(cmd, directory, fail_ok=False):
  32. """Runs a command in a directory and returns its standard output stream.
  33. Captures the standard error stream.
  34. Raises a RuntimeError if the command fails to launch or otherwise fails.
  35. """
  36. if VERBOSE:
  37. print('In {d}: {cmd}'.format(d=directory, cmd=cmd))
  38. p = subprocess.Popen(cmd,
  39. cwd=directory,
  40. stdout=subprocess.PIPE)
  41. (stdout, _) = p.communicate()
  42. if p.returncode != 0 and not fail_ok:
  43. raise RuntimeError('Failed to run {} in {}'.format(cmd, directory))
  44. if VERBOSE:
  45. print(stdout)
  46. return stdout
  47. def command_retval(cmd, directory):
  48. """Runs a command in a directory and returns its return value.
  49. Captures the standard error stream.
  50. """
  51. p = subprocess.Popen(cmd,
  52. cwd=directory,
  53. stdout=subprocess.PIPE)
  54. p.communicate()
  55. return p.returncode
  56. class GoodCommit(object):
  57. """Represents a good commit for a repository."""
  58. def __init__(self, json):
  59. """Initializes this good commit object.
  60. Args:
  61. 'json': A fully populated JSON object describing the commit.
  62. """
  63. self._json = json
  64. self.name = json['name']
  65. self.site = json['site']
  66. self.subrepo = json['subrepo']
  67. self.subdir = json['subdir'] if ('subdir' in json) else '.'
  68. self.commit = json['commit']
  69. def GetUrl(self):
  70. """Returns the URL for the repository."""
  71. host = SITE_TO_HOST[self.site]
  72. return '{host}{subrepo}'.format(
  73. host=host,
  74. subrepo=self.subrepo)
  75. def AddRemote(self):
  76. """Add the remote 'known-good' if it does not exist."""
  77. remotes = command_output(['git', 'remote'], self.subdir).splitlines()
  78. if 'known-good' not in remotes:
  79. command_output(['git', 'remote', 'add', 'known-good', self.GetUrl()], self.subdir)
  80. def HasCommit(self):
  81. """Check if the repository contains the known-good commit."""
  82. return 0 == subprocess.call(['git', 'rev-parse', '--verify', '--quiet',
  83. self.commit + "^{commit}"],
  84. cwd=self.subdir)
  85. def Clone(self):
  86. distutils.dir_util.mkpath(self.subdir)
  87. command_output(['git', 'clone', self.GetUrl(), '.'], self.subdir)
  88. def Fetch(self):
  89. command_output(['git', 'fetch', 'known-good'], self.subdir)
  90. def Checkout(self):
  91. if not os.path.exists(os.path.join(self.subdir,'.git')):
  92. self.Clone()
  93. self.AddRemote()
  94. if not self.HasCommit():
  95. self.Fetch()
  96. command_output(['git', 'checkout', self.commit], self.subdir)
  97. def GetGoodCommits(site):
  98. """Returns the latest list of GoodCommit objects."""
  99. known_good_file = SITE_TO_KNOWN_GOOD_FILE[site]
  100. with open(known_good_file) as known_good:
  101. return [GoodCommit(c) for c in json.loads(known_good.read())['commits']]
  102. def main():
  103. parser = argparse.ArgumentParser(description='Get Glslang source dependencies at a known-good commit')
  104. parser.add_argument('--dir', dest='dir', default='.',
  105. help="Set target directory for Glslang source root. Default is \'.\'.")
  106. parser.add_argument('--site', dest='site', default='github',
  107. help="Set git server site. Default is github.")
  108. args = parser.parse_args()
  109. commits = GetGoodCommits(args.site)
  110. distutils.dir_util.mkpath(args.dir)
  111. print('Change directory to {d}'.format(d=args.dir))
  112. os.chdir(args.dir)
  113. # Create the subdirectories in sorted order so that parent git repositories
  114. # are created first.
  115. for c in sorted(commits, key=lambda x: x.subdir):
  116. print('Get {n}\n'.format(n=c.name))
  117. c.Checkout()
  118. sys.exit(0)
  119. if __name__ == '__main__':
  120. main()