blob: 65be2f6a2ce52aea9f1f23d32db4b8138b79ade5 [file] [log] [blame]
GregFcd1f1692017-09-21 18:40:22 -06001#!/usr/bin/env python
2
3# Copyright 2017 The Glslang Authors. All rights reserved.
4#
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
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
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.
16
17"""Get source files for Glslang and its dependencies from public repositories.
18"""
19
20from __future__ import print_function
21
22import argparse
23import json
24import distutils.dir_util
25import os.path
26import subprocess
27import sys
28
29KNOWN_GOOD_FILE = 'known_good.json'
30
GregF484cbd02018-02-08 15:56:54 -070031SITE_TO_KNOWN_GOOD_FILE = { 'github' : 'known_good.json',
32 'gitlab' : 'known_good_khr.json' }
33
GregFcd1f1692017-09-21 18:40:22 -060034# Maps a site name to its hostname.
GregF484cbd02018-02-08 15:56:54 -070035SITE_TO_HOST = { 'github' : 'https://github.com/',
36 'gitlab' : 'git@gitlab.khronos.org:' }
GregFcd1f1692017-09-21 18:40:22 -060037
38VERBOSE = True
39
40
41def command_output(cmd, directory, fail_ok=False):
42 """Runs a command in a directory and returns its standard output stream.
43
44 Captures the standard error stream.
45
46 Raises a RuntimeError if the command fails to launch or otherwise fails.
47 """
48 if VERBOSE:
49 print('In {d}: {cmd}'.format(d=directory, cmd=cmd))
50 p = subprocess.Popen(cmd,
51 cwd=directory,
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))
56 if VERBOSE:
57 print(stdout)
58 return stdout
59
60
61def command_retval(cmd, directory):
62 """Runs a command in a directory and returns its return value.
63
64 Captures the standard error stream.
65 """
66 p = subprocess.Popen(cmd,
67 cwd=directory,
68 stdout=subprocess.PIPE)
Karl Schultzfa403b92018-06-21 17:30:07 -060069 p.communicate()
GregFcd1f1692017-09-21 18:40:22 -060070 return p.returncode
71
72
73class GoodCommit(object):
74 """Represents a good commit for a repository."""
75
76 def __init__(self, json):
77 """Initializes this good commit object.
78
79 Args:
80 'json': A fully populated JSON object describing the commit.
81 """
82 self._json = json
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']
88
GregF484cbd02018-02-08 15:56:54 -070089 def GetUrl(self):
GregFcd1f1692017-09-21 18:40:22 -060090 """Returns the URL for the repository."""
91 host = SITE_TO_HOST[self.site]
GregF484cbd02018-02-08 15:56:54 -070092 return '{host}{subrepo}'.format(
GregFcd1f1692017-09-21 18:40:22 -060093 host=host,
GregFcd1f1692017-09-21 18:40:22 -060094 subrepo=self.subrepo)
95
96 def AddRemote(self):
97 """Add the remote 'known-good' if it does not exist."""
Karl Schultzfa403b92018-06-21 17:30:07 -060098 remotes = command_output(['git', 'remote'], self.subdir).splitlines()
Karl Schultz0cb8ad52018-08-27 14:06:38 -060099 if b'known-good' not in remotes:
GregFcd1f1692017-09-21 18:40:22 -0600100 command_output(['git', 'remote', 'add', 'known-good', self.GetUrl()], self.subdir)
101
102 def HasCommit(self):
103 """Check if the repository contains the known-good commit."""
104 return 0 == subprocess.call(['git', 'rev-parse', '--verify', '--quiet',
105 self.commit + "^{commit}"],
106 cwd=self.subdir)
107
108 def Clone(self):
109 distutils.dir_util.mkpath(self.subdir)
110 command_output(['git', 'clone', self.GetUrl(), '.'], self.subdir)
111
112 def Fetch(self):
113 command_output(['git', 'fetch', 'known-good'], self.subdir)
114
115 def Checkout(self):
116 if not os.path.exists(os.path.join(self.subdir,'.git')):
117 self.Clone()
118 self.AddRemote()
119 if not self.HasCommit():
120 self.Fetch()
121 command_output(['git', 'checkout', self.commit], self.subdir)
122
123
GregF484cbd02018-02-08 15:56:54 -0700124def GetGoodCommits(site):
GregFcd1f1692017-09-21 18:40:22 -0600125 """Returns the latest list of GoodCommit objects."""
GregF484cbd02018-02-08 15:56:54 -0700126 known_good_file = SITE_TO_KNOWN_GOOD_FILE[site]
127 with open(known_good_file) as known_good:
GregFcd1f1692017-09-21 18:40:22 -0600128 return [GoodCommit(c) for c in json.loads(known_good.read())['commits']]
129
130
131def main():
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 \'.\'.")
GregF484cbd02018-02-08 15:56:54 -0700135 parser.add_argument('--site', dest='site', default='github',
136 help="Set git server site. Default is github.")
GregFcd1f1692017-09-21 18:40:22 -0600137
138 args = parser.parse_args()
139
GregF484cbd02018-02-08 15:56:54 -0700140 commits = GetGoodCommits(args.site)
GregFcd1f1692017-09-21 18:40:22 -0600141
142 distutils.dir_util.mkpath(args.dir)
143 print('Change directory to {d}'.format(d=args.dir))
144 os.chdir(args.dir)
145
146 # Create the subdirectories in sorted order so that parent git repositories
147 # are created first.
GregF3f9c03b2017-10-27 15:39:45 -0600148 for c in sorted(commits, key=lambda x: x.subdir):
GregFcd1f1692017-09-21 18:40:22 -0600149 print('Get {n}\n'.format(n=c.name))
150 c.Checkout()
151 sys.exit(0)
152
153
154if __name__ == '__main__':
155 main()