'Generate metadata for a repository using its files on disk.  To generate metadata
for changeset revisions older than the repository tip, the repository will have been
cloned to a temporary location and updated to a specified changeset revision to access
that changeset revision\'s disk files, so the value of self.repository_files_dir will not
always be self.repository.repo_path( self.app ) (it could be an absolute path to a temporary
directory containing a clone).  If it is an absolute path, the value of self.relative_install_dir
must contain repository.repo_path( self.app ).
The value of self.persist will be True when the installed repository contains a valid
tool_data_table_conf.xml.sample file, in which case the entries should ultimately be
persisted to the file referred to by self.app.config.shed_tool_data_table_config.'

def generate_metadata_for_changeset_revision(self):

    tv = tool_validator.ToolValidator(self.app)
    if (self.shed_config_dict is None):
        self.shed_config_dict = {}
    if self.updating_installed_repository:
        original_repository_metadata = self.repository.metadata
    else:
        original_repository_metadata = None
    readme_file_names = readme_util.get_readme_file_names(str(self.repository.name))
    if (self.app.name == 'galaxy'):
        metadata_dict = {'shed_config_filename': self.shed_config_dict.get('config_filename')}
    else:
        metadata_dict = {}
    readme_files = []
    invalid_tool_configs = []
    if self.resetting_all_metadata_on_repository:
        if (not self.relative_install_dir):
            raise Exception('The    value    of    self.repository.repo_path    must    be    set    when    resetting    all    metadata    on    a    repository.')
        work_dir = self.repository_files_dir
        files_dir = self.repository_files_dir
    else:
        work_dir = tempfile.mkdtemp(prefix='tmp-toolshed-gmfcr')
        files_dir = self.relative_install_dir
        if self.shed_config_dict.get('tool_path'):
            files_dir = os.path.join(self.shed_config_dict['tool_path'], files_dir)
    datatypes_config = hg_util.get_config_from_disk(suc.DATATYPES_CONFIG_FILENAME, files_dir)
    if datatypes_config:
        metadata_dict = self.generate_datatypes_metadata(tv, files_dir, datatypes_config, metadata_dict)
    (sample_file_metadata_paths, sample_file_copy_paths) = self.get_sample_files_from_disk(repository_files_dir=files_dir, tool_path=self.shed_config_dict.get('tool_path'), relative_install_dir=self.relative_install_dir)
    if sample_file_metadata_paths:
        metadata_dict['sample_files'] = sample_file_metadata_paths
    for sample_file in sample_file_copy_paths:
        tool_util.copy_sample_file(self.app, sample_file, dest_path=work_dir)
        (relative_path, filename) = os.path.split(sample_file)
        if (filename == 'tool_data_table_conf.xml.sample'):
            (new_table_elems, error_message) = self.app.tool_data_tables.add_new_entries_from_config_file(config_filename=sample_file, tool_data_path=work_dir, shed_tool_data_table_config=work_dir, persist=False)
            if error_message:
                self.invalid_file_tups.append((filename, error_message))
    for (root, dirs, files) in os.walk(files_dir):
        if ((root.find('.hg') < 0) and (root.find('hgrc') < 0)):
            if ('.hg' in dirs):
                dirs.remove('.hg')
            for name in files:
                if (name == rt_util.REPOSITORY_DEPENDENCY_DEFINITION_FILENAME):
                    path_to_repository_dependencies_config = os.path.join(root, name)
                    (metadata_dict, error_message) = self.generate_repository_dependency_metadata(path_to_repository_dependencies_config, metadata_dict)
                    if error_message:
                        self.invalid_file_tups.append((name, error_message))
                elif (name.lower() in readme_file_names):
                    relative_path_to_readme = self.get_relative_path_to_repository_file(root, name, self.relative_install_dir, work_dir, self.shed_config_dict)
                    readme_files.append(relative_path_to_readme)
                elif looks_like_a_tool(os.path.join(root, name), invalid_names=self.NOT_TOOL_CONFIGS):
                    full_path = str(os.path.abspath(os.path.join(root, name)))
                    (element_tree, error_message) = xml_util.parse_xml(full_path)
                    if (element_tree is None):
                        is_tool = False
                    else:
                        element_tree_root = element_tree.getroot()
                        is_tool = (element_tree_root.tag == 'tool')
                    if is_tool:
                        (tool, valid, error_message) = tv.load_tool_from_config(self.app.security.encode_id(self.repository.id), full_path)
                        if (tool is None):
                            if (not valid):
                                invalid_tool_configs.append(name)
                                self.invalid_file_tups.append((name, error_message))
                        else:
                            invalid_files_and_errors_tups = tv.check_tool_input_params(files_dir, name, tool, sample_file_copy_paths)
                            can_set_metadata = True
                            for tup in invalid_files_and_errors_tups:
                                if (name in tup):
                                    can_set_metadata = False
                                    invalid_tool_configs.append(name)
                                    break
                            if can_set_metadata:
                                relative_path_to_tool_config = self.get_relative_path_to_repository_file(root, name, self.relative_install_dir, work_dir, self.shed_config_dict)
                                metadata_dict = self.generate_tool_metadata(relative_path_to_tool_config, tool, metadata_dict)
                            else:
                                for tup in invalid_files_and_errors_tups:
                                    self.invalid_file_tups.append(tup)
                elif name.endswith('.ga'):
                    relative_path = os.path.join(root, name)
                    if (os.path.getsize(os.path.abspath(relative_path)) > 0):
                        fp = open(relative_path, 'rb')
                        workflow_text = fp.read()
                        fp.close()
                        if workflow_text:
                            valid_exported_galaxy_workflow = True
                            try:
                                exported_workflow_dict = json.loads(workflow_text)
                            except Exception:
                                log.exception('Skipping    file    %s    since    it    does    not    seem    to    be    a    valid    exported    Galaxy    workflow', str(relative_path))
                                valid_exported_galaxy_workflow = False
                        if (valid_exported_galaxy_workflow and ('a_galaxy_workflow' in exported_workflow_dict) and (exported_workflow_dict['a_galaxy_workflow'] == 'true')):
                            metadata_dict = self.generate_workflow_metadata(relative_path, exported_workflow_dict, metadata_dict)
    data_manager_config = hg_util.get_config_from_disk(suc.REPOSITORY_DATA_MANAGER_CONFIG_FILENAME, files_dir)
    metadata_dict = self.generate_data_manager_metadata(files_dir, data_manager_config, metadata_dict, shed_config_dict=self.shed_config_dict)
    if readme_files:
        metadata_dict['readme_files'] = readme_files
    tool_dependencies_config = hg_util.get_config_from_disk(rt_util.TOOL_DEPENDENCY_DEFINITION_FILENAME, files_dir)
    if tool_dependencies_config:
        (metadata_dict, error_message) = self.generate_tool_dependency_metadata(tool_dependencies_config, metadata_dict, original_repository_metadata=original_repository_metadata)
        if error_message:
            self.invalid_file_tups.append((rt_util.TOOL_DEPENDENCY_DEFINITION_FILENAME, error_message))
    if invalid_tool_configs:
        metadata_dict['invalid_tools'] = invalid_tool_configs
    self.metadata_dict = metadata_dict
    basic_util.remove_dir(work_dir)
