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