dom/imptests/importTestsuite.py

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/imptests/importTestsuite.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,189 @@
     1.4 +#!/usr/bin/env python
     1.5 +# This Source Code Form is subject to the terms of the Mozilla Public
     1.6 +# License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.7 +# You can obtain one at http://mozilla.org/MPL/2.0/.
     1.8 +
     1.9 +"""
    1.10 +Imports a test suite from a remote repository. Takes one argument, a file in
    1.11 +the format described in README.
    1.12 +Note: removes both source and destination directory before starting. Do not
    1.13 +      use with outstanding changes in either directory.
    1.14 +"""
    1.15 +
    1.16 +from __future__ import print_function, unicode_literals
    1.17 +
    1.18 +import os
    1.19 +import shutil
    1.20 +import subprocess
    1.21 +import sys
    1.22 +
    1.23 +import parseManifest
    1.24 +import writeBuildFiles
    1.25 +
    1.26 +def readManifests(iden, dirs):
    1.27 +    def parseManifestFile(iden, path):
    1.28 +        pathstr = "hg-%s/%s/MANIFEST" % (iden, path)
    1.29 +        subdirs, mochitests, reftests, _, supportfiles = parseManifest.parseManifestFile(pathstr)
    1.30 +        return subdirs, mochitests, reftests, supportfiles
    1.31 +
    1.32 +    data = []
    1.33 +    for path in dirs:
    1.34 +        subdirs, mochitests, reftests, supportfiles = parseManifestFile(iden, path)
    1.35 +        data.append({
    1.36 +          "path": path,
    1.37 +          "mochitests": mochitests,
    1.38 +          "reftests": reftests,
    1.39 +          "supportfiles": supportfiles,
    1.40 +        })
    1.41 +        data.extend(readManifests(iden, ["%s/%s" % (path, d) for d in subdirs]))
    1.42 +    return data
    1.43 +
    1.44 +
    1.45 +def getData(confFile):
    1.46 +    """This function parses a file of the form
    1.47 +    (hg or git)|URL of remote repository|identifier for the local directory
    1.48 +    First directory of tests
    1.49 +    ...
    1.50 +    Last directory of tests"""
    1.51 +    vcs = ""
    1.52 +    url = ""
    1.53 +    iden = ""
    1.54 +    directories = []
    1.55 +    try:
    1.56 +        with open(confFile, 'r') as fp:
    1.57 +            first = True
    1.58 +            for line in fp:
    1.59 +                if first:
    1.60 +                    vcs, url, iden = line.strip().split("|")
    1.61 +                    first = False
    1.62 +                else:
    1.63 +                    directories.append(line.strip())
    1.64 +    finally:
    1.65 +        return vcs, url, iden, directories
    1.66 +
    1.67 +
    1.68 +def makePathInternal(a, b):
    1.69 +    if not b:
    1.70 +        # Empty directory, i.e., the repository root.
    1.71 +        return a
    1.72 +    return "%s/%s" % (a, b)
    1.73 +
    1.74 +
    1.75 +def makeSourcePath(a, b):
    1.76 +    """Make a path in the source (upstream) directory."""
    1.77 +    return makePathInternal("hg-%s" % a, b)
    1.78 +
    1.79 +
    1.80 +def makeDestPath(a, b):
    1.81 +    """Make a path in the destination (mozilla-central) directory, shortening as
    1.82 +    appropriate."""
    1.83 +    def shorten(path):
    1.84 +        path = path.replace('dom-tree-accessors', 'dta')
    1.85 +        path = path.replace('document.getElementsByName', 'doc.gEBN')
    1.86 +        path = path.replace('requirements-for-implementations', 'implreq')
    1.87 +        path = path.replace('other-elements-attributes-and-apis', 'oeaaa')
    1.88 +        return path
    1.89 +
    1.90 +    return shorten(makePathInternal(a, b))
    1.91 +
    1.92 +
    1.93 +def extractReftestFiles(reftests):
    1.94 +    """Returns the set of files referenced in the reftests argument"""
    1.95 +    files = set()
    1.96 +    for line in reftests:
    1.97 +        files.update([line[1], line[2]])
    1.98 +    return files
    1.99 +
   1.100 +
   1.101 +def copy(dest, directories):
   1.102 +    """Copy mochitests and support files from the external HG directory to their
   1.103 +    place in mozilla-central.
   1.104 +    """
   1.105 +    print("Copying tests...")
   1.106 +    for d in directories:
   1.107 +        sourcedir = makeSourcePath(dest, d["path"])
   1.108 +        destdir = makeDestPath(dest, d["path"])
   1.109 +        os.makedirs(destdir)
   1.110 +
   1.111 +        reftestfiles = extractReftestFiles(d["reftests"])
   1.112 +
   1.113 +        for mochitest in d["mochitests"]:
   1.114 +            shutil.copy("%s/%s" % (sourcedir, mochitest), "%s/test_%s" % (destdir, mochitest))
   1.115 +        for reftest in sorted(reftestfiles):
   1.116 +            shutil.copy("%s/%s" % (sourcedir, reftest), "%s/%s" % (destdir, reftest))
   1.117 +        for support in d["supportfiles"]:
   1.118 +            shutil.copy("%s/%s" % (sourcedir, support), "%s/%s" % (destdir, support))
   1.119 +
   1.120 +def printBuildFiles(dest, directories):
   1.121 +    """Create a mochitest.ini that all the contains tests we import.
   1.122 +    """
   1.123 +    print("Creating manifest...")
   1.124 +    all_mochitests = set()
   1.125 +    all_support = set()
   1.126 +
   1.127 +    for d in directories:
   1.128 +        path = makeDestPath(dest, d["path"])
   1.129 +
   1.130 +        all_mochitests |= set('%s/test_%s' % (d['path'], mochitest)
   1.131 +            for mochitest in d['mochitests'])
   1.132 +        all_support |= set('%s/%s' % (d['path'], p) for p in d['supportfiles'])
   1.133 +
   1.134 +        if d["reftests"]:
   1.135 +            with open(path + "/reftest.list", "w") as fh:
   1.136 +                result = writeBuildFiles.substReftestList("importTestsuite.py",
   1.137 +                    d["reftests"])
   1.138 +                fh.write(result)
   1.139 +
   1.140 +    manifest_path = dest + '/mochitest.ini'
   1.141 +    with open(manifest_path, 'w') as fh:
   1.142 +        result = writeBuildFiles.substManifest('importTestsuite.py',
   1.143 +            all_mochitests, all_support)
   1.144 +        fh.write(result)
   1.145 +    subprocess.check_call(["hg", "add", manifest_path])
   1.146 +
   1.147 +def hgadd(dest, directories):
   1.148 +    """Inform hg of the files in |directories|."""
   1.149 +    print("hg addremoving...")
   1.150 +    for d in directories:
   1.151 +        subprocess.check_call(["hg", "addremove", makeDestPath(dest, d)])
   1.152 +
   1.153 +def removeAndCloneRepo(vcs, url, dest):
   1.154 +    """Replaces the repo at dest by a fresh clone from url using vcs"""
   1.155 +    assert vcs in ('hg', 'git')
   1.156 +
   1.157 +    print("Removing %s..." % dest)
   1.158 +    subprocess.check_call(["rm", "-rf", dest])
   1.159 +
   1.160 +    print("Cloning %s to %s with %s..." % (url, dest, vcs))
   1.161 +    subprocess.check_call([vcs, "clone", url, dest])
   1.162 +
   1.163 +def importRepo(confFile):
   1.164 +    try:
   1.165 +        vcs, url, iden, directories = getData(confFile)
   1.166 +        dest = iden
   1.167 +        hgdest = "hg-%s" % iden
   1.168 +
   1.169 +        print("Removing %s..." % dest)
   1.170 +        subprocess.check_call(["rm", "-rf", dest])
   1.171 +
   1.172 +        removeAndCloneRepo(vcs, url, hgdest)
   1.173 +
   1.174 +        data = readManifests(iden, directories)
   1.175 +        print("Going to import %s..." % [d["path"] for d in data])
   1.176 +
   1.177 +        copy(dest, data)
   1.178 +        printBuildFiles(dest, data)
   1.179 +        hgadd(dest, directories)
   1.180 +        print("Removing %s again..." % hgdest)
   1.181 +        subprocess.check_call(["rm", "-rf", hgdest])
   1.182 +    except subprocess.CalledProcessError as e:
   1.183 +        print(e.returncode)
   1.184 +    finally:
   1.185 +        print("Done")
   1.186 +
   1.187 +if __name__ == "__main__":
   1.188 +    if len(sys.argv) != 2:
   1.189 +        print("Need one argument.")
   1.190 +    else:
   1.191 +        importRepo(sys.argv[1])
   1.192 +

mercurial