def get_smi_and_parse(site_catalog_file):
    """Function to get the smi files from sftp server
    The smi files are picked up according to the details in the site-catalog.xml
    """
    if not os.path.exists(site_catalog_file):
        raise GSMLogger().LogException(
            "Error: site_catalog xml file not found at \
            file not found at "
            + site_catalog_file
        )
    else:
        catalog = open(site_catalog_file, "r")
    site_data = etree.parse(site_catalog_file)
    site_num = len(site_data.findall(".//site"))
    gsmlogger.logger.info(str(site_num) + " total subject site entries read into tree.")
    """The reference site code is the current site on which
    generate_subject_map.py is running.
    As we need to get the only smi from the sftp to this site.

    """
    for site in site_data.iter("site"):
        site_name = site.findtext("site_name")
        if site_name == "source":
            host, port = gsm_lib.parse_host_and_port(site.findtext("site_URI"))
            site_uname = site.findtext("site_uname")
            site_password = site.findtext("site_password")
            site_key_path = site.findtext("site_key_path")
            site_contact_email = site.findtext("site_contact_email")
            """Pick up the smi file from the server and place it in the proj_root

            """
            site_remotepath = site.findtext("site_remotepath")
            file_name = site_remotepath.split("/")[-1]
            site_localpath = configuration_directory + file_name

            info = "Retrieving file: " + site_remotepath + " from " + host
            print info
            gsmlogger.logger.info(info)

            info = "Errors will be reported to: " + site_contact_email
            print info
            gsmlogger.logger.info(info)

            sftp_instance = SFTPClient(host, port, site_uname, site_password, site_key_path)
            sftp_instance.get_file_from_uri(site_remotepath, site_localpath, site_contact_email)
    catalog.close()
    gsmlogger.logger.info("site catalog XML file closed.")
    return site_localpath
Example #2
0
def get_smi_and_parse(site_catalog_file, settings, logger):
    '''Function to get the smi files from sftp server
    The smi files are picked up according to the details in the site-catalog.xml
    '''
    dikt = gsm_lib.get_site_details_as_dict(site_catalog_file, 'data_source')
    site_uri = dikt['site_URI']
    host, port = gsm_lib.parse_host_and_port(site_uri)

    site_uname = dikt['site_uname']
    site_password = dikt['site_password']
    site_remotepath = dikt['site_remotepath']
    site_key_path = dikt['site_key_path']
    site_contact_email = dikt['site_contact_email']

    file_name = os.path.basename(site_remotepath)
    site_localpath = os.path.join(configuration_directory, file_name)

    # Pick up the smi file from the server and place it in the proj_root
    logger.info('Retrieving file: %s from %s:%s', site_remotepath, host, port)
    logger.info('Errors will be reported to: ' + site_contact_email)

    email_props_sftp = EmailProps(
        settings.smtp_host,
        settings.smtp_port,
        settings.sender_email,
        [site_contact_email],
        [],
        'Research Subject Mapper Notification')

    sftp_instance = SFTPClient(
        host,
        port,
        site_uname,
        site_password,
        site_key_path)

    sftp_instance.get_file_from_uri(site_remotepath, site_localpath, email_props_sftp)
    return site_localpath