File size: 3,605 Bytes
065fa03 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | #!/usr/bin/env python3
import json
import logging
import argparse
import os
import boto3
from smart_open import open
s3 = None
def setup_logging():
"""Sets up logging for the script."""
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
def download_contents(blob_id, src_encoding):
"""Downloads and decodes content from S3.
Args:
blob_id (str): The S3 blob ID.
src_encoding (str): The source encoding of the content.
Returns:
str: Decoded content.
"""
logging.info(f"Downloading {blob_id} ...")
s3_url = f"s3://softwareheritage/content/{blob_id}"
try:
with open(s3_url, "rb", compression=".gz", transport_params={"client": s3}) as fin:
content = fin.read().decode(src_encoding)
logging.info(f"Successfully downloaded {blob_id}.")
except Exception as e:
logging.error(f"Failed to download {blob_id}: {e}")
raise
return content
def process_json(js, id_):
"""Processes a single JSON object, downloading its associated content.
Args:
js (dict): JSON object containing blob details.
id_ (int): Unique ID for the processed entry.
Returns:
dict: New JSON entry with downloaded content.
"""
blob_id = js["blob_id"]
src_encoding = js["src_encoding"]
content = download_contents(blob_id, src_encoding)
new_entry = {
"repo_name": js["repo_name"],
"repo_path": js["path"],
"id": id_,
"content": content
}
return new_entry
def download_stack_files(stackv2linksds, stackds):
"""Downloads and processes stack files, saving the results.
Args:
stackv2linksds (str): Path to the input file containing JSON lines.
stackds (str): Path to the output file for processed JSON lines.
"""
logging.info("Initializing S3 session...")
session = boto3.Session(
aws_access_key_id=os.environ.get("AWS_ACCESS_KEY_ID"),
aws_secret_access_key=os.environ.get("AWS_SECRET_ACCESS_KEY")
)
global s3
s3 = session.client("s3")
try:
with open(stackv2linksds) as fin:
lines = fin.readlines()
logging.info(f"Loaded {len(lines)} entries from {stackv2linksds}.")
except Exception as e:
logging.error(f"Failed to read input file {stackv2linksds}: {e}")
raise
data = []
for id_, line in enumerate(lines):
try:
js = json.loads(line)
new_entry = process_json(js, id_)
data.append(json.dumps(new_entry) + "\n")
except Exception as e:
logging.error(f"Error processing line {id_}: {e}")
try:
with open(stackds, 'w') as fout:
fout.writelines(data)
logging.info(f"Successfully wrote processed data to {stackds}.")
except Exception as e:
logging.error(f"Failed to write output file {stackds}: {e}")
raise
def main():
"""Main function to parse arguments and execute the script."""
parser = argparse.ArgumentParser(description="Download and process stack files.")
parser.add_argument("input_file", help="Path to the input file containing JSON lines.")
parser.add_argument("output_file", help="Path to the output file for processed JSON lines.")
args = parser.parse_args()
setup_logging()
try:
download_stack_files(args.input_file, args.output_file)
except Exception as e:
logging.critical(f"Script failed with error: {e}")
sys.exit(1)
if __name__ == "__main__":
main()
|