#!/data_systems/opt/bin/python3 """ check_for_new_prelim_daisy See if a new preliminary daisy has been created. If so copy it to the destination directory and send out the daily email Created June 2018 @author: Bill Barrett """ import os import re import glob import subprocess import socket # The current AIM CIPS season CURRENT_SEASON = 'north_2018' # The pattern for the preliminary daisy. A typical file name is: # cips_sci_3a_2018-158_v05.20_r01_prelim.png FILE_PATTERN = re.compile(r'''.*(?Pcips_sci_3a_ (?P20\d{2}) \-(?P\d{3}) _v(?P0\d\.\d{2}) _r(?P\d{2}) _prelim.png)''', re.VERBOSE) # The directory where the file is initially CREATION_DIRECTORY = os.environ.get('SDS_ROOT', '/aim/sds') # The root for the destination directory DESTINATION_DIRECTORY_ROOT = os.environ.get('DATA_1A', '/aim/data/cips/') def main(season): """The main driver. Parameters ---------- season : str An AIM CIPS season which is either 'north' or 'south' + _ + the four digit year Examples are 'north_2018', 'south_2009' """ destination_directory = '' processed_daisies = [] # The file is created in '/aim/sds' candidate_files = glob.glob('/aim/sds/*prelim.png') for file in glob.glob('/aim/sds/*prelim.png'): file_match = FILE_PATTERN.match(file) if file_match is not None: filename = file_match.group('filename') if len(destination_directory) == 0: destination_directory = (DESTINATION_DIRECTORY_ROOT + season + '/level_3/ver_' + file_match.group('version') + '/rev_' + file_match.group('revision')) processed_daisies = glob.glob(destination_directory + '/*prelim.png') for daisy in processed_daisies: if daisy.endswith(filename): print('{0} is already in {1}'.format(filename, destination_directory)) candidate_files.remove(file) # Are there any available files to be sent out and has the destination been established? if len(candidate_files) > 0 and len(destination_directory) > 0: for file in candidate_files: file_match = FILE_PATTERN.match(file) create_and_execute_commands(file_match.group('filename'), destination_directory, file_match.group('year'), file_match.group('day_of_year')) else: print('No new preliminary daisy files are available at present') def create_and_execute_commands(file, destination_directory, year, day_of_year): """ Create the commands to copy the file to the destination directory and send out the preliminary daisy email Parameters ---------- file : str the name of the file to be transferred destination_directory : str the directory to which the file is being written year : str a four digit year as a string, e.g. '2018' day_of_year : str a three digit day of year as a string, e.g. '014' """ # Preserve as many of the file attributes, especially the date, as possible # when copying the file to the destination directory copy_command = 'cp -p ' + file + ' ' + destination_directory + '/' print_and_execute_command(copy_command) # Send the email out perl_command = '/aim/sds/cips/scripts/send_prelim_images.pl ' + year + day_of_year print_and_execute_command(perl_command) def print_and_execute_command(command): """ Print a shell command, execute it, and print the results Parameters ---------- command : str a command to be executed by the shell """ print(command) result = execute_shell_command(command) print(result) def execute_shell_command(command): """ Tokenize a string assumed to a shell command, execute it, and return stdout and stderr as a string Parameters ---------- command : str a command to be executed by the shell Returns ------- : str the output, decoded to a string, from executing the command """ # This fails unless the command is tokenized command_and_args = command.split() result = subprocess.run(command_and_args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) # convert the annoying byte array to a string before returning it return result.stdout.decode('utf-8') if __name__ == '__main__': """ Making this executable """ # This should only be run from aimcips-gpu because it is at present the only aim # server allowed to send out email if socket.gethostname().find('.') >= 0: hostname = socket.gethostname() else: hostname = socket.gethostbyaddr(socket.gethostname())[0] print('hostname: {0}'.format(hostname)) if not hostname.startswith('aimcips-gpu'): print('This program must be run from aimcips-gpu in order to send out the preliminary daisy email') exit(1) # On the right host so lets do it main(CURRENT_SEASON)