def __init__(self): self.regression_dir = expand_path( Config.get("logging", "results_output_dir")) self.script_output_dir = expand_path( Config.get("logging", "temp_script_output_dir")) self.reset_plugins_between_scripts = Config.getboolean( "core", "reset_plugins_between_scripts") self.json_results = Config.getboolean("logging", "json_results")
def import_ccsds_header_types(): headers_path = expand_path(Config.get("ccsds", "CCSDS_header_path")) if not os.path.exists(headers_path): log.error("ccsds:CCSDS_header_path is not a valid path: {}".format( headers_path)) return None log.info("Importing CCSDS header definitions from {}".format(headers_path)) sys.path.append(os.path.dirname(headers_path)) try: headers_module = importlib.import_module( os.path.basename(headers_path).replace(".py", "")) except Exception as e: log.error("Unable to import module {}".format(headers_path), e) return None if not hasattr(headers_module, "CcsdsPrimaryHeader"): log.error("No definition for CcsdsPrimaryHeader found in {}".format( headers_path)) return None if not hasattr(headers_module, "CcsdsCommand"): log.error( "No definition for CcsdsCommand found in {}".format(headers_path)) return None if not hasattr(headers_module, "CcsdsTelemetry"): log.error("No definition for CcsdsTelemetry found in {}".format( headers_path)) return None ccsds_primary_header = getattr(headers_module, "CcsdsPrimaryHeader") if not (issubclass(ccsds_primary_header, ctypes.Structure) and callable(getattr(ccsds_primary_header, "get_msg_id", None)) and callable(getattr(ccsds_primary_header, "is_command", None))): log.error( "CcsdsPrimaryHeader definition does not meet interface requirements" ) return None ccsds_command = getattr(headers_module, "CcsdsCommand") if not (issubclass(ccsds_command, ctypes.Structure) and callable(getattr(ccsds_command, "get_msg_id", None)) and callable(getattr(ccsds_command, "get_function_code", None))): log.error( "CcsdsCommand definition does not meet interface requirements") return None ccsds_telemetry = getattr(headers_module, "CcsdsTelemetry") if not (issubclass(ccsds_telemetry, ctypes.Structure) and callable(getattr(ccsds_telemetry, "get_msg_id", None))): log.error( "CcsdsTelemetry definition does not meet interface requirements") return None return CcsdsHeaderTypes(ccsds_primary_header, ccsds_command, ccsds_telemetry)
def set_logger_options_from_config(): log_outputDir = expand_path( Config.get("logging", "temp_script_output_dir", fallback="./temp")) shutil.rmtree(log_outputDir, ignore_errors=True) log_output = os.path.join( os.path.abspath(log_outputDir), Config.get("logging", "ctf_log_file", fallback="./CTF_Log.log")) Global.CTF_log_dir = os.path.abspath(log_outputDir) Global.CTF_log_dir_file = log_output if (os.path.exists(Global.CTF_log_dir)): shutil.rmtree(Global.CTF_log_dir, ignore_errors=True) os.makedirs(Global.CTF_log_dir) change_log_file(Global.CTF_log_dir_file)
def init_connection(self, host, user=None, port=None, gateway=None, ssh_config_path=None, args=None): try: if self.connection and self.connection.is_connected: log.debug("Closing connection to {}".format(self.connection.host)) self.connection.close() if ssh_config_path: ssh_config_path = expand_path(ssh_config_path) config = fabric.Config(runtime_ssh_path=ssh_config_path) self.connection = fabric.Connection(host, user=user, port=port, gateway=gateway, config=config, connect_kwargs=args) log.debug("Opening connection to {}...".format(self.connection.host)) self.connection.open() return True except Exception as e: log.error("Remote connection failed: {}".format(e)) return False
def download_ftputil(self, host, remote_path, local_path, id='anonymous'): self.ip = host try: local_path = expand_path(local_path) # noinspection PyDeprecation with ftputil.FTPHost(host, id, '') as ftp: for path, dirs, files in ftp.walk(remote_path): rel_path = os.path.relpath(path, remote_path) local_dir = os.path.join(local_path, rel_path) if not ftp.path.exists(local_dir): log.debug( "Creating local directory {}...".format(local_dir)) os.makedirs(local_dir) for file in files: log.debug("Downloading {}...".format(file)) remote_file = ftp.path.join(path, file) local_file = os.path.join(local_dir, file) ftp.download(remote_file, local_file) log.debug("FTP download complete.") except Exception as e: log.warn("FTP download failed: {}".format(e)) return False else: return True
def upload_ftputil(self, host, local_path, remote_path, id='anonymous'): self.ip = host try: os.chdir(expand_path(local_path)) # noinspection PyDeprecation with ftputil.FTPHost(host, id, '') as ftp: ftp.login() for path, dirs, files in os.walk('.'): remote_dir = ftp.path.join(remote_path, path) if not ftp.path.exists(remote_dir): log.debug("Creating remote directory {}...".format( remote_dir)) ftp.makedirs(remote_dir) for file in files: log.debug("Uploading {}...".format(file)) local_file = os.path.join(path, file) remote_file = ftp.path.join(remote_dir, file) ftp.upload(local_file, remote_file) log.debug("FTP upload complete.") except Exception as e: log.warn("FTP upload failed: {}".format(e)) return False else: return True
def validate_directory(self, directory): new_directory = expand_path(directory) if not os.path.isdir(new_directory): new_directory = None log.warn("Directory does not exist - {}".format(directory)) return new_directory
def expand_directory(self, directory): new_directory = expand_path(directory) return new_directory