blob: dcca993e35016bc23b3238ec28f7ca63c0316841 [file] [edit]
"""This script is part of the generation process that produces the list of licenses in src/CREDITS.fuchsia."""
import json
def snip_string(text, prefix):
lines = text.splitlines()
for i, line in enumerate(lines):
if line.startswith(prefix):
return '\n'.join(lines[:i])
return text
def remove_line(text, prefix):
lines = text.splitlines()
new_lines = [line for line in lines if not line.startswith(prefix)]
return '\n'.join(new_lines)
def extract_data(filepath):
"""
Loads a JSON file and extracts Name, Filepath, and Text from each object.
Args:
filepath: The path to the JSON file.
Returns:
A list of dictionaries, where each dictionary contains the extracted data
for one object.
"""
try:
with open(filepath, 'r') as f:
data = json.load(f)
except FileNotFoundError:
print(f"Error: File not found at {filepath}")
return None
except json.JSONDecodeError:
print(f"Error: Invalid JSON format in {filepath}")
return None
extracted_data = []
for item in data:
filepath = item["Filepath"]
if filepath in [
"METADATA",
"docs/license.rst",
"src/compiler/nir/nir_intrinsics_c.py",
"src/compiler/nir/nir_intrinsics_h.py",
"src/compiler/nir/nir_intrinsics_indices_h.py",
"src/imagination/csbgen/gen_pack_header.py",
"src/intel/perf/gen_perf.py",
"fuchsia-build/third_party/fuchsia-infra-bazel-rules/tests/examples/hello_world_cpp/meta/hello_death_test.cml",
]:
continue
current_extracted_data = []
for classification in item["Classifications"]:
name = classification["Name"]
text = classification["Text"]
if name == "Unclassified":
continue
text = remove_line(text, "#ifndef")
text = remove_line(text, "#define")
text = snip_string(text, "#include")
text = snip_string(text, "import")
text = snip_string(text, "struct")
text = snip_string(text, "#ifdef")
text = snip_string(text, "from")
text = snip_string(text, "# error")
text = snip_string(text, r" * \file rogue")
text = snip_string(text, "#pragma")
text = snip_string(text, "static")
text = snip_string(text, '"""Helper functions')
text = snip_string(text, '# A BUILD file')
text = snip_string(text, '# TODO')
text = snip_string(text, 'def ')
text = snip_string(text, 'exports_files')
text = snip_string(text, 'load(')
text = snip_string(text, 'min_shac_version')
text = snip_string(text, '# Autogenerated')
text = snip_string(text, "template <")
text = snip_string(text, "# Leave this line")
text = snip_string(text, "/* Only leaving the")
current_extracted_data.append({
"Name": name,
"Filepath": filepath,
"Text": text.strip()
})
if len(current_extracted_data) > 0:
extracted_data.extend(current_extracted_data)
return extracted_data
# Example usage:
filepath = "output-include-text.json" # Replace with your actual file path
extracted_data = extract_data(filepath)
mymap = {}
for d in extracted_data:
if d["Text"] not in mymap:
mymap[d["Text"]] = {
"Name": d["Name"],
"Text": d["Text"],
"Filepaths": []
}
mymap[d["Text"]]["Filepaths"].append(d["Filepath"])
# Open the file in write mode ('w')
file = open("CREDITS.fuchsia", "w")
for key, value in sorted(mymap.items(),
key=lambda item:
(item[1]['Name'].lower(), item[1]['Text'])):
file.write("=============================\n")
file.write("{}\n\n".format(value["Name"]))
for f in value["Filepaths"]:
file.write("-> File: {}\n".format(f))
file.write("\n")
file.write(value["Text"] + "\n\n")
file.close()
if extracted_data:
print(f"Successfully extracted data from {len(extracted_data)} objects.")
# Print the first 5 items as an example:
for i in range(min(5, len(extracted_data))):
print(f"Item {i+1}: {extracted_data[i]}")
print("-" * 20)
# You can now process the extracted_data list further as needed.