Corentin Wallez | 59382b7 | 2020-04-17 20:43:07 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Corentin Wallez | 59e7fad | 2018-08-16 15:32:35 +0200 | [diff] [blame] | 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. |
| 15 | import os, sys, json |
| 16 | |
| 17 | if __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 Kleckner | 48e7da7 | 2020-01-30 01:25:08 +0000 | [diff] [blame] | 27 | for (name, content) in files.items(): |
Corentin Wallez | 59e7fad | 2018-08-16 15:32:35 +0200 | [diff] [blame] | 28 | output_file = output_dir + os.path.sep + name |
| 29 | |
Corentin Wallez | c48e487 | 2021-03-23 19:06:02 +0000 | [diff] [blame] | 30 | # Create the output directory if needed. |
Corentin Wallez | 59e7fad | 2018-08-16 15:32:35 +0200 | [diff] [blame] | 31 | directory = os.path.dirname(output_file) |
| 32 | if not os.path.exists(directory): |
| 33 | os.makedirs(directory) |
| 34 | |
Corentin Wallez | c48e487 | 2021-03-23 19:06:02 +0000 | [diff] [blame] | 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 | |
Corentin Wallez | 59e7fad | 2018-08-16 15:32:35 +0200 | [diff] [blame] | 43 | with open(output_file, 'w') as outfile: |
| 44 | outfile.write(content) |