|
| 1 | +# |
| 2 | +# Copyright (c) nexB Inc. and others. All rights reserved. |
| 3 | +# ScanCode is a trademark of nexB Inc. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | +# See http://www.apache.org/licenses/LICENSE-2.0 for the license text. |
| 6 | +# See https://github.com/nexB/extractcode for support or download. |
| 7 | +# See https://aboutcode.org for more information about nexB OSS projects. |
| 8 | +# |
| 9 | + |
| 10 | +import json |
| 11 | +import os.path |
| 12 | +import posixpath |
| 13 | + |
| 14 | +from commoncode import fileutils |
| 15 | +from commoncode import paths |
| 16 | + |
| 17 | +import extractcode |
| 18 | + |
| 19 | +""" |
| 20 | +Utilities to parse source map files and treat them as if they were |
| 21 | +archives containing files. |
| 22 | +""" |
| 23 | + |
| 24 | + |
| 25 | +def extract(location, target_dir): |
| 26 | + """ |
| 27 | + Extract each source in sourcesContent list of a map file at `location` as |
| 28 | + files in a target_dir directory tree mimicking the directory in which the |
| 29 | + sources would be present. |
| 30 | + |
| 31 | + Return a list of warning messages. Raise Exception errors. |
| 32 | + """ |
| 33 | + for path, content in extract_source_content_from_map(location): |
| 34 | + # Convert path to safe posix path |
| 35 | + map_subfile_path = paths.safe_path(path, preserve_spaces=True) |
| 36 | + |
| 37 | + # Create directories |
| 38 | + parent_dir = posixpath.dirname(map_subfile_path) |
| 39 | + parent_target_dir = os.path.join(target_dir, parent_dir) |
| 40 | + fileutils.create_dir(parent_target_dir) |
| 41 | + |
| 42 | + subfile_path = os.path.join(target_dir, map_subfile_path) |
| 43 | + with open(subfile_path, "w") as subfile: |
| 44 | + subfile.write(content) |
| 45 | + |
| 46 | + return [] |
| 47 | + |
| 48 | + |
| 49 | +def extract_source_content_from_map(location): |
| 50 | + """ |
| 51 | + Return a list of tuples of (source, content) |
| 52 | + for each source in sourcesContent of a map file at location. |
| 53 | +
|
| 54 | + Raise an exception if the file is not a JSON file or cannot be parsed. |
| 55 | + """ |
| 56 | + try: |
| 57 | + with open(location, "r") as map_file: |
| 58 | + map_data = json.load(map_file) |
| 59 | + except json.JSONDecodeError as e: |
| 60 | + msg = f"Unable to decode map file:{location} {e}" |
| 61 | + raise extractcode.ExtractErrorFailedToExtract(msg) |
| 62 | + |
| 63 | + if "sourcesContent" in map_data: |
| 64 | + sources_content = map_data["sourcesContent"] |
| 65 | + sources = map_data.get("sources", []) |
| 66 | + |
| 67 | + # Inconsistent source map. In a valid source map, each entry in the ``sources`` |
| 68 | + # list should have a corresponding entry in the ``sourcesContent`` list. |
| 69 | + # Use dummy filenames as `source` path in such scenario. |
| 70 | + if len(sources) != len(sources_content): |
| 71 | + sources = [ |
| 72 | + f"source_content{i + 1}.txt" for i in range(len(sources_content)) |
| 73 | + ] |
| 74 | + |
| 75 | + sources_and_content = list(zip(sources, sources_content)) |
| 76 | + return sources_and_content |
| 77 | + |
| 78 | + return [] |
0 commit comments