import boto3
import os

bucket_name = 'p81-devops'
folder_path = 'files/live_automation_piplines/'

s3_client = boto3.client('s3')

try:
    # Use the list_objects_v2 method to list objects in the specified folder
    response = s3_client.list_objects_v2(Bucket=bucket_name, Prefix=folder_path)
except Exception as e:
    print(f"An error occurred: {e}")
    os.system(f'echo "FILE_NOT_EXIST=true" >> $GITHUB_ENV')
else:
    # Check if the folder is empty or not
    if 'Contents' not in response:
        print("No files are there.")
        os.system(f'echo "FILE_NOT_EXIST=true" >> $GITHUB_ENV')
    else:
        os.system(f'echo "FILE_EXIST=true" >> $GITHUB_ENV')
        print("Live automation pipelines:")
        os.system(f'echo ":stop_sign: some pipelines are live!!!!!!! aborting update" >> $GITHUB_STEP_SUMMARY')
        os.system(f'echo "Live automation pipelines:" >> $GITHUB_STEP_SUMMARY')
        for obj in response['Contents']:
            # Extract the file name and creation time
            file_key = obj['Key'].replace(folder_path, '')
            if file_key:
                creation_time = obj['LastModified'].strftime("%Y-%m-%d %H:%M:%S")
                print(f"{file_key} - Started at: {creation_time}")
                os.system(f'echo "{file_key} - Started at: {creation_time}" >> $GITHUB_STEP_SUMMARY')
