'Import a repository archive contained within a repository capsule.'

def import_repository_archive(self, repository, repository_archive_dict):

    rdah = attribute_handlers.RepositoryDependencyAttributeHandler(self.app, unpopulate=False)
    tdah = attribute_handlers.ToolDependencyAttributeHandler(self.app, unpopulate=False)
    archive_file_name = repository_archive_dict.get('archive_file_name', None)
    capsule_file_name = repository_archive_dict['capsule_file_name']
    encoded_file_path = repository_archive_dict['encoded_file_path']
    file_path = encoding_util.tool_shed_decode(encoded_file_path)
    results_dict = dict(ok=True, error_message='')
    archive_file_path = os.path.join(file_path, archive_file_name)
    archive = tarfile.open(archive_file_path, 'r:*')
    repo_dir = repository.repo_path(self.app)
    hg_util.get_repo_for_repository(self.app, repository=None, repo_path=repo_dir, create=False)
    undesirable_dirs_removed = 0
    undesirable_files_removed = 0
    check_results = commit_util.check_archive(repository, archive)
    if ((not check_results.invalid) and (not check_results.undesirable_dirs)):
        full_path = os.path.abspath(repo_dir)
        archive.extractall(path=full_path, members=check_results.valid)
        archive.close()
        for tar_member in check_results.valid:
            filename = tar_member.name
            uploaded_file_name = os.path.join(full_path, filename)
            if (os.path.split(uploaded_file_name)[(-1)] == rt_util.REPOSITORY_DEPENDENCY_DEFINITION_FILENAME):
                (altered, root_elem, error_message) = rdah.handle_tag_attributes(uploaded_file_name)
                if error_message:
                    results_dict['ok'] = False
                    results_dict['error_message'] += error_message
                if altered:
                    tmp_filename = xml_util.create_and_write_tmp_file(root_elem)
                    shutil.move(tmp_filename, uploaded_file_name)
            elif (os.path.split(uploaded_file_name)[(-1)] == rt_util.TOOL_DEPENDENCY_DEFINITION_FILENAME):
                (altered, root_elem, error_message) = tdah.handle_tag_attributes(uploaded_file_name)
                if error_message:
                    results_dict['ok'] = False
                    results_dict['error_message'] += error_message
                if altered:
                    tmp_filename = xml_util.create_and_write_tmp_file(root_elem)
                    shutil.move(tmp_filename, uploaded_file_name)
        commit_message = ('Imported    from    capsule    %s' % str(capsule_file_name))
        new_repo_alert = True
        remove_repo_files_not_in_tar = False
        filenames_in_archive = [member.name for member in check_results.valid]
        undesirable_files_removed = len(check_results.undesirable_files)
        undesirable_dirs_removed = 0
        (ok, error_message, files_to_remove, content_alert_str, undesirable_dirs_removed, undesirable_files_removed) = commit_util.handle_directory_changes(self.app, self.host, self.user.username, repository, full_path, filenames_in_archive, remove_repo_files_not_in_tar, new_repo_alert, commit_message, undesirable_dirs_removed, undesirable_files_removed)
        if error_message:
            results_dict['ok'] = False
            results_dict['error_message'] += error_message
        try:
            rmm = repository_metadata_manager.RepositoryMetadataManager(app=self.app, user=self.user, repository=repository)
            (status, error_message) = rmm.set_repository_metadata_due_to_new_tip(self.host, content_alert_str=content_alert_str)
            if error_message:
                results_dict['ok'] = False
                results_dict['error_message'] += error_message
        except Exception as e:
            log.debug(('Error    setting    metadata    on    repository    %s    created    from    imported    archive    %s:    %s' % (str(repository.name), str(archive_file_name), str(e))))
    else:
        archive.close()
        results_dict['ok'] = False
        results_dict['error_message'] += 'Capsule    errors    were    found:    '
        if check_results.invalid:
            results_dict['error_message'] += ('%s    Invalid    files    were:    %s.' % ('    '.join(check_results.errors), ',    '.join(check_results.invalid)))
        if check_results.undesirable_dirs:
            results_dict['error_message'] += ('    Undesirable    directories    were:    %s.' % ',    '.join(check_results.undesirable_dirs))
    return results_dict
