import csv
import os
import shutil
import subprocess
import tempfile


def get_all_magnets():
    # Create temporary csv file
    temp_dir = tempfile.mkdtemp()
    temp_csv_path = os.path.join(temp_dir, 'magnet_devices_oracle.csv')
    os.environ['temp_csv_path'] = temp_csv_path

    # Get data from Oracle
    subprocess.check_output(['sqlplus', '/@SLACPROD', '@mgnt_get_magnets'])

    # Read out elements
    magnets = []
    with open(temp_csv_path) as f:
        reader = csv.reader(f)
        for row in reader:
            magnets.append(row)

    shutil.rmtree(temp_dir)
    return magnets

if __name__ == '__main__':
    elements = get_all_magnets()
    for element in elements:
        print(element)
