def build_and_copy(branch, folder_name, versions_available, themes_dir, validate_links=False):
    call(('git checkout %s' % branch))
    call(('git pull origin %s' % branch))
    with open('versions.json', 'w') as f:
        f.write(json.dumps(versions_available))
    shutil.rmtree('_themes')
    copytree(themes_dir, '_themes')
    call('make html > /dev/null')
    if validate_links:
        call('make spelling > /dev/null')
        call('make linkcheck')
    call('make latexpdf > /dev/null')
    tmp_dir = tempfile.mkdtemp()
    copytree('_build/html/', tmp_dir)
    shutil.copy2('_build/latex/conan.pdf', tmp_dir)
    shutil.rmtree('_build')
    call('git stash')
    call('git stash drop || true')
    call('git clean -d -f')
    call('git checkout gh-pages')
    if (not os.path.exists('en')):
        os.mkdir('en')
    version_folders = [('en/%s' % folder_name)]
    if (branch == 'master'):
        version_folders.append('en/latest')
    for version_folder in version_folders:
        if os.path.exists(version_folder):
            shutil.rmtree(version_folder)
        os.mkdir(version_folder)
        copytree(tmp_dir, version_folder)
        call('git add -A .')
        call(("git commit --message 'committed version %s'" % folder_name), ignore_error=True)
def build_and_copy(branch, folder_name, versions_available, themes_dir, validate_links=False):
    call(('git checkout %s' % branch))
    call(('git pull origin %s' % branch))
    with open('versions.json', 'w') as f:
        f.write(json.dumps(versions_available))
    shutil.rmtree('_themes')
    copytree(themes_dir, '_themes')
    call('make html > /dev/null')
    if validate_links:
        call('make spelling > /dev/null')
        call('make linkcheck')
    call('make latexpdf > /dev/null')
    tmp_dir = tempfile.mkdtemp()
    copytree('_build/html/', tmp_dir)
    shutil.copy2('_build/latex/conan.pdf', tmp_dir)
    shutil.rmtree('_build')
    call('git stash')
    call('git stash drop || true')
    call('git clean -d -f')
    call('git checkout gh-pages')
    if (not os.path.exists('en')):
        os.mkdir('en')
    version_folders = [('en/%s' % folder_name)]
    if (branch == 'master'):
        version_folders.append('en/latest')
    for version_folder in version_folders:
        if os.path.exists(version_folder):
            shutil.rmtree(version_folder)
        os.mkdir(version_folder)
        copytree(tmp_dir, version_folder)
        call('git add -A .')
        call(("git commit --message 'committed version %s'" % folder_name), ignore_error=True)