Source code

Revision control

Other Tools

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
#!/usr/bin/env python
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import argparse
import datetime
import os
import re
import shutil
import tarfile
import urllib
from subprocess import Popen, PIPE, STDOUT


def prepare_upstream(base, commit):
    upstream_url = 'https://chromium.googlesource.com/libyuv/libyuv'
    tarball_file = os.path.join(base, 'libyuv.tar.gz')
    lib_path = os.path.join(base, 'libyuv')

    print(upstream_url + '/+archive/' + commit + '.tar.gz')
    urllib.urlretrieve(upstream_url + '/+archive/' + commit + '.tar.gz',
                       tarball_file)
    shutil.rmtree(lib_path)
    tarfile.open(tarball_file).extractall(path=lib_path)
    os.remove(tarball_file)

    shutil.copy2(os.path.join(lib_path, "LICENSE"), os.path.join(base, "LICENSE"))


def get_commit_date(commit):
    upstream_url = 'https://chromium.googlesource.com/libyuv/libyuv/+/' + commit
    text = urllib.urlopen(upstream_url).read()
    regex = r'<tr><th class="Metadata-title">committer</th>' \
            r'<td>.+</td><td>[^\s]+ ([0-9a-zA-Z: ]+)\s*\+*[0-9]*</td></tr>'
    date = re.search(regex, text).group(1)
    return datetime.datetime.strptime(date, "%b %d %H:%M:%S %Y")


def cleanup_upstream(base):
    os.remove(os.path.join(base, 'libyuv/.gitignore'))


def apply_patches(base):
    patches = [
        # update gyp build files
        "update_gyp.patch",
        # fix build errors
        'fix_build_errors.patch',
        # make mjpeg printfs optional at build time
        'make_mjpeg_printfs_optional.patch',
        # allow disabling of inline ASM and AVX2 code
        'allow_disabling_asm_avx2.patch',
        # add H444ToARGB() variant
        'add_H444ToARGB.patch',
        # fix the x86 mingw-clang build
        'bug_1491848.patch',
    ]

    for patch in patches:
        print('\nApplying patch %s' % patch)
        with open(os.path.join(base, patch)) as f:
            Popen(["patch", "-p3"], stdin=f, cwd=base).wait()


def update_moz_yaml(base, commit, commitdate):
    moz_yaml_file = os.path.join(base, 'moz.yaml')
    with open(moz_yaml_file) as f:
        moz_yaml = f.read()

    new_moz_yaml = re.sub(r'\n\s+release:.+\n',
                          '\n  release: "%s (%s)"\n' % (commit, commitdate),
                          moz_yaml)

    if moz_yaml != new_moz_yaml:
        with open(moz_yaml_file, 'w') as f:
            f.write(new_moz_yaml)


def main():
    parser = argparse.ArgumentParser(description='Update libyuv')
    parser.add_argument('--no-patches', dest='no_patches', action="store_true")
    parser.add_argument('--commit', dest='commit', default='master')
    args = parser.parse_args()

    commit = args.commit
    no_patches = args.no_patches
    base = os.path.realpath(os.path.dirname(__file__))

    prepare_upstream(base, commit)
    commitdate = get_commit_date(commit)

    if not no_patches:
        apply_patches(base)

    update_moz_yaml(base, commit, commitdate)

    print('\nPatches applied; '
          'run "hg addremove --similarity 70 libyuv" before committing changes')

    cleanup_upstream(base)


if __name__ == '__main__':
    main()