def curate_bids_tree(fw, project, reset=False, template_file=None, update=True):
    project_files = project.get('files', [])
    template = templates.DEFAULT_TEMPLATE
    if (not template_file):
        template_filename = utils.find_custom_template(project_files)
        if template_filename:
            (fd, path) = tempfile.mkstemp('.json')
            os.close(fd)
            logger.info('Using project template: {0}'.format(template_filename))
            fw.download_file_from_project(project['id'], template_filename, path)
            template_file = path
    if template_file:
        template = templates.loadTemplate(template_file)
    for context in project.context_iter():
        ctype = context['container_type']
        parent_ctype = context['parent_container_type']
        if reset:
            clear_meta_info(context[ctype], template)
        elif (context[ctype].get('info', {}).get('BIDS') == 'NA'):
            continue
        if (ctype == 'project'):
            bidsify_flywheel.process_matching_templates(context, template)
        elif (ctype == 'session'):
            bidsify_flywheel.process_matching_templates(context, template)
            context['run_counters'] = utils.RunCounterMap()
        elif (ctype == 'acquisition'):
            bidsify_flywheel.process_matching_templates(context, template)
        elif (ctype == 'file'):
            if ((parent_ctype == 'project') and PROJECT_TEMPLATE_FILE_NAME_REGEX.search(context['file']['name'])):
                continue
            context['file'] = bidsify_flywheel.process_matching_templates(context, template)
            validate_meta_info(context['file'], template)
    session = None
    for context in project.context_iter():
        bidsify_flywheel.process_resolvers(context, template)
    if update:
        for context in project.context_iter():
            ctype = context['container_type']
            node = context[ctype]
            if node.is_dirty():
                update_meta_info(fw, context)
def curate_bids_tree(fw, project, reset=False, template_file=None, update=True):
    project_files = project.get('files', [])
    template = templates.DEFAULT_TEMPLATE
    if (not template_file):
        template_filename = utils.find_custom_template(project_files)
        if template_filename:
            (fd, path) = tempfile.mkstemp('.json')
            os.close(fd)
            logger.info('Using project template: {0}'.format(template_filename))
            fw.download_file_from_project(project['id'], template_filename, path)
            template_file = path
    if template_file:
        template = templates.loadTemplate(template_file)
    for context in project.context_iter():
        ctype = context['container_type']
        parent_ctype = context['parent_container_type']
        if reset:
            clear_meta_info(context[ctype], template)
        elif (context[ctype].get('info', {}).get('BIDS') == 'NA'):
            continue
        if (ctype == 'project'):
            bidsify_flywheel.process_matching_templates(context, template)
        elif (ctype == 'session'):
            bidsify_flywheel.process_matching_templates(context, template)
            context['run_counters'] = utils.RunCounterMap()
        elif (ctype == 'acquisition'):
            bidsify_flywheel.process_matching_templates(context, template)
        elif (ctype == 'file'):
            if ((parent_ctype == 'project') and PROJECT_TEMPLATE_FILE_NAME_REGEX.search(context['file']['name'])):
                continue
            context['file'] = bidsify_flywheel.process_matching_templates(context, template)
            validate_meta_info(context['file'], template)
    session = None
    for context in project.context_iter():
        bidsify_flywheel.process_resolvers(context, template)
    if update:
        for context in project.context_iter():
            ctype = context['container_type']
            node = context[ctype]
            if node.is_dirty():
                update_meta_info(fw, context)