Source

whipped-cream / build.py

Full commit
#!/usr/bin/env python3


import base64
import io
import lzma
import os.path
import shutil
import subprocess


VIM_MERCURIAL_PATH = 'https://vim.googlecode.com/hg'
BUILD_CMD = b"""\
/Td6WFoAAATm1rRGAgAhARYAAAB0L+Wj4AVUAxRdAB1gfKhoMvijybpGnlMg4Bh1Lp+PJQFQfB6P
4JzuDU5I8/RRXynZO6a3iI4Siy39muQKNOY0Nq4FQL7AF2RpBfbyOmHn+tCM9wPWvAKy41USqxvN
TB1Z0beher8cDZwBQQvL+i8iazHs6/ywOEVvWO8HZ2eKUM1YWsrw4OVxwj8HX2+u3IkwM0HVmtfk
1SxFsDpffZL54BIWvn0V+YVXBws26/kws2O9Sv2ZuvfL3wZBogPx2c2BcDi/QYo6TThyUCPRgcNd
5jT0UmQuaDInmiSz1QCFUAggyqDLGb/JcupbsELfd949fBmKeLRVBRgYrQMQ7LM3DnK9lUnHwE15
ale4t4BxCuoC5dQ2UpvDorh6lOHuLJcAOjo0dCBaU+syq8/zkmSZpNDdaKZWGtnVHFguDpoK85Y+
6wt9I7DDz9WfSRfsVD1wvuh8DxFm07MqqZhGGdwc2yTByyl/+28x9oPE+y3AZs9xuS1gy/e3+ma6
4LtxqzJiYYTzpToLTidKGMv3eBS3m3d+D6SzrLQfsc6Nl8fFvyqTpJQTQDZbdeI9WFsr2k5sf89Z
dRQJiiHq22R87aPleCDipoRSIMGsqEIeesPczMgeds0O/BqyOuw76SN8LmJwPo7y4nXb4JIfIeDF
LK0apPGqv+vdZ5aCgEzyC16WdRCCsE7Af+JYzcDIlm/DrLEJVH48UbXLevazQtKbYxPQBhLYYskv
1bYJVNtAFX+lEy+me5p0qF0aSIydKW8w/P+Z75vAqqmXQAum03WzjedX758/30zXXBKTODbmJMV3
KYqbEZYAn8gmhV/PpRWeYmzJ8YNsTod4KH4HXQhvBvQAnInLhXL6QE8V79ZepLH9YmIdKJqdAI/4
q+smW38hJz9F3NbwL8U72xepSZYJM0nQ+hb3BIqLKqsRThAy9oY7yPWuj/hs4GFtok4rrERXcyww
RtE015wmi8EYVtn7nFJlm3739WyO+Sm+gX7zZbnHWYZ5RAdB8LZ/J8g+fMxKuC9HeFVLowOdVUqf
mYQlPvWKMjm0XxNkosmuuHefsAkCANZSFHJtUCi1AAGwBtUKAAB1/sJKscRn+wIAAAAABFla
"""


def _extract_build_script(workdir):
    path = os.path.join(workdir, 'build.cmd')
    source = io.BytesIO(base64.decodebytes(BUILD_CMD))
    with open(path, 'wb') as data_out:
        with lzma.open(source) as data_in:
            shutil.copyfileobj(data_in, data_out)
    return path


def _hg(command, *args, **kwargs):
    pargs = ['hg', command]
    for k, v in kwargs.items():
        if v is False:
            continue
        pargs.append('--' if len(k) > 1 else '-')
        if v is not True:
            pargs.append(str(v))
    pargs += args
    p = subprocess.Popen(pargs, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
    stdout, stderr = p.communicate()
    return (p.returncode, stdout.decode('utf-8'), stderr.decode('utf-8'))


def _init_repo(path):
    if not os.path.exists(path):
        print('Cloning VIM from {}...'.format(VIM_MERCURIAL_PATH))
        if _hg('clone', VIM_MERCURIAL_PATH, path)[0] != 0:
            print('Clone failed.')
            return None
        print('Repository successfully cloned.')
    else:
        pwd = os.getcwd()
        os.chdir(path)
        result = _hg('root')
        if result[0] != 0 and result[1].strip() != path:
            print('Not a Mercurial repository: ' + path)
            return None
        print('Updating VIM source code...')
        result = _hg('pull', update=True)
        if result[0] != 0:
            print('Update failed.')
            return None
        print('Repository successfully updated.')
        os.chdir(pwd)
    return path


def main():
    # Make sure we have a place to do our work.
    workdir = os.path.abspath('work')
    try:
        os.mkdir(workdir)
    except FileExistsError:
        pass
    os.chdir(workdir)

    # Get the latest VIM source code.
    vimdir = _init_repo(os.path.abspath('vim'))
    if vimdir is None:
        return 1

    build_script = _extract_build_script(workdir)
    # TODO: Run the build script.

    return 0


if __name__ == '__main__':
    exit(main())