Build a site.

def build(dburl, sitedir, mode):
    """Build a site."""
    if mode == 'force':
        amode = ['-a']
    else:
        amode = []
    oldcwd = os.getcwd()
    os.chdir(sitedir)
    db = StrictRedis.from_url(dburl)
    job = get_current_job(db)
    job.meta.update({'out': '', 'milestone': 0, 'total': 1, 'return': None,
                     'status': None})
    job.save()
    p = subprocess.Popen([executable, '-m', 'nikola', 'build'] + amode,
                         stderr=subprocess.PIPE)

    milestones = {
        'done!': 0,
        'render_posts': 0,
        'render_pages': 0,
        'generate_rss': 0,
        'render_indexes': 0,
        'sitemap': 0
    }
    out = []

    while p.poll() is None:
        nl = p.stderr.readline().decode('utf-8')
        for k in milestones:
            if k in nl:
                milestones[k] = 1
        out.append(nl)
        job.meta.update({'milestone': sum(milestones.values()), 'total':
                         len(milestones), 'out': ''.join(out), 'return': None,
                         'status': None})
        job.save()

    out += p.stderr.readlines()

    out = ''.join(out)
    job.meta.update({'milestone': len(milestones), 'total': len(milestones),
                     'out': ''.join(out), 'return': p.returncode, 'status':
                     p.returncode == 0})
    job.save()
    os.chdir(oldcwd)
    return p.returncode