update_glslang_sources.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #!/usr/bin/env python3
  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. import argparse
  18. import json
  19. import os
  20. import subprocess
  21. import sys
  22. KNOWN_GOOD_FILE = 'known_good.json'
  23. SITE_TO_KNOWN_GOOD_FILE = { 'github' : 'known_good.json',
  24. 'gitlab' : 'known_good_khr.json' }
  25. # Maps a site name to its hostname.
  26. SITE_TO_HOST = { 'github' : 'https://github.com/',
  27. 'gitlab' : 'git@gitlab.khronos.org:' }
  28. VERBOSE = True
  29. def command_output(cmd, directory, fail_ok=False):
  30. """Runs a command in a directory and returns its standard output stream.
  31. Captures the standard error stream.
  32. Raises a RuntimeError if the command fails to launch or otherwise fails.
  33. """
  34. if VERBOSE:
  35. print('In {d}: {cmd}'.format(d=directory, cmd=cmd))
  36. p = subprocess.Popen(cmd,
  37. cwd=directory,
  38. stdout=subprocess.PIPE)
  39. (stdout, _) = p.communicate()
  40. if p.returncode != 0 and not fail_ok:
  41. raise RuntimeError('Failed to run {} in {}'.format(cmd, directory))
  42. if VERBOSE:
  43. print(stdout)
  44. return stdout
  45. def command_retval(cmd, directory):
  46. """Runs a command in a directory and returns its return value.
  47. Captures the standard error stream.
  48. """
  49. p = subprocess.Popen(cmd,
  50. cwd=directory,
  51. stdout=subprocess.PIPE)
  52. p.communicate()
  53. return p.returncode
  54. class GoodCommit(object):
  55. """Represents a good commit for a repository."""
  56. def __init__(self, json):
  57. """Initializes this good commit object.
  58. Args:
  59. 'json': A fully populated JSON object describing the commit.
  60. """
  61. self._json = json
  62. self.name = json['name']
  63. self.site = json['site']
  64. self.subrepo = json['subrepo']
  65. self.subdir = json['subdir'] if ('subdir' in json) else '.'
  66. self.commit = json['commit']
  67. def GetUrl(self):
  68. """Returns the URL for the repository."""
  69. host = SITE_TO_HOST[self.site]
  70. return '{host}{subrepo}'.format(
  71. host=host,
  72. subrepo=self.subrepo)
  73. def AddRemote(self):
  74. """Add the remote 'known-good' if it does not exist."""
  75. remotes = command_output(['git', 'remote'], self.subdir).splitlines()
  76. if b'known-good' not in remotes:
  77. command_output(['git', 'remote', 'add', 'known-good', self.GetUrl()], self.subdir)
  78. def HasCommit(self):
  79. """Check if the repository contains the known-good commit."""
  80. return 0 == subprocess.call(['git', 'rev-parse', '--verify', '--quiet',
  81. self.commit + "^{commit}"],
  82. cwd=self.subdir)
  83. def Clone(self):
  84. os.makedirs(self.subdir, exist_ok=True)
  85. command_output(['git', 'clone', self.GetUrl(), '.'], self.subdir)
  86. def Fetch(self):
  87. command_output(['git', 'fetch', 'known-good'], self.subdir)
  88. def Checkout(self):
  89. if not os.path.exists(os.path.join(self.subdir,'.git')):
  90. self.Clone()
  91. self.AddRemote()
  92. if not self.HasCommit():
  93. self.Fetch()
  94. command_output(['git', 'checkout', self.commit], self.subdir)
  95. def GetGoodCommits(site):
  96. """Returns the latest list of GoodCommit objects."""
  97. known_good_file = SITE_TO_KNOWN_GOOD_FILE[site]
  98. with open(known_good_file) as known_good:
  99. return [GoodCommit(c) for c in json.loads(known_good.read())['commits']]
  100. def main():
  101. parser = argparse.ArgumentParser(description='Get Glslang source dependencies at a known-good commit')
  102. parser.add_argument('--dir', dest='dir', default='.',
  103. help="Set target directory for Glslang source root. Default is \'.\'.")
  104. parser.add_argument('--site', dest='site', default='github',
  105. help="Set git server site. Default is github.")
  106. args = parser.parse_args()
  107. commits = GetGoodCommits(args.site)
  108. os.makedirs(args.dir, exist_ok=True)
  109. print('Change directory to {d}'.format(d=args.dir))
  110. os.chdir(args.dir)
  111. # Create the subdirectories in sorted order so that parent git repositories
  112. # are created first.
  113. for c in sorted(commits, key=lambda x: x.subdir):
  114. print('Get {n}\n'.format(n=c.name))
  115. c.Checkout()
  116. sys.exit(0)
  117. if __name__ == '__main__':
  118. main()