Matthew Barth | ccb7f85 | 2016-11-23 17:43:02 -0600 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | """ |
| 4 | This script determines the given package's openbmc dependencies from its |
| 5 | configure.ac file where it downloads, configures, builds, and installs each of |
| 6 | these dependencies. Then the given package is configured, built, and installed |
| 7 | prior to executing its unit tests. |
| 8 | """ |
| 9 | |
| 10 | from urlparse import urljoin |
| 11 | import os |
| 12 | import sys |
| 13 | |
| 14 | |
| 15 | def clone_pkg(pkg): |
| 16 | pkg_repo = urljoin('https://gerrit.openbmc-project.xyz/openbmc/', pkg) |
| 17 | os.chdir(WORKSPACE) |
| 18 | os.system("git clone "+pkg_repo) # TODO Replace with subprocess call |
| 19 | |
| 20 | |
| 21 | def build_depends(pkg, pkgdir, dep_installed): |
| 22 | """ |
| 23 | For each package(pkg), starting with the package to be unit tested, |
| 24 | parse its 'configure.ac' file from within the package's directory(pkgdir) |
| 25 | for each package dependency defined recursively doing the same thing |
| 26 | on each package found as a dependency. |
| 27 | |
| 28 | Parameter descriptions: |
| 29 | pkg Name of the package |
| 30 | pkgdir Directory where package source is located |
| 31 | dep_installed Current list of dependencies and installation status |
| 32 | """ |
| 33 | os.chdir(pkgdir) |
| 34 | # Open package's configure.ac |
| 35 | with open("configure.ac", "rt") as infile: |
| 36 | for line in infile: # TODO Handle line breaks |
| 37 | # Find any defined dependency |
| 38 | for macro_key in DEPENDENCIES: |
| 39 | if not line.startswith(macro_key): |
| 40 | continue |
| 41 | for dep_key in DEPENDENCIES[macro_key]: |
| 42 | if line.find(dep_key) == -1: |
| 43 | continue |
| 44 | dep_pkg = DEPENDENCIES[macro_key][dep_key] |
| 45 | # Dependency package not already known |
| 46 | if dep_installed.get(dep_pkg) is None: |
| 47 | # Dependency package not installed |
| 48 | dep_installed[dep_pkg] = False |
| 49 | clone_pkg(dep_pkg) |
| 50 | # Determine this dependency package's |
| 51 | # dependencies and install them before |
| 52 | # returning to install this package |
| 53 | dep_pkgdir = os.path.join(WORKSPACE, dep_pkg) |
| 54 | dep_installed = build_depends(dep_pkg, dep_pkgdir, |
| 55 | dep_installed) |
| 56 | else: |
| 57 | # Dependency package known and installed |
| 58 | if dep_installed[dep_pkg]: |
| 59 | continue |
| 60 | else: |
| 61 | # Cyclic dependency failure |
| 62 | raise Exception("Cyclic dependencies \ |
| 63 | found in "+pkg) |
| 64 | |
| 65 | # Build & install this package |
| 66 | if not dep_installed[pkg]: |
| 67 | conf_flags = "" |
| 68 | os.chdir(pkgdir) |
| 69 | # Add any necessary configure flags for package |
| 70 | if CONFIGURE_FLAGS.get(pkg) is not None: |
| 71 | conf_flags = " ".join(CONFIGURE_FLAGS.get(pkg)) |
| 72 | os.system("./bootstrap.sh && ./configure " + |
| 73 | conf_flags + "&& make && make install") |
| 74 | dep_installed[pkg] = True |
| 75 | |
| 76 | return dep_installed |
| 77 | |
| 78 | |
| 79 | if __name__ == '__main__': |
| 80 | # CONFIGURE_FLAGS = [GIT REPO]:[CONFIGURE FLAGS] |
| 81 | CONFIGURE_FLAGS = { |
| 82 | 'phosphor-objmgr': ['--enable-unpatched-systemd'] |
| 83 | } |
| 84 | |
| 85 | # DEPENDENCIES = [MACRO]:[library/header]:[GIT REPO] |
| 86 | DEPENDENCIES = { |
| 87 | 'AC_CHECK_LIB': {'mapper': 'phosphor-objmgr'}, |
| 88 | 'AC_CHECK_HEADER': {'host-ipmid/ipmid-api.h': 'phosphor-host-ipmid'} |
| 89 | } |
| 90 | |
| 91 | # Get workspace directory (package source to be tested) |
| 92 | WORKSPACE = os.environ.get('WORKSPACE') |
| 93 | if WORKSPACE is None: |
| 94 | raise Exception("Environment variable 'WORKSPACE' not set") |
| 95 | |
| 96 | # Determine package name |
| 97 | UNIT_TEST_PKG = os.environ.get('UNIT_TEST_PKG') |
| 98 | if UNIT_TEST_PKG is None: |
| 99 | raise Exception("Environment variable 'UNIT_TEST_PKG' not set") |
| 100 | |
| 101 | prev_umask = os.umask(000) |
| 102 | # Determine dependencies and install them |
| 103 | dep_installed = dict() |
| 104 | dep_installed[UNIT_TEST_PKG] = False |
| 105 | dep_installed = build_depends(UNIT_TEST_PKG, |
| 106 | os.path.join(WORKSPACE, UNIT_TEST_PKG), |
| 107 | dep_installed) |
| 108 | os.chdir(os.path.join(WORKSPACE, UNIT_TEST_PKG)) |
| 109 | # Run package unit tests |
| 110 | os.system("make check") # TODO Verify all fails halt Jenkins |
| 111 | os.umask(prev_umask) |