• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# 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
27    for (name, content) in files.items():
28        output_file = output_dir + os.path.sep + name
29
30        # Create the output directory if needed.
31        directory = os.path.dirname(output_file)
32        if not os.path.exists(directory):
33            os.makedirs(directory)
34
35        # 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
43        with open(output_file, 'w') as outfile:
44            outfile.write(content)
45