build/gen_mach_buildprops.py
author Wes Kocher <wkocher@mozilla.com>
Tue, 17 Jun 2014 13:31:33 -0700
changeset 209965 88607fe05a3fb21a264ab2594e1705ede5a226c0
parent 209462 c0efa7f1e54c2dfbc9dfa77c49bf0411bc8b891d
child 210697 26b95471c5f8f41dfcb207c9358ae70d0f7ad434
permissions -rw-r--r--
Touch CLOBBER for bug 102262

#!/usr/bin/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 sys, os, sha, json, re
from argparse import ArgumentParser

def getFileHashAndSize(filename):
    sha1Hash = 'UNKNOWN'
    size = 'UNKNOWN'

    try:
        # open in binary mode to make sure we get consistent results
        # across all platforms
        f = open(filename, "rb")
        shaObj = sha.new(f.read())
        sha1Hash = shaObj.hexdigest()

        size = os.path.getsize(filename)
    except:
        pass

    return (sha1Hash, size)

def getMarProperties(filename):
    (complete_mar_hash, complete_mar_size) = getFileHashAndSize(filename)
    return {
        'completeMarFilename': os.path.basename(filename),
        'completeMarSize': complete_mar_size,
        'completeMarHash': complete_mar_hash,
    }

def getUrlProperties(filename):
    # let's create a switch case using name-spaces/dict
    # rather than a long if/else with duplicate code
    property_conditions = [
        # key: property name, value: condition
        ('symbolsUrl', lambda m: m.endswith('crashreporter-symbols.zip') or
                       m.endswith('crashreporter-symbols-full.zip')),
        ('testsUrl', lambda m: m.endswith(('tests.tar.bz2', 'tests.zip'))),
        ('unsignedApkUrl', lambda m: m.endswith('apk') and
                           'unsigned-unaligned' in m),
        ('robocopApkUrl', lambda m: m.endswith('apk') and 'robocop' in m),
        ('jsshellUrl', lambda m: 'jsshell-' in m and m.endswith('.zip')),
        ('completeMarUrl', lambda m: m.endswith('.complete.mar')),
        # packageUrl must be last!
        ('packageUrl', lambda m: True),
    ]
    url_re = re.compile(r'''^(https?://.*?\.(?:tar\.bz2|dmg|zip|apk|rpm|mar|tar\.gz))$''')
    properties = {}

    with open(filename) as f:
        for line in f:
            m = url_re.match(line)
            if m:
                m = m.group(1)
                for prop, condition in property_conditions:
                    if condition(m):
                        properties.update({prop: m})
                        break
    return properties

if __name__ == '__main__':
    parser = ArgumentParser(description='Generate mach_build_properties.json for automation builds.')
    parser.add_argument("--complete-mar-file", required=True,
                        action="store", dest="complete_mar_file",
                        help="Path to the complete MAR file, relative to the objdir.")
    parser.add_argument("--upload-output", required=True,
                        action="store", dest="upload_output",
                        help="Path to the text output of 'make upload'")
    args = parser.parse_args()

    json_data = getMarProperties(args.complete_mar_file)
    json_data.update(getUrlProperties(args.upload_output))

    with open('mach_build_properties.json', 'w') as outfile:
        json.dump(json_data, outfile, indent=4)