blob: 67114bf4fb65e6e0a99516945db4cc224fb2399e [file] [log] [blame]
Corentin Wallez59382b72020-04-17 20:43:07 +00001#!/usr/bin/env python3
Corentin Wallez59e7fad2018-08-16 15:32:35 +02002# Copyright 2018 The Dawn Authors
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.
15import os, sys, json
16
17if __name__ == "__main__":
18 if len(sys.argv) != 3:
19 print("Usage: extract_json.py JSON DIR")
20 sys.exit(1)
21
22 with open(sys.argv[1]) as f:
23 files = json.loads(f.read())
24
25 output_dir = sys.argv[2]
26
Reid Kleckner48e7da72020-01-30 01:25:08 +000027 for (name, content) in files.items():
Corentin Wallez59e7fad2018-08-16 15:32:35 +020028 output_file = output_dir + os.path.sep + name
29
Corentin Wallezc48e4872021-03-23 19:06:02 +000030 # Create the output directory if needed.
Corentin Wallez59e7fad2018-08-16 15:32:35 +020031 directory = os.path.dirname(output_file)
32 if not os.path.exists(directory):
33 os.makedirs(directory)
34
Corentin Wallezc48e4872021-03-23 19:06:02 +000035 # Skip writing to the file if it already has the correct content.
36 try:
37 with open(output_file, 'r') as outfile:
38 if outfile.read() == content:
39 continue
40 except (OSError, EnvironmentError):
41 pass
42
Corentin Wallez59e7fad2018-08-16 15:32:35 +020043 with open(output_file, 'w') as outfile:
44 outfile.write(content)