3 # Copyright 2017 The Glslang Authors. All rights reserved.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 """Get source files for Glslang and its dependencies from public repositories.
20 from __future__ import print_function
24 import distutils.dir_util
29 KNOWN_GOOD_FILE = 'known_good.json'
31 SITE_TO_KNOWN_GOOD_FILE = { 'github' : 'known_good.json',
32 'gitlab' : 'known_good_khr.json' }
34 # Maps a site name to its hostname.
35 SITE_TO_HOST = { 'github' : 'https://github.com/',
36 'gitlab' : 'git@gitlab.khronos.org:' }
41 def command_output(cmd, directory, fail_ok=False):
42 """Runs a command in a directory and returns its standard output stream.
44 Captures the standard error stream.
46 Raises a RuntimeError if the command fails to launch or otherwise fails.
49 print('In {d}: {cmd}'.format(d=directory, cmd=cmd))
50 p = subprocess.Popen(cmd,
52 stdout=subprocess.PIPE)
53 (stdout, _) = p.communicate()
54 if p.returncode != 0 and not fail_ok:
55 raise RuntimeError('Failed to run {} in {}'.format(cmd, directory))
61 def command_retval(cmd, directory):
62 """Runs a command in a directory and returns its return value.
64 Captures the standard error stream.
66 p = subprocess.Popen(cmd,
68 stdout=subprocess.PIPE)
73 class GoodCommit(object):
74 """Represents a good commit for a repository."""
76 def __init__(self, json):
77 """Initializes this good commit object.
80 'json': A fully populated JSON object describing the commit.
83 self.name = json['name']
84 self.site = json['site']
85 self.subrepo = json['subrepo']
86 self.subdir = json['subdir'] if ('subdir' in json) else '.'
87 self.commit = json['commit']
90 """Returns the URL for the repository."""
91 host = SITE_TO_HOST[self.site]
92 return '{host}{subrepo}'.format(
97 """Add the remote 'known-good' if it does not exist."""
98 remotes = command_output(['git', 'remote'], self.subdir).splitlines()
99 if b'known-good' not in remotes:
100 command_output(['git', 'remote', 'add', 'known-good', self.GetUrl()], self.subdir)
103 """Check if the repository contains the known-good commit."""
104 return 0 == subprocess.call(['git', 'rev-parse', '--verify', '--quiet',
105 self.commit + "^{commit}"],
109 distutils.dir_util.mkpath(self.subdir)
110 command_output(['git', 'clone', self.GetUrl(), '.'], self.subdir)
113 command_output(['git', 'fetch', 'known-good'], self.subdir)
116 if not os.path.exists(os.path.join(self.subdir,'.git')):
119 if not self.HasCommit():
121 command_output(['git', 'checkout', self.commit], self.subdir)
124 def GetGoodCommits(site):
125 """Returns the latest list of GoodCommit objects."""
126 known_good_file = SITE_TO_KNOWN_GOOD_FILE[site]
127 with open(known_good_file) as known_good:
128 return [GoodCommit(c) for c in json.loads(known_good.read())['commits']]
132 parser = argparse.ArgumentParser(description='Get Glslang source dependencies at a known-good commit')
133 parser.add_argument('--dir', dest='dir', default='.',
134 help="Set target directory for Glslang source root. Default is \'.\'.")
135 parser.add_argument('--site', dest='site', default='github',
136 help="Set git server site. Default is github.")
138 args = parser.parse_args()
140 commits = GetGoodCommits(args.site)
142 distutils.dir_util.mkpath(args.dir)
143 print('Change directory to {d}'.format(d=args.dir))
146 # Create the subdirectories in sorted order so that parent git repositories
148 for c in sorted(commits, key=lambda x: x.subdir):
149 print('Get {n}\n'.format(n=c.name))
154 if __name__ == '__main__':