def get_last_nonquery_config_path()

in VMEncryption/main/Utils/HandlerUtil.py [0:0]


    def get_last_nonquery_config_path(self):
        # pre: internal self._context._config_dir and _seq_no, _settings_file must be set prior to call
        # post: returns path to last nonquery settings file in current config, archived folder, or None
        
        # validate that internal preconditions are satisfied and internal variables are initialized
        if self._context._seq_no < 0:
            self.error("current context sequence number must be initialized and non-negative")
        if not self._context._config_dir or not os.path.isdir(self._context._config_dir):
            self.error("current context config dir must be initialized and point to a path that exists")
        if not self._context._settings_file or not os.path.exists(self._context._settings_file):
            self.error("current context settings file variable must be initialized and point to a file that exists")

        # check timestamp of pointer to last archived settings file 
        curr_path = self._context._settings_file
        last_path = os.path.join(self.config_archive_folder, "lnq.settings")
        
        # if an archived nonquery settings file exists, use it if no current settings file exists, or it is newer than current settings
        if os.path.exists(last_path) and ((not os.path.exists(curr_path)) or (os.path.exists(curr_path) and (os.stat(last_path).st_mtime > os.stat(curr_path).st_mtime))):
            return last_path
        else:
            # reverse iterate through numbered settings files in config dir
            # and return path to the first nonquery settings file found
            for i in range(self._context._seq_no,-1,-1):
                curr_path = os.path.join(self._context._config_dir, str(i) + '.settings')
                if self.is_valid_nonquery(curr_path):                    
                    return curr_path
            
            # nothing was found in the current config settings, check the archived settings
            if os.path.exists(last_path):
                return last_path
            else:
                if os.path.exists(self.config_archive_folder):                        
                    # walk through any archived [n].settings files found in archived settings folder 
                    # sorted by reverse timestamp (processing newest to oldest) until a nonquery settings file found 
                    files = sorted(os.listdir(self.config_archive_folder), key=os.path.getctime, reverse=True)
                    for f in files:
                        curr_path = os.path.join(self._context._config_dir, f)
                        # TODO: check that file name matches the [n].settings format
                        if self.is_valid_nonquery(curr_path):
                            # found, copy to last_nonquery_settings in archived settings
                            return curr_path

        # unable to find any nonquery settings file 
        return None