<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def generate(args):
 """Generates the presentation and returns a list of files used"""


    source_files = {args.presentation}

    # Parse the template info
    template_info = Template(args.template)
    if args.css:
        presentation_dir = os.path.split(args.presentation)[0]
        target_path = os.path.relpath(args.css, presentation_dir)
        template_info.add_resource(args.css, CSS_RESOURCE, target=target_path, extra_info='all')
        source_files.add(args.css)
    if args.js:
        presentation_dir = os.path.split(args.presentation)[0]
        target_path = os.path.relpath(args.js, presentation_dir)
        template_info.add_resource(args.js, JS_RESOURCE, target=target_path, extra_info=JS_POSITION_BODY)
        source_files.add(args.js)

    # Make the resulting HTML
    htmldata, dependencies = rst2html(args.presentation, template_info,
                                      args.auto_console, args.skip_help,
                                      args.skip_notes, args.mathjax,
                                      args.slide_numbers)
    source_files.update(dependencies)

    # Write the HTML out
    if not os.path.exists(args.targetdir):
        os.makedirs(args.targetdir)
    with open(os.path.join(args.targetdir, 'index.html'), 'wb') as outfile:
        outfile.write(htmldata)

    # Copy supporting files
    source_files.update(template_info.copy_resources(args.targetdir))

    # Copy images from the source:
    sourcedir = os.path.split(os.path.abspath(args.presentation))[0]
    tree = html.fromstring(htmldata)
    for image in tree.iterdescendants('img'):
        filename = image.attrib['src']
        source_files.add(copy_resource(filename, sourcedir, args.targetdir))

    RE_CSS_URL = re.compile(br"""url\(['"]?(.*?)['"]?[\)\?\#]""")

    # Copy any files referenced by url() in the css-files:
    for resource in template_info.resources:
        if resource.resource_type != CSS_RESOURCE:
            continue
        # path in CSS is relative to CSS file; construct source/dest accordingly
        css_base = template_info.template_root if resource.is_in_template else sourcedir
        css_sourcedir = os.path.dirname(os.path.join(css_base, resource.filepath))
        css_targetdir = os.path.dirname(os.path.join(args.targetdir, resource.final_path()))
        uris = RE_CSS_URL.findall(template_info.read_data(resource))
        uris = [uri.decode() for uri in uris]
        if resource.is_in_template and template_info.builtin_template:
            for filename in uris:
                template_info.add_resource(filename, OTHER_RESOURCE, target=css_targetdir,
                                           is_in_template=True)
        else:
            for filename in uris:
                source_files.add(copy_resource(filename, css_sourcedir, css_targetdir))

    # All done!

    return {os.path.abspath(f) for f in source_files if f}