Merge pull request #3021 from qingyuanzNV/fix_opline_prepending_opfunction_with_pp
[platform/upstream/glslang.git] / update_glslang_sources.py
1 #!/usr/bin/env python3
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 SITE_TO_KNOWN_GOOD_FILE = { 'github' : 'known_good.json',
32                             'gitlab' : 'known_good_khr.json' }
33
34 # Maps a site name to its hostname.
35 SITE_TO_HOST = { 'github' : 'https://github.com/',
36                  'gitlab' : 'git@gitlab.khronos.org:' }
37
38 VERBOSE = True
39
40
41 def 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
61 def 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)
69     p.communicate()
70     return p.returncode
71
72
73 class 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
89     def GetUrl(self):
90         """Returns the URL for the repository."""
91         host = SITE_TO_HOST[self.site]
92         return '{host}{subrepo}'.format(
93                     host=host,
94                     subrepo=self.subrepo)
95
96     def AddRemote(self):
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)
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
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']]
129
130
131 def 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 \'.\'.")
135     parser.add_argument('--site', dest='site', default='github',
136                         help="Set git server site. Default is github.")
137
138     args = parser.parse_args()
139
140     commits = GetGoodCommits(args.site)
141
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.
148     for c in sorted(commits, key=lambda x: x.subdir):
149         print('Get {n}\n'.format(n=c.name))
150         c.Checkout()
151     sys.exit(0)
152
153
154 if __name__ == '__main__':
155     main()